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"
55 #include "BKE_curve.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_free_animdata((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);
243 static void extern_local_curve(Curve *cu)
245 id_lib_extern((ID *)cu->vfont);
246 id_lib_extern((ID *)cu->vfontb);
247 id_lib_extern((ID *)cu->vfonti);
248 id_lib_extern((ID *)cu->vfontbi);
251 extern_local_matarar(cu->mat, cu->totcol);
255 void BKE_curve_make_local(Curve *cu)
257 Main *bmain = G.main;
259 int is_local = FALSE, is_lib = FALSE;
261 /* - when there are only lib users: don't do
262 * - when there are only local users: set flag
266 if (cu->id.lib == NULL)
269 if (cu->id.us == 1) {
270 id_clear_lib_data(bmain, &cu->id);
271 extern_local_curve(cu);
275 for (ob = bmain->object.first; ob && ELEM(0, is_lib, is_local); ob = ob->id.next) {
276 if (ob->data == cu) {
277 if (ob->id.lib) is_lib = TRUE;
278 else is_local = TRUE;
282 if (is_local && is_lib == FALSE) {
283 id_clear_lib_data(bmain, &cu->id);
284 extern_local_curve(cu);
286 else if (is_local && is_lib) {
287 Curve *cu_new = BKE_curve_copy(cu);
290 BKE_id_lib_local_paths(bmain, cu->id.lib, &cu_new->id);
292 for (ob = bmain->object.first; ob; ob = ob->id.next) {
293 if (ob->data == cu) {
294 if (ob->id.lib == NULL) {
304 /* Get list of nurbs from editnurbs structure */
305 ListBase *BKE_curve_editNurbs_get(Curve *cu)
308 return &cu->editnurb->nurbs;
314 short BKE_curve_type_get(Curve *cu)
326 for (nu = cu->nurb.first; nu; nu = nu->next) {
336 void BKE_curve_curve_dimension_update(Curve *cu)
338 ListBase *nurbs = BKE_curve_nurbs_get(cu);
339 Nurb *nu = nurbs->first;
341 if (cu->flag & CU_3D) {
342 for (; nu; nu = nu->next) {
347 for (; nu; nu = nu->next) {
351 /* since the handles are moved they need to be auto-located again */
352 if (nu->type == CU_BEZIER)
353 BKE_nurb_handles_calc(nu);
358 void BKE_curve_type_test(Object *ob)
360 ob->type = BKE_curve_type_get(ob->data);
362 if (ob->type == OB_CURVE)
363 BKE_curve_curve_dimension_update((Curve *)ob->data);
366 void BKE_curve_boundbox_calc(Curve *cu, float r_loc[3], float r_size[3])
369 float min[3], max[3];
370 float mloc[3], msize[3];
372 if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
375 if (!r_loc) r_loc = mloc;
376 if (!r_size) r_size = msize;
378 INIT_MINMAX(min, max);
379 if (!BKE_curve_minmax(cu, true, min, max)) {
380 min[0] = min[1] = min[2] = -1.0f;
381 max[0] = max[1] = max[2] = 1.0f;
384 mid_v3_v3v3(r_loc, min, max);
386 r_size[0] = (max[0] - min[0]) / 2.0f;
387 r_size[1] = (max[1] - min[1]) / 2.0f;
388 r_size[2] = (max[2] - min[2]) / 2.0f;
390 BKE_boundbox_init_from_minmax(bb, min, max);
392 bb->flag &= ~BOUNDBOX_DIRTY;
395 BoundBox *BKE_curve_boundbox_get(Object *ob)
397 Curve *cu = ob->data;
402 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
403 BKE_curve_texspace_calc(cu);
409 void BKE_curve_texspace_calc(Curve *cu)
411 float loc[3], size[3];
414 BKE_curve_boundbox_calc(cu, loc, size);
416 if (cu->texflag & CU_AUTOSPACE) {
417 for (a = 0; a < 3; a++) {
418 if (size[a] == 0.0f) size[a] = 1.0f;
419 else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
420 else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
423 copy_v3_v3(cu->loc, loc);
424 copy_v3_v3(cu->size, size);
429 void BKE_curve_texspace_get(Curve *cu, float r_loc[3], float r_rot[3], float r_size[3])
431 if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
432 BKE_curve_texspace_calc(cu);
435 if (r_loc) copy_v3_v3(r_loc, cu->loc);
436 if (r_rot) copy_v3_v3(r_rot, cu->rot);
437 if (r_size) copy_v3_v3(r_size, cu->size);
440 int BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
445 for (nu = nurb->first; nu; nu = nu->next) {
447 if (nu->type == CU_BEZIER) {
449 if (index - tot < tot_nu) {
450 copy_v3_v3(r_co, nu->bezt[index - tot].vec[1]);
455 tot_nu = nu->pntsu * nu->pntsv;
456 if (index - tot < tot_nu) {
457 copy_v3_v3(r_co, nu->bp[index - tot].vec);
467 int BKE_nurbList_verts_count(ListBase *nurb)
475 tot += 3 * nu->pntsu;
477 tot += nu->pntsu * nu->pntsv;
484 int BKE_nurbList_verts_count_without_handles(ListBase *nurb)
494 tot += nu->pntsu * nu->pntsv;
501 /* **************** NURBS ROUTINES ******************** */
503 void BKE_nurb_free(Nurb *nu)
506 if (nu == NULL) return;
515 MEM_freeN(nu->knotsu);
518 MEM_freeN(nu->knotsv);
520 /* if (nu->trim.first) freeNurblist(&(nu->trim)); */
527 void BKE_nurbList_free(ListBase *lb)
531 if (lb == NULL) return;
539 BLI_listbase_clear(lb);
542 Nurb *BKE_nurb_duplicate(Nurb *nu)
547 newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "duplicateNurb");
548 if (newnu == NULL) return NULL;
549 memcpy(newnu, nu, sizeof(Nurb));
553 (BezTriple *)MEM_mallocN((nu->pntsu) * sizeof(BezTriple), "duplicateNurb2");
554 memcpy(newnu->bezt, nu->bezt, nu->pntsu * sizeof(BezTriple));
557 len = nu->pntsu * nu->pntsv;
559 (BPoint *)MEM_mallocN((len) * sizeof(BPoint), "duplicateNurb3");
560 memcpy(newnu->bp, nu->bp, len * sizeof(BPoint));
562 newnu->knotsu = newnu->knotsv = NULL;
567 newnu->knotsu = MEM_mallocN(len * sizeof(float), "duplicateNurb4");
568 memcpy(newnu->knotsu, nu->knotsu, sizeof(float) * len);
571 if (nu->pntsv > 1 && nu->knotsv) {
574 newnu->knotsv = MEM_mallocN(len * sizeof(float), "duplicateNurb5");
575 memcpy(newnu->knotsv, nu->knotsv, sizeof(float) * len);
582 /* copy the nurb but allow for different number of points (to be copied after this) */
583 Nurb *BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
585 Nurb *newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "copyNurb");
586 memcpy(newnu, src, sizeof(Nurb));
588 if (pntsu == 1) SWAP(int, pntsu, pntsv);
589 newnu->pntsu = pntsu;
590 newnu->pntsv = pntsv;
593 newnu->bezt = (BezTriple *)MEM_mallocN(pntsu * pntsv * sizeof(BezTriple), "copyNurb2");
596 newnu->bp = (BPoint *)MEM_mallocN(pntsu * pntsv * sizeof(BPoint), "copyNurb3");
602 void BKE_nurbList_duplicate(ListBase *lb1, ListBase *lb2)
606 BKE_nurbList_free(lb1);
610 nun = BKE_nurb_duplicate(nu);
611 BLI_addtail(lb1, nun);
617 void BKE_nurb_test2D(Nurb *nu)
623 if ((nu->flag & CU_2D) == 0)
626 if (nu->type == CU_BEZIER) {
630 bezt->vec[0][2] = 0.0;
631 bezt->vec[1][2] = 0.0;
632 bezt->vec[2][2] = 0.0;
637 a = nu->pntsu * nu->pntsv;
646 /* if use_radius is truth, minmax will take points' radius into account,
647 * which will make boundbox closer to bevelled curve.
649 void BKE_nurb_minmax(Nurb *nu, bool use_radius, float min[3], float max[3])
656 if (nu->type == CU_BEZIER) {
661 float radius_vector[3];
662 radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
664 add_v3_v3v3(point, bezt->vec[1], radius_vector);
665 minmax_v3v3_v3(min, max, point);
667 sub_v3_v3v3(point, bezt->vec[1], radius_vector);
668 minmax_v3v3_v3(min, max, point);
671 minmax_v3v3_v3(min, max, bezt->vec[1]);
673 minmax_v3v3_v3(min, max, bezt->vec[0]);
674 minmax_v3v3_v3(min, max, bezt->vec[2]);
679 a = nu->pntsu * nu->pntsv;
682 if (nu->pntsv == 1 && use_radius) {
683 float radius_vector[3];
684 radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
686 add_v3_v3v3(point, bp->vec, radius_vector);
687 minmax_v3v3_v3(min, max, point);
689 sub_v3_v3v3(point, bp->vec, radius_vector);
690 minmax_v3v3_v3(min, max, point);
693 /* Surfaces doesn't use bevel, so no need to take radius into account. */
694 minmax_v3v3_v3(min, max, bp->vec);
701 /* be sure to call makeknots after this */
702 void BKE_nurb_points_add(Nurb *nu, int number)
704 BPoint *tmp = nu->bp;
706 nu->bp = (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
709 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
713 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
715 for (i = 0, tmp = nu->bp + nu->pntsu; i < number; i++, tmp++) {
722 void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
724 BezTriple *tmp = nu->bezt;
726 nu->bezt = (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
729 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
733 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
735 for (i = 0, tmp = nu->bezt + nu->pntsu; i < number; i++, tmp++) {
743 BezTriple *BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
745 BezTriple *bezt_next;
747 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
749 if (bezt == &nu->bezt[nu->pntsu - 1]) {
750 if (nu->flagu & CU_NURB_CYCLIC) {
751 bezt_next = nu->bezt;
758 bezt_next = bezt + 1;
764 BPoint *BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
768 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
770 if (bp == &nu->bp[nu->pntsu - 1]) {
771 if (nu->flagu & CU_NURB_CYCLIC) {
785 BezTriple *BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
787 BezTriple *bezt_prev;
789 BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
791 if (bezt == nu->bezt) {
792 if (nu->flagu & CU_NURB_CYCLIC) {
793 bezt_prev = &nu->bezt[nu->pntsu - 1];
800 bezt_prev = bezt - 1;
806 BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
810 BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
813 if (nu->flagu & CU_NURB_CYCLIC) {
814 bp_prev = &nu->bp[nu->pntsu - 1];
827 void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), struct BezTriple *bezt, float r_normal[3])
829 /* calculate the axis matrix from the spline */
830 float dir_prev[3], dir_next[3];
832 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
833 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
835 normalize_v3(dir_prev);
836 normalize_v3(dir_next);
838 add_v3_v3v3(r_normal, dir_prev, dir_next);
839 normalize_v3(r_normal);
842 void BKE_nurb_bezt_calc_plane(struct Nurb *nu, struct BezTriple *bezt, float r_plane[3])
844 float dir_prev[3], dir_next[3];
846 sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
847 sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
849 normalize_v3(dir_prev);
850 normalize_v3(dir_next);
852 cross_v3_v3v3(r_plane, dir_prev, dir_next);
853 if (normalize_v3(r_plane) < FLT_EPSILON) {
854 BezTriple *bezt_prev = BKE_nurb_bezt_get_prev(nu, bezt);
855 BezTriple *bezt_next = BKE_nurb_bezt_get_next(nu, bezt);
858 sub_v3_v3v3(dir_prev, bezt_prev->vec[1], bezt->vec[1]);
859 normalize_v3(dir_prev);
862 sub_v3_v3v3(dir_next, bezt->vec[1], bezt_next->vec[1]);
863 normalize_v3(dir_next);
865 cross_v3_v3v3(r_plane, dir_prev, dir_next);
868 /* matches with bones more closely */
870 float dir_mid[3], tvec[3];
871 add_v3_v3v3(dir_mid, dir_prev, dir_next);
872 cross_v3_v3v3(tvec, r_plane, dir_mid);
873 copy_v3_v3(r_plane, tvec);
876 normalize_v3(r_plane);
879 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
882 static void calcknots(float *knots, const int pnts, const short order, const short flag)
884 /* knots: number of pnts NOT corrected for cyclic */
885 const int pnts_order = pnts + order;
889 switch (flag & (CU_NURB_ENDPOINT | CU_NURB_BEZIER)) {
890 case CU_NURB_ENDPOINT:
892 for (a = 1; a <= pnts_order; a++) {
894 if (a >= order && a <= pnts)
899 /* Warning, the order MUST be 2 or 4,
900 * if this is not enforced, the displist will be corrupt */
903 for (a = 0; a < pnts_order; a++) {
904 knots[a] = floorf(k);
908 else if (order == 3) {
910 for (a = 0; a < pnts_order; a++) {
911 if (a >= order && a <= pnts)
913 knots[a] = floorf(k);
917 printf("bez nurb curve order is not 3 or 4, should never happen\n");
921 for (a = 0; a < pnts_order; a++) {
928 static void makecyclicknots(float *knots, int pnts, short order)
929 /* pnts, order: number of pnts NOT corrected for cyclic */
938 /* do first long rows (order -1), remove identical knots at endpoints */
941 for (a = 1; a < order2; a++) {
942 if (knots[b] != knots[b - a])
946 knots[pnts + order - 2] += 1.0f;
950 c = pnts + order + order2;
951 for (a = pnts + order2; a < c; a++) {
952 knots[a] = knots[a - 1] + (knots[b] - knots[b - 1]);
959 static void makeknots(Nurb *nu, short uv)
961 if (nu->type == CU_NURBS) {
964 MEM_freeN(nu->knotsu);
965 if (BKE_nurb_check_valid_u(nu)) {
966 nu->knotsu = MEM_callocN(4 + sizeof(float) * KNOTSU(nu), "makeknots");
967 if (nu->flagu & CU_NURB_CYCLIC) {
968 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
969 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
972 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
980 MEM_freeN(nu->knotsv);
981 if (BKE_nurb_check_valid_v(nu)) {
982 nu->knotsv = MEM_callocN(4 + sizeof(float) * KNOTSV(nu), "makeknots");
983 if (nu->flagv & CU_NURB_CYCLIC) {
984 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
985 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
988 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
998 void BKE_nurb_knot_calc_u(Nurb *nu)
1003 void BKE_nurb_knot_calc_v(Nurb *nu)
1008 static void basisNurb(float t, short order, int pnts, float *knots, float *basis, int *start, int *end)
1011 int i, i1 = 0, i2 = 0, j, orderpluspnts, opp2, o2;
1013 orderpluspnts = order + pnts;
1014 opp2 = orderpluspnts - 1;
1016 /* this is for float inaccuracy */
1019 else if (t > knots[opp2])
1022 /* this part is order '1' */
1024 for (i = 0; i < opp2; i++) {
1025 if (knots[i] != knots[i + 1] && t >= knots[i] && t <= knots[i + 1]) {
1042 /* this is order 2, 3, ... */
1043 for (j = 2; j <= order; j++) {
1045 if (i2 + j >= orderpluspnts) i2 = opp2 - j;
1047 for (i = i1; i <= i2; i++) {
1048 if (basis[i] != 0.0f)
1049 d = ((t - knots[i]) * basis[i]) / (knots[i + j - 1] - knots[i]);
1053 if (basis[i + 1] != 0.0f)
1054 e = ((knots[i + j] - t) * basis[i + 1]) / (knots[i + j] - knots[i + 1]);
1065 for (i = i1; i <= i2; i++) {
1066 if (basis[i] > 0.0f) {
1068 if (*start == 1000) *start = i;
1074 void BKE_nurb_makeFaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
1075 /* coord_array has to be (3 * 4 * resolu * resolv) in size, and zero-ed */
1078 float *basisu, *basis, *basisv, *sum, *fp, *in;
1079 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
1080 int i, j, iofs, jofs, cycl, len, curu, curv;
1081 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
1083 int totu = nu->pntsu * resolu, totv = nu->pntsv * resolv;
1085 if (nu->knotsu == NULL || nu->knotsv == NULL)
1087 if (nu->orderu > nu->pntsu)
1089 if (nu->orderv > nu->pntsv)
1091 if (coord_array == NULL)
1094 /* allocate and initialize */
1099 sum = (float *)MEM_callocN(sizeof(float) * len, "makeNurbfaces1");
1108 i = nu->pntsu * nu->pntsv;
1111 if (bp->vec[3] != 1.0f) {
1119 ustart = fp[nu->orderu - 1];
1120 if (nu->flagu & CU_NURB_CYCLIC)
1121 uend = fp[nu->pntsu + nu->orderu - 1];
1123 uend = fp[nu->pntsu];
1124 ustep = (uend - ustart) / ((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
1126 basisu = (float *)MEM_mallocN(sizeof(float) * KNOTSU(nu), "makeNurbfaces3");
1129 vstart = fp[nu->orderv - 1];
1131 if (nu->flagv & CU_NURB_CYCLIC)
1132 vend = fp[nu->pntsv + nu->orderv - 1];
1134 vend = fp[nu->pntsv];
1135 vstep = (vend - vstart) / ((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
1138 basisv = (float *)MEM_mallocN(sizeof(float) * len * totv, "makeNurbfaces3");
1139 jstart = (int *)MEM_mallocN(sizeof(float) * totv, "makeNurbfaces4");
1140 jend = (int *)MEM_mallocN(sizeof(float) * totv, "makeNurbfaces5");
1142 /* precalculation of basisv and jstart, jend */
1143 if (nu->flagv & CU_NURB_CYCLIC)
1144 cycl = nu->orderv - 1;
1150 basisNurb(v, nu->orderv, nu->pntsv + cycl, nu->knotsv, basis, jstart + curv, jend + curv);
1151 basis += KNOTSV(nu);
1155 if (nu->flagu & CU_NURB_CYCLIC)
1156 cycl = nu->orderu - 1;
1163 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1168 jsta = jstart[curv];
1175 for (j = jsta; j <= jen; j++) {
1178 jofs = (j - nu->pntsv);
1181 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1183 for (i = istart; i <= iend; i++, fp++) {
1184 if (i >= nu->pntsu) {
1185 iofs = i - nu->pntsu;
1186 bp = nu->bp + nu->pntsu * jofs + iofs;
1192 *fp = basisu[i] * basis[j] * bp->vec[3];
1196 *fp = basisu[i] * basis[j];
1202 for (j = jsta; j <= jen; j++) {
1203 for (i = istart; i <= iend; i++, fp++) {
1209 /* one! (1.0) real point now */
1211 for (j = jsta; j <= jen; j++) {
1214 jofs = (j - nu->pntsv);
1217 bp = nu->bp + nu->pntsu * jofs + istart - 1;
1219 for (i = istart; i <= iend; i++, fp++) {
1220 if (i >= nu->pntsu) {
1221 iofs = i - nu->pntsu;
1222 bp = nu->bp + nu->pntsu * jofs + iofs;
1228 madd_v3_v3fl(in, bp->vec, *fp);
1234 basis += KNOTSV(nu);
1238 in = (float *) (((unsigned char *) in) + (rowstride - 3 * totv * sizeof(*in)));
1250 * \param coord_array Has to be 3 * 4 * pntsu * resolu in size and zero-ed
1251 * \param tilt_array set when non-NULL
1252 * \param radius_array set when non-NULL
1254 void BKE_nurb_makeCurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array,
1255 int resolu, int stride)
1258 float u, ustart, uend, ustep, sumdiv;
1259 float *basisu, *sum, *fp;
1260 float *coord_fp = coord_array, *tilt_fp = tilt_array, *radius_fp = radius_array, *weight_fp = weight_array;
1261 int i, len, istart, iend, cycl;
1263 if (nu->knotsu == NULL)
1265 if (nu->orderu > nu->pntsu)
1267 if (coord_array == NULL)
1270 /* allocate and initialize */
1274 sum = (float *)MEM_callocN(sizeof(float) * len, "makeNurbcurve1");
1276 resolu = (resolu * SEGMENTSU(nu));
1284 ustart = fp[nu->orderu - 1];
1285 if (nu->flagu & CU_NURB_CYCLIC)
1286 uend = fp[nu->pntsu + nu->orderu - 1];
1288 uend = fp[nu->pntsu];
1289 ustep = (uend - ustart) / (resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
1291 basisu = (float *)MEM_mallocN(sizeof(float) * KNOTSU(nu), "makeNurbcurve3");
1293 if (nu->flagu & CU_NURB_CYCLIC)
1294 cycl = nu->orderu - 1;
1300 basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
1305 bp = nu->bp + istart - 1;
1306 for (i = istart; i <= iend; i++, fp++) {
1308 bp = nu->bp + (i - nu->pntsu);
1312 *fp = basisu[i] * bp->vec[3];
1315 if ((sumdiv != 0.0f) && (sumdiv < 0.999f || sumdiv > 1.001f)) {
1316 /* is normalizing needed? */
1318 for (i = istart; i <= iend; i++, fp++) {
1323 /* one! (1.0) real point */
1325 bp = nu->bp + istart - 1;
1326 for (i = istart; i <= iend; i++, fp++) {
1328 bp = nu->bp + (i - nu->pntsu);
1333 madd_v3_v3fl(coord_fp, bp->vec, *fp);
1336 (*tilt_fp) += (*fp) * bp->alfa;
1339 (*radius_fp) += (*fp) * bp->radius;
1342 (*weight_fp) += (*fp) * bp->weight;
1346 coord_fp = (float *)(((char *)coord_fp) + stride);
1349 tilt_fp = (float *)(((char *)tilt_fp) + stride);
1351 radius_fp = (float *)(((char *)radius_fp) + stride);
1353 weight_fp = (float *)(((char *)weight_fp) + stride);
1363 /* forward differencing method for bezier curve */
1364 void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1366 float rt0, rt1, rt2, rt3, f;
1371 rt1 = 3.0f * (q1 - q0) / f;
1373 rt2 = 3.0f * (q0 - 2.0f * q1 + q2) / f;
1375 rt3 = (q3 - q0 + 3.0f * (q1 - q2)) / f;
1378 q1 = rt1 + rt2 + rt3;
1379 q2 = 2 * rt2 + 6 * rt3;
1382 for (a = 0; a <= it; a++) {
1384 p = (float *)(((char *)p) + stride);
1391 static void forward_diff_bezier_cotangent(const float p0[3], const float p1[3], const float p2[3], const float p3[3],
1392 float p[3], int it, int stride)
1394 /* note that these are not perpendicular to the curve
1395 * they need to be rotated for this,
1397 * This could also be optimized like BKE_curve_forward_diff_bezier */
1399 for (a = 0; a <= it; a++) {
1400 float t = (float)a / (float)it;
1403 for (i = 0; i < 3; i++) {
1404 p[i] = (-6.0f * t + 6.0f) * p0[i] +
1405 ( 18.0f * t - 12.0f) * p1[i] +
1406 (-18.0f * t + 6.0f) * p2[i] +
1407 ( 6.0f * t) * p3[i];
1410 p = (float *)(((char *)p) + stride);
1414 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1416 float *BKE_curve_surf_make_orco(Object *ob)
1418 /* Note: this function is used in convertblender only atm, so
1419 * suppose nonzero curve's render resolution should always be used */
1420 Curve *cu = ob->data;
1425 float *fp, *coord_array;
1427 /* first calculate the size of the datablock */
1428 nu = cu->nurb.first;
1430 /* as we want to avoid the seam in a cyclic nurbs
1431 * texture wrapping, reserve extra orco data space to save these extra needed
1432 * vertex based UV coordinates for the meridian vertices.
1433 * Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1434 * the renderface/vert construction.
1436 * See also convertblender.c: init_render_surf()
1439 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1440 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1442 sizeu = nu->pntsu * resolu;
1443 sizev = nu->pntsv * resolv;
1444 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1445 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1446 if (nu->pntsv > 1) tot += sizeu * sizev;
1450 /* makeNurbfaces wants zeros */
1451 fp = coord_array = MEM_callocN(3 * sizeof(float) * tot, "make_orco");
1453 nu = cu->nurb.first;
1455 resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1456 resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1458 if (nu->pntsv > 1) {
1459 sizeu = nu->pntsu * resolu;
1460 sizev = nu->pntsv * resolv;
1462 if (nu->flagu & CU_NURB_CYCLIC)
1464 if (nu->flagv & CU_NURB_CYCLIC)
1467 if (cu->flag & CU_UV_ORCO) {
1468 for (b = 0; b < sizeu; b++) {
1469 for (a = 0; a < sizev; a++) {
1474 fp[0] = -1.0f + 2.0f * ((float)a) / (sizev - 1);
1479 fp[1] = -1.0f + 2.0f * ((float)b) / (sizeu - 1);
1488 int size = (nu->pntsu * resolu) * (nu->pntsv * resolv) * 3 * sizeof(float);
1489 float *_tdata = MEM_callocN(size, "temp data");
1490 float *tdata = _tdata;
1492 BKE_nurb_makeFaces(nu, tdata, 0, resolu, resolv);
1494 for (b = 0; b < sizeu; b++) {
1496 if (b == sizeu - 1 && (nu->flagu & CU_NURB_CYCLIC))
1499 for (a = 0; a < sizev; a++) {
1501 if (a == sizev - 1 && (nu->flagv & CU_NURB_CYCLIC))
1504 tdata = _tdata + 3 * (use_b * (nu->pntsv * resolv) + use_a);
1506 fp[0] = (tdata[0] - cu->loc[0]) / cu->size[0];
1507 fp[1] = (tdata[1] - cu->loc[1]) / cu->size[1];
1508 fp[2] = (tdata[2] - cu->loc[2]) / cu->size[2];
1523 /* NOTE: This routine is tied to the order of vertex
1524 * built by displist and as passed to the renderer.
1526 float *BKE_curve_make_orco(Scene *scene, Object *ob, int *r_numVerts)
1528 Curve *cu = ob->data;
1531 float *fp, *coord_array;
1532 ListBase disp = {NULL, NULL};
1534 BKE_displist_make_curveTypes_forOrco(scene, ob, &disp);
1537 for (dl = disp.first; dl; dl = dl->next) {
1538 if (dl->type == DL_INDEX3) {
1541 else if (dl->type == DL_SURF) {
1542 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only
1543 * (closed circle beveling)
1545 if (dl->flag & DL_CYCL_U) {
1546 if (dl->flag & DL_CYCL_V)
1547 numVerts += (dl->parts + 1) * (dl->nr + 1);
1549 numVerts += dl->parts * (dl->nr + 1);
1551 else if (dl->flag & DL_CYCL_V) {
1552 numVerts += (dl->parts + 1) * dl->nr;
1555 numVerts += dl->parts * dl->nr;
1560 *r_numVerts = numVerts;
1562 fp = coord_array = MEM_mallocN(3 * sizeof(float) * numVerts, "cu_orco");
1563 for (dl = disp.first; dl; dl = dl->next) {
1564 if (dl->type == DL_INDEX3) {
1565 for (u = 0; u < dl->nr; u++, fp += 3) {
1566 if (cu->flag & CU_UV_ORCO) {
1567 fp[0] = 2.0f * u / (dl->nr - 1) - 1.0f;
1572 copy_v3_v3(fp, &dl->verts[u * 3]);
1574 fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1575 fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1576 fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1580 else if (dl->type == DL_SURF) {
1581 int sizeu = dl->nr, sizev = dl->parts;
1583 /* exception as handled in convertblender.c too */
1584 if (dl->flag & DL_CYCL_U) {
1586 if (dl->flag & DL_CYCL_V)
1589 else if (dl->flag & DL_CYCL_V) {
1593 for (u = 0; u < sizev; u++) {
1594 for (v = 0; v < sizeu; v++, fp += 3) {
1595 if (cu->flag & CU_UV_ORCO) {
1596 fp[0] = 2.0f * u / (sizev - 1) - 1.0f;
1597 fp[1] = 2.0f * v / (sizeu - 1) - 1.0f;
1602 int realv = v % dl->nr;
1603 int realu = u % dl->parts;
1605 vert = dl->verts + 3 * (dl->nr * realu + realv);
1606 copy_v3_v3(fp, vert);
1608 fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
1609 fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
1610 fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
1617 BKE_displist_free(&disp);
1623 /* ***************** BEVEL ****************** */
1625 void BKE_curve_bevel_make(Scene *scene, Object *ob, ListBase *disp,
1626 const bool for_render, const bool use_render_resolution)
1628 DispList *dl, *dlnew;
1630 float *fp, facx, facy, angle, dangle;
1634 BLI_listbase_clear(disp);
1636 /* if a font object is being edited, then do nothing */
1637 // XXX if ( ob == obedit && ob->type == OB_FONT ) return;
1640 if (cu->bevobj->type != OB_CURVE)
1643 bevcu = cu->bevobj->data;
1644 if (bevcu->ext1 == 0.0f && bevcu->ext2 == 0.0f) {
1645 ListBase bevdisp = {NULL, NULL};
1646 facx = cu->bevobj->size[0];
1647 facy = cu->bevobj->size[1];
1650 BKE_displist_make_curveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution);
1653 else if (cu->bevobj->curve_cache) {
1654 dl = cu->bevobj->curve_cache->disp.first;
1657 BLI_assert(cu->bevobj->curve_cache != NULL);
1662 if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
1663 dlnew = MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1665 dlnew->verts = MEM_mallocN(3 * sizeof(float) * dl->parts * dl->nr, "makebevelcurve1");
1666 memcpy(dlnew->verts, dl->verts, 3 * sizeof(float) * dl->parts * dl->nr);
1668 if (dlnew->type == DL_SEGM)
1669 dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
1671 BLI_addtail(disp, dlnew);
1673 nr = dlnew->parts * dlnew->nr;
1675 fp[2] = fp[1] * facy;
1676 fp[1] = -fp[0] * facx;
1684 BKE_displist_free(&bevdisp);
1687 else if (cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
1690 else if (cu->ext2 == 0.0f) {
1691 dl = MEM_callocN(sizeof(DispList), "makebevelcurve2");
1692 dl->verts = MEM_mallocN(2 * 3 * sizeof(float), "makebevelcurve2");
1693 BLI_addtail(disp, dl);
1696 dl->flag = DL_FRONT_CURVE | DL_BACK_CURVE;
1700 fp[0] = fp[1] = 0.0;
1702 fp[3] = fp[4] = 0.0;
1705 else if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0 && cu->ext1 == 0.0f) { // we make a full round bevel in that case
1706 nr = 4 + 2 * cu->bevresol;
1708 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1709 dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p1");
1710 BLI_addtail(disp, dl);
1713 dl->flag = DL_BACK_CURVE;
1718 dangle = (2.0f * (float)M_PI / (nr));
1719 angle = -(nr - 1) * dangle;
1721 for (a = 0; a < nr; a++) {
1723 fp[1] = (cosf(angle) * (cu->ext2));
1724 fp[2] = (sinf(angle) * (cu->ext2)) - cu->ext1;
1732 /* bevel now in three parts, for proper vertex normals */
1735 if ((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1736 dnr = nr = 2 + cu->bevresol;
1737 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1738 nr = 3 + 2 * cu->bevresol;
1740 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1741 dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p1");
1742 BLI_addtail(disp, dl);
1745 dl->flag = DL_BACK_CURVE;
1750 dangle = (0.5 * M_PI / (dnr - 1));
1751 angle = -(nr - 1) * dangle;
1753 for (a = 0; a < nr; a++) {
1755 fp[1] = (float)(cosf(angle) * (cu->ext2));
1756 fp[2] = (float)(sinf(angle) * (cu->ext2)) - cu->ext1;
1762 /* part 2, sidefaces */
1763 if (cu->ext1 != 0.0f) {
1766 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1767 dl->verts = MEM_callocN(nr * 3 * sizeof(float), "makebevelcurve p2");
1768 BLI_addtail(disp, dl);
1779 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0) {
1780 dl = MEM_dupallocN(dl);
1781 dl->verts = MEM_dupallocN(dl->verts);
1782 BLI_addtail(disp, dl);
1793 if ((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1794 dnr = nr = 2 + cu->bevresol;
1795 if ( (cu->flag & (CU_FRONT | CU_BACK)) == 0)
1796 nr = 3 + 2 * cu->bevresol;
1798 dl = MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1799 dl->verts = MEM_mallocN(nr * 3 * sizeof(float), "makebevelcurve p3");
1800 BLI_addtail(disp, dl);
1802 dl->flag = DL_FRONT_CURVE;
1809 dangle = (0.5 * M_PI / (dnr - 1));
1811 for (a = 0; a < nr; a++) {
1813 fp[1] = (float)(cosf(angle) * (cu->ext2));
1814 fp[2] = (float)(sinf(angle) * (cu->ext2)) + cu->ext1;
1822 static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
1823 short cox, short coy,
1824 float *lambda, float *mu, float vec[3])
1828 * 0: no intersection of segments
1829 * 1: exact intersection of segments
1830 * 2: cross-intersection of segments
1834 deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
1838 *lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
1839 *lambda = -(*lambda / deler);
1841 deler = v3[coy] - v4[coy];
1843 deler = v3[cox] - v4[cox];
1844 *mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
1847 *mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
1849 vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
1850 vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
1852 if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
1853 if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f)
1861 static bool bevelinside(BevList *bl1, BevList *bl2)
1863 /* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
1864 /* returns '1' if correct hole */
1865 BevPoint *bevp, *prevbevp;
1866 float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
1867 int nr, links = 0, rechts = 0, mode;
1869 /* take first vertex of possible hole */
1871 bevp = (BevPoint *)(bl2 + 1);
1872 hvec1[0] = bevp->vec[0];
1873 hvec1[1] = bevp->vec[1];
1875 copy_v3_v3(hvec2, hvec1);
1878 /* test it with all edges of potential surounding poly */
1879 /* count number of transitions left-right */
1881 bevp = (BevPoint *)(bl1 + 1);
1883 prevbevp = bevp + (nr - 1);
1886 min = prevbevp->vec[1];
1890 max = prevbevp->vec[1];
1893 if (min <= hvec1[1] && max >= hvec1[1]) {
1894 /* there's a transition, calc intersection point */
1895 mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1896 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1897 * only allow for one situation: we choose lab= 1.0
1899 if (mode >= 0 && lab != 0.0f) {
1900 if (vec[0] < hvec1[0]) links++;
1909 return (links & 1) && (rechts & 1);
1919 static int vergxcobev(const void *a1, const void *a2)
1921 const struct BevelSort *x1 = a1, *x2 = a2;
1923 if (x1->left > x2->left)
1925 else if (x1->left < x2->left)
1930 /* this function cannot be replaced with atan2, but why? */
1932 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1934 float t01, t02, x3, y3;
1936 t01 = sqrtf(x1 * x1 + y1 * y1);
1937 t02 = sqrtf(x2 * x2 + y2 * y2);
1948 t02 = x1 * x2 + y1 * y2;
1949 if (fabsf(t02) >= 1.0f)
1952 t02 = (saacos(t02)) / 2.0f;
1960 if (x3 == 0 && y3 == 0) {
1965 t01 = sqrtf(x3 * x3 + y3 * y3);
1975 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array,
1976 float *weight_array, int resolu, int stride)
1978 BezTriple *pprev, *next, *last;
1979 float fac, dfac, t[4];
1982 if (tilt_array == NULL && radius_array == NULL)
1985 last = nu->bezt + (nu->pntsu - 1);
1987 /* returns a point */
1988 if (prevbezt == nu->bezt) {
1989 if (nu->flagu & CU_NURB_CYCLIC)
1995 pprev = prevbezt - 1;
1999 if (nu->flagu & CU_NURB_CYCLIC)
2008 dfac = 1.0f / (float)resolu;
2010 for (a = 0; a < resolu; a++, fac += dfac) {
2012 if (nu->tilt_interp == KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
2013 *tilt_array = prevbezt->alfa +
2014 (bezt->alfa - prevbezt->alfa) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2017 key_curve_position_weights(fac, t, nu->tilt_interp);
2018 *tilt_array = t[0] * pprev->alfa + t[1] * prevbezt->alfa + t[2] * bezt->alfa + t[3] * next->alfa;
2021 tilt_array = (float *)(((char *)tilt_array) + stride);
2025 if (nu->radius_interp == KEY_CU_EASE) {
2026 /* Support 2.47 ease interp
2027 * Note! - this only takes the 2 points into account,
2028 * giving much more localized results to changes in radius, sometimes you want that */
2029 *radius_array = prevbezt->radius +
2030 (bezt->radius - prevbezt->radius) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2034 /* reuse interpolation from tilt if we can */
2035 if (tilt_array == NULL || nu->tilt_interp != nu->radius_interp) {
2036 key_curve_position_weights(fac, t, nu->radius_interp);
2038 *radius_array = t[0] * pprev->radius + t[1] * prevbezt->radius +
2039 t[2] * bezt->radius + t[3] * next->radius;
2042 radius_array = (float *)(((char *)radius_array) + stride);
2046 /* basic interpolation for now, could copy tilt interp too */
2047 *weight_array = prevbezt->weight +
2048 (bezt->weight - prevbezt->weight) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
2050 weight_array = (float *)(((char *)weight_array) + stride);
2055 /* make_bevel_list_3D_* funcs, at a minimum these must
2056 * fill in the bezp->quat and bezp->dir values */
2058 /* utility for make_bevel_list_3D_* funcs */
2059 static void bevel_list_calc_bisect(BevList *bl)
2061 BevPoint *bevp2, *bevp1, *bevp0;
2063 bool is_cyclic = bl->poly != -1;
2066 bevp2 = (BevPoint *)(bl + 1);
2067 bevp1 = bevp2 + (bl->nr - 1);
2072 /* If spline is not cyclic, direction of first and
2073 * last bevel points matches direction of CV handle.
2075 * This is getting calculated earlier when we know
2076 * CV's handles and here we might simply skip evaluation
2077 * of direction for this guys.
2080 bevp0 = (BevPoint *)(bl + 1);
2088 /* totally simple */
2089 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2096 static void bevel_list_flip_tangents(BevList *bl)
2098 BevPoint *bevp2, *bevp1, *bevp0;
2101 bevp2 = (BevPoint *)(bl + 1);
2102 bevp1 = bevp2 + (bl->nr - 1);
2107 if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f))
2108 negate_v3(bevp1->tan);
2115 /* apply user tilt */
2116 static void bevel_list_apply_tilt(BevList *bl)
2118 BevPoint *bevp2, *bevp1;
2122 bevp2 = (BevPoint *)(bl + 1);
2123 bevp1 = bevp2 + (bl->nr - 1);
2127 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2128 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2129 normalize_qt(bevp1->quat);
2135 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
2136 static void bevel_list_smooth(BevList *bl, int smooth_iter)
2138 BevPoint *bevp2, *bevp1, *bevp0;
2142 float bevp0_quat[4];
2145 for (a = 0; a < smooth_iter; a++) {
2146 bevp2 = (BevPoint *)(bl + 1);
2147 bevp1 = bevp2 + (bl->nr - 1);
2152 if (bl->poly == -1) { /* check its not cyclic */
2153 /* skip the first point */
2154 /* bevp0 = bevp1; */
2165 copy_qt_qt(bevp0_quat, bevp0->quat);
2168 /* interpolate quats */
2169 float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
2170 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
2173 mul_qt_v3(q, zaxis);
2174 cross_v3_v3v3(cross, zaxis, bevp1->dir);
2175 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
2178 copy_qt_qt(bevp0_quat, bevp1->quat);
2179 mul_qt_qtqt(q, q2, q);
2180 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
2181 normalize_qt(bevp1->quat);
2183 /* bevp0 = bevp1; */ /* UNUSED */
2190 static void make_bevel_list_3D_zup(BevList *bl)
2192 BevPoint *bevp = (BevPoint *)(bl + 1);
2195 bevel_list_calc_bisect(bl);
2198 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2203 static void minimum_twist_between_two_points(BevPoint *current_point, BevPoint *previous_point)
2205 float angle = angle_normalized_v3v3(previous_point->dir, current_point->dir);
2208 if (angle > 0.0f) { /* otherwise we can keep as is */
2210 cross_v3_v3v3(cross_tmp, previous_point->dir, current_point->dir);
2211 axis_angle_to_quat(q, cross_tmp, angle);
2212 mul_qt_qtqt(current_point->quat, q, previous_point->quat);
2215 copy_qt_qt(current_point->quat, previous_point->quat);
2219 static void make_bevel_list_3D_minimum_twist(BevList *bl)
2221 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2225 bevel_list_calc_bisect(bl);
2227 bevp2 = (BevPoint *)(bl + 1);
2228 bevp1 = bevp2 + (bl->nr - 1);
2234 if (nr + 4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
2235 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2238 minimum_twist_between_two_points(bevp1, bevp0);
2246 if (bl->poly != -1) { /* check for cyclic */
2248 /* Need to correct for the start/end points not matching
2249 * do this by calculating the tilt angle difference, then apply
2250 * the rotation gradually over the entire curve
2252 * note that the split is between last and second last, rather than first/last as youd expect.
2254 * real order is like this
2255 * 0,1,2,3,4 --> 1,2,3,4,0
2257 * this is why we compare last with second last
2259 float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
2261 BevPoint *bevp_first;
2262 BevPoint *bevp_last;
2265 bevp_first = (BevPoint *)(bl + 1);
2266 bevp_first += bl->nr - 1;
2267 bevp_last = bevp_first;
2270 /* quats and vec's are normalized, should not need to re-normalize */
2271 mul_qt_v3(bevp_first->quat, vec_1);
2272 mul_qt_v3(bevp_last->quat, vec_2);
2273 normalize_v3(vec_1);
2274 normalize_v3(vec_2);
2276 /* align the vector, can avoid this and it looks 98% OK but
2277 * better to align the angle quat roll's before comparing */
2279 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
2280 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
2281 axis_angle_to_quat(q, cross_tmp, angle);
2282 mul_qt_v3(q, vec_2);
2285 angle = angle_normalized_v3v3(vec_1, vec_2);
2287 /* flip rotation if needs be */
2288 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
2289 normalize_v3(cross_tmp);
2290 if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
2293 bevp2 = (BevPoint *)(bl + 1);
2294 bevp1 = bevp2 + (bl->nr - 1);
2299 ang_fac = angle * (1.0f - ((float)nr / bl->nr)); /* also works */
2301 axis_angle_to_quat(q, bevp1->dir, ang_fac);
2302 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2310 /* Need to correct quat for the first/last point,
2311 * this is so because previously it was only calculated
2312 * using it's own direction, which might not correspond
2313 * the twist of neighbor point.
2315 bevp1 = (BevPoint *)(bl + 1);
2317 minimum_twist_between_two_points(bevp1, bevp0);
2319 bevp2 = (BevPoint *)(bl + 1);
2320 bevp1 = bevp2 + (bl->nr - 1);
2322 minimum_twist_between_two_points(bevp1, bevp0);
2326 static void make_bevel_list_3D_tangent(BevList *bl)
2328 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
2333 bevel_list_calc_bisect(bl);
2334 bevel_list_flip_tangents(bl);
2336 /* correct the tangents */
2337 bevp2 = (BevPoint *)(bl + 1);
2338 bevp1 = bevp2 + (bl->nr - 1);
2344 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2345 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
2346 normalize_v3(bevp1->tan);
2354 /* now for the real twist calc */
2355 bevp2 = (BevPoint *)(bl + 1);
2356 bevp1 = bevp2 + (bl->nr - 1);
2359 copy_v3_v3(bevp0_tan, bevp0->tan);
2363 /* make perpendicular, modify tan in place, is ok */
2365 float zero[3] = {0, 0, 0};
2367 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
2368 normalize_v3(cross_tmp);
2369 tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
2371 /* bevp0 = bevp1; */ /* UNUSED */
2377 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2379 switch (twist_mode) {
2380 case CU_TWIST_TANGENT:
2381 make_bevel_list_3D_tangent(bl);
2383 case CU_TWIST_MINIMUM:
2384 make_bevel_list_3D_minimum_twist(bl);
2386 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2387 make_bevel_list_3D_zup(bl);
2392 bevel_list_smooth(bl, smooth_iter);
2394 bevel_list_apply_tilt(bl);
2397 /* only for 2 points */
2398 static void make_bevel_list_segment_3D(BevList *bl)
2402 BevPoint *bevp2 = (BevPoint *)(bl + 1);
2403 BevPoint *bevp1 = bevp2 + 1;
2405 /* simple quat/dir */
2406 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2407 normalize_v3(bevp1->dir);
2409 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2411 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2412 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2413 normalize_qt(bevp1->quat);
2414 copy_v3_v3(bevp2->dir, bevp1->dir);
2415 copy_qt_qt(bevp2->quat, bevp1->quat);
2418 /* only for 2 points */
2419 static void make_bevel_list_segment_2D(BevList *bl)
2421 BevPoint *bevp2 = (BevPoint *)(bl + 1);
2422 BevPoint *bevp1 = bevp2 + 1;
2424 const float x1 = bevp1->vec[0] - bevp2->vec[0];
2425 const float y1 = bevp1->vec[1] - bevp2->vec[1];
2427 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2428 bevp2->sina = bevp1->sina;
2429 bevp2->cosa = bevp1->cosa;
2431 /* fill in dir & quat */
2432 make_bevel_list_segment_3D(bl);
2435 static void make_bevel_list_2D(BevList *bl)
2437 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2438 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2440 BevPoint *bevp0, *bevp1, *bevp2;
2443 if (bl->poly != -1) {
2444 bevp2 = (BevPoint *)(bl + 1);
2445 bevp1 = bevp2 + (bl->nr - 1);
2450 bevp0 = (BevPoint *)(bl + 1);
2458 const float x1 = bevp1->vec[0] - bevp0->vec[0];
2459 const float x2 = bevp1->vec[0] - bevp2->vec[0];
2460 const float y1 = bevp1->vec[1] - bevp0->vec[1];
2461 const float y2 = bevp1->vec[1] - bevp2->vec[1];
2463 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2465 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2466 * no need for tricky tilt calculation as with 3D curves */
2467 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2468 vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
2469 /* done with inline make_bevel_list_3D_zup */
2476 /* correct non-cyclic cases */
2477 if (bl->poly == -1) {
2482 bevp = (BevPoint *)(bl + 1);
2483 angle = atan2(bevp->dir[0], bevp->dir[1]) - M_PI / 2.0;
2484 bevp->sina = sinf(angle);
2485 bevp->cosa = cosf(angle);
2486 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2489 bevp = (BevPoint *)(bl + 1);
2490 bevp += (bl->nr - 1);
2491 angle = atan2(bevp->dir[0], bevp->dir[1]) - M_PI / 2.0;
2492 bevp->sina = sinf(angle);
2493 bevp->cosa = cosf(angle);
2494 vec_to_quat(bevp->quat, bevp->dir, 5, 1);
2498 static void bevlist_firstlast_direction_calc_from_bpoint(Nurb *nu, BevList *bl)
2500 if (nu->pntsu > 1) {
2501 BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
2502 BevPoint *first_bevp, *last_bevp;
2504 first_bevp = (BevPoint *)(bl + 1);
2505 last_bevp = first_bevp + (bl->nr - 1);
2507 sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
2508 normalize_v3(first_bevp->dir);
2510 sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
2511 normalize_v3(last_bevp->dir);
2515 void BKE_curve_bevelList_make(Object *ob, ListBase *nurbs, bool for_render)
2518 * - convert all curves to polys, with indication of resol and flags for double-vertices
2519 * - possibly; do a smart vertice removal (in case Nurb)
2520 * - separate in individual blicks with BoundBox
2521 * - AutoHole detection
2525 BezTriple *bezt, *prevbezt;
2527 BevList *bl, *blnew, *blnext;
2528 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2530 struct BevelSort *sortdata, *sd, *sd1;
2531 int a, b, nr, poly, resolu = 0, len = 0;
2532 int do_tilt, do_radius, do_weight;
2533 bool is_editmode = false;
2536 /* this function needs an object, because of tflag and upflag */
2539 bev = &ob->curve_cache->bev;
2541 /* do we need to calculate the radius for each point? */
2542 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2544 /* STEP 1: MAKE POLYS */
2546 BLI_freelistN(&(ob->curve_cache->bev));
2548 if (cu->editnurb && ob->type != OB_FONT) {
2552 for (; nu; nu = nu->next) {
2554 if (nu->hide && is_editmode)
2557 /* check if we will calculate tilt data */
2558 do_tilt = CU_DO_TILT(cu, nu);
2559 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2562 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2563 * enforced in the UI but can go wrong possibly */
2564 if (!BKE_nurb_check_valid_u(nu)) {
2565 bl = MEM_callocN(sizeof(BevList) + 1 * sizeof(BevPoint), "makeBevelList1");
2566 BLI_addtail(bev, bl);
2568 bl->charidx = nu->charidx;
2571 if (for_render && cu->resolu_ren != 0)
2572 resolu = cu->resolu_ren;
2574 resolu = nu->resolu;
2576 if (nu->type == CU_POLY) {
2578 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList2");
2579 BLI_addtail(bev, bl);
2581 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2584 bl->charidx = nu->charidx;
2585 bevp = (BevPoint *)(bl + 1);
2589 copy_v3_v3(bevp->vec, bp->vec);
2590 bevp->alfa = bp->alfa;
2591 bevp->radius = bp->radius;
2592 bevp->weight = bp->weight;
2593 bevp->split_tag = TRUE;
2598 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2599 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2602 else if (nu->type == CU_BEZIER) {
2603 /* in case last point is not cyclic */
2604 len = resolu * (nu->pntsu + (nu->flagu & CU_NURB_CYCLIC) - 1) + 1;
2605 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelBPoints");
2606 BLI_addtail(bev, bl);
2608 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2609 bl->charidx = nu->charidx;
2610 bevp = (BevPoint *)(bl + 1);
2614 if (nu->flagu & CU_NURB_CYCLIC) {
2616 prevbezt = nu->bezt + (nu->pntsu - 1);
2623 sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
2624 normalize_v3(bevp->dir);
2627 if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
2629 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2630 bevp->alfa = prevbezt->alfa;
2631 bevp->radius = prevbezt->radius;
2632 bevp->weight = prevbezt->weight;
2633 bevp->split_tag = TRUE;
2634 bevp->dupe_tag = FALSE;
2640 /* always do all three, to prevent data hanging around */
2643 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2644 for (j = 0; j < 3; j++) {
2645 BKE_curve_forward_diff_bezier(prevbezt->vec[1][j], prevbezt->vec[2][j],
2646 bezt->vec[0][j], bezt->vec[1][j],
2647 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2650 /* if both arrays are NULL do nothiong */
2651 alfa_bezpart(prevbezt, bezt, nu,
2652 do_tilt ? &bevp->alfa : NULL,
2653 do_radius ? &bevp->radius : NULL,
2654 do_weight ? &bevp->weight : NULL,
2655 resolu, sizeof(BevPoint));
2658 if (cu->twist_mode == CU_TWIST_TANGENT) {
2659 forward_diff_bezier_cotangent(prevbezt->vec[1], prevbezt->vec[2],
2660 bezt->vec[0], bezt->vec[1],
2661 bevp->tan, resolu, sizeof(BevPoint));
2664 /* indicate with handlecodes double points */
2665 if (prevbezt->h1 == prevbezt->h2) {
2666 if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2667 bevp->split_tag = TRUE;
2670 if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
2671 bevp->split_tag = TRUE;
2672 else if (prevbezt->h2 == 0 || prevbezt->h2 == HD_VECT)
2673 bevp->split_tag = TRUE;
2682 if ((nu->flagu & CU_NURB_CYCLIC) == 0) { /* not cyclic: endpoint */
2683 copy_v3_v3(bevp->vec, prevbezt->vec[1]);
2684 bevp->alfa = prevbezt->alfa;
2685 bevp->radius = prevbezt->radius;
2686 bevp->weight = prevbezt->weight;
2688 sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
2689 normalize_v3(bevp->dir);
2694 else if (nu->type == CU_NURBS) {
2695 if (nu->pntsv == 1) {
2696 len = (resolu * SEGMENTSU(nu));
2698 bl = MEM_callocN(sizeof(BevList) + len * sizeof(BevPoint), "makeBevelList3");
2699 BLI_addtail(bev, bl);
2702 bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
2703 bl->charidx = nu->charidx;
2704 bevp = (BevPoint *)(bl + 1);
2706 BKE_nurb_makeCurve(nu, &bevp->vec[0],
2707 do_tilt ? &bevp->alfa : NULL,
2708 do_radius ? &bevp->radius : NULL,
2709 do_weight ? &bevp->weight : NULL,
2710 resolu, sizeof(BevPoint));
2712 if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
2713 bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
2720 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2723 if (bl->nr) { /* null bevel items come from single points */
2724 bool is_cyclic = bl->poly != -1;
2727 bevp1 = (BevPoint *)(bl + 1);
2728 bevp0 = bevp1 + (nr - 1);
2731 bevp0 = (BevPoint *)(bl + 1);
2736 if (fabsf(bevp0->vec[0] - bevp1->vec[0]) < 0.00001f) {
2737 if (fabsf(bevp0->vec[1] - bevp1->vec[1]) < 0.00001f) {
2738 if (fabsf(bevp0->vec[2] - bevp1->vec[2]) < 0.00001f) {
2739 bevp0->dupe_tag = TRUE;
2753 if (bl->nr && bl->dupe_nr) {
2754 nr = bl->nr - bl->dupe_nr + 1; /* +1 because vectorbezier sets flag too */
2755 blnew = MEM_mallocN(sizeof(BevList) + nr * sizeof(BevPoint), "makeBevelList4");
2756 memcpy(blnew, bl, sizeof(BevList));
2758 BLI_remlink(bev, bl);
2759 BLI_insertlinkbefore(bev, blnext, blnew); /* to make sure bevlijst is tuned with nurblist */
2760 bevp0 = (BevPoint *)(bl + 1);
2761 bevp1 = (BevPoint *)(blnew + 1);
2764 if (bevp0->dupe_tag == 0) {
2765 memcpy(bevp1, bevp0, sizeof(BevPoint));
2777 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2781 if (bl->nr && bl->poly >= 0) {
2789 /* find extreme left points, also test (turning) direction */
2791 sd = sortdata = MEM_mallocN(sizeof(struct BevelSort) * poly, "makeBevelList5");
2797 bevp = (BevPoint *)(bl + 1);
2800 if (min > bevp->vec[0]) {
2809 bevp = (BevPoint *)(bl + 1);
2811 bevp0 = bevp + (bl->nr - 1);
2814 bevp = bevp + (bl->nr - 1);
2816 bevp2 = (BevPoint *)(bl + 1);
2820 inp = ((bevp1->vec[0] - bevp0->vec[0]) * (bevp0->vec[1] - bevp2->vec[1]) +
2821 (bevp0->vec[1] - bevp1->vec[1]) * (bevp0->vec[0] - bevp2->vec[0]));
2833 qsort(sortdata, poly, sizeof(struct BevelSort), vergxcobev);
2836 for (a = 1; a < poly; a++, sd++) {
2837 bl = sd->bl; /* is bl a hole? */
2838 sd1 = sortdata + (a - 1);
2839 for (b = a - 1; b >= 0; b--, sd1--) { /* all polys to the left */
2840 if (sd1->bl->charidx == bl->charidx) { /* for text, only check matching char */
2841 if (bevelinside(sd1->bl, bl)) {
2842 bl->hole = 1 - sd1->bl->hole;
2849 /* turning direction */
2850 if ((cu->flag & CU_3D) == 0) {
2852 for (a = 0; a < poly; a++, sd++) {
2853 if (sd->bl->hole == sd->dir) {
2855 bevp1 = (BevPoint *)(bl + 1);
2856 bevp2 = bevp1 + (bl->nr - 1);
2859 SWAP(BevPoint, *bevp1, *bevp2);
2866 MEM_freeN(sortdata);
2869 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2870 if ((cu->flag & CU_3D) == 0) {
2872 for (bl = bev->first; bl; bl = bl->next) {
2876 else if (bl->nr == 2) { /* 2 pnt, treat separate */
2877 make_bevel_list_segment_2D(bl);
2880 make_bevel_list_2D(bl);
2886 for (bl = bev->first; bl; bl = bl->next) {
2890 else if (bl->nr == 2) { /* 2 pnt, treat separate */
2891 make_bevel_list_segment_3D(bl);
2894 make_bevel_list_3D(bl, (int)(resolu * cu->twist_smooth), cu->twist_mode);
2900 /* ****************** HANDLES ************** */
2902 static void calchandleNurb_intern(BezTriple *bezt, BezTriple *prev, BezTriple *next,
2903 bool is_fcurve, bool skip_align)
2905 /* defines to avoid confusion */
2906 #define p2_h1 (p2 - 3)
2907 #define p2_h2 (p2 + 3)
2909 float *p1, *p2, *p3, pt[3];
2910 float dvec_a[3], dvec_b[3];
2911 float len, len_a, len_b;
2912 const float eps = 1e-5;
2914 if (bezt->h1 == 0 && bezt->h2 == 0) {
2922 pt[0] = 2.0f * p2[0] - p3[0];
2923 pt[1] = 2.0f * p2[1] - p3[1];
2924 pt[2] = 2.0f * p2[2] - p3[2];
2932 pt[0] = 2.0f * p2[0] - p1[0];
2933 pt[1] = 2.0f * p2[1] - p1[1];
2934 pt[2] = 2.0f * p2[2] - p1[2];
2941 sub_v3_v3v3(dvec_a, p2, p1);
2942 sub_v3_v3v3(dvec_b, p3, p2);
2949 len_a = len_v3(dvec_a);
2950 len_b = len_v3(dvec_b);
2953 if (len_a == 0.0f) len_a = 1.0f;
2954 if (len_b == 0.0f) len_b = 1.0f;
2957 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
2959 tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
2960 tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
2961 tvec[2] = dvec_b[2] / len_b + dvec_a[2] / len_a;
2972 /* only for fcurves */
2973 bool leftviolate = false, rightviolate = false;
2975 if (len_a > 5.0f * len_b)
2976 len_a = 5.0f * len_b;
2977 if (len_b > 5.0f * len_a)
2978 len_b = 5.0f * len_a;
2980 if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
2982 madd_v3_v3v3fl(p2_h1, p2, tvec, -len_a);
2984 if ((bezt->h1 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
2985 float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
2986 float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
2987 if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
2988 bezt->vec[0][1] = bezt->vec[1][1];
2990 else { /* handles should not be beyond y coord of two others */
2991 if (ydiff1 <= 0.0f) {
2992 if (prev->vec[1][1] > bezt->vec[0][1]) {
2993 bezt->vec[0][1] = prev->vec[1][1];
2998 if (prev->vec[1][1] < bezt->vec[0][1]) {
2999 bezt->vec[0][1] = prev->vec[1][1];
3006 if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
3008 madd_v3_v3v3fl(p2_h2, p2, tvec, len_b);
3010 if ((bezt->h2 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
3011 float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
3012 float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
3013 if ( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
3014 bezt->vec[2][1] = bezt->vec[1][1];
3016 else { /* andles should not be beyond y coord of two others */
3017 if (ydiff1 <= 0.0f) {
3018 if (next->vec[1][1] < bezt->vec[2][1]) {
3019 bezt->vec[2][1] = next->vec[1][1];
3024 if (next->vec[1][1] > bezt->vec[2][1]) {
3025 bezt->vec[2][1] = next->vec[1][1];
3032 if (leftviolate || rightviolate) { /* align left handle */
3033 BLI_assert(is_fcurve);
3038 /* simple 2d calculation */
3039 float h1_x = p2_h1[0] - p2[0];
3040 float h2_x = p2[0] - p2_h2[0];
3043 p2_h2[1] = p2[1] + ((p2[1] - p2_h1[1]) / h1_x) * h2_x;
3046 p2_h1[1] = p2[1] + ((p2[1] - p2_h2[1]) / h2_x) * h1_x;
3054 sub_v3_v3v3(h1, p2_h1, p2);
3055 sub_v3_v3v3(h2, p2, p2_h2);
3057 len_a = normalize_v3(h1);
3058 len_b = normalize_v3(h2);
3060 dot = dot_v3v3(h1, h2);
3063 mul_v3_fl(h1, dot * len_b);