2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/curve.c
33 #include <math.h> // floor
37 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_ghash.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_material_types.h"
47 /* for dereferencing pointers */
48 #include "DNA_key_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_object_types.h"
53 #include "BKE_animsys.h"
54 #include "BKE_curve.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_displist.h"
58 #include "BKE_global.h"
60 #include "BKE_library.h"
62 #include "BKE_object.h"
63 #include "BKE_material.h"
68 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
70 float *lambda, float *mu, float vec[3]);
72 void BKE_curve_unlink(Curve *cu)
76 for (a = 0; a < cu->totcol; a++) {
77 if (cu->mat[a]) cu->mat[a]->id.us--;
101 /* frees editcurve entirely */
102 void BKE_curve_editfont_free(Curve *cu)
105 EditFont *ef = cu->editfont;
108 MEM_freeN(ef->textbuf);
110 MEM_freeN(ef->textbufinfo);
112 MEM_freeN(ef->copybuf);
114 MEM_freeN(ef->copybufinfo);
116 MEM_freeN(ef->selboxes);
123 void BKE_curve_editNurb_keyIndex_free(EditNurb *editnurb)
125 if (!editnurb->keyindex) {
128 BLI_ghash_free(editnurb->keyindex, NULL, MEM_freeN);
129 editnurb->keyindex = NULL;
132 void BKE_curve_editNurb_free(Curve *cu)
135 BKE_nurbList_free(&cu->editnurb->nurbs);
136 BKE_curve_editNurb_keyIndex_free(cu->editnurb);
137 MEM_freeN(cu->editnurb);
142 /* don't free curve itself */
143 void BKE_curve_free(Curve *cu)
145 BKE_nurbList_free(&cu->nurb);
146 BKE_curve_editfont_free(cu);
148 BKE_curve_editNurb_free(cu);
149 BKE_curve_unlink(cu);
150 BKE_animdata_free((ID *)cu);
157 MEM_freeN(cu->strinfo);
164 Curve *BKE_curve_add(Main *bmain, const char *name, int type)
168 cu = BKE_libblock_alloc(bmain, ID_CU, name);
169 copy_v3_fl(cu->size, 1.0f);
170 cu->flag = CU_FRONT | CU_BACK | CU_DEFORM_BOUNDS_OFF | CU_PATH_RADIUS;
172 cu->resolu = cu->resolv = (type == OB_SURF) ? 4 : 12;
175 cu->spacing = cu->linedist = 1.0;
178 cu->texflag = CU_AUTOSPACE;
179 cu->smallcaps_scale = 0.75f;
180 /* XXX: this one seems to be the best one in most cases, at least for curve deform... */
181 cu->twist_mode = CU_TWIST_MINIMUM;
185 cu->bevfac1_mapping = CU_BEVFAC_MAP_RESOLU;
186 cu->bevfac2_mapping = CU_BEVFAC_MAP_RESOLU;
188 cu->bb = BKE_boundbox_alloc_unit();
190 if (type == OB_FONT) {
191 cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_get();
192 cu->vfont->id.us += 4;
193 cu->str = MEM_mallocN(12, "str");
194 BLI_strncpy(cu->str, "Text", 12);
195 cu->len = cu->len_wchar = cu->pos = 4;
196 cu->strinfo = MEM_callocN(12 * sizeof(CharInfo), "strinfo new");
197 cu->totbox = cu->actbox = 1;
198 cu->tb = MEM_callocN(MAXTEXTBOX * sizeof(TextBox), "textbox");
199 cu->tb[0].w = cu->tb[0].h = 0.0;
205 Curve *BKE_curve_copy(Curve *cu)
210 cun = BKE_libblock_copy(&cu->id);
211 BLI_listbase_clear(&cun->nurb);
212 BKE_nurbList_duplicate(&(cun->nurb), &(cu->nurb));
214 cun->mat = MEM_dupallocN(cu->mat);
215 for (a = 0; a < cun->totcol; a++) {
216 id_us_plus((ID *)cun->mat[a]);
219 cun->str = MEM_dupallocN(cu->str);
220 cun->strinfo = MEM_dupallocN(cu->strinfo);
221 cun->tb = MEM_dupallocN(cu->tb);
222 cun->bb = MEM_dupallocN(cu->bb);
224 cun->key = BKE_key_copy(cu->key);
225 if (cun->key) cun->key->from = (ID *)cun;
227 cun->editnurb = NULL;
228 cun->editfont = NULL;
230 #if 0 // XXX old animation system
231 /* single user ipo too */
232 if (cun->ipo) cun->ipo = copy_ipo(cun->ipo);
233 #endif // XXX old animation system
235 id_us_plus((ID *)cun->vfont);
236 id_us_plus((ID *)cun->vfontb);
237 id_us_plus((ID *)cun->vfonti);
238 id_us_plus((ID *)cun->vfontbi);
241 BKE_id_lib_local_paths(G.main, cu->id.lib, &cun->id);
247 static void extern_local_curve(Curve *cu)
249 id_lib_extern((ID *)cu->vfont);
250 id_lib_extern((ID *)cu->vfontb);
251 id_lib_extern((ID *)cu->vfonti);
252 id_lib_extern((ID *)cu->vfontbi);
255 extern_local_matarar(cu->mat, cu->totcol);
259 void BKE_curve_make_local(Curve *cu)
261 Main *bmain = G.main;
263 bool is_local = false, is_lib = false;
265 /* - when there are only lib users: don't do
266 * - when there are only local users: set flag
270 if (cu->id.lib == NULL)
273 if (cu->id.us == 1) {
274 id_clear_lib_data(bmain, &cu->id);
275 extern_local_curve(cu);
279 for (ob = bmain->object.first; ob && ELEM(0, is_lib, is_local); ob = ob->id.next) {
280 if (ob->data == cu) {
281 if (ob->id.lib) is_lib = true;
282 else is_local = true;
286 if (is_local && is_lib == false) {
287 id_clear_lib_data(bmain, &cu->id);
288 extern_local_curve(cu);
290 else if (is_local && is_lib) {
291 Curve *cu_new = BKE_curve_copy(cu);
294 BKE_id_lib_local_paths(bmain, cu->id.lib, &cu_new->id);
296 for (ob = bmain->object.first; ob; ob = ob->id.next) {
297 if (ob->data == cu) {
298 if (ob->id.lib == NULL) {
308 /* Get list of nurbs from editnurbs structure */
309 ListBase *BKE_curve_editNurbs_get(Curve *cu)
312 return &cu->editnurb->nurbs;
318 short BKE_curve_type_get(Curve *cu)
330 for (nu = cu->nurb.first; nu; nu = nu->next) {
340 void BKE_curve_curve_dimension_update(Curve *cu)
342 ListBase *nurbs = BKE_curve_nurbs_get(cu);
343 Nurb *nu = nurbs->first;
345 if (cu->flag & CU_3D) {
346 for (; nu; nu = nu->next) {
351 for (; nu; nu = nu->next) {
355 /* since the handles are moved they need to be auto-located again */
356 if (nu->type == CU_BEZIER)
357 BKE_nurb_handles_calc(nu);
362 void BKE_curve_type_test(Object *ob)
364 ob->type = BKE_curve_type_get(ob->data);
366 if (ob->type == OB_CURVE)
367 BKE_curve_curve_dimension_update((Curve *)ob->data);
370 void BKE_curve_boundbox_calc(Curve *cu, float r_loc[3], float r_size[3])
373 float min[3], max[3];
374 float mloc[3], msize[3];
376 if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
379 if (!r_loc) r_loc = mloc;
380 if (!r_size) r_size = msize;
382 INIT_MINMAX(min, max);
383 if (!BKE_curve_minmax(cu, true, min, max)) {
384 min[0] = min[1] = min[2] = -1.0f;
385 max[0] = max[1] = max[2] = 1.0f;
388 mid_v3_v3v3(r_loc, min, max);
390 r_size[0] = (max[0] - min[0]) / 2.0f;
391 r_size[1] = (max[1] - min[1]) / 2.0f;
392 r_size[2] = (max[2] - min[2]) / 2.0f;
394 BKE_boundbox_init_from_minmax(bb, min, max);
396 bb->flag &= ~BOUNDBOX_DIRTY;
399 BoundBox *BKE_curve_boundbox_get(Object *ob)
401 Curve *cu = ob->data;
406 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
407 BKE_curve_texspace_calc(cu);
413 void BKE_curve_texspace_calc(Curve *cu)
415 float loc[3], size[3];
418 BKE_curve_boundbox_calc(cu, loc, size);
420 if (cu->texflag & CU_AUTOSPACE) {
421 for (a = 0; a < 3; a++) {
422 if (size[a] == 0.0f) size[a] = 1.0f;
423 else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
424 else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
427 copy_v3_v3(cu->loc, loc);
428 copy_v3_v3(cu->size, size);
433 void BKE_curve_texspace_get(Curve *cu, float r_loc[3], float r_rot[3], float r_size[3])
435 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
436 BKE_curve_texspace_calc(cu);
439 if (r_loc) copy_v3_v3(r_loc, cu->loc);
440 if (r_rot) copy_v3_v3(r_rot, cu->rot);
441 if (r_size) copy_v3_v3(r_size, cu->size);
444 bool BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
449 for (nu = nurb->first; nu; nu = nu->next) {
451 if (nu->type == CU_BEZIER) {
453 if (index - tot < tot_nu) {
454 copy_v3_v3(r_co, nu->bezt[index - tot].vec[1]);
459 tot_nu = nu->pntsu * nu->pntsv;
460 if (index - tot < tot_nu) {
461 copy_v3_v3(r_co, nu->bp[index - tot].vec);
471 int BKE_nurbList_verts_count(ListBase *nurb)
479 tot += 3 * nu->pntsu;
481 tot += nu->pntsu * nu->pntsv;
488 int BKE_nurbList_verts_count_without_handles(ListBase *nurb)
498 tot += nu->pntsu * nu->pntsv;
505 /* **************** NURBS ROUTINES ******************** */
507 void BKE_nurb_free(Nurb *nu)
510 if (nu == NULL) return;
519 MEM_freeN(nu->knotsu);
522 MEM_freeN(nu->knotsv);
524 /* if (nu->trim.first) freeNurblist(&(nu->trim)); */
531 void BKE_nurbList_free(ListBase *lb)
535 if (lb == NULL) return;
543 BLI_listbase_clear(lb);
546 Nurb *BKE_nurb_duplicate(Nurb *nu)
551 newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "duplicateNurb");
552 if (newnu == NULL) return NULL;
553 memcpy(newnu, nu, sizeof(Nurb));
557 (BezTriple *)MEM_mallocN((nu->pntsu) * sizeof(BezTriple), "duplicateNurb2");
558 memcpy(newnu->bezt, nu->bezt, nu->pntsu * sizeof(BezTriple));
561 len = nu->pntsu * nu->pntsv;
563 (BPoint *)MEM_mallocN((len) * sizeof(BPoint), "duplicateNurb3");
564 memcpy(newnu->bp, nu->bp, len * sizeof(BPoint));
566 newnu->knotsu = newnu->knotsv = NULL;
571 newnu->knotsu = MEM_mallocN(len * sizeof(float), "duplicateNurb4");
572 memcpy(newnu->knotsu, nu->knotsu, sizeof(float) * len);
575 if (nu->pntsv > 1 && nu->knotsv) {
578 newnu->knotsv = MEM_mallocN(len * sizeof(float), "duplicateNurb5");
579 memcpy(newnu->knotsv, nu->knotsv, sizeof(float) * len);
586 /* copy the nurb but allow for different number of points (to be copied after this) */
587 Nurb *BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
589 Nurb *newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "copyNurb");
590 memcpy(newnu, src, sizeof(Nurb));
592 if (pntsu == 1) SWAP(int, pntsu, pntsv);
593 newnu->pntsu = pntsu;
594 newnu->pntsv = pntsv;
596 /* caller can manually handle these arrays */
597 newnu->knotsu = NULL;
598 newnu->knotsv = NULL;
601 newnu->bezt = (BezTriple *)MEM_mallocN(pntsu * pntsv * sizeof(BezTriple), "copyNurb2");
604 newnu->bp = (BPoint *)MEM_mallocN(pntsu * pntsv * sizeof(BPoint), "copyNurb3");
610 void BKE_nurbList_duplicate(ListBase *lb1, ListBase *lb2)
614 BKE_nurbList_free(lb1);
618 nun = BKE_nurb_duplicate(nu);
619 BLI_addtail(lb1, nun);
625 void BKE_nurb_test2D(Nurb *nu)
631 if ((nu->flag & CU_2D) == 0)
634 if (nu->type == CU_BEZIER) {
638 bezt->vec[0][2] = 0.0;
639 bezt->vec[1][2] = 0.0;
640 bezt->vec[2][2] = 0.0;
645 a = nu->pntsu * nu->pntsv;
655 * if use_radius is truth, minmax will take points' radius into account,
656 * which will make boundbox closer to beveled curve.
658 void BKE_nurb_minmax(Nurb *nu, bool use_radius, float min[3], float max[3])
665 if (nu->type == CU_BEZIER) {
670 float radius_vector[3];
671 radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
673 add_v3_v3v3(point, bezt->vec[1], radius_vector);
674 minmax_v3v3_v3(min, max, point);
676 sub_v3_v3v3(point, bezt->vec[1], radius_vector);
677 minmax_v3v3_v3(min, max, point);
680 minmax_v3v3_v3(min, max, bezt->vec[1]);
682 minmax_v3v3_v3(min, max, bezt->vec[0]);
683 minmax_v3v3_v3(min, max, bezt->vec[2]);
688 a = nu->pntsu * nu->pntsv;
691 if (nu->pntsv == 1 && use_radius) {
692 float radius_vector[3];
693 radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
695 add_v3_v3v3(point, bp->vec, radius_vector);
696 minmax_v3v3_v3(min, max, point);
698 sub_v3_v3v3(point, bp->vec, radius_vector);
699 minmax_v3v3_v3(min, max, point);
702 /* Surfaces doesn't use bevel, so no need to take radius into account. */
703 minmax_v3v3_v3(min, max, bp->vec);
710 /* be sure to call makeknots after this */
711 void BKE_nurb_points_add(Nurb *nu, int number)
716 nu->bp = MEM_recallocN(nu->bp, (nu->pntsu + number) * sizeof(BPoint));
718 for (i = 0, bp = &nu->bp[nu->pntsu]; i < number; i++, bp++) {
725 void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
730 nu->bezt = MEM_recallocN(nu->bezt, (nu->pntsu + number) * sizeof(BezTriple));
732 for (i = 0, bezt = &nu->bezt[nu->pntsu]; i < number; i++, bezt++) {
740 BezTriple *BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
742 BezTriple *bezt_next;
744 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
746 if (bezt == &nu->bezt[nu->pntsu - 1]) {
747 if (nu->flagu & CU_NURB_CYCLIC) {
748 bezt_next = nu->bezt;
755 bezt_next = bezt + 1;
761 BPoint *BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
765 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
767 if (bp == &nu->bp[nu->pntsu - 1]) {
768 if (nu->flagu & CU_NURB_CYCLIC) {
782 BezTriple *BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
784 BezTriple *bezt_prev;
786 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
788 if (bezt == nu->bezt) {
789 if (nu->flagu & CU_NURB_CYCLIC) {
790 bezt_prev = &nu->bezt[nu->pntsu - 1];
797 bezt_prev = bezt - 1;
803 BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
807 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
810 if (nu->flagu & CU_NURB_CYCLIC) {
811 bp_prev = &nu->bp[nu->pntsu - 1];
824 void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), struct BezTriple *bezt, float r_normal[3])
826 /* calculate the axis matrix from the spline */
827 float dir_prev[3], dir_next[3];
829 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
830 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
832 normalize_v3(dir_prev);
833 normalize_v3(dir_next);
835 add_v3_v3v3(r_normal, dir_prev, dir_next);
836 normalize_v3(r_normal);
839 void BKE_nurb_bezt_calc_plane(struct Nurb *nu, struct BezTriple *bezt, float r_plane[3])
841 float dir_prev[3], dir_next[3];
843 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
844 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
846 normalize_v3(dir_prev);
847 normalize_v3(dir_next);
849 cross_v3_v3v3(r_plane, dir_prev, dir_next);
850 if (normalize_v3(r_plane) < FLT_EPSILON) {
851 BezTriple *bezt_prev = BKE_nurb_bezt_get_prev(nu, bezt);
852 BezTriple *bezt_next = BKE_nurb_bezt_get_next(nu, bezt);
855 sub_v3_v3v3(dir_prev, bezt_prev->vec[1], bezt->vec[1]);
856 normalize_v3(dir_prev);
859 sub_v3_v3v3(dir_next, bezt->vec[1], bezt_next->vec[1]);
860 normalize_v3(dir_next);
862 cross_v3_v3v3(r_plane, dir_prev, dir_next);
865 /* matches with bones more closely */
867 float dir_mid[3], tvec[3];
868 add_v3_v3v3(dir_mid, dir_prev, dir_next);
869 cross_v3_v3v3(tvec, r_plane, dir_mid);
870 copy_v3_v3(r_plane, tvec);
873 normalize_v3(r_plane);
876 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
879 static void calcknots(float *knots, const int pnts, const short order, const short flag)
881 /* knots: number of pnts NOT corrected for cyclic */
882 const int pnts_order = pnts + order;
886 switch (flag & (CU_NURB_ENDPOINT | CU_NURB_BEZIER)) {
887 case CU_NURB_ENDPOINT:
889 for (a = 1; a <= pnts_order; a++) {
891 if (a >= order && a <= pnts)
896 /* Warning, the order MUST be 2 or 4,
897 * if this is not enforced, the displist will be corrupt */
900 for (a = 0; a < pnts_order; a++) {
901 knots[a] = floorf(k);
905 else if (order == 3) {
907 for (a = 0; a < pnts_order; a++) {
908 if (a >= order && a <= pnts)
910 knots[a] = floorf(k);
914 printf("bez nurb curve order is not 3 or 4, should never happen\n");
918 for (a = 0; a < pnts_order; a++) {
925 static void makecyclicknots(float *knots, int pnts, short order)
926 /* pnts, order: number of pnts NOT corrected for cyclic */
935 /* do first long rows (order -1), remove identical knots at endpoints */
938 for (a = 1; a < order2; a++) {
939 if (knots[b] != knots[b - a])
943 knots[pnts + order - 2] += 1.0f;
947 c = pnts + order + order2;
948 for (a = pnts + order2; a < c; a++) {
949 knots[a] = knots[a - 1] + (knots[b] - knots[b - 1]);
956 static void makeknots(Nurb *nu, short uv)
958 if (nu->type == CU_NURBS) {
961 MEM_freeN(nu->knotsu);
962 if (BKE_nurb_check_valid_u(nu)) {
963 nu->knotsu = MEM_callocN(4 + sizeof(float) * KNOTSU(nu), "makeknots");
964 if (nu->flagu & CU_NURB_CYCLIC) {
965 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
966 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
969 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
977 MEM_freeN(nu->knotsv);
978 if (BKE_nurb_check_valid_v(nu)) {
979 nu->knotsv = MEM_callocN(4 + sizeof(float) * KNOTSV(nu), "makeknots");
980 if (nu->flagv & CU_NURB_CYCLIC) {
981 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
982 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
985 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
995 void BKE_nurb_knot_calc_u(Nurb *nu)
1000 void BKE_nurb_knot_calc_v(Nurb *nu)
1005 static void basisNurb(float t, short order, int pnts, float *knots, float *basis, int *start, int *end)
1008 int i, i1 = 0, i2 = 0, j, orderpluspnts, opp2, o2;
1010 orderpluspnts = order + pnts;
1011 opp2 = orderpluspnts - 1;
1013 /* this is for float inaccuracy */
1016 else if (t > knots[opp2])
1019 /* this part is order '1' */
1021 for (i = 0; i < opp2; i++) {
1022 if (knots[i] != knots[i + 1] && t >= knots[i] && t <= knots[i + 1]) {
1039 /* this is order 2, 3, ... */
1040 for (j = 2; j <= order; j++) {
1042 if (i2 + j >= orderpluspnts) i2 = opp2 - j;
1044 for (i = i1; i <= i2; i++) {
1045 if (basis[i] != 0.0f)
1046 d = ((t - knots[i]) * basis[i]) / (knots[i + j - 1] - knots[i]);
1050 if (basis[i + 1] != 0.0f)
1051 e = ((knots[i + j] - t) * basis[i + 1]) / (knots[i + j] - knots[i + 1]);
1062 for (i = i1; i <= i2; i++) {
1063 if (basis[i] > 0.0f) {
1065 if (*start == 1000) *start = i;
1071 void BKE_nurb_makeFaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
1072 /* coord_array has to be (3 * 4 * resolu * resolv) in size, and zero-ed */
1075 float *basisu, *basis, *basisv, *sum, *fp, *in;
1076 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
1077 int i, j, iofs, jofs, cycl, len, curu, curv;
1078 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
1080 int totu = nu->pntsu * resolu, totv = nu->pntsv * resolv;
1082 if (nu->knotsu == NULL || nu->knotsv == NULL)
1084 if (nu->orderu > nu->pntsu)
1086 if (nu->orderv > nu->pntsv)
1088 if (coord_array == NULL)
1091 /* allocate and initialize */
1096 sum = (float *)MEM_callocN(sizeof(float) * len, "makeNurbfaces1");
1099 i = nu->pntsu * nu->pntsv;
1102 if (bp->vec[3] != 1.0f) {
1110 ustart = fp[nu->orderu - 1];
1111 if (nu->flagu & CU_NURB_CYCLIC)
1112 uend = fp[nu->pntsu + nu->orderu - 1];
1114 uend = fp[nu->pntsu];
1115 ustep = (uend - ustart) / ((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
1117 basisu = (float *)MEM_mallocN(sizeof(float) * KNOTSU(nu), "makeNurbfaces3");
1120 vstart = fp[nu->orderv - 1];
1122 if (nu->flagv & CU_NURB_CYCLIC)
1123 vend = fp[nu->pntsv + nu->orderv - 1];
1125 vend = fp[nu->pntsv];
1126 vstep = (vend - vstart) / ((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
1129 basisv = (float *)MEM_mallocN(sizeof(float) * len * totv, "makeNurbfaces3");
1130 jstart = (int *)MEM_mallocN(sizeof(float) * totv, "makeNurbfaces4");
1131 jend = (int *)MEM_mallocN(sizeof(float) * totv, "makeNurbfaces5");
1133 /* precalculation of basisv and jstart, jend */
1134 if (nu->flagv & CU_NURB_CYCLIC)
1135 cycl = nu->orderv - 1;
1141 basisNurb(v, nu->orderv, nu->pntsv + cycl, nu->knotsv, basis, jstart + curv, jend + curv);
1142 basis += KNOTSV(nu);
1146 if (nu->flagu & CU_NURB_CYCLIC)
1147 cycl = nu->orderu - 1;
1154 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1159 jsta = jstart[curv];
1166 for (j = jsta; j <= jen; j++) {
1169 jofs = (j - nu->pntsv);
1172 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1174 for (i = istart; i <= iend; i++, fp++) {
1175 if (i >= nu->pntsu) {
1176 iofs = i - nu->pntsu;
1177 bp = nu->bp + nu->pntsu * jofs + iofs;
1183 *fp = basisu[i] * basis[j] * bp->vec[3];
1187 *fp = basisu[i] * basis[j];
1193 for (j = jsta; j <= jen; j++) {
1194 for (i = istart; i <= iend; i++, fp++) {
1202 /* one! (1.0) real point now */
1204 for (j = jsta; j <= jen; j++) {
1207 jofs = (j - nu->pntsv);
1210 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1212 for (i = istart; i <= iend; i++, fp++) {
1213 if (i >= nu->pntsu) {
1214 iofs = i - nu->pntsu;
1215 bp = nu->bp + nu->pntsu * jofs + iofs;
1221 madd_v3_v3fl(in, bp->vec, *fp);
1227 basis += KNOTSV(nu);
1231 in = (float *) (((unsigned char *) in) + (rowstride - 3 * totv * sizeof(*in)));
1243 * \param coord_array Has to be 3 * 4 * pntsu * resolu in size and zero-ed
1244 * \param tilt_array set when non-NULL
1245 * \param radius_array set when non-NULL
1247 void BKE_nurb_makeCurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array,
1248 int resolu, int stride)
1250 const float eps = 1e-6f;
1252 float u, ustart, uend, ustep, sumdiv;
1253 float *basisu, *sum, *fp;
1254 float *coord_fp = coord_array, *tilt_fp = tilt_array, *radius_fp = radius_array, *weight_fp = weight_array;
1255 int i, len, istart, iend, cycl;
1257 if (nu->knotsu == NULL)
1259 if (nu->orderu > nu->pntsu)
1261 if (coord_array == NULL)
1264 /* allocate and initialize */
1268 sum = (float *)MEM_callocN(sizeof(float) * len, "makeNurbcurve1");
1270 resolu = (resolu * SEGMENTSU(nu));
1278 ustart = fp[nu->orderu - 1];
1279 if (nu->flagu & CU_NURB_CYCLIC)
1280 uend = fp[nu->pntsu + nu->orderu - 1];
1282 uend = fp[nu->pntsu];
1283 ustep = (uend - ustart) / (resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
1285 basisu = (float *)MEM_mallocN(sizeof(float) * KNOTSU(nu), "makeNurbcurve3");
1287 if (nu->flagu & CU_NURB_CYCLIC)
1288 cycl = nu->orderu - 1;
1294 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1299 bp = nu->bp + istart - 1;
1300 for (i = istart; i <= iend; i++, fp++) {
1302 bp = nu->bp + (i - nu->pntsu);
1306 *fp = basisu[i] * bp->vec[3];
1309 if ((sumdiv != 0.0f) && (sumdiv < 1.0f - eps || sumdiv > 1.0f + eps)) {
1310 /* is normalizing needed? */
1312 for (i = istart; i <= iend; i++, fp++) {
1319 /* one! (1.0) real point */
1321 bp = nu->bp + istart - 1;
1322 for (i = istart; i <= iend; i++, fp++) {
1324 bp = nu->bp + (i - nu->pntsu);
1329 madd_v3_v3fl(coord_fp, bp->vec, *fp);
1332 (*tilt_fp) += (*fp) * bp->alfa;
1335 (*radius_fp) += (*fp) * bp->radius;
1338 (*weight_fp) += (*fp) * bp->weight;
1342 coord_fp = (float *)(((char *)coord_fp) + stride);
1345 tilt_fp = (float *)(((char *)tilt_fp) + stride);
1347 radius_fp = (float *)(((char *)radius_fp) + stride);
1349 weight_fp = (float *)(((char *)weight_fp) + stride);
1359 /* forward differencing method for bezier curve */
1360 void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1362 float rt0, rt1, rt2, rt3, f;
1367 rt1 = 3.0f * (q1 - q0) / f;
1369 rt2 = 3.0f * (q0 - 2.0f * q1 + q2) / f;
1371 rt3 = (q3 - q0 + 3.0f * (q1 - q2)) / f;
1374 q1 = rt1 + rt2 + rt3;
1375 q2 = 2 * rt2 + 6 * rt3;
1378 for (a = 0; a <= it; a++) {
1380 p = (float *)(((char *)p) + stride);
1387 static void forward_diff_bezier_cotangent(const float p0[3], const float p1[3], const float p2[3], const float p3[3],
1388 float p[3], int it, int stride)
1390 /* note that these are not perpendicular to the curve
1391 * they need to be rotated for this,
1393 * This could also be optimized like BKE_curve_forward_diff_bezier */
1395 for (a = 0; a <= it; a++) {
1396 float t = (float)a / (float)it;
1399 for (i = 0; i < 3; i++) {
1400 p[i] = (-6.0f * t + 6.0f) * p0[i] +
1401 ( 18.0f * t - 12.0f) * p1[i] +
1402 (-18.0f * t + 6.0f) * p2[i] +
1403 ( 6.0f * t) * p3[i];
1406 p = (float *)(((char *)p) + stride);
1410 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1412 float *BKE_curve_surf_make_orco(Object *ob)
1414 /* Note: this function is used in convertblender only atm, so
1415 * suppose nonzero curve's render resolution should always be used */
1416 Curve *cu = ob->data;
1421 float *fp, *coord_array;
1423 /* first calculate the size of the datablock */
1424 nu = cu->nurb.first;
1426 /* as we want to avoid the seam in a cyclic nurbs
1427 * texture wrapping, reserve extra orco data space to save these extra needed
1428 * vertex based UV coordinates for the meridian vertices.
1429 * Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1430 * the renderface/vert construction.
1432 * See also convertblender.c: init_render_surf()
1435 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1436 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1438 sizeu = nu->pntsu * resolu;
1439 sizev = nu->pntsv * resolv;
1440 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1441 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1442 if (nu->pntsv > 1) tot += sizeu * sizev;
1446 /* makeNurbfaces wants zeros */
1447 fp = coord_array = MEM_callocN(3 * sizeof(float) * tot, "make_orco");
1449 nu = cu->nurb.first;
1451 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1452 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1454 if (nu->pntsv > 1) {
1455 sizeu = nu->pntsu * resolu;
1456 sizev = nu->pntsv * resolv;
1458 if (nu->flagu & CU_NURB_CYCLIC)
1460 if (nu->flagv & CU_NURB_CYCLIC)
1463 if (cu->flag & CU_UV_ORCO) {
1464 for (b = 0; b < sizeu; b++) {
1465 for (a = 0; a < sizev; a++) {
1470 fp[0] = -1.0f + 2.0f * ((float)a) / (sizev - 1);
1475 fp[1] = -1.0f + 2.0f * ((float)b) / (sizeu - 1);
1484 int size = (nu->pntsu * resolu) * (nu->pntsv * resolv) * 3 * sizeof(float);
1485 float *_tdata = MEM_mallocN(size, "temp data");
1486 float *tdata = _tdata;
1488 BKE_nurb_makeFaces(nu, tdata, 0, resolu, resolv);
1490 for (b = 0; b < sizeu; b++) {
1492 if (b == sizeu - 1 && (nu->flagu & CU_NURB_CYCLIC))
1495 for (a = 0; a < sizev; a++) {
1497 if (a == sizev - 1 && (nu->flagv & CU_NURB_CYCLIC))
1500 tdata = _tdata + 3 * (use_b * (nu->pntsv * resolv) + use_a);
1502 fp[0] = (tdata[0] - cu->loc[0]) / cu->size[0];
1503 fp[1] = (tdata[1] - cu->loc[1]) / cu->size[1];
1504 fp[2] = (tdata[2] - cu->loc[2]) / cu->size[2];
1519 /* NOTE: This routine is tied to the order of vertex
1520 * built by displist and as passed to the renderer.
1522 float *BKE_curve_make_orco(Scene *scene, Object *ob, int *r_numVerts)
1524 Curve *cu = ob->data;
1527 float *fp, *coord_array;
1528 ListBase disp = {NULL, NULL};
1530 BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1533 for (dl = disp.first; dl; dl = dl->next) {
1534 if (dl->type == DL_INDEX3) {
1537 else if (dl->type == DL_SURF) {
1538 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only
1539 * (closed circle beveling)
1541 if (dl->flag & DL_CYCL_U) {
1542 if (dl->flag & DL_CYCL_V)
1543 numVerts += (dl->parts + 1) * (dl->nr + 1);
1545 numVerts += dl->parts * (dl->nr + 1);
1547 else if (dl->flag & DL_CYCL_V) {
1548 numVerts += (dl->parts + 1) * dl->nr;
1551 numVerts += dl->parts * dl->nr;
1556 *r_numVerts = numVerts;
1558 fp = coord_array = MEM_mallocN(3 * sizeof(float) * numVerts, "cu_orco");
1559 for (dl = disp.first; dl; dl = dl->next) {
1560 if (dl->type == DL_INDEX3) {
1561 for (u = 0; u < dl->nr; u++, fp += 3) {
1562 if (cu->flag & CU_UV_ORCO) {
1563 fp[0] = 2.0f * u / (dl->nr - 1) - 1.0f;
1568 copy_v3_v3(fp, &dl->verts[u * 3]);
1570 fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1571 fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1572 fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1576 else if (dl->type == DL_SURF) {
1577 int sizeu = dl->nr, sizev = dl->parts;
1579 /* exception as handled in convertblender.c too */
1580 if (dl->flag & DL_CYCL_U) {
1582 if (dl->flag & DL_CYCL_V)
1585 else if (dl->flag & DL_CYCL_V) {
1589 for (u = 0; u < sizev; u++) {
1590 for (v = 0; v < sizeu; v++, fp += 3) {
1591 if (cu->flag & CU_UV_ORCO) {
1592 fp[0] = 2.0f * u / (sizev - 1) - 1.0f;
1593 fp[1] = 2.0f * v / (sizeu - 1) - 1.0f;
1598 int realv = v % dl->nr;
1599 int realu = u % dl->parts;
1601 vert = dl->verts + 3 * (dl->nr * realu + realv);
1602 copy_v3_v3(fp, vert);
1604 fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1605 fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1606 fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1613 BKE_displist_free(&disp);
1619 /* ***************** BEVEL ****************** */
1621 void BKE_curve_bevel_make(Scene *scene, Object *ob, ListBase *disp,
1622 const bool for_render, const bool use_render_resolution)
1624 DispList *dl, *dlnew;
1626 float *fp, facx, facy, angle, dangle;
1630 BLI_listbase_clear(disp);
1632 /* if a font object is being edited, then do nothing */
1633 // XXX if ( ob == obedit && ob->type == OB_FONT ) return;
1636 if (cu->bevobj->type != OB_CURVE)
1639 bevcu = cu->bevobj->data;
1640 if (bevcu->ext1 == 0.0f && bevcu->ext2 == 0.0f) {
1641 ListBase bevdisp = {NULL, NULL};
1642 facx = cu->bevobj->size[0];
1643 facy = cu->bevobj->size[1];
1646 BKE_displist_make_curveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution);
1649 else if (cu->bevobj->curve_cache) {
1650 dl = cu->bevobj->curve_cache->disp.first;
1653 BLI_assert(cu->bevobj->curve_cache != NULL);
1658 if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
1659 dlnew = MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1661 dlnew->verts = MEM_mallocN(3 * sizeof(float) * dl->parts * dl->nr, "makebevelcurve1");
1662 memcpy(dlnew->verts, dl->verts, 3 * sizeof(float) * dl->parts * dl->nr);
1664 if (dlnew->type == DL_SEGM)
1665 dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
1667 BLI_addtail(disp, dlnew);
1669 nr = dlnew->parts * dlnew->nr;
1671 fp[2] = fp[1] * facy;
1672 fp[1] = -fp[0] * facx;
1680 BKE_displist_free(&bevdisp);
1683 else if (cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
1686 else if (cu->ext2 == 0.0f) {
1687 dl = MEM_callocN(sizeof(DispList), "makebevelcurve2");
1688 dl->verts = MEM_mallocN(2 * sizeof(float[3]), "makebevelcurve2");
1689 BLI_addtail(disp, dl);
1692 dl->flag = DL_FRONT_CURVE | DL_BACK_CURVE;
1696 fp[0] = fp[1] = 0.0;
1698 fp[3] = fp[4] = 0.0;
1701 else if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0 && cu->ext1 == 0.0f) { // we make a full round bevel in that case
1702 nr = 4 + 2 * cu->bevresol;
1704 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1705 dl->verts = MEM_mallocN(nr * sizeof(float[3]), "makebevelcurve p1");
1706 BLI_addtail(disp, dl);
1709 dl->flag = DL_BACK_CURVE;
1714 dangle = (2.0f * (float)M_PI / (nr));
1715 angle = -(nr - 1) * dangle;
1717 for (a = 0; a < nr; a++) {
1719 fp[1] = (cosf(angle) * (cu->ext2));
1720 fp[2] = (sinf(angle) * (cu->ext2)) - cu->ext1;
1728 /* bevel now in three parts, for proper vertex normals */
1731 if ((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1732 dnr = nr = 2 + cu->bevresol;
1733 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1734 nr = 3 + 2 * cu->bevresol;
1736 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1737 dl->verts = MEM_mallocN(nr * sizeof(float[3]), "makebevelcurve p1");
1738 BLI_addtail(disp, dl);
1741 dl->flag = DL_BACK_CURVE;
1746 dangle = ((float)M_PI_2 / (dnr - 1));
1747 angle = -(nr - 1) * dangle;
1749 for (a = 0; a < nr; a++) {
1751 fp[1] = (float)(cosf(angle) * (cu->ext2));
1752 fp[2] = (float)(sinf(angle) * (cu->ext2)) - cu->ext1;
1758 /* part 2, sidefaces */
1759 if (cu->ext1 != 0.0f) {
1762 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1763 dl->verts = MEM_mallocN(nr * sizeof(float[3]), "makebevelcurve p2");
1764 BLI_addtail(disp, dl);
1775 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0) {
1776 dl = MEM_dupallocN(dl);
1777 dl->verts = MEM_dupallocN(dl->verts);
1778 BLI_addtail(disp, dl);
1789 if ((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1790 dnr = nr = 2 + cu->bevresol;
1791 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1792 nr = 3 + 2 * cu->bevresol;
1794 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1795 dl->verts = MEM_mallocN(nr * sizeof(float[3]), "makebevelcurve p3");
1796 BLI_addtail(disp, dl);
1798 dl->flag = DL_FRONT_CURVE;
1805 dangle = ((float)M_PI_2 / (dnr - 1));
1807 for (a = 0; a < nr; a++) {
1809 fp[1] = (float)(cosf(angle) * (cu->ext2));
1810 fp[2] = (float)(sinf(angle) * (cu->ext2)) + cu->ext1;
1818 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
1819 short cox, short coy,
1820 float *lambda, float *mu, float vec[3])
1824 * 0: no intersection of segments
1825 * 1: exact intersection of segments
1826 * 2: cross-intersection of segments
1830 deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
1834 *lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
1835 *lambda = -(*lambda / deler);
1837 deler = v3[coy] - v4[coy];
1839 deler = v3[cox] - v4[cox];
1840 *mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
1843 *mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
1845 vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
1846 vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
1848 if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
1849 if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f)
1857 static bool bevelinside(BevList *bl1, BevList *bl2)
1859 /* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
1860 /* returns '1' if correct hole */
1861 BevPoint *bevp, *prevbevp;
1862 float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
1863 int nr, links = 0, rechts = 0, mode;
1865 /* take first vertex of possible hole */
1867 bevp = bl2->bevpoints;
1868 hvec1[0] = bevp->vec[0];
1869 hvec1[1] = bevp->vec[1];
1871 copy_v3_v3(hvec2, hvec1);
1874 /* test it with all edges of potential surounding poly */
1875 /* count number of transitions left-right */
1877 bevp = bl1->bevpoints;
1879 prevbevp = bevp + (nr - 1);
1882 min = prevbevp->vec[1];
1886 max = prevbevp->vec[1];
1889 if (min <= hvec1[1] && max >= hvec1[1]) {
1890 /* there's a transition, calc intersection point */
1891 mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1892 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1893 * only allow for one situation: we choose lab= 1.0
1895 if (mode >= 0 && lab != 0.0f) {
1896 if (vec[0] < hvec1[0]) links++;
1905 return (links & 1) && (rechts & 1);
1915 static int vergxcobev(const void *a1, const void *a2)
1917 const struct BevelSort *x1 = a1, *x2 = a2;
1919 if (x1->left > x2->left)
1921 else if (x1->left < x2->left)
1926 /* this function cannot be replaced with atan2, but why? */
1928 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2,
1929 float *r_sina, float *r_cosa)
1931 float t01, t02, x3, y3;
1933 t01 = sqrtf(x1 * x1 + y1 * y1);
1934 t02 = sqrtf(x2 * x2 + y2 * y2);
1945 t02 = x1 * x2 + y1 * y2;
1946 if (fabsf(t02) >= 1.0f)
1949 t02 = (saacos(t02)) / 2.0f;
1957 if (x3 == 0 && y3 == 0) {
1962 t01 = sqrtf(x3 * x3 + y3 * y3);
1967 *r_sina = -y3 / t02;
1972 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array,
1973 float *weight_array, int resolu, int stride)
1975 BezTriple *pprev, *next, *last;
1976 float fac, dfac, t[4];
1979 if (tilt_array == NULL && radius_array == NULL)
1982 last = nu->bezt + (nu->pntsu - 1);
1984 /* returns a point */
1985 if (prevbezt == nu->bezt) {
1986 if (nu->flagu & CU_NURB_CYCLIC)
1992 pprev = prevbezt - 1;
1996 if (nu->flagu & CU_NURB_CYCLIC)
2005 dfac = 1.0f / (float)resolu;
2007 for (a = 0; a < resolu; a++, fac += dfac) {
2009 if (nu->tilt_interp == KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
2010 *tilt_array = prevbezt->alfa +
2011 (bezt->alfa - prevbezt->alfa) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2014 key_curve_position_weights(fac, t, nu->tilt_interp);
2015 *tilt_array = t[0] * pprev->alfa + t[1] * prevbezt->alfa + t[2] * bezt->alfa + t[3] * next->alfa;
2018 tilt_array = (float *)(((char *)tilt_array) + stride);
2022 if (nu->radius_interp == KEY_CU_EASE) {
2023 /* Support 2.47 ease interp
2024 * Note! - this only takes the 2 points into account,
2025 * giving much more localized results to changes in radius, sometimes you want that */
2026 *radius_array = prevbezt->radius +
2027 (bezt->radius - prevbezt->radius) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2031 /* reuse interpolation from tilt if we can */
2032 if (tilt_array == NULL || nu->tilt_interp != nu->radius_interp) {
2033 key_curve_position_weights(fac, t, nu->radius_interp);
2035 *radius_array = t[0] * pprev->radius + t[1] * prevbezt->radius +
2036 t[2] * bezt->radius + t[3] * next->radius;
2039 radius_array = (float *)(((char *)radius_array) + stride);
2043 /* basic interpolation for now, could copy tilt interp too */
2044 *weight_array = prevbezt->weight +
2045 (bezt->weight - prevbezt->weight) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2047 weight_array = (float *)(((char *)weight_array) + stride);
2052 /* make_bevel_list_3D_* funcs, at a minimum these must
2053 * fill in the bezp->quat and bezp->dir values */
2055 /* utility for make_bevel_list_3D_* funcs */
2056 static void bevel_list_calc_bisect(BevList *bl)
2058 BevPoint *bevp2, *bevp1, *bevp0;
2060 bool is_cyclic = bl->poly != -1;
2063 bevp2 = bl->bevpoints;
2064 bevp1 = bevp2 + (bl->nr - 1);
2069 /* If spline is not cyclic, direction of first and
2070 * last bevel points matches direction of CV handle.
2072 * This is getting calculated earlier when we know
2073 * CV's handles and here we might simply skip evaluation
2074 * of direction for this guys.
2077 bevp0 = bl->bevpoints;
2085 /* totally simple */
2086 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2093 static void bevel_list_flip_tangents(BevList *bl)
2095 BevPoint *bevp2, *bevp1, *bevp0;
2098 bevp2 = bl->bevpoints;
2099 bevp1 = bevp2 + (bl->nr - 1);
2104 if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f))
2105 negate_v3(bevp1->tan);
2112 /* apply user tilt */
2113 static void bevel_list_apply_tilt(BevList *bl)
2115 BevPoint *bevp2, *bevp1;
2119 bevp2 = bl->bevpoints;
2120 bevp1 = bevp2 + (bl->nr - 1);
2124 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2125 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2126 normalize_qt(bevp1->quat);
2132 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
2133 static void bevel_list_smooth(BevList *bl, int smooth_iter)
2135 BevPoint *bevp2, *bevp1, *bevp0;
2139 float bevp0_quat[4];
2142 for (a = 0; a < smooth_iter; a++) {
2143 bevp2 = bl->bevpoints;
2144 bevp1 = bevp2 + (bl->nr - 1);
2149 if (bl->poly == -1) { /* check its not cyclic */
2150 /* skip the first point */
2151 /* bevp0 = bevp1; */
2162 copy_qt_qt(bevp0_quat, bevp0->quat);
2165 /* interpolate quats */
2166 float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
2167 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
2170 mul_qt_v3(q, zaxis);
2171 cross_v3_v3v3(cross, zaxis, bevp1->dir);
2172 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
2175 copy_qt_qt(bevp0_quat, bevp1->quat);
2176 mul_qt_qtqt(q, q2, q);
2177 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
2178 normalize_qt(bevp1->quat);
2180 /* bevp0 = bevp1; */ /* UNUSED */
2187 static void make_bevel_list_3D_zup(BevList *bl)
2189 BevPoint *bevp = bl->bevpoints;
2192 bevel_list_calc_bisect(bl);
2195 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2200 static void minimum_twist_between_two_points(BevPoint *current_point, BevPoint *previous_point)
2202 float angle = angle_normalized_v3v3(previous_point->dir, current_point->dir);
2205 if (angle > 0.0f) { /* otherwise we can keep as is */
2207 cross_v3_v3v3(cross_tmp, previous_point->dir, current_point->dir);
2208 axis_angle_to_quat(q, cross_tmp, angle);
2209 mul_qt_qtqt(current_point->quat, q, previous_point->quat);
2212 copy_qt_qt(current_point->quat, previous_point->quat);
2216 static void make_bevel_list_3D_minimum_twist(BevList *bl)
2218 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2222 bevel_list_calc_bisect(bl);
2224 bevp2 = bl->bevpoints;
2225 bevp1 = bevp2 + (bl->nr - 1);
2231 if (nr + 4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
2232 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2235 minimum_twist_between_two_points(bevp1, bevp0);
2243 if (bl->poly != -1) { /* check for cyclic */
2245 /* Need to correct for the start/end points not matching
2246 * do this by calculating the tilt angle difference, then apply
2247 * the rotation gradually over the entire curve
2249 * note that the split is between last and second last, rather than first/last as youd expect.
2251 * real order is like this
2252 * 0,1,2,3,4 --> 1,2,3,4,0
2254 * this is why we compare last with second last
2256 float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
2258 BevPoint *bevp_first;
2259 BevPoint *bevp_last;
2262 bevp_first = bl->bevpoints;
2263 bevp_first += bl->nr - 1;
2264 bevp_last = bevp_first;
2267 /* quats and vec's are normalized, should not need to re-normalize */
2268 mul_qt_v3(bevp_first->quat, vec_1);
2269 mul_qt_v3(bevp_last->quat, vec_2);
2270 normalize_v3(vec_1);
2271 normalize_v3(vec_2);
2273 /* align the vector, can avoid this and it looks 98% OK but
2274 * better to align the angle quat roll's before comparing */
2276 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
2277 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
2278 axis_angle_to_quat(q, cross_tmp, angle);
2279 mul_qt_v3(q, vec_2);
2282 angle = angle_normalized_v3v3(vec_1, vec_2);
2284 /* flip rotation if needs be */
2285 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
2286 normalize_v3(cross_tmp);
2287 if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
2290 bevp2 = bl->bevpoints;
2291 bevp1 = bevp2 + (bl->nr - 1);
2296 ang_fac = angle * (1.0f - ((float)nr / bl->nr)); /* also works */
2298 axis_angle_to_quat(q, bevp1->dir, ang_fac);
2299 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2307 /* Need to correct quat for the first/last point,
2308 * this is so because previously it was only calculated
2309 * using it's own direction, which might not correspond
2310 * the twist of neighbor point.
2312 bevp1 = bl->bevpoints;
2314 minimum_twist_between_two_points(bevp1, bevp0);
2316 bevp2 = bl->bevpoints;
2317 bevp1 = bevp2 + (bl->nr - 1);
2319 minimum_twist_between_two_points(bevp1, bevp0);
2323 static void make_bevel_list_3D_tangent(BevList *bl)
2325 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2330 bevel_list_calc_bisect(bl);
2331 bevel_list_flip_tangents(bl);
2333 /* correct the tangents */
2334 bevp2 = bl->bevpoints;
2335 bevp1 = bevp2 + (bl->nr - 1);
2341 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2342 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
2343 normalize_v3(bevp1->tan);
2351 /* now for the real twist calc */
2352 bevp2 = bl->bevpoints;
2353 bevp1 = bevp2 + (bl->nr - 1);
2356 copy_v3_v3(bevp0_tan, bevp0->tan);
2360 /* make perpendicular, modify tan in place, is ok */
2362 float zero[3] = {0, 0, 0};
2364 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2365 normalize_v3(cross_tmp);
2366 tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
2368 /* bevp0 = bevp1; */ /* UNUSED */
2374 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2376 switch (twist_mode) {
2377 case CU_TWIST_TANGENT:
2378 make_bevel_list_3D_tangent(bl);
2380 case CU_TWIST_MINIMUM:
2381 make_bevel_list_3D_minimum_twist(bl);
2383 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2384 make_bevel_list_3D_zup(bl);
2389 bevel_list_smooth(bl, smooth_iter);
2391 bevel_list_apply_tilt(bl);
2394 /* only for 2 points */
2395 static void make_bevel_list_segment_3D(BevList *bl)
2399 BevPoint *bevp2 = bl->bevpoints;
2400 BevPoint *bevp1 = bevp2 + 1;
2402 /* simple quat/dir */
2403 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2404 normalize_v3(bevp1->dir);
2406 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2408 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2409 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2410 normalize_qt(bevp1->quat);
2411 copy_v3_v3(bevp2->dir, bevp1->dir);
2412 copy_qt_qt(bevp2->quat, bevp1->quat);
2415 /* only for 2 points */
2416 static void make_bevel_list_segment_2D(BevList *bl)
2418 BevPoint *bevp2 = bl->bevpoints;
2419 BevPoint *bevp1 = bevp2 + 1;
2421 const float x1 = bevp1->vec[0] - bevp2->vec[0];
2422 const float y1 = bevp1->vec[1] - bevp2->vec[1];
2424 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2425 bevp2->sina = bevp1->sina;
2426 bevp2->cosa = bevp1->cosa;
2428 /* fill in dir & quat */
2429 make_bevel_list_segment_3D(bl);
2432 static void make_bevel_list_2D(BevList *bl)
2434 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2435 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2437 BevPoint *bevp0, *bevp1, *bevp2;
2440 if (bl->poly != -1) {
2441 bevp2 = bl->bevpoints;
2442 bevp1 = bevp2 + (bl->nr - 1);
2447 bevp0 = bl->bevpoints;
2455 const float x1 = bevp1->vec[0] - bevp0->vec[0];
2456 const float x2 = bevp1->vec[0] - bevp2->vec[0];
2457 const float y1 = bevp1->vec[1] - bevp0->vec[1];
2458 const float y2 = bevp1->vec[1] - bevp2->vec[1];
2460 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2462 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2463 * no need for tricky tilt calculation as with 3D curves */
2464 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2465 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2466 /* done with inline make_bevel_list_3D_zup */
2473 /* correct non-cyclic cases */
2474 if (bl->poly == -1) {
2479 bevp = bl->bevpoints;
2480 angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
2481 bevp->sina = sinf(angle);
2482 bevp->cosa = cosf(angle);
2483 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2486 bevp = bl->bevpoints;
2487 bevp += (bl->nr - 1);
2488 angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
2489 bevp->sina = sinf(angle);
2490 bevp->cosa = cosf(angle);
2491 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2495 static void bevlist_firstlast_direction_calc_from_bpoint(Nurb *nu, BevList *bl)
2497 if (nu->pntsu > 1) {
2498 BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
2499 BevPoint *first_bevp, *last_bevp;
2501 first_bevp = bl->bevpoints;
2502 last_bevp = first_bevp + (bl->nr - 1);
2504 sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
2505 normalize_v3(first_bevp->dir);
2507 sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
2508 normalize_v3(last_bevp->dir);
2512 void BKE_curve_bevelList_free(ListBase *bev)
2514 BevList *bl, *blnext;
2515 for (bl = bev->first; bl != NULL; bl = blnext) {
2517 if (bl->seglen != NULL) {
2518 MEM_freeN(bl->seglen);
2520 if (bl->segbevcount != NULL) {
2521 MEM_freeN(bl->segbevcount);
2525 bev->first = bev->last = NULL;
2528 void BKE_curve_bevelList_make(Object *ob, ListBase *nurbs, bool for_render)
2531 * - convert all curves to polys, with indication of resol and flags for double-vertices
2532 * - possibly; do a smart vertice removal (in case Nurb)
2533 * - separate in individual blocks with BoundBox
2534 * - AutoHole detection
2537 /* this function needs an object, because of tflag and upflag */
2538 Curve *cu = ob->data;
2540 BezTriple *bezt, *prevbezt;
2542 BevList *bl, *blnew, *blnext;
2543 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2544 const float treshold = 0.00001f;
2547 struct BevelSort *sortdata, *sd, *sd1;
2548 int a, b, nr, poly, resolu = 0, len = 0, segcount;
2550 bool do_tilt, do_radius, do_weight;
2551 bool is_editmode = false;
2554 /* segbevcount alsp requires seglen. */
2555 const bool need_seglen =
2556 ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
2557 ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE);
2560 bev = &ob->curve_cache->bev;
2562 /* do we need to calculate the radius for each point? */
2563 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2565 /* STEP 1: MAKE POLYS */
2567 BKE_curve_bevelList_free(&ob->curve_cache->bev);
2569 if (cu->editnurb && ob->type != OB_FONT) {
2573 for (; nu; nu = nu->next) {
2575 if (nu->hide && is_editmode)
2578 /* check if we will calculate tilt data */
2579 do_tilt = CU_DO_TILT(cu, nu);
2580 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2583 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2584 * enforced in the UI but can go wrong possibly */
2585 if (!BKE_nurb_check_valid_u(nu)) {
2586 bl = MEM_callocN(sizeof(BevList) + 1 * sizeof(BevPoint), "makeBevelList1");
2587 BLI_addtail(bev, bl);
2589 bl->charidx = nu->charidx;
2592 if (for_render && cu->resolu_ren != 0)
2593 resolu = cu->resolu_ren;
2595 resolu = nu->resolu;
2597 segcount = SEGMENTSU(nu);
2599 if (nu->type == CU_POLY) {
2601 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList2");
2602 if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
2603 bl->seglen = MEM_mallocN(segcount * sizeof(float), "makeBevelList2_seglen");
2604 bl->segbevcount = MEM_mallocN(segcount * sizeof(int), "makeBevelList2_segbevcount");
2606 BLI_addtail(bev, bl);
2608 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2611 bl->charidx = nu->charidx;
2612 bevp = bl->bevpoints;
2615 seglen = bl->seglen;
2616 segbevcount = bl->segbevcount;
2619 copy_v3_v3(bevp->vec, bp->vec);
2620 bevp->alfa = bp->alfa;
2621 bevp->radius = bp->radius;
2622 bevp->weight = bp->weight;
2623 bevp->split_tag = true;
2625 if (seglen != NULL && len != 0) {
2626 *seglen = len_v3v3(bevp->vec, bp->vec);
2628 bevp->offset = *seglen;
2629 if (*seglen > treshold) *segbevcount = 1;
2630 else *segbevcount = 0;
2639 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2640 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2643 else if (nu->type == CU_BEZIER) {
2644 /* in case last point is not cyclic */
2645 len = segcount * resolu + 1;
2647 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelBPoints");
2648 if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
2649 bl->seglen = MEM_mallocN(segcount * sizeof(float), "makeBevelBPoints_seglen");
2650 bl->segbevcount = MEM_mallocN(segcount * sizeof(int), "makeBevelBPoints_segbevcount");
2652 BLI_addtail(bev, bl);
2654 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2655 bl->charidx = nu->charidx;
2657 bevp = bl->bevpoints;
2658 seglen = bl->seglen;
2659 segbevcount = bl->segbevcount;
2662 if (seglen != NULL) {
2669 if (nu->flagu & CU_NURB_CYCLIC) {
2671 prevbezt = nu->bezt + (nu->pntsu - 1);
2678 sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
2679 normalize_v3(bevp->dir);
2681 BLI_assert(segcount >= a);
2684 if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
2686 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2687 bevp->alfa = prevbezt->alfa;
2688 bevp->radius = prevbezt->radius;
2689 bevp->weight = prevbezt->weight;
2690 bevp->split_tag = true;
2691 bevp->dupe_tag = false;
2695 if (seglen != NULL) {
2696 *seglen = len_v3v3(prevbezt->vec[1], bezt->vec[1]);
2697 bevp->offset = *seglen;
2699 /* match segbevcount to the cleaned up bevel lists (see STEP 2) */
2700 if (bevp->offset > treshold) *segbevcount = 1;
2705 /* always do all three, to prevent data hanging around */
2708 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2709 for (j = 0; j < 3; j++) {
2710 BKE_curve_forward_diff_bezier(prevbezt->vec[1][j], prevbezt->vec[2][j],
2711 bezt->vec[0][j], bezt->vec[1][j],
2712 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2715 /* if both arrays are NULL do nothiong */
2716 alfa_bezpart(prevbezt, bezt, nu,
2717 do_tilt ? &bevp->alfa : NULL,
2718 do_radius ? &bevp->radius : NULL,
2719 do_weight ? &bevp->weight : NULL,
2720 resolu, sizeof(BevPoint));
2723 if (cu->twist_mode == CU_TWIST_TANGENT) {
2724 forward_diff_bezier_cotangent(prevbezt->vec[1], prevbezt->vec[2],
2725 bezt->vec[0], bezt->vec[1],
2726 bevp->tan, resolu, sizeof(BevPoint));
2729 /* indicate with handlecodes double points */
2730 if (prevbezt->h1 == prevbezt->h2) {
2731 if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2732 bevp->split_tag = true;
2735 if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2736 bevp->split_tag = true;
2737 else if (prevbezt->h2 == 0 || prevbezt->h2 == HD_VECT)
2738 bevp->split_tag = true;
2742 if (seglen != NULL) {
2745 for (j = 0; j < resolu; j++) {
2748 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
2749 /* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
2750 if (bevp->offset > treshold) {
2751 *seglen += bevp->offset;
2767 if ((nu->flagu & CU_NURB_CYCLIC) == 0) { /* not cyclic: endpoint */
2768 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2769 bevp->alfa = prevbezt->alfa;
2770 bevp->radius = prevbezt->radius;
2771 bevp->weight = prevbezt->weight;
2773 sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
2774 normalize_v3(bevp->dir);
2779 else if (nu->type == CU_NURBS) {
2780 if (nu->pntsv == 1) {
2781 len = (resolu * segcount);
2783 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList3");
2784 if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
2785 bl->seglen = MEM_mallocN(segcount * sizeof(float), "makeBevelList3_seglen");
2786 bl->segbevcount = MEM_mallocN(segcount * sizeof(int), "makeBevelList3_segbevcount");
2788 BLI_addtail(bev, bl);
2791 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2792 bl->charidx = nu->charidx;
2794 bevp = bl->bevpoints;
2795 seglen = bl->seglen;
2796 segbevcount = bl->segbevcount;
2798 BKE_nurb_makeCurve(nu, &bevp->vec[0],
2799 do_tilt ? &bevp->alfa : NULL,
2800 do_radius ? &bevp->radius : NULL,
2801 do_weight ? &bevp->weight : NULL,
2802 resolu, sizeof(BevPoint));
2804 /* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
2805 if (seglen != NULL) {
2813 /* We keep last bevel segment zero-length. */
2814 for (j = 0; j < ((nr == 1) ? (resolu - 1) : resolu); j++) {
2815 bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
2816 if (bevp->offset > treshold) {
2817 *seglen += bevp->offset;
2829 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2830 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2837 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2840 if (bl->nr) { /* null bevel items come from single points */
2841 bool is_cyclic = bl->poly != -1;
2844 bevp1 = bl->bevpoints;
2845 bevp0 = bevp1 + (nr - 1);
2848 bevp0 = bl->bevpoints;
2854 if (seglen != NULL) {
2855 if (fabsf(bevp1->offset) < treshold) {
2856 bevp0->dupe_tag = true;
2861 if (fabsf(bevp0->vec[0] - bevp1->vec[0]) < 0.00001f) {
2862 if (fabsf(bevp0->vec[1] - bevp1->vec[1]) < 0.00001f) {
2863 if (fabsf(bevp0->vec[2] - bevp1->vec[2]) < 0.00001f) {
2864 bevp0->dupe_tag = true;
2879 if (bl->nr && bl->dupe_nr) {
2880 nr = bl->nr - bl->dupe_nr + 1; /* +1 because vectorbezier sets flag too */
2881 blnew = MEM_mallocN(sizeof(BevList) + nr * sizeof(BevPoint), "makeBevelList4");
2882 memcpy(blnew, bl, sizeof(BevList));
2883 blnew->segbevcount = bl->segbevcount;
2884 blnew->seglen = bl->seglen;
2886 BLI_remlink(bev, bl);
2887 BLI_insertlinkbefore(bev, blnext, blnew); /* to make sure bevlijst is tuned with nurblist */
2888 bevp0 = bl->bevpoints;
2889 bevp1 = blnew->bevpoints;
2892 if (bevp0->dupe_tag == 0) {
2893 memcpy(bevp1, bevp0, sizeof(BevPoint));
2905 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2909 if (bl->nr && bl->poly >= 0) {
2917 /* find extreme left points, also test (turning) direction */
2919 sd = sortdata = MEM_mallocN(sizeof(struct BevelSort) * poly, "makeBevelList5");
2925 bevp = bl->bevpoints;
2928 if (min > bevp->vec[0]) {
2937 bevp = bl->bevpoints;
2939 bevp0 = bevp + (bl->nr - 1);
2942 bevp = bevp + (bl->nr - 1);
2944 bevp2 = bl->bevpoints;
2948 inp = ((bevp1->vec[0] - bevp0->vec[0]) * (bevp0->vec[1] - bevp2->vec[1]) +
2949 (bevp0->vec[1] - bevp1->vec[1]) * (bevp0->vec[0] - bevp2->vec[0]));
2961 qsort(sortdata, poly, sizeof(struct BevelSort), vergxcobev);
2964 for (a = 1; a < poly; a++, sd++) {
2965 bl = sd->bl; /* is bl a hole? */
2966 sd1 = sortdata + (a - 1);
2967 for (b = a - 1; b >= 0; b--, sd1--) { /* all polys to the left */
2968 if (sd1->bl->charidx == bl->charidx) { /* for text, only check matching char */
2969 if (bevelinside(sd1->bl, bl)) {
2970 bl->hole = 1 - sd1->bl->hole;
2977 /* turning direction */
2978 if ((cu->flag & CU_3D) == 0) {
2980 for (a = 0; a < poly; a++, sd++) {
2981 if (sd->bl->hole == sd->dir) {
2983 bevp1 = bl->bevpoints;
2984 bevp2 = bevp1 + (bl->nr - 1);
2987 SWAP(BevPoint, *bevp1, *bevp2);
2994 MEM_freeN(sortdata);
2997 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2998 if ((cu->flag & CU_3D) == 0) {
3000 for (bl = bev->first; bl; bl = bl->next) {
3002 BevPoint *bevp = bl->bevpoints;
3003 unit_qt(bevp->quat);
3005 else if (bl->nr == 2) { /* 2 pnt, treat separate */
3006 make_bevel_list_segment_2D(bl);
3009 make_bevel_list_2D(bl);
3015 for (bl = bev->first; bl; bl = bl->next) {
3017 BevPoint *bevp = bl->bevpoints;
3018 unit_qt(bevp->quat);
3020 else if (bl->nr == 2) { /* 2 pnt, treat separate */
3021 make_bevel_list_segment_3D(bl);
3024 make_bevel_list_3D(bl, (int)(resolu * cu->twist_smooth), cu->twist_mode);
3030 /* ****************** HANDLES ************** */
3032 static void calchandleNurb_intern(BezTriple *bezt, BezTriple *prev, BezTriple *next,
3033 bool is_fcurve, bool skip_align)
3035 /* defines to avoid confusion */
3036 #define p2_h1 (p2 - 3)
3037 #define p2_h2 (p2 + 3)