7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/curve.c
38 #include <math.h> // floor
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_material_types.h"
52 /* for dereferencing pointers */
53 #include "DNA_key_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_vfont_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_object_types.h"
59 #include "BKE_animsys.h"
61 #include "BKE_curve.h"
62 #include "BKE_displist.h"
64 #include "BKE_global.h"
66 #include "BKE_library.h"
68 #include "BKE_object.h"
69 #include "BKE_material.h"
74 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
76 float *labda, float *mu, float *vec);
78 void unlink_curve(Curve *cu)
82 for(a=0; a<cu->totcol; a++) {
83 if(cu->mat[a]) cu->mat[a]->id.us--;
86 if(cu->vfont) cu->vfont->id.us--;
89 if(cu->vfontb) cu->vfontb->id.us--;
92 if(cu->vfonti) cu->vfonti->id.us--;
95 if(cu->vfontbi) cu->vfontbi->id.us--;
98 if(cu->key) cu->key->id.us--;
102 /* frees editcurve entirely */
103 void BKE_free_editfont(Curve *cu)
106 EditFont *ef= cu->editfont;
108 if(ef->oldstr) MEM_freeN(ef->oldstr);
109 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
110 if(ef->textbuf) MEM_freeN(ef->textbuf);
111 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
112 if(ef->copybuf) MEM_freeN(ef->copybuf);
113 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
120 void free_curve_editNurb_keyIndex(EditNurb *editnurb)
122 if (!editnurb->keyindex) {
125 BLI_ghash_free(editnurb->keyindex, NULL, (GHashValFreeFP)MEM_freeN);
126 editnurb->keyindex= NULL;
129 void free_curve_editNurb (Curve *cu)
132 freeNurblist(&cu->editnurb->nurbs);
133 free_curve_editNurb_keyIndex(cu->editnurb);
134 MEM_freeN(cu->editnurb);
139 /* don't free curve itself */
140 void free_curve(Curve *cu)
142 freeNurblist(&cu->nurb);
143 BLI_freelistN(&cu->bev);
144 freedisplist(&cu->disp);
145 BKE_free_editfont(cu);
147 free_curve_editNurb(cu);
149 BKE_free_animdata((ID *)cu);
151 if(cu->mat) MEM_freeN(cu->mat);
152 if(cu->str) MEM_freeN(cu->str);
153 if(cu->strinfo) MEM_freeN(cu->strinfo);
154 if(cu->bb) MEM_freeN(cu->bb);
155 if(cu->path) free_path(cu->path);
156 if(cu->tb) MEM_freeN(cu->tb);
159 Curve *add_curve(const char *name, int type)
163 cu= alloc_libblock(&G.main->curve, ID_CU, name);
165 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
166 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
168 cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
171 cu->spacing= cu->linedist= 1.0;
174 cu->texflag= CU_AUTOSPACE;
175 cu->smallcaps_scale= 0.75f;
176 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
178 cu->bb= unit_boundbox();
181 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
183 cu->str= MEM_mallocN(12, "str");
184 BLI_strncpy(cu->str, "Text", 12);
186 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
187 cu->totbox= cu->actbox= 1;
188 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
189 cu->tb[0].w = cu->tb[0].h = 0.0;
195 Curve *copy_curve(Curve *cu)
200 cun= copy_libblock(cu);
201 cun->nurb.first= cun->nurb.last= NULL;
202 duplicateNurblist( &(cun->nurb), &(cu->nurb));
204 cun->mat= MEM_dupallocN(cu->mat);
205 for(a=0; a<cun->totcol; a++) {
206 id_us_plus((ID *)cun->mat[a]);
209 cun->str= MEM_dupallocN(cu->str);
210 cun->strinfo= MEM_dupallocN(cu->strinfo);
211 cun->tb= MEM_dupallocN(cu->tb);
212 cun->bb= MEM_dupallocN(cu->bb);
214 cun->key= copy_key(cu->key);
215 if(cun->key) cun->key->from= (ID *)cun;
217 cun->disp.first= cun->disp.last= NULL;
218 cun->bev.first= cun->bev.last= NULL;
225 #if 0 // XXX old animation system
226 /* single user ipo too */
227 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
228 #endif // XXX old animation system
230 id_us_plus((ID *)cun->vfont);
231 id_us_plus((ID *)cun->vfontb);
232 id_us_plus((ID *)cun->vfonti);
233 id_us_plus((ID *)cun->vfontbi);
238 static void extern_local_curve(Curve *cu)
240 id_lib_extern((ID *)cu->vfont);
241 id_lib_extern((ID *)cu->vfontb);
242 id_lib_extern((ID *)cu->vfonti);
243 id_lib_extern((ID *)cu->vfontbi);
246 extern_local_matarar(cu->mat, cu->totcol);
250 void make_local_curve(Curve *cu)
256 /* - when there are only lib users: don't do
257 * - when there are only local users: set flag
261 if(cu->id.lib==NULL) return;
265 cu->id.flag= LIB_LOCAL;
267 new_id(&bmain->curve, (ID *)cu, NULL);
268 extern_local_curve(cu);
272 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
274 if(ob->id.lib) lib= 1;
279 if(local && lib==0) {
281 cu->id.flag= LIB_LOCAL;
283 new_id(&bmain->curve, (ID *)cu, NULL);
284 extern_local_curve(cu);
286 else if(local && lib) {
287 Curve *cun= copy_curve(cu);
290 for(ob= bmain->object.first; ob; ob= ob->id.next) {
292 if(ob->id.lib==NULL) {
302 /* Get list of nurbs from editnurbs structure */
303 ListBase *curve_editnurbs(Curve *cu)
306 return &cu->editnurb->nurbs;
312 short curve_type(Curve *cu)
318 for (nu= cu->nurb.first; nu; nu= nu->next) {
327 void test_curve_type(Object *ob)
329 ob->type = curve_type(ob->data);
332 void tex_space_curve(Curve *cu)
336 float *fp, min[3], max[3];
339 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
342 INIT_MINMAX(min, max);
347 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
348 else tot= dl->nr*dl->parts;
353 DO_MINMAX(fp, min, max);
360 min[0] = min[1] = min[2] = -1.0f;
361 max[0] = max[1] = max[2] = 1.0f;
364 boundbox_set_from_min_max(bb, min, max);
366 if(cu->texflag & CU_AUTOSPACE) {
367 mid_v3_v3v3(cu->loc, min, max);
368 cu->size[0]= (max[0]-min[0])/2.0f;
369 cu->size[1]= (max[1]-min[1])/2.0f;
370 cu->size[2]= (max[2]-min[2])/2.0f;
372 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
374 if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
375 else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
376 else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
378 if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
379 else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
380 else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
382 if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
383 else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
384 else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
389 int count_curveverts(ListBase *nurb)
396 if(nu->bezt) tot+= 3*nu->pntsu;
397 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
404 int count_curveverts_without_handles(ListBase *nurb)
411 if(nu->bezt) tot+= nu->pntsu;
412 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
419 /* **************** NURBS ROUTINES ******************** */
421 void freeNurb(Nurb *nu)
426 if(nu->bezt) MEM_freeN(nu->bezt);
428 if(nu->bp) MEM_freeN(nu->bp);
430 if(nu->knotsu) MEM_freeN(nu->knotsu);
432 if(nu->knotsv) MEM_freeN(nu->knotsv);
434 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
441 void freeNurblist(ListBase *lb)
453 lb->first= lb->last= NULL;
456 Nurb *duplicateNurb(Nurb *nu)
461 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
462 if(newnu==NULL) return NULL;
463 memcpy(newnu, nu, sizeof(Nurb));
467 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
468 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
471 len= nu->pntsu*nu->pntsv;
473 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
474 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
476 newnu->knotsu= newnu->knotsv= NULL;
481 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
482 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
485 if(nu->pntsv>1 && nu->knotsv) {
488 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
489 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
496 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
504 nun= duplicateNurb(nu);
505 BLI_addtail(lb1, nun);
511 void test2DNurb(Nurb *nu)
517 if((nu->flag & CU_2D)==0)
520 if(nu->type == CU_BEZIER) {
524 bezt->vec[0][2]= 0.0;
525 bezt->vec[1][2]= 0.0;
526 bezt->vec[2][2]= 0.0;
531 a= nu->pntsu*nu->pntsv;
540 void minmaxNurb(Nurb *nu, float *min, float *max)
546 if(nu->type == CU_BEZIER) {
550 DO_MINMAX(bezt->vec[0], min, max);
551 DO_MINMAX(bezt->vec[1], min, max);
552 DO_MINMAX(bezt->vec[2], min, max);
557 a= nu->pntsu*nu->pntsv;
560 DO_MINMAX(bp->vec, min, max);
566 /* be sure to call makeknots after this */
567 void addNurbPoints(Nurb *nu, int number)
571 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
574 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
578 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
580 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
587 void addNurbPointsBezier(Nurb *nu, int number)
589 BezTriple *tmp= nu->bezt;
591 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
594 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
598 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
600 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
607 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
610 static void calcknots(float *knots, const short pnts, const short order, const short flag)
612 /* knots: number of pnts NOT corrected for cyclic */
613 const int pnts_order= pnts + order;
617 switch(flag & (CU_NURB_ENDPOINT|CU_NURB_BEZIER)) {
618 case CU_NURB_ENDPOINT:
620 for(a=1; a <= pnts_order; a++) {
622 if(a >= order && a <= pnts) k+= 1.0f;
626 /* Warning, the order MUST be 2 or 4,
627 * if this is not enforced, the displist will be corrupt */
630 for(a=0; a < pnts_order; a++) {
637 for(a=0; a < pnts_order; a++) {
638 if(a >= order && a <= pnts) k+= 0.5f;
643 printf("bez nurb curve order is not 3 or 4, should never happen\n");
647 for(a=0; a < pnts_order; a++) {
654 static void makecyclicknots(float *knots, short pnts, short order)
655 /* pnts, order: number of pnts NOT corrected for cyclic */
659 if(knots==NULL) return;
663 /* do first long rows (order -1), remove identical knots at endpoints */
666 for(a=1; a<order2; a++) {
667 if(knots[b]!= knots[b-a]) break;
669 if(a==order2) knots[pnts+order-2]+= 1.0f;
673 c=pnts + order + order2;
674 for(a=pnts+order2; a<c; a++) {
675 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
682 static void makeknots(Nurb *nu, short uv)
684 if(nu->type == CU_NURBS) {
686 if(nu->knotsu) MEM_freeN(nu->knotsu);
687 if(check_valid_nurb_u(nu)) {
688 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
689 if(nu->flagu & CU_NURB_CYCLIC) {
690 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
691 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
693 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
696 else nu->knotsu= NULL;
699 if(nu->knotsv) MEM_freeN(nu->knotsv);
700 if(check_valid_nurb_v(nu)) {
701 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
702 if(nu->flagv & CU_NURB_CYCLIC) {
703 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
704 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
706 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
709 else nu->knotsv= NULL;
714 void nurbs_knot_calc_u(Nurb *nu)
719 void nurbs_knot_calc_v(Nurb *nu)
724 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
727 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
729 orderpluspnts= order+pnts;
730 opp2 = orderpluspnts-1;
732 /* this is for float inaccuracy */
733 if(t < knots[0]) t= knots[0];
734 else if(t > knots[opp2]) t= knots[opp2];
736 /* this part is order '1' */
738 for(i=0;i<opp2;i++) {
739 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
755 /* this is order 2,3,... */
756 for(j=2; j<=order; j++) {
758 if(i2+j>= orderpluspnts) i2= opp2-j;
760 for(i= i1; i<=i2; i++) {
762 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
766 if(basis[i+1] != 0.0f)
767 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
778 for(i=i1; i<=i2; i++) {
779 if(basis[i] > 0.0f) {
781 if(*start==1000) *start= i;
787 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
788 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
791 float *basisu, *basis, *basisv, *sum, *fp, *in;
792 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
793 int i, j, iofs, jofs, cycl, len, curu, curv;
794 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
796 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
798 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
799 if(nu->orderu>nu->pntsu) return;
800 if(nu->orderv>nu->pntsv) return;
801 if(coord_array==NULL) return;
803 /* allocate and initialize */
809 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
818 i= nu->pntsu*nu->pntsv;
821 if(bp->vec[3] != 1.0f) {
829 ustart= fp[nu->orderu-1];
830 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
831 else uend= fp[nu->pntsu];
832 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
834 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
837 vstart= fp[nu->orderv-1];
839 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
840 else vend= fp[nu->pntsv];
841 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
844 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
845 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
846 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
848 /* precalculation of basisv and jstart,jend */
849 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
855 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
860 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
867 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
880 for(j= jsta; j<=jen; j++) {
882 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
884 bp= nu->bp+ nu->pntsu*jofs+istart-1;
886 for(i= istart; i<=iend; i++, fp++) {
890 bp= nu->bp+ nu->pntsu*jofs+iofs;
895 *fp= basisu[i]*basis[j]*bp->vec[3];
898 else *fp= basisu[i]*basis[j];
904 for(j= jsta; j<=jen; j++) {
905 for(i= istart; i<=iend; i++, fp++) {
911 /* one! (1.0) real point now */
913 for(j= jsta; j<=jen; j++) {
915 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
917 bp= nu->bp+ nu->pntsu*jofs+istart-1;
919 for(i= istart; i<=iend; i++, fp++) {
923 bp= nu->bp+ nu->pntsu*jofs+iofs;
928 in[0]+= (*fp) * bp->vec[0];
929 in[1]+= (*fp) * bp->vec[1];
930 in[2]+= (*fp) * bp->vec[2];
939 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
950 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
951 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
952 * tilt_array and radius_array will be written to if valid */
955 float u, ustart, uend, ustep, sumdiv;
956 float *basisu, *sum, *fp;
957 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
958 int i, len, istart, iend, cycl;
960 if(nu->knotsu==NULL) return;
961 if(nu->orderu>nu->pntsu) return;
962 if(coord_array==NULL) return;
964 /* allocate and initialize */
967 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
969 resolu= (resolu*SEGMENTSU(nu));
977 ustart= fp[nu->orderu-1];
978 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
979 else uend= fp[nu->pntsu];
980 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
982 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
984 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
990 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
994 bp= nu->bp+ istart-1;
995 for(i= istart; i<=iend; i++, fp++) {
997 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
1000 *fp= basisu[i]*bp->vec[3];
1003 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
1004 /* is normalizing needed? */
1006 for(i= istart; i<=iend; i++, fp++) {
1011 /* one! (1.0) real point */
1013 bp= nu->bp+ istart-1;
1014 for(i= istart; i<=iend; i++, fp++) {
1016 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
1021 coord_fp[0]+= (*fp) * bp->vec[0];
1022 coord_fp[1]+= (*fp) * bp->vec[1];
1023 coord_fp[2]+= (*fp) * bp->vec[2];
1026 (*tilt_fp) += (*fp) * bp->alfa;
1029 (*radius_fp) += (*fp) * bp->radius;
1032 (*weight_fp) += (*fp) * bp->weight;
1037 coord_fp = (float *)(((char *)coord_fp) + stride);
1039 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1040 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1041 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1051 /* forward differencing method for bezier curve */
1052 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1054 float rt0,rt1,rt2,rt3,f;
1059 rt1= 3.0f*(q1-q0)/f;
1061 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1063 rt3= (q3-q0+3.0f*(q1-q2))/f;
1070 for(a=0; a<=it; a++) {
1072 p = (float *)(((char *)p)+stride);
1079 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1081 /* note that these are not purpendicular to the curve
1082 * they need to be rotated for this,
1084 * This could also be optimized like forward_diff_bezier */
1086 for(a=0; a<=it; a++) {
1087 float t = (float)a / (float)it;
1090 for(i=0; i<3; i++) {
1091 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1094 p = (float *)(((char *)p)+stride);
1098 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1100 float *make_orco_surf(Object *ob)
1102 /* Note: this function is used in convertblender only atm, so
1103 * suppose nonzero curve's render resolution should always be used */
1104 Curve *cu= ob->data;
1109 float *fp, *coord_array;
1111 /* first calculate the size of the datablock */
1114 /* as we want to avoid the seam in a cyclic nurbs
1115 texture wrapping, reserve extra orco data space to save these extra needed
1116 vertex based UV coordinates for the meridian vertices.
1117 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1118 the renderface/vert construction.
1120 See also convertblender.c: init_render_surf()
1123 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1124 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1126 sizeu = nu->pntsu*resolu;
1127 sizev = nu->pntsv*resolv;
1128 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1129 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1130 if(nu->pntsv>1) tot+= sizeu * sizev;
1134 /* makeNurbfaces wants zeros */
1135 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1139 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1140 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1143 sizeu = nu->pntsu*resolu;
1144 sizev = nu->pntsv*resolv;
1145 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1146 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1148 if(cu->flag & CU_UV_ORCO) {
1149 for(b=0; b< sizeu; b++) {
1150 for(a=0; a< sizev; a++) {
1152 if(sizev <2) fp[0]= 0.0f;
1153 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1155 if(sizeu <2) fp[1]= 0.0f;
1156 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1165 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1166 float *tdata= _tdata;
1168 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1170 for(b=0; b<sizeu; b++) {
1172 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1175 for(a=0; a<sizev; a++) {
1177 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1180 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1182 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1183 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1184 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1199 /* NOTE: This routine is tied to the order of vertex
1200 * built by displist and as passed to the renderer.
1202 float *make_orco_curve(Scene *scene, Object *ob)
1204 Curve *cu = ob->data;
1207 float *fp, *coord_array;
1208 ListBase disp = {NULL, NULL};
1210 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1213 for (dl=disp.first; dl; dl=dl->next) {
1214 if (dl->type==DL_INDEX3) {
1216 } else if (dl->type==DL_SURF) {
1217 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1218 if (dl->flag & DL_CYCL_U) {
1219 if (dl->flag & DL_CYCL_V)
1220 numVerts += (dl->parts+1)*(dl->nr+1);
1222 numVerts += dl->parts*(dl->nr+1);
1225 numVerts += dl->parts*dl->nr;
1229 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1230 for (dl=disp.first; dl; dl=dl->next) {
1231 if (dl->type==DL_INDEX3) {
1232 for (u=0; u<dl->nr; u++, fp+=3) {
1233 if (cu->flag & CU_UV_ORCO) {
1234 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1238 VECCOPY(fp, &dl->verts[u*3]);
1240 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1241 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1242 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1245 } else if (dl->type==DL_SURF) {
1246 int sizeu= dl->nr, sizev= dl->parts;
1248 /* exception as handled in convertblender.c too */
1249 if (dl->flag & DL_CYCL_U) {
1251 if (dl->flag & DL_CYCL_V)
1255 for (u=0; u<sizev; u++) {
1256 for (v=0; v<sizeu; v++,fp+=3) {
1257 if (cu->flag & CU_UV_ORCO) {
1258 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1259 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1263 int realv= v % dl->nr;
1264 int realu= u % dl->parts;
1266 vert= dl->verts + 3*(dl->nr*realu + realv);
1269 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1270 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1271 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1278 freedisplist(&disp);
1284 /* ***************** BEVEL ****************** */
1286 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1288 DispList *dl, *dlnew;
1290 float *fp, facx, facy, angle, dangle;
1294 disp->first = disp->last = NULL;
1296 /* if a font object is being edited, then do nothing */
1297 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1300 if (cu->bevobj->type!=OB_CURVE) return;
1302 bevcu= cu->bevobj->data;
1303 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1304 ListBase bevdisp= {NULL, NULL};
1305 facx= cu->bevobj->size[0];
1306 facy= cu->bevobj->size[1];
1309 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1312 dl= cu->bevobj->disp.first;
1314 makeDispListCurveTypes(scene, cu->bevobj, 0);
1315 dl= cu->bevobj->disp.first;
1320 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1321 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1323 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1324 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1326 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1328 BLI_addtail(disp, dlnew);
1330 nr= dlnew->parts*dlnew->nr;
1341 freedisplist(&bevdisp);
1344 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1347 else if(cu->ext2==0.0f) {
1348 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1349 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1350 BLI_addtail(disp, dl);
1353 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1362 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1364 nr= 4+ 2*cu->bevresol;
1366 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1367 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1368 BLI_addtail(disp, dl);
1371 dl->flag= DL_BACK_CURVE;
1376 dangle= (2.0f*(float)M_PI/(nr));
1377 angle= -(nr-1)*dangle;
1379 for(a=0; a<nr; a++) {
1381 fp[1]= (cosf(angle)*(cu->ext2));
1382 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1390 /* bevel now in three parts, for proper vertex normals */
1393 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1394 dnr= nr= 2+ cu->bevresol;
1395 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1396 nr= 3+ 2*cu->bevresol;
1398 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1399 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1400 BLI_addtail(disp, dl);
1403 dl->flag= DL_BACK_CURVE;
1408 dangle= (0.5*M_PI/(dnr-1));
1409 angle= -(nr-1)*dangle;
1411 for(a=0; a<nr; a++) {
1413 fp[1]= (float)(cosf(angle)*(cu->ext2));
1414 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1420 /* part 2, sidefaces */
1421 if(cu->ext1!=0.0f) {
1424 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1425 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1426 BLI_addtail(disp, dl);
1437 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1438 dl= MEM_dupallocN(dl);
1439 dl->verts= MEM_dupallocN(dl->verts);
1440 BLI_addtail(disp, dl);
1451 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1452 dnr= nr= 2+ cu->bevresol;
1453 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1454 nr= 3+ 2*cu->bevresol;
1456 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1457 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1458 BLI_addtail(disp, dl);
1460 dl->flag= DL_FRONT_CURVE;
1467 dangle= (0.5*M_PI/(dnr-1));
1469 for(a=0; a<nr; a++) {
1471 fp[1]= (float)(cosf(angle)*(cu->ext2));
1472 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1480 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1484 0: no intersection of segments
1485 1: exact intersection of segments
1486 2: cross-intersection of segments
1490 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1491 if(deler==0.0f) return -1;
1493 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1494 *labda= -(*labda/deler);
1496 deler= v3[coy]-v4[coy];
1498 deler=v3[cox]-v4[cox];
1499 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1501 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1503 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1504 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1506 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1507 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1514 static short bevelinside(BevList *bl1,BevList *bl2)
1516 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1517 /* returns '1' if correct hole */
1518 BevPoint *bevp, *prevbevp;
1519 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1520 int nr, links=0,rechts=0,mode;
1522 /* take first vertex of possible hole */
1524 bevp= (BevPoint *)(bl2+1);
1525 hvec1[0]= bevp->vec[0];
1526 hvec1[1]= bevp->vec[1];
1528 VECCOPY(hvec2,hvec1);
1531 /* test it with all edges of potential surounding poly */
1532 /* count number of transitions left-right */
1534 bevp= (BevPoint *)(bl1+1);
1536 prevbevp= bevp+(nr-1);
1539 min= prevbevp->vec[1];
1543 max= prevbevp->vec[1];
1546 if(min<=hvec1[1] && max>=hvec1[1]) {
1547 /* there's a transition, calc intersection point */
1548 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1549 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1550 only allow for one situation: we choose lab= 1.0
1552 if(mode >= 0 && lab != 0.0f) {
1553 if(vec[0]<hvec1[0]) links++;
1562 if( (links & 1) && (rechts & 1) ) return 1;
1573 static int vergxcobev(const void *a1, const void *a2)
1575 const struct bevelsort *x1=a1,*x2=a2;
1577 if( x1->left > x2->left ) return 1;
1578 else if( x1->left < x2->left) return -1;
1582 /* this function cannot be replaced with atan2, but why? */
1584 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1586 float t01, t02, x3, y3;
1588 t01= (float)sqrt(x1*x1+y1*y1);
1589 t02= (float)sqrt(x2*x2+y2*y2);
1590 if(t01==0.0f) t01= 1.0f;
1591 if(t02==0.0f) t02= 1.0f;
1599 if(fabs(t02)>=1.0) t02= .5*M_PI;
1600 else t02= (saacos(t02))/2.0f;
1602 t02= (float)sin(t02);
1603 if(t02==0.0f) t02= 1.0f;
1607 if(x3==0 && y3==0) {
1611 t01= (float)sqrt(x3*x3+y3*y3);
1621 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1623 BezTriple *pprev, *next, *last;
1624 float fac, dfac, t[4];
1627 if(tilt_array==NULL && radius_array==NULL)
1630 last= nu->bezt+(nu->pntsu-1);
1632 /* returns a point */
1633 if(prevbezt==nu->bezt) {
1634 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1635 else pprev= prevbezt;
1637 else pprev= prevbezt-1;
1641 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1647 dfac= 1.0f/(float)resolu;
1649 for(a=0; a<resolu; a++, fac+= dfac) {
1651 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1652 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1654 key_curve_position_weights(fac, t, nu->tilt_interp);
1655 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1658 tilt_array = (float *)(((char *)tilt_array) + stride);
1662 if (nu->radius_interp==KEY_CU_EASE) {
1663 /* Support 2.47 ease interp
1664 * Note! - this only takes the 2 points into account,
1665 * giving much more localized results to changes in radius, sometimes you want that */
1666 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1669 /* reuse interpolation from tilt if we can */
1670 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1671 key_curve_position_weights(fac, t, nu->radius_interp);
1673 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1676 radius_array = (float *)(((char *)radius_array) + stride);
1680 /* basic interpolation for now, could copy tilt interp too */
1681 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1683 weight_array = (float *)(((char *)weight_array) + stride);
1688 /* make_bevel_list_3D_* funcs, at a minimum these must
1689 * fill in the bezp->quat and bezp->dir values */
1691 /* correct non-cyclic cases by copying direction and rotation
1692 * values onto the first & last end-points */
1693 static void bevel_list_cyclic_fix_3D(BevList *bl)
1695 BevPoint *bevp, *bevp1;
1697 bevp= (BevPoint *)(bl+1);
1699 QUATCOPY(bevp->quat, bevp1->quat);
1700 VECCOPY(bevp->dir, bevp1->dir);
1701 VECCOPY(bevp->tan, bevp1->tan);
1702 bevp= (BevPoint *)(bl+1);
1705 QUATCOPY(bevp->quat, bevp1->quat);
1706 VECCOPY(bevp->dir, bevp1->dir);
1707 VECCOPY(bevp->tan, bevp1->tan);
1709 /* utility for make_bevel_list_3D_* funcs */
1710 static void bevel_list_calc_bisect(BevList *bl)
1712 BevPoint *bevp2, *bevp1, *bevp0;
1715 bevp2= (BevPoint *)(bl+1);
1716 bevp1= bevp2+(bl->nr-1);
1721 /* totally simple */
1722 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1729 static void bevel_list_flip_tangents(BevList *bl)
1731 BevPoint *bevp2, *bevp1, *bevp0;
1734 bevp2= (BevPoint *)(bl+1);
1735 bevp1= bevp2+(bl->nr-1);
1740 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1741 negate_v3(bevp1->tan);
1748 /* apply user tilt */
1749 static void bevel_list_apply_tilt(BevList *bl)
1751 BevPoint *bevp2, *bevp1;
1755 bevp2= (BevPoint *)(bl+1);
1756 bevp1= bevp2+(bl->nr-1);
1760 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1761 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1762 normalize_qt(bevp1->quat);
1768 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1769 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1771 BevPoint *bevp2, *bevp1, *bevp0;
1775 float bevp0_quat[4];
1778 for(a=0; a < smooth_iter; a++) {
1780 bevp2= (BevPoint *)(bl+1);
1781 bevp1= bevp2+(bl->nr-1);
1786 if(bl->poly== -1) { /* check its not cyclic */
1787 /* skip the first point */
1800 QUATCOPY(bevp0_quat, bevp0->quat);
1803 /* interpolate quats */
1804 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1805 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1808 mul_qt_v3(q, zaxis);
1809 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1810 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1813 QUATCOPY(bevp0_quat, bevp1->quat);
1814 mul_qt_qtqt(q, q2, q);
1815 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1816 normalize_qt(bevp1->quat);
1819 /* bevp0= bevp1; */ /* UNUSED */
1826 static void make_bevel_list_3D_zup(BevList *bl)
1828 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1831 bevp2= (BevPoint *)(bl+1);
1832 bevp1= bevp2+(bl->nr-1);
1837 /* totally simple */
1838 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1839 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1847 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1849 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1853 bevel_list_calc_bisect(bl);
1855 bevp2= (BevPoint *)(bl+1);
1856 bevp1= bevp2+(bl->nr-1);
1862 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1863 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1866 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1868 if(angle > 0.0f) { /* otherwise we can keep as is */
1870 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1871 axis_angle_to_quat(q, cross_tmp, angle);
1872 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1875 QUATCOPY(bevp1->quat, bevp0->quat);
1884 if(bl->poly != -1) { /* check for cyclic */
1886 /* Need to correct for the start/end points not matching
1887 * do this by calculating the tilt angle difference, then apply
1888 * the rotation gradually over the entire curve
1890 * note that the split is between last and second last, rather than first/last as youd expect.
1892 * real order is like this
1893 * 0,1,2,3,4 --> 1,2,3,4,0
1895 * this is why we compare last with second last
1897 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1899 BevPoint *bevp_first;
1900 BevPoint *bevp_last;
1903 bevp_first= (BevPoint *)(bl+1);
1904 bevp_first+= bl->nr-1;
1905 bevp_last = bevp_first;
1908 /* quats and vec's are normalized, should not need to re-normalize */
1909 mul_qt_v3(bevp_first->quat, vec_1);
1910 mul_qt_v3(bevp_last->quat, vec_2);
1911 normalize_v3(vec_1);
1912 normalize_v3(vec_2);
1914 /* align the vector, can avoid this and it looks 98% OK but
1915 * better to align the angle quat roll's before comparing */
1917 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1918 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1919 axis_angle_to_quat(q, cross_tmp, angle);
1920 mul_qt_v3(q, vec_2);
1923 angle= angle_normalized_v3v3(vec_1, vec_2);
1925 /* flip rotation if needs be */
1926 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1927 normalize_v3(cross_tmp);
1928 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
1931 bevp2= (BevPoint *)(bl+1);
1932 bevp1= bevp2+(bl->nr-1);
1937 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1939 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1940 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1949 static void make_bevel_list_3D_tangent(BevList *bl)
1951 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1954 float bevp0_tan[3], cross_tmp[3];
1956 bevel_list_calc_bisect(bl);
1957 if(bl->poly== -1) /* check its not cyclic */
1958 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1959 bevel_list_flip_tangents(bl);
1961 /* correct the tangents */
1962 bevp2= (BevPoint *)(bl+1);
1963 bevp1= bevp2+(bl->nr-1);
1969 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1970 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1971 normalize_v3(bevp1->tan);
1979 /* now for the real twist calc */
1980 bevp2= (BevPoint *)(bl+1);
1981 bevp1= bevp2+(bl->nr-1);
1984 VECCOPY(bevp0_tan, bevp0->tan);
1989 /* make perpendicular, modify tan in place, is ok */
1991 float zero[3] = {0,0,0};
1993 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1994 normalize_v3(cross_tmp);
1995 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1997 /* bevp0= bevp1; */ /* UNUSED */
2003 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2005 switch(twist_mode) {
2006 case CU_TWIST_TANGENT:
2007 make_bevel_list_3D_tangent(bl);
2009 case CU_TWIST_MINIMUM:
2010 make_bevel_list_3D_minimum_twist(bl);
2012 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2013 make_bevel_list_3D_zup(bl);
2016 if(bl->poly== -1) /* check its not cyclic */
2017 bevel_list_cyclic_fix_3D(bl);
2020 bevel_list_smooth(bl, smooth_iter);
2022 bevel_list_apply_tilt(bl);
2027 /* only for 2 points */
2028 static void make_bevel_list_segment_3D(BevList *bl)
2032 BevPoint *bevp2= (BevPoint *)(bl+1);
2033 BevPoint *bevp1= bevp2+1;
2035 /* simple quat/dir */
2036 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2037 normalize_v3(bevp1->dir);
2039 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2041 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2042 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2043 normalize_qt(bevp1->quat);
2044 VECCOPY(bevp2->dir, bevp1->dir);
2045 QUATCOPY(bevp2->quat, bevp1->quat);
2050 void makeBevelList(Object *ob)
2053 - convert all curves to polys, with indication of resol and flags for double-vertices
2054 - possibly; do a smart vertice removal (in case Nurb)
2055 - separate in individual blicks with BoundBox
2056 - AutoHole detection
2060 BezTriple *bezt, *prevbezt;
2062 BevList *bl, *blnew, *blnext;
2063 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2064 float min, inp, x1, x2, y1, y2;
2065 struct bevelsort *sortdata, *sd, *sd1;
2066 int a, b, nr, poly, resolu = 0, len = 0;
2067 int do_tilt, do_radius, do_weight;
2069 /* this function needs an object, because of tflag and upflag */
2072 /* do we need to calculate the radius for each point? */
2073 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2075 /* STEP 1: MAKE POLYS */
2077 BLI_freelistN(&(cu->bev));
2078 if(cu->editnurb && ob->type!=OB_FONT) {
2079 ListBase *nurbs= curve_editnurbs(cu);
2081 } else nu= cu->nurb.first;
2085 /* check if we will calculate tilt data */
2086 do_tilt = CU_DO_TILT(cu, nu);
2087 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2090 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2091 * enforced in the UI but can go wrong possibly */
2092 if(!check_valid_nurb_u(nu)) {
2093 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2094 BLI_addtail(&(cu->bev), bl);
2097 if(G.rendering && cu->resolu_ren!=0)
2098 resolu= cu->resolu_ren;
2102 if(nu->type == CU_POLY) {
2104 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2105 BLI_addtail(&(cu->bev), bl);
2107 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2111 bevp= (BevPoint *)(bl+1);
2115 VECCOPY(bevp->vec, bp->vec);
2116 bevp->alfa= bp->alfa;
2117 bevp->radius= bp->radius;
2118 bevp->weight= bp->weight;
2119 bevp->split_tag= TRUE;
2124 else if(nu->type == CU_BEZIER) {
2126 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2127 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2128 BLI_addtail(&(cu->bev), bl);
2130 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2132 bevp= (BevPoint *)(bl+1);
2136 if(nu->flagu & CU_NURB_CYCLIC) {
2138 prevbezt= nu->bezt+(nu->pntsu-1);
2146 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2148 VECCOPY(bevp->vec, prevbezt->vec[1]);
2149 bevp->alfa= prevbezt->alfa;
2150 bevp->radius= prevbezt->radius;
2151 bevp->weight= prevbezt->weight;
2152 bevp->split_tag= TRUE;
2153 bevp->dupe_tag= FALSE;
2159 /* always do all three, to prevent data hanging around */
2162 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2163 for(j=0; j<3; j++) {
2164 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2165 bezt->vec[0][j], bezt->vec[1][j],
2166 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2169 /* if both arrays are NULL do nothiong */
2170 alfa_bezpart( prevbezt, bezt, nu,
2171 do_tilt ? &bevp->alfa : NULL,
2172 do_radius ? &bevp->radius : NULL,
2173 do_weight ? &bevp->weight : NULL,
2174 resolu, sizeof(BevPoint));
2177 if(cu->twist_mode==CU_TWIST_TANGENT) {
2178 forward_diff_bezier_cotangent(
2179 prevbezt->vec[1], prevbezt->vec[2],
2180 bezt->vec[0], bezt->vec[1],
2181 bevp->tan, resolu, sizeof(BevPoint));
2184 /* indicate with handlecodes double points */
2185 if(prevbezt->h1==prevbezt->h2) {
2186 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2189 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2190 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2199 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2200 VECCOPY(bevp->vec, prevbezt->vec[1]);
2201 bevp->alfa= prevbezt->alfa;
2202 bevp->radius= prevbezt->radius;
2203 bevp->weight= prevbezt->weight;
2207 else if(nu->type == CU_NURBS) {
2209 len= (resolu*SEGMENTSU(nu));
2211 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2212 BLI_addtail(&(cu->bev), bl);
2215 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2217 bevp= (BevPoint *)(bl+1);
2219 makeNurbcurve( nu, &bevp->vec[0],
2220 do_tilt ? &bevp->alfa : NULL,
2221 do_radius ? &bevp->radius : NULL,
2222 do_weight ? &bevp->weight : NULL,
2223 resolu, sizeof(BevPoint));
2230 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2233 if (bl->nr) { /* null bevel items come from single points */
2235 bevp1= (BevPoint *)(bl+1);
2236 bevp0= bevp1+(nr-1);
2239 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2240 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2241 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2242 bevp0->dupe_tag= TRUE;
2256 if(bl->nr && bl->dupe_nr) {
2257 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2258 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2259 memcpy(blnew, bl, sizeof(BevList));
2261 BLI_remlink(&(cu->bev), bl);
2262 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2263 bevp0= (BevPoint *)(bl+1);
2264 bevp1= (BevPoint *)(blnew+1);
2267 if(bevp0->dupe_tag==0) {
2268 memcpy(bevp1, bevp0, sizeof(BevPoint));
2280 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2284 if(bl->nr && bl->poly>=0) {
2293 /* find extreme left points, also test (turning) direction */
2295 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2301 bevp= (BevPoint *)(bl+1);
2304 if(min>bevp->vec[0]) {
2313 bevp= (BevPoint *)(bl+1);
2314 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2315 else bevp0= bevp1-1;
2316 bevp= bevp+ (bl->nr-1);
2317 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2318 else bevp2= bevp1+1;
2320 inp= (bevp1->vec[0]- bevp0->vec[0]) * (bevp0->vec[1]- bevp2->vec[1]) + (bevp0->vec[1]- bevp1->vec[1]) * (bevp0->vec[0]- bevp2->vec[0]);
2322 if(inp > 0.0f) sd->dir= 1;
2330 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2333 for(a=1; a<poly; a++, sd++) {
2334 bl= sd->bl; /* is bl a hole? */
2335 sd1= sortdata+ (a-1);
2336 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2337 if(bevelinside(sd1->bl, bl)) {
2338 bl->hole= 1- sd1->bl->hole;
2344 /* turning direction */
2345 if((cu->flag & CU_3D)==0) {
2347 for(a=0; a<poly; a++, sd++) {
2348 if(sd->bl->hole==sd->dir) {
2350 bevp1= (BevPoint *)(bl+1);
2351 bevp2= bevp1+ (bl->nr-1);
2354 SWAP(BevPoint, *bevp1, *bevp2);
2361 MEM_freeN(sortdata);
2364 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2365 if((cu->flag & CU_3D)==0) {
2366 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2367 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2374 else if(bl->nr==2) { /* 2 pnt, treat separate */
2375 bevp2= (BevPoint *)(bl+1);
2378 x1= bevp1->vec[0]- bevp2->vec[0];
2379 y1= bevp1->vec[1]- bevp2->vec[1];
2381 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2382 bevp2->sina= bevp1->sina;
2383 bevp2->cosa= bevp1->cosa;
2385 /* fill in dir & quat */
2386 make_bevel_list_segment_3D(bl);
2389 bevp2= (BevPoint *)(bl+1);
2390 bevp1= bevp2+(bl->nr-1);
2395 x1= bevp1->vec[0]- bevp0->vec[0];
2396 x2= bevp1->vec[0]- bevp2->vec[0];
2397 y1= bevp1->vec[1]- bevp0->vec[1];
2398 y2= bevp1->vec[1]- bevp2->vec[1];
2400 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2402 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2403 * no need for tricky tilt calculation as with 3D curves */
2404 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2405 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2406 /* done with inline make_bevel_list_3D_zup */
2413 /* correct non-cyclic cases */
2415 bevp= (BevPoint *)(bl+1);
2417 bevp->sina= bevp1->sina;
2418 bevp->cosa= bevp1->cosa;
2419 bevp= (BevPoint *)(bl+1);
2422 bevp->sina= bevp1->sina;
2423 bevp->cosa= bevp1->cosa;
2425 /* correct for the dir/quat, see above why its needed */
2426 bevel_list_cyclic_fix_3D(bl);
2432 else { /* 3D Curves */
2439 else if(bl->nr==2) { /* 2 pnt, treat separate */
2440 make_bevel_list_segment_3D(bl);
2443 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2450 /* ****************** HANDLES ************** */
2454 * 0: nothing, 1:auto, 2:vector, 3:aligned
2457 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2458 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2460 float *p1,*p2,*p3, pt[3];
2461 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2462 const float eps= 1e-5;
2464 if(bezt->h1==0 && bezt->h2==0) return;
2470 pt[0]= 2*p2[0]- p3[0];
2471 pt[1]= 2*p2[1]- p3[1];
2472 pt[2]= 2*p2[2]- p3[2];
2475 else p1= prev->vec[1];
2478 pt[0]= 2*p2[0]- p1[0];
2479 pt[1]= 2*p2[1]- p1[1];
2480 pt[2]= 2*p2[2]- p1[2];
2483 else p3= next->vec[1];
2490 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2497 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2499 if(len1==0.0f) len1=1.0f;
2500 if(len2==0.0f) len2=1.0f;
2503 if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM) || ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) { /* auto */
2504 vx= dx1/len2 + dx/len1;
2505 vy= dy1/len2 + dy/len1;
2506 vz= dz1/len2 + dz/len1;
2507 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2509 int leftviolate=0, rightviolate=0; /* for mode==2 */
2511 if(len1>5.0f*len2) len1= 5.0f*len2;
2512 if(len2>5.0f*len1) len2= 5.0f*len1;
2514 if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM)) {
2516 *(p2-3)= *p2-vx*len1;
2517 *(p2-2)= *(p2+1)-vy*len1;
2518 *(p2-1)= *(p2+2)-vz*len1;
2520 if((bezt->h1==HD_AUTO_ANIM) && next && prev) { // keep horizontal if extrema
2521 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2522 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2523 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2524 bezt->vec[0][1]= bezt->vec[1][1];
2526 else { // handles should not be beyond y coord of two others
2527 if(ydiff1 <= 0.0f) {
2528 if(prev->vec[1][1] > bezt->vec[0][1]) {
2529 bezt->vec[0][1]= prev->vec[1][1];
2534 if(prev->vec[1][1] < bezt->vec[0][1]) {
2535 bezt->vec[0][1]= prev->vec[1][1];
2542 if(ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) {
2544 *(p2+3)= *p2+vx*len2;
2545 *(p2+4)= *(p2+1)+vy*len2;
2546 *(p2+5)= *(p2+2)+vz*len2;
2548 if((bezt->h2==HD_AUTO_ANIM) && next && prev) { // keep horizontal if extrema
2549 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2550 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2551 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2552 bezt->vec[2][1]= bezt->vec[1][1];
2554 else { // handles should not be beyond y coord of two others
2555 if(ydiff1 <= 0.0f) {
2556 if(next->vec[1][1] < bezt->vec[2][1]) {
2557 bezt->vec[2][1]= next->vec[1][1];
2562 if(next->vec[1][1] > bezt->vec[2][1]) {
2563 bezt->vec[2][1]= next->vec[1][1];
2570 if(leftviolate || rightviolate) { /* align left handle */
2573 sub_v3_v3v3(h1, p2-3, p2);
2574 sub_v3_v3v3(h2, p2, p2+3);
2575 len1= normalize_v3(h1);
2576 len2= normalize_v3(h2);
2578 vz= dot_v3v3(h1, h2);
2581 *(p2+3)= *(p2) - vz*len2*h1[0];
2582 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2583 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2586 *(p2-3)= *(p2) + vz*len1*h2[0];
2587 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2588 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2595 if(bezt->h1==HD_VECT) { /* vector */
2600 *(p2-2)= *(p2+1)-dy;
2601 *(p2-1)= *(p2+2)-dz;
2603 if(bezt->h2==HD_VECT) {
2608 *(p2+4)= *(p2+1)+dy1;
2609 *(p2+5)= *(p2+2)+dz1;
2612 len2= len_v3v3(p2, p2+3);
2613 len1= len_v3v3(p2, p2-3);
2614 if(len1==0.0f) len1= 1.0f;
2615 if(len2==0.0f) len2= 1.0f;
2617 if(bezt->f1 & SELECT) { /* order of calculation */
2618 if(bezt->h2==HD_ALIGN) { /* aligned */
2621 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2622 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2623 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2626 if(bezt->h1==HD_ALIGN) {
2629 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2630 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2631 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2636 if(bezt->h1==HD_ALIGN) {
2639 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2640 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2641 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2644 if(bezt->h2==HD_ALIGN) { /* aligned */
2647 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2648 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2649 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2655 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2657 BezTriple *bezt, *prev, *next;
2660 if(nu->type != CU_BEZIER) return;
2661 if(nu->pntsu<2) return;
2665 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2670 calchandleNurb(bezt, prev, next, 0);
2673 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2683 void testhandlesNurb(Nurb *nu)
2685 /* use when something has changed with handles.
2686 it treats all BezTriples with the following rules:
2687 PHASE 1: do types have to be altered?
2688 Auto handles: become aligned when selection status is NOT(000 || 111)
2689 Vector handles: become 'nothing' when (one half selected AND other not)
2690 PHASE 2: recalculate handles
2695 if(nu->type != CU_BEZIER) return;
2701 if(bezt->f1 & SELECT) flag++;
2702 if(bezt->f2 & SELECT) flag += 2;
2703 if(bezt->f3 & SELECT) flag += 4;
2705 if( !(flag==0 || flag==7) ) {
2706 if(ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
2709 if(ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
2713 if(bezt->h1==HD_VECT) { /* vector */
2714 if(flag < 4) bezt->h1= 0;
2716 if(bezt->h2==HD_VECT) { /* vector */
2717 if( flag > 3) bezt->h2= 0;
2723 calchandlesNurb(nu);
2726 void autocalchandlesNurb(Nurb *nu, int flag)
2728 /* checks handle coordinates and calculates type */
2730 BezTriple *bezt2, *bezt1, *bezt0;
2731 int i, align, leftsmall, rightsmall;
2733 if(nu==NULL || nu->bezt==NULL) return;
2736 bezt1 = bezt2 + (nu->pntsu-1);
2742 align= leftsmall= rightsmall= 0;
2745 if(flag==0 || (bezt1->f1 & flag) ) {
2747 /* distance too short: vectorhandle */
2748 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2753 /* aligned handle? */
2754 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2756 bezt1->h1= HD_ALIGN;
2758 /* or vector handle? */
2759 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2765 if(flag==0 || (bezt1->f3 & flag) ) {
2767 /* distance too short: vectorhandle */
2768 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2773 /* aligned handle? */
2774 if(align) bezt1->h2= HD_ALIGN;
2776 /* or vector handle? */
2777 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2782 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2783 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2785 /* undesired combination: */
2786 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2787 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2794 calchandlesNurb(nu);
2797 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2801 nu= editnurb->first;
2803 autocalchandlesNurb(nu, flag);
2808 void sethandlesNurb(ListBase *editnurb, short code)
2810 /* code==1: set autohandle */
2811 /* code==2: set vectorhandle */
2812 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2813 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2814 /* code==5: Set align, like 3 but no toggle */
2815 /* code==6: Clear align, like 3 but no toggle */
2820 if(code==1 || code==2) {
2821 nu= editnurb->first;
2823 if(nu->type == CU_BEZIER) {
2827 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2828 if(bezt->f1 & SELECT) bezt->h1= code;
2829 if(bezt->f3 & SELECT) bezt->h2= code;
2830 if(bezt->h1!=bezt->h2) {
2831 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2832 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2837 calchandlesNurb(nu);
2843 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2845 nu= editnurb->first;
2848 } else if (code == 6) {
2853 if(nu->type == CU_BEZIER) {
2857 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2858 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2868 nu= editnurb->first;
2870 if(nu->type == CU_BEZIER) {
2874 if(bezt->f1 & SELECT) bezt->h1= ok;
2875 if(bezt->f3 & SELECT) bezt->h2= ok;
2879 calchandlesNurb(nu);
2886 static void swapdata(void *adr1, void *adr2, int len)
2894 memcpy(adr, adr1, len);
2895 memcpy(adr1, adr2, len);
2896 memcpy(adr2, adr, len);
2901 adr= (char *)MEM_mallocN(len, "curve swap");
2902 memcpy(adr, adr1, len);
2903 memcpy(adr1, adr2, len);
2904 memcpy(adr2, adr, len);
2909 void switchdirectionNurb(Nurb *nu)
2911 BezTriple *bezt1, *bezt2;
2913 float *fp1, *fp2, *tempf;
2916 if(nu->pntsu==1 && nu->pntsv==1) return;
2918 if(nu->type == CU_BEZIER) {
2922 if(a & 1) a+= 1; /* if odd, also swap middle content */
2925 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2927 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2928 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2930 SWAP(char, bezt1->h1, bezt1->h2);
2931 SWAP(short, bezt1->f1, bezt1->f3);
2934 SWAP(char, bezt2->h1, bezt2->h2);
2935 SWAP(short, bezt2->f1, bezt2->f3);
2936 bezt1->alfa= -bezt1->alfa;
2937 bezt2->alfa= -bezt2->alfa;
2944 else if(nu->pntsv==1) {
2949 while(bp1!=bp2 && a>0) {
2950 SWAP(BPoint, *bp1, *bp2);
2952 bp1->alfa= -bp1->alfa;
2953 bp2->alfa= -bp2->alfa;
2957 if(nu->type == CU_NURBS) {
2958 /* no knots for too short paths */