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 #include <math.h> // floor
37 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_material_types.h"
44 /* for dereferencing pointers */
45 #include "DNA_key_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_vfont_types.h"
48 #include "DNA_object_types.h"
50 #include "BKE_animsys.h"
52 #include "BKE_curve.h"
53 #include "BKE_displist.h"
55 #include "BKE_global.h"
57 #include "BKE_library.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h" // VECCOPY
67 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
69 float *labda, float *mu, float *vec);
71 void unlink_curve(Curve *cu)
75 for(a=0; a<cu->totcol; a++) {
76 if(cu->mat[a]) cu->mat[a]->id.us--;
79 if(cu->vfont) cu->vfont->id.us--;
82 if(cu->vfontb) cu->vfontb->id.us--;
85 if(cu->vfonti) cu->vfonti->id.us--;
88 if(cu->vfontbi) cu->vfontbi->id.us--;
91 if(cu->key) cu->key->id.us--;
95 /* frees editcurve entirely */
96 void BKE_free_editfont(Curve *cu)
99 EditFont *ef= cu->editfont;
101 if(ef->oldstr) MEM_freeN(ef->oldstr);
102 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
103 if(ef->textbuf) MEM_freeN(ef->textbuf);
104 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
105 if(ef->copybuf) MEM_freeN(ef->copybuf);
106 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
113 /* don't free curve itself */
114 void free_curve(Curve *cu)
116 freeNurblist(&cu->nurb);
117 BLI_freelistN(&cu->bev);
118 freedisplist(&cu->disp);
119 BKE_free_editfont(cu);
121 free_curve_editNurb(cu);
123 BKE_free_animdata((ID *)cu);
125 if(cu->mat) MEM_freeN(cu->mat);
126 if(cu->str) MEM_freeN(cu->str);
127 if(cu->strinfo) MEM_freeN(cu->strinfo);
128 if(cu->bb) MEM_freeN(cu->bb);
129 if(cu->path) free_path(cu->path);
130 if(cu->tb) MEM_freeN(cu->tb);
133 Curve *add_curve(char *name, int type)
137 cu= alloc_libblock(&G.main->curve, ID_CU, name);
139 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
140 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
142 cu->resolu= cu->resolv= 12;
145 cu->spacing= cu->linedist= 1.0;
148 cu->texflag= CU_AUTOSPACE;
149 cu->smallcaps_scale= 0.75f;
150 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
152 cu->bb= unit_boundbox();
155 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
157 cu->str= MEM_mallocN(12, "str");
158 BLI_strncpy(cu->str, "Text", 12);
160 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
161 cu->totbox= cu->actbox= 1;
162 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
163 cu->tb[0].w = cu->tb[0].h = 0.0;
169 Curve *copy_curve(Curve *cu)
174 cun= copy_libblock(cu);
175 cun->nurb.first= cun->nurb.last= 0;
176 duplicateNurblist( &(cun->nurb), &(cu->nurb));
178 cun->mat= MEM_dupallocN(cu->mat);
179 for(a=0; a<cun->totcol; a++) {
180 id_us_plus((ID *)cun->mat[a]);
183 cun->str= MEM_dupallocN(cu->str);
184 cun->strinfo= MEM_dupallocN(cu->strinfo);
185 cun->tb= MEM_dupallocN(cu->tb);
186 cun->bb= MEM_dupallocN(cu->bb);
188 cun->key= copy_key(cu->key);
189 if(cun->key) cun->key->from= (ID *)cun;
191 cun->disp.first= cun->disp.last= 0;
192 cun->bev.first= cun->bev.last= 0;
198 #if 0 // XXX old animation system
199 /* single user ipo too */
200 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
201 #endif // XXX old animation system
203 id_us_plus((ID *)cun->vfont);
204 id_us_plus((ID *)cun->vfontb);
205 id_us_plus((ID *)cun->vfonti);
206 id_us_plus((ID *)cun->vfontbi);
211 void make_local_curve(Curve *cu)
217 /* - when there are only lib users: don't do
218 * - when there are only local users: set flag
222 if(cu->id.lib==0) return;
224 if(cu->vfont) cu->vfont->id.lib= NULL;
225 if(cu->vfontb) cu->vfontb->id.lib= NULL;
226 if(cu->vfonti) cu->vfonti->id.lib= NULL;
227 if(cu->vfontbi) cu->vfontbi->id.lib= NULL;
231 cu->id.flag= LIB_LOCAL;
232 new_id(0, (ID *)cu, 0);
236 ob= G.main->object.first;
239 if(ob->id.lib) lib= 1;
245 if(local && lib==0) {
247 cu->id.flag= LIB_LOCAL;
248 new_id(0, (ID *)cu, 0);
250 else if(local && lib) {
254 ob= G.main->object.first;
269 short curve_type(Curve *cu)
275 for (nu= cu->nurb.first; nu; nu= nu->next) {
284 void test_curve_type(Object *ob)
286 ob->type = curve_type(ob->data);
289 void tex_space_curve(Curve *cu)
293 float *fp, min[3], max[3];
296 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
299 INIT_MINMAX(min, max);
304 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
305 else tot= dl->nr*dl->parts;
310 DO_MINMAX(fp, min, max);
317 min[0] = min[1] = min[2] = -1.0f;
318 max[0] = max[1] = max[2] = 1.0f;
321 boundbox_set_from_min_max(bb, min, max);
323 if(cu->texflag & CU_AUTOSPACE) {
324 mid_v3_v3v3(cu->loc, min, max);
325 cu->size[0]= (max[0]-min[0])/2.0f;
326 cu->size[1]= (max[1]-min[1])/2.0f;
327 cu->size[2]= (max[2]-min[2])/2.0f;
329 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
331 if(cu->size[0]==0.0) cu->size[0]= 1.0;
332 else if(cu->size[0]>0.0 && cu->size[0]<0.00001) cu->size[0]= 0.00001;
333 else if(cu->size[0]<0.0 && cu->size[0]> -0.00001) cu->size[0]= -0.00001;
335 if(cu->size[1]==0.0) cu->size[1]= 1.0;
336 else if(cu->size[1]>0.0 && cu->size[1]<0.00001) cu->size[1]= 0.00001;
337 else if(cu->size[1]<0.0 && cu->size[1]> -0.00001) cu->size[1]= -0.00001;
339 if(cu->size[2]==0.0) cu->size[2]= 1.0;
340 else if(cu->size[2]>0.0 && cu->size[2]<0.00001) cu->size[2]= 0.00001;
341 else if(cu->size[2]<0.0 && cu->size[2]> -0.00001) cu->size[2]= -0.00001;
347 int count_curveverts(ListBase *nurb)
354 if(nu->bezt) tot+= 3*nu->pntsu;
355 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
362 int count_curveverts_without_handles(ListBase *nurb)
369 if(nu->bezt) tot+= nu->pntsu;
370 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
377 /* **************** NURBS ROUTINES ******************** */
379 void freeNurb(Nurb *nu)
384 if(nu->bezt) MEM_freeN(nu->bezt);
386 if(nu->bp) MEM_freeN(nu->bp);
388 if(nu->knotsu) MEM_freeN(nu->knotsu);
390 if(nu->knotsv) MEM_freeN(nu->knotsv);
392 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
399 void freeNurblist(ListBase *lb)
411 lb->first= lb->last= 0;
414 Nurb *duplicateNurb(Nurb *nu)
419 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
420 if(newnu==0) return 0;
421 memcpy(newnu, nu, sizeof(Nurb));
425 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
426 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
429 len= nu->pntsu*nu->pntsv;
431 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
432 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
434 newnu->knotsu= newnu->knotsv= NULL;
439 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
440 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
443 if(nu->pntsv>1 && nu->knotsv) {
446 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
447 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
454 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
462 nun= duplicateNurb(nu);
463 BLI_addtail(lb1, nun);
469 void test2DNurb(Nurb *nu)
475 if((nu->flag & CU_2D)==0)
478 if(nu->type == CU_BEZIER) {
482 bezt->vec[0][2]= 0.0;
483 bezt->vec[1][2]= 0.0;
484 bezt->vec[2][2]= 0.0;
489 a= nu->pntsu*nu->pntsv;
498 void minmaxNurb(Nurb *nu, float *min, float *max)
504 if(nu->type == CU_BEZIER) {
508 DO_MINMAX(bezt->vec[0], min, max);
509 DO_MINMAX(bezt->vec[1], min, max);
510 DO_MINMAX(bezt->vec[2], min, max);
515 a= nu->pntsu*nu->pntsv;
518 DO_MINMAX(bp->vec, min, max);
524 /* be sure to call makeknots after this */
525 void addNurbPoints(Nurb *nu, int number)
529 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
532 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
536 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
538 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
545 void addNurbPointsBezier(Nurb *nu, int number)
547 BezTriple *tmp= nu->bezt;
549 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
552 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
556 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
558 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
565 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
568 static void calcknots(float *knots, short aantal, short order, short type)
569 /* knots: number of pnts NOT corrected for cyclic */
570 /* type; 0: uniform, 1: endpoints, 2: bezier */
586 if(a>=order && a<=aantal) k+= 1.0;
590 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
594 knots[a]= (float)floor(k);
601 if(a>=order && a<=aantal) k+= (0.5);
602 knots[a]= (float)floor(k);
606 printf("bez nurb curve order is not 3 or 4, should never happen\n");
611 static void makecyclicknots(float *knots, short pnts, short order)
612 /* pnts, order: number of pnts NOT corrected for cyclic */
620 /* do first long rows (order -1), remove identical knots at endpoints */
623 for(a=1; a<order2; a++) {
624 if(knots[b]!= knots[b-a]) break;
626 if(a==order2) knots[pnts+order-2]+= 1.0;
630 c=pnts + order + order2;
631 for(a=pnts+order2; a<c; a++) {
632 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
639 static void makeknots(Nurb *nu, short uv)
641 if(nu->type == CU_NURBS) {
643 if(nu->knotsu) MEM_freeN(nu->knotsu);
644 if(check_valid_nurb_u(nu)) {
645 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
646 if(nu->flagu & CU_NURB_CYCLIC) {
647 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
648 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
650 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
653 else nu->knotsu= NULL;
656 if(nu->knotsv) MEM_freeN(nu->knotsv);
657 if(check_valid_nurb_v(nu)) {
658 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
659 if(nu->flagv & CU_NURB_CYCLIC) {
660 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
661 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
663 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
666 else nu->knotsv= NULL;
671 void nurbs_knot_calc_u(Nurb *nu)
676 void nurbs_knot_calc_v(Nurb *nu)
681 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
684 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
686 orderpluspnts= order+pnts;
687 opp2 = orderpluspnts-1;
689 /* this is for float inaccuracy */
690 if(t < knots[0]) t= knots[0];
691 else if(t > knots[opp2]) t= knots[opp2];
693 /* this part is order '1' */
695 for(i=0;i<opp2;i++) {
696 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
712 /* this is order 2,3,... */
713 for(j=2; j<=order; j++) {
715 if(i2+j>= orderpluspnts) i2= opp2-j;
717 for(i= i1; i<=i2; i++) {
719 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
724 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
735 for(i=i1; i<=i2; i++) {
738 if(*start==1000) *start= i;
744 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
745 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
748 float *basisu, *basis, *basisv, *sum, *fp, *in;
749 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
750 int i, j, iofs, jofs, cycl, len, curu, curv;
751 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
753 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
755 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
756 if(nu->orderu>nu->pntsu) return;
757 if(nu->orderv>nu->pntsv) return;
758 if(coord_array==NULL) return;
760 /* allocate and initialize */
766 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
775 i= nu->pntsu*nu->pntsv;
778 if(bp->vec[3]!=1.0) {
786 ustart= fp[nu->orderu-1];
787 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
788 else uend= fp[nu->pntsu];
789 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
791 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
794 vstart= fp[nu->orderv-1];
796 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
797 else vend= fp[nu->pntsv];
798 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
801 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
802 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
803 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
805 /* precalculation of basisv and jstart,jend */
806 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
812 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
817 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
824 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
837 for(j= jsta; j<=jen; j++) {
839 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
841 bp= nu->bp+ nu->pntsu*jofs+istart-1;
843 for(i= istart; i<=iend; i++, fp++) {
847 bp= nu->bp+ nu->pntsu*jofs+iofs;
852 *fp= basisu[i]*basis[j]*bp->vec[3];
855 else *fp= basisu[i]*basis[j];
861 for(j= jsta; j<=jen; j++) {
862 for(i= istart; i<=iend; i++, fp++) {
868 /* one! (1.0) real point now */
870 for(j= jsta; j<=jen; j++) {
872 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
874 bp= nu->bp+ nu->pntsu*jofs+istart-1;
876 for(i= istart; i<=iend; i++, fp++) {
880 bp= nu->bp+ nu->pntsu*jofs+iofs;
885 in[0]+= (*fp) * bp->vec[0];
886 in[1]+= (*fp) * bp->vec[1];
887 in[2]+= (*fp) * bp->vec[2];
896 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
907 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
908 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
909 * tilt_array and radius_array will be written to if valid */
912 float u, ustart, uend, ustep, sumdiv;
913 float *basisu, *sum, *fp;
914 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
915 int i, len, istart, iend, cycl;
917 if(nu->knotsu==NULL) return;
918 if(nu->orderu>nu->pntsu) return;
919 if(coord_array==0) return;
921 /* allocate and initialize */
924 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
926 resolu= (resolu*SEGMENTSU(nu));
934 ustart= fp[nu->orderu-1];
935 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
936 else uend= fp[nu->pntsu];
937 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
939 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
941 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
947 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
951 bp= nu->bp+ istart-1;
952 for(i= istart; i<=iend; i++, fp++) {
954 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
957 *fp= basisu[i]*bp->vec[3];
960 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
961 /* is normalizing needed? */
963 for(i= istart; i<=iend; i++, fp++) {
968 /* one! (1.0) real point */
970 bp= nu->bp+ istart-1;
971 for(i= istart; i<=iend; i++, fp++) {
973 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
978 coord_fp[0]+= (*fp) * bp->vec[0];
979 coord_fp[1]+= (*fp) * bp->vec[1];
980 coord_fp[2]+= (*fp) * bp->vec[2];
983 (*tilt_fp) += (*fp) * bp->alfa;
986 (*radius_fp) += (*fp) * bp->radius;
989 (*weight_fp) += (*fp) * bp->weight;
994 coord_fp = (float *)(((char *)coord_fp) + stride);
996 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
997 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
998 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1008 /* forward differencing method for bezier curve */
1009 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1011 float rt0,rt1,rt2,rt3,f;
1016 rt1= 3.0f*(q1-q0)/f;
1018 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1020 rt3= (q3-q0+3.0f*(q1-q2))/f;
1027 for(a=0; a<=it; a++) {
1029 p = (float *)(((char *)p)+stride);
1036 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1038 /* note that these are not purpendicular to the curve
1039 * they need to be rotated for this,
1041 * This could also be optimized like forward_diff_bezier */
1043 for(a=0; a<=it; a++) {
1044 float t = (float)a / (float)it;
1047 for(i=0; i<3; i++) {
1048 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1051 p = (float *)(((char *)p)+stride);
1055 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1057 float *make_orco_surf(Object *ob)
1059 /* Note: this function is used in convertblender only atm, so
1060 * suppose nonzero curve's render resolution should always be used */
1061 Curve *cu= ob->data;
1066 float *fp, *coord_array;
1068 /* first calculate the size of the datablock */
1071 /* as we want to avoid the seam in a cyclic nurbs
1072 texture wrapping, reserve extra orco data space to save these extra needed
1073 vertex based UV coordinates for the meridian vertices.
1074 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1075 the renderface/vert construction.
1077 See also convertblender.c: init_render_surf()
1080 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1081 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1083 sizeu = nu->pntsu*resolu;
1084 sizev = nu->pntsv*resolv;
1085 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1086 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1087 if(nu->pntsv>1) tot+= sizeu * sizev;
1091 /* makeNurbfaces wants zeros */
1092 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1096 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1097 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1100 sizeu = nu->pntsu*resolu;
1101 sizev = nu->pntsv*resolv;
1102 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1103 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1105 if(cu->flag & CU_UV_ORCO) {
1106 for(b=0; b< sizeu; b++) {
1107 for(a=0; a< sizev; a++) {
1109 if(sizev <2) fp[0]= 0.0f;
1110 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1112 if(sizeu <2) fp[1]= 0.0f;
1113 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1122 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1123 float *tdata= _tdata;
1125 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1127 for(b=0; b<sizeu; b++) {
1129 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1132 for(a=0; a<sizev; a++) {
1134 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1137 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1139 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1140 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1141 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1156 /* NOTE: This routine is tied to the order of vertex
1157 * built by displist and as passed to the renderer.
1159 float *make_orco_curve(Scene *scene, Object *ob)
1161 Curve *cu = ob->data;
1164 float *fp, *coord_array;
1165 ListBase disp = {NULL, NULL};
1167 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1170 for (dl=disp.first; dl; dl=dl->next) {
1171 if (dl->type==DL_INDEX3) {
1173 } else if (dl->type==DL_SURF) {
1174 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1175 if (dl->flag & DL_CYCL_U) {
1176 if (dl->flag & DL_CYCL_V)
1177 numVerts += (dl->parts+1)*(dl->nr+1);
1179 numVerts += dl->parts*(dl->nr+1);
1182 numVerts += dl->parts*dl->nr;
1186 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1187 for (dl=disp.first; dl; dl=dl->next) {
1188 if (dl->type==DL_INDEX3) {
1189 for (u=0; u<dl->nr; u++, fp+=3) {
1190 if (cu->flag & CU_UV_ORCO) {
1191 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1195 VECCOPY(fp, &dl->verts[u*3]);
1197 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1198 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1199 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1202 } else if (dl->type==DL_SURF) {
1203 int sizeu= dl->nr, sizev= dl->parts;
1205 /* exception as handled in convertblender.c too */
1206 if (dl->flag & DL_CYCL_U) {
1208 if (dl->flag & DL_CYCL_V)
1212 for (u=0; u<sizev; u++) {
1213 for (v=0; v<sizeu; v++,fp+=3) {
1214 if (cu->flag & CU_UV_ORCO) {
1215 fp[0]= 2.0f*u/(dl->parts-1) - 1.0f;
1216 fp[1]= 2.0f*v/(dl->nr-1) - 1.0f;
1220 int realv= v % dl->nr;
1221 int realu= u % dl->parts;
1223 vert= dl->verts + 3*(dl->nr*realu + realv);
1226 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1227 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1228 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1235 freedisplist(&disp);
1241 /* ***************** BEVEL ****************** */
1243 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1245 DispList *dl, *dlnew;
1247 float *fp, facx, facy, angle, dangle;
1251 disp->first = disp->last = NULL;
1253 /* if a font object is being edited, then do nothing */
1254 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1257 if (cu->bevobj->type!=OB_CURVE) return;
1259 bevcu= cu->bevobj->data;
1260 if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1261 ListBase bevdisp= {NULL, NULL};
1262 facx= cu->bevobj->size[0];
1263 facy= cu->bevobj->size[1];
1266 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1269 dl= bevcu->disp.first;
1271 makeDispListCurveTypes(scene, cu->bevobj, 0);
1272 dl= bevcu->disp.first;
1277 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1278 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1280 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1281 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1283 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1285 BLI_addtail(disp, dlnew);
1287 nr= dlnew->parts*dlnew->nr;
1298 freedisplist(&bevdisp);
1301 else if(cu->ext1==0.0 && cu->ext2==0.0) {
1304 else if(cu->ext2==0.0) {
1305 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1306 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1307 BLI_addtail(disp, dl);
1310 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1319 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1321 nr= 4+ 2*cu->bevresol;
1323 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1324 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1325 BLI_addtail(disp, dl);
1328 dl->flag= DL_BACK_CURVE;
1333 dangle= (2.0f*M_PI/(nr));
1334 angle= -(nr-1)*dangle;
1336 for(a=0; a<nr; a++) {
1338 fp[1]= (float)(cos(angle)*(cu->ext2));
1339 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1347 /* bevel now in three parts, for proper vertex normals */
1350 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1351 dnr= nr= 2+ cu->bevresol;
1352 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1353 nr= 3+ 2*cu->bevresol;
1355 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1356 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1357 BLI_addtail(disp, dl);
1360 dl->flag= DL_BACK_CURVE;
1365 dangle= (0.5*M_PI/(dnr-1));
1366 angle= -(nr-1)*dangle;
1368 for(a=0; a<nr; a++) {
1370 fp[1]= (float)(cos(angle)*(cu->ext2));
1371 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1377 /* part 2, sidefaces */
1381 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1382 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1383 BLI_addtail(disp, dl);
1394 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1395 dl= MEM_dupallocN(dl);
1396 dl->verts= MEM_dupallocN(dl->verts);
1397 BLI_addtail(disp, dl);
1408 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1409 dnr= nr= 2+ cu->bevresol;
1410 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1411 nr= 3+ 2*cu->bevresol;
1413 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1414 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1415 BLI_addtail(disp, dl);
1417 dl->flag= DL_FRONT_CURVE;
1424 dangle= (0.5*M_PI/(dnr-1));
1426 for(a=0; a<nr; a++) {
1428 fp[1]= (float)(cos(angle)*(cu->ext2));
1429 fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1437 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1441 0: no intersection of segments
1442 1: exact intersection of segments
1443 2: cross-intersection of segments
1447 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1448 if(deler==0.0) return -1;
1450 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1451 *labda= -(*labda/deler);
1453 deler= v3[coy]-v4[coy];
1455 deler=v3[cox]-v4[cox];
1456 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1458 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1460 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1461 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1463 if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1464 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1471 static short bevelinside(BevList *bl1,BevList *bl2)
1473 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1474 /* returns '1' if correct hole */
1475 BevPoint *bevp, *prevbevp;
1476 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1477 int nr, links=0,rechts=0,mode;
1479 /* take first vertex of possible hole */
1481 bevp= (BevPoint *)(bl2+1);
1482 hvec1[0]= bevp->vec[0];
1483 hvec1[1]= bevp->vec[1];
1485 VECCOPY(hvec2,hvec1);
1488 /* test it with all edges of potential surounding poly */
1489 /* count number of transitions left-right */
1491 bevp= (BevPoint *)(bl1+1);
1493 prevbevp= bevp+(nr-1);
1496 min= prevbevp->vec[1];
1500 max= prevbevp->vec[1];
1503 if(min<=hvec1[1] && max>=hvec1[1]) {
1504 /* there's a transition, calc intersection point */
1505 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1506 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1507 only allow for one situation: we choose lab= 1.0
1509 if(mode>=0 && lab!=0.0) {
1510 if(vec[0]<hvec1[0]) links++;
1519 if( (links & 1) && (rechts & 1) ) return 1;
1530 static int vergxcobev(const void *a1, const void *a2)
1532 const struct bevelsort *x1=a1,*x2=a2;
1534 if( x1->left > x2->left ) return 1;
1535 else if( x1->left < x2->left) return -1;
1539 /* this function cannot be replaced with atan2, but why? */
1541 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1543 float t01, t02, x3, y3;
1545 t01= (float)sqrt(x1*x1+y1*y1);
1546 t02= (float)sqrt(x2*x2+y2*y2);
1547 if(t01==0.0) t01= 1.0;
1548 if(t02==0.0) t02= 1.0;
1556 if(fabs(t02)>=1.0) t02= .5*M_PI;
1557 else t02= (saacos(t02))/2.0f;
1559 t02= (float)sin(t02);
1560 if(t02==0.0) t02= 1.0;
1564 if(x3==0 && y3==0) {
1568 t01= (float)sqrt(x3*x3+y3*y3);
1578 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1580 BezTriple *pprev, *next, *last;
1581 float fac, dfac, t[4];
1584 if(tilt_array==NULL && radius_array==NULL)
1587 last= nu->bezt+(nu->pntsu-1);
1589 /* returns a point */
1590 if(prevbezt==nu->bezt) {
1591 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1592 else pprev= prevbezt;
1594 else pprev= prevbezt-1;
1598 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1604 dfac= 1.0f/(float)resolu;
1606 for(a=0; a<resolu; a++, fac+= dfac) {
1608 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1609 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1611 key_curve_position_weights(fac, t, nu->tilt_interp);
1612 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1615 tilt_array = (float *)(((char *)tilt_array) + stride);
1619 if (nu->radius_interp==KEY_CU_EASE) {
1620 /* Support 2.47 ease interp
1621 * Note! - this only takes the 2 points into account,
1622 * giving much more localized results to changes in radius, sometimes you want that */
1623 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1626 /* reuse interpolation from tilt if we can */
1627 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1628 key_curve_position_weights(fac, t, nu->radius_interp);
1630 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1633 radius_array = (float *)(((char *)radius_array) + stride);
1637 /* basic interpolation for now, could copy tilt interp too */
1638 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1640 weight_array = (float *)(((char *)weight_array) + stride);
1645 /* make_bevel_list_3D_* funcs, at a minimum these must
1646 * fill in the bezp->quat and bezp->dir values */
1648 /* correct non-cyclic cases by copying direction and rotation
1649 * values onto the first & last end-points */
1650 static void bevel_list_cyclic_fix_3D(BevList *bl)
1652 BevPoint *bevp, *bevp1;
1654 bevp= (BevPoint *)(bl+1);
1656 QUATCOPY(bevp->quat, bevp1->quat);
1657 VECCOPY(bevp->dir, bevp1->dir);
1658 VECCOPY(bevp->tan, bevp1->tan);
1659 bevp= (BevPoint *)(bl+1);
1662 QUATCOPY(bevp->quat, bevp1->quat);
1663 VECCOPY(bevp->dir, bevp1->dir);
1664 VECCOPY(bevp->tan, bevp1->tan);
1666 /* utility for make_bevel_list_3D_* funcs */
1667 static void bevel_list_calc_bisect(BevList *bl)
1669 BevPoint *bevp2, *bevp1, *bevp0;
1672 bevp2= (BevPoint *)(bl+1);
1673 bevp1= bevp2+(bl->nr-1);
1678 /* totally simple */
1679 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1686 static void bevel_list_flip_tangents(BevList *bl)
1688 BevPoint *bevp2, *bevp1, *bevp0;
1691 bevp2= (BevPoint *)(bl+1);
1692 bevp1= bevp2+(bl->nr-1);
1697 if(RAD2DEG(angle_v2v2(bevp0->tan, bevp1->tan)) > 90)
1698 negate_v3(bevp1->tan);
1705 /* apply user tilt */
1706 static void bevel_list_apply_tilt(BevList *bl)
1708 BevPoint *bevp2, *bevp1, *bevp0;
1712 bevp2= (BevPoint *)(bl+1);
1713 bevp1= bevp2+(bl->nr-1);
1718 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1719 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1720 normalize_qt(bevp1->quat);
1727 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1728 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1730 BevPoint *bevp2, *bevp1, *bevp0;
1734 float bevp0_quat[4];
1737 for(a=0; a < smooth_iter; a++) {
1739 bevp2= (BevPoint *)(bl+1);
1740 bevp1= bevp2+(bl->nr-1);
1745 if(bl->poly== -1) { /* check its not cyclic */
1746 /* skip the first point */
1759 QUATCOPY(bevp0_quat, bevp0->quat);
1762 /* interpolate quats */
1763 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1764 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1767 mul_qt_v3(q, zaxis);
1768 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1769 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1772 QUATCOPY(bevp0_quat, bevp1->quat);
1773 mul_qt_qtqt(q, q2, q);
1774 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1775 normalize_qt(bevp1->quat);
1785 static void make_bevel_list_3D_zup(BevList *bl)
1787 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1790 bevp2= (BevPoint *)(bl+1);
1791 bevp1= bevp2+(bl->nr-1);
1796 /* totally simple */
1797 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1798 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1806 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1808 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1814 bevel_list_calc_bisect(bl);
1816 bevp2= (BevPoint *)(bl+1);
1817 bevp1= bevp2+(bl->nr-1);
1823 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1824 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1827 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1829 if(angle > 0.0f) { /* otherwise we can keep as is */
1830 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1831 axis_angle_to_quat(q, cross_tmp, angle);
1832 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1835 QUATCOPY(bevp1->quat, bevp0->quat);
1844 if(bl->poly != -1) { /* check for cyclic */
1846 /* Need to correct for the start/end points not matching
1847 * do this by calculating the tilt angle difference, then apply
1848 * the rotation gradually over the entire curve
1850 * note that the split is between last and second last, rather then first/last as youd expect.
1852 * real order is like this
1853 * 0,1,2,3,4 --> 1,2,3,4,0
1855 * this is why we compare last with second last
1857 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1859 BevPoint *bevp_first;
1860 BevPoint *bevp_last;
1863 bevp_first= (BevPoint *)(bl+1);
1864 bevp_first+= bl->nr-1;
1865 bevp_last = bevp_first;
1868 /* quats and vec's are normalized, should not need to re-normalize */
1869 mul_qt_v3(bevp_first->quat, vec_1);
1870 mul_qt_v3(bevp_last->quat, vec_2);
1871 normalize_v3(vec_1);
1872 normalize_v3(vec_2);
1874 /* align the vector, can avoid this and it looks 98% OK but
1875 * better to align the angle quat roll's before comparing */
1877 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1878 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1879 axis_angle_to_quat(q, cross_tmp, angle);
1880 mul_qt_v3(q, vec_2);
1883 angle= angle_normalized_v3v3(vec_1, vec_2);
1885 /* flip rotation if needs be */
1886 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1887 normalize_v3(cross_tmp);
1888 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90/(180.0/M_PI))
1891 bevp2= (BevPoint *)(bl+1);
1892 bevp1= bevp2+(bl->nr-1);
1897 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1899 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1900 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1909 static void make_bevel_list_3D_tangent(BevList *bl)
1911 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1914 float bevp0_tan[3], cross_tmp[3];
1916 bevel_list_calc_bisect(bl);
1917 if(bl->poly== -1) /* check its not cyclic */
1918 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1919 bevel_list_flip_tangents(bl);
1921 /* correct the tangents */
1922 bevp2= (BevPoint *)(bl+1);
1923 bevp1= bevp2+(bl->nr-1);
1929 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1930 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1931 normalize_v3(bevp1->tan);
1939 /* now for the real twist calc */
1940 bevp2= (BevPoint *)(bl+1);
1941 bevp1= bevp2+(bl->nr-1);
1944 VECCOPY(bevp0_tan, bevp0->tan);
1949 /* make perpendicular, modify tan in place, is ok */
1951 float zero[3] = {0,0,0};
1953 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1954 normalize_v3(cross_tmp);
1955 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1963 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1965 switch(twist_mode) {
1966 case CU_TWIST_TANGENT:
1967 make_bevel_list_3D_tangent(bl);
1969 case CU_TWIST_MINIMUM:
1970 make_bevel_list_3D_minimum_twist(bl);
1972 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1973 make_bevel_list_3D_zup(bl);
1976 if(bl->poly== -1) /* check its not cyclic */
1977 bevel_list_cyclic_fix_3D(bl);
1980 bevel_list_smooth(bl, smooth_iter);
1982 bevel_list_apply_tilt(bl);
1987 /* only for 2 points */
1988 static void make_bevel_list_segment_3D(BevList *bl)
1992 BevPoint *bevp2= (BevPoint *)(bl+1);
1993 BevPoint *bevp1= bevp2+1;
1995 /* simple quat/dir */
1996 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
1997 normalize_v3(bevp1->dir);
1999 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2001 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2002 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2003 normalize_qt(bevp1->quat);
2004 VECCOPY(bevp2->dir, bevp1->dir);
2005 QUATCOPY(bevp2->quat, bevp1->quat);
2010 void makeBevelList(Object *ob)
2013 - convert all curves to polys, with indication of resol and flags for double-vertices
2014 - possibly; do a smart vertice removal (in case Nurb)
2015 - separate in individual blicks with BoundBox
2016 - AutoHole detection
2020 BezTriple *bezt, *prevbezt;
2022 BevList *bl, *blnew, *blnext;
2023 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2024 float min, inp, x1, x2, y1, y2;
2025 struct bevelsort *sortdata, *sd, *sd1;
2026 int a, b, nr, poly, resolu = 0, len = 0;
2027 int do_tilt, do_radius, do_weight;
2029 /* this function needs an object, because of tflag and upflag */
2032 /* do we need to calculate the radius for each point? */
2033 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2035 /* STEP 1: MAKE POLYS */
2037 BLI_freelistN(&(cu->bev));
2038 if(cu->editnurb && ob->type!=OB_FONT) {
2039 ListBase *nurbs= ED_curve_editnurbs(cu);
2041 } else nu= cu->nurb.first;
2045 /* check if we will calculate tilt data */
2046 do_tilt = CU_DO_TILT(cu, nu);
2047 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2050 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2051 * enforced in the UI but can go wrong possibly */
2052 if(!check_valid_nurb_u(nu)) {
2053 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2054 BLI_addtail(&(cu->bev), bl);
2057 if(G.rendering && cu->resolu_ren!=0)
2058 resolu= cu->resolu_ren;
2062 if(nu->type == CU_POLY) {
2064 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2065 BLI_addtail(&(cu->bev), bl);
2067 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2071 bevp= (BevPoint *)(bl+1);
2075 VECCOPY(bevp->vec, bp->vec);
2076 bevp->alfa= bp->alfa;
2077 bevp->radius= bp->radius;
2078 bevp->weight= bp->weight;
2079 bevp->split_tag= TRUE;
2084 else if(nu->type == CU_BEZIER) {
2086 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2087 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2088 BLI_addtail(&(cu->bev), bl);
2090 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2092 bevp= (BevPoint *)(bl+1);
2096 if(nu->flagu & CU_NURB_CYCLIC) {
2098 prevbezt= nu->bezt+(nu->pntsu-1);
2106 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2108 VECCOPY(bevp->vec, prevbezt->vec[1]);
2109 bevp->alfa= prevbezt->alfa;
2110 bevp->radius= prevbezt->radius;
2111 bevp->weight= prevbezt->weight;
2112 bevp->split_tag= TRUE;
2113 bevp->dupe_tag= FALSE;
2119 /* always do all three, to prevent data hanging around */
2122 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2123 for(j=0; j<3; j++) {
2124 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2125 bezt->vec[0][j], bezt->vec[1][j],
2126 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2129 /* if both arrays are NULL do nothiong */
2130 alfa_bezpart( prevbezt, bezt, nu,
2131 do_tilt ? &bevp->alfa : NULL,
2132 do_radius ? &bevp->radius : NULL,
2133 do_weight ? &bevp->weight : NULL,
2134 resolu, sizeof(BevPoint));
2137 if(cu->twist_mode==CU_TWIST_TANGENT) {
2138 forward_diff_bezier_cotangent(
2139 prevbezt->vec[1], prevbezt->vec[2],
2140 bezt->vec[0], bezt->vec[1],
2141 bevp->tan, resolu, sizeof(BevPoint));
2144 /* indicate with handlecodes double points */
2145 if(prevbezt->h1==prevbezt->h2) {
2146 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2149 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2150 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2159 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2160 VECCOPY(bevp->vec, prevbezt->vec[1]);
2161 bevp->alfa= prevbezt->alfa;
2162 bevp->radius= prevbezt->radius;
2163 bevp->weight= prevbezt->weight;
2167 else if(nu->type == CU_NURBS) {
2169 len= (resolu*SEGMENTSU(nu));
2171 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2172 BLI_addtail(&(cu->bev), bl);
2175 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2177 bevp= (BevPoint *)(bl+1);
2179 makeNurbcurve( nu, &bevp->vec[0],
2180 do_tilt ? &bevp->alfa : NULL,
2181 do_radius ? &bevp->radius : NULL,
2182 do_weight ? &bevp->weight : NULL,
2183 resolu, sizeof(BevPoint));
2190 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2193 if (bl->nr) { /* null bevel items come from single points */
2195 bevp1= (BevPoint *)(bl+1);
2196 bevp0= bevp1+(nr-1);
2199 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2200 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2201 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2202 bevp0->dupe_tag= TRUE;
2216 if(bl->nr && bl->dupe_nr) {
2217 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2218 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2219 memcpy(blnew, bl, sizeof(BevList));
2221 BLI_remlink(&(cu->bev), bl);
2222 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2223 bevp0= (BevPoint *)(bl+1);
2224 bevp1= (BevPoint *)(blnew+1);
2227 if(bevp0->dupe_tag==0) {
2228 memcpy(bevp1, bevp0, sizeof(BevPoint));
2240 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2244 if(bl->nr && bl->poly>=0) {
2253 /* find extreme left points, also test (turning) direction */
2255 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2261 bevp= (BevPoint *)(bl+1);
2264 if(min>bevp->vec[0]) {
2273 bevp= (BevPoint *)(bl+1);
2274 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2275 else bevp0= bevp1-1;
2276 bevp= bevp+ (bl->nr-1);
2277 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2278 else bevp2= bevp1+1;
2280 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]);
2282 if(inp>0.0) sd->dir= 1;
2290 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2293 for(a=1; a<poly; a++, sd++) {
2294 bl= sd->bl; /* is bl a hole? */
2295 sd1= sortdata+ (a-1);
2296 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2297 if(bevelinside(sd1->bl, bl)) {
2298 bl->hole= 1- sd1->bl->hole;
2304 /* turning direction */
2305 if((cu->flag & CU_3D)==0) {
2307 for(a=0; a<poly; a++, sd++) {
2308 if(sd->bl->hole==sd->dir) {
2310 bevp1= (BevPoint *)(bl+1);
2311 bevp2= bevp1+ (bl->nr-1);
2314 SWAP(BevPoint, *bevp1, *bevp2);
2321 MEM_freeN(sortdata);
2324 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2325 if((cu->flag & CU_3D)==0) {
2326 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2327 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2334 else if(bl->nr==2) { /* 2 pnt, treat separate */
2335 bevp2= (BevPoint *)(bl+1);
2338 x1= bevp1->vec[0]- bevp2->vec[0];
2339 y1= bevp1->vec[1]- bevp2->vec[1];
2341 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2342 bevp2->sina= bevp1->sina;
2343 bevp2->cosa= bevp1->cosa;
2345 /* fill in dir & quat */
2346 make_bevel_list_segment_3D(bl);
2349 bevp2= (BevPoint *)(bl+1);
2350 bevp1= bevp2+(bl->nr-1);
2355 x1= bevp1->vec[0]- bevp0->vec[0];
2356 x2= bevp1->vec[0]- bevp2->vec[0];
2357 y1= bevp1->vec[1]- bevp0->vec[1];
2358 y2= bevp1->vec[1]- bevp2->vec[1];
2360 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2362 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2363 * no need for tricky tilt calculation as with 3D curves */
2364 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2365 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2366 /* done with inline make_bevel_list_3D_zup */
2373 /* correct non-cyclic cases */
2375 bevp= (BevPoint *)(bl+1);
2377 bevp->sina= bevp1->sina;
2378 bevp->cosa= bevp1->cosa;
2379 bevp= (BevPoint *)(bl+1);
2382 bevp->sina= bevp1->sina;
2383 bevp->cosa= bevp1->cosa;
2385 /* correct for the dir/quat, see above why its needed */
2386 bevel_list_cyclic_fix_3D(bl);
2392 else { /* 3D Curves */
2399 else if(bl->nr==2) { /* 2 pnt, treat separate */
2400 make_bevel_list_segment_3D(bl);
2403 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2410 /* ****************** HANDLES ************** */
2414 * 0: nothing, 1:auto, 2:vector, 3:aligned
2417 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2418 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2420 float *p1,*p2,*p3, pt[3];
2421 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2423 if(bezt->h1==0 && bezt->h2==0) return;
2429 pt[0]= 2*p2[0]- p3[0];
2430 pt[1]= 2*p2[1]- p3[1];
2431 pt[2]= 2*p2[2]- p3[2];
2434 else p1= prev->vec[1];
2437 pt[0]= 2*p2[0]- p1[0];
2438 pt[1]= 2*p2[1]- p1[1];
2439 pt[2]= 2*p2[2]- p1[2];
2442 else p3= next->vec[1];
2449 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2456 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2458 if(len1==0.0f) len1=1.0f;
2459 if(len2==0.0f) len2=1.0f;
2462 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2463 vx= dx1/len2 + dx/len1;
2464 vy= dy1/len2 + dy/len1;
2465 vz= dz1/len2 + dz/len1;
2466 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2468 int leftviolate=0, rightviolate=0; /* for mode==2 */
2470 if(len1>5.0f*len2) len1= 5.0f*len2;
2471 if(len2>5.0f*len1) len2= 5.0f*len1;
2473 if(bezt->h1==HD_AUTO) {
2475 *(p2-3)= *p2-vx*len1;
2476 *(p2-2)= *(p2+1)-vy*len1;
2477 *(p2-1)= *(p2+2)-vz*len1;
2479 if(mode==2 && next && prev) { // keep horizontal if extrema
2480 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2481 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2482 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2483 bezt->vec[0][1]= bezt->vec[1][1];
2485 else { // handles should not be beyond y coord of two others
2487 if(prev->vec[1][1] > bezt->vec[0][1]) {
2488 bezt->vec[0][1]= prev->vec[1][1];
2493 if(prev->vec[1][1] < bezt->vec[0][1]) {
2494 bezt->vec[0][1]= prev->vec[1][1];
2501 if(bezt->h2==HD_AUTO) {
2503 *(p2+3)= *p2+vx*len2;
2504 *(p2+4)= *(p2+1)+vy*len2;
2505 *(p2+5)= *(p2+2)+vz*len2;
2507 if(mode==2 && next && prev) { // keep horizontal if extrema
2508 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2509 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2510 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2511 bezt->vec[2][1]= bezt->vec[1][1];
2513 else { // handles should not be beyond y coord of two others
2515 if(next->vec[1][1] < bezt->vec[2][1]) {
2516 bezt->vec[2][1]= next->vec[1][1];
2521 if(next->vec[1][1] > bezt->vec[2][1]) {
2522 bezt->vec[2][1]= next->vec[1][1];
2529 if(leftviolate || rightviolate) { /* align left handle */
2532 sub_v3_v3v3(h1, p2-3, p2);
2533 sub_v3_v3v3(h2, p2, p2+3);
2534 len1= normalize_v3(h1);
2535 len2= normalize_v3(h2);
2540 *(p2+3)= *(p2) - vz*len2*h1[0];
2541 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2542 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2545 *(p2-3)= *(p2) + vz*len1*h2[0];
2546 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2547 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2554 if(bezt->h1==HD_VECT) { /* vector */
2559 *(p2-2)= *(p2+1)-dy;
2560 *(p2-1)= *(p2+2)-dz;
2562 if(bezt->h2==HD_VECT) {
2567 *(p2+4)= *(p2+1)+dy1;
2568 *(p2+5)= *(p2+2)+dz1;
2571 len2= len_v3v3(p2, p2+3);
2572 len1= len_v3v3(p2, p2-3);
2573 if(len1==0.0) len1=1.0;
2574 if(len2==0.0) len2=1.0;
2576 if(bezt->f1 & SELECT) { /* order of calculation */
2577 if(bezt->h2==HD_ALIGN) { /* aligned */
2579 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2580 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2581 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2583 if(bezt->h1==HD_ALIGN) {
2585 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2586 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2587 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2591 if(bezt->h1==HD_ALIGN) {
2593 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2594 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2595 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2597 if(bezt->h2==HD_ALIGN) { /* aligned */
2599 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2600 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2601 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2606 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2608 BezTriple *bezt, *prev, *next;
2611 if(nu->type != CU_BEZIER) return;
2612 if(nu->pntsu<2) return;
2616 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2621 calchandleNurb(bezt, prev, next, 0);
2624 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2634 void testhandlesNurb(Nurb *nu)
2636 /* use when something has changed with handles.
2637 it treats all BezTriples with the following rules:
2638 PHASE 1: do types have to be altered?
2639 Auto handles: become aligned when selection status is NOT(000 || 111)
2640 Vector handles: become 'nothing' when (one half selected AND other not)
2641 PHASE 2: recalculate handles
2646 if(nu->type != CU_BEZIER) return;
2652 if(bezt->f1 & SELECT) flag++;
2653 if(bezt->f2 & SELECT) flag += 2;
2654 if(bezt->f3 & SELECT) flag += 4;
2656 if( !(flag==0 || flag==7) ) {
2657 if(bezt->h1==HD_AUTO) { /* auto */
2660 if(bezt->h2==HD_AUTO) { /* auto */
2664 if(bezt->h1==HD_VECT) { /* vector */
2665 if(flag < 4) bezt->h1= 0;
2667 if(bezt->h2==HD_VECT) { /* vector */
2668 if( flag > 3) bezt->h2= 0;
2674 calchandlesNurb(nu);
2677 void autocalchandlesNurb(Nurb *nu, int flag)
2679 /* checks handle coordinates and calculates type */
2681 BezTriple *bezt2, *bezt1, *bezt0;
2682 int i, align, leftsmall, rightsmall;
2684 if(nu==0 || nu->bezt==0) return;
2687 bezt1 = bezt2 + (nu->pntsu-1);
2693 align= leftsmall= rightsmall= 0;
2696 if(flag==0 || (bezt1->f1 & flag) ) {
2698 /* distance too short: vectorhandle */
2699 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2704 /* aligned handle? */
2705 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2707 bezt1->h1= HD_ALIGN;
2709 /* or vector handle? */
2710 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2716 if(flag==0 || (bezt1->f3 & flag) ) {
2718 /* distance too short: vectorhandle */
2719 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2724 /* aligned handle? */
2725 if(align) bezt1->h2= HD_ALIGN;
2727 /* or vector handle? */
2728 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2733 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2734 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2736 /* undesired combination: */
2737 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2738 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2745 calchandlesNurb(nu);
2748 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2752 nu= editnurb->first;
2754 autocalchandlesNurb(nu, flag);
2759 void sethandlesNurb(ListBase *editnurb, short code)
2761 /* code==1: set autohandle */
2762 /* code==2: set vectorhandle */
2763 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2764 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2765 /* code==5: Set align, like 3 but no toggle */
2766 /* code==6: Clear align, like 3 but no toggle */
2771 if(code==1 || code==2) {
2772 nu= editnurb->first;
2774 if(nu->type == CU_BEZIER) {
2778 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2779 if(bezt->f1 & SELECT) bezt->h1= code;
2780 if(bezt->f3 & SELECT) bezt->h2= code;
2781 if(bezt->h1!=bezt->h2) {
2782 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2783 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2788 calchandlesNurb(nu);
2794 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2796 nu= editnurb->first;
2799 } else if (code == 6) {
2804 if(nu->type == CU_BEZIER) {
2808 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2809 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2819 nu= editnurb->first;
2821 if(nu->type == CU_BEZIER) {
2825 if(bezt->f1 & SELECT) bezt->h1= ok;
2826 if(bezt->f3 & SELECT) bezt->h2= ok;
2830 calchandlesNurb(nu);
2837 static void swapdata(void *adr1, void *adr2, int len)
2845 memcpy(adr, adr1, len);
2846 memcpy(adr1, adr2, len);
2847 memcpy(adr2, adr, len);
2852 adr= (char *)MEM_mallocN(len, "curve swap");
2853 memcpy(adr, adr1, len);
2854 memcpy(adr1, adr2, len);
2855 memcpy(adr2, adr, len);
2860 void switchdirectionNurb(Nurb *nu)
2862 BezTriple *bezt1, *bezt2;
2864 float *fp1, *fp2, *tempf;
2867 if(nu->pntsu==1 && nu->pntsv==1) return;
2869 if(nu->type == CU_BEZIER) {
2873 if(a & 1) a+= 1; /* if odd, also swap middle content */
2876 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2878 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2879 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2881 SWAP(char, bezt1->h1, bezt1->h2);
2882 SWAP(short, bezt1->f1, bezt1->f3);
2885 SWAP(char, bezt2->h1, bezt2->h2);
2886 SWAP(short, bezt2->f1, bezt2->f3);
2887 bezt1->alfa= -bezt1->alfa;
2888 bezt2->alfa= -bezt2->alfa;
2895 else if(nu->pntsv==1) {
2900 while(bp1!=bp2 && a>0) {
2901 SWAP(BPoint, *bp1, *bp2);
2903 bp1->alfa= -bp1->alfa;
2904 bp2->alfa= -bp2->alfa;
2908 if(nu->type == CU_NURBS) {
2914 while(fp1!=fp2 && a>0) {
2915 SWAP(float, *fp1, *fp2);
2920 /* and make in increasing order again */
2923 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2925 fp2[0]= fabs(fp1[1]-fp1[0]);
2936 fp1[0]= fp1[-1]+fp2[0];
2945 for(b=0; b<nu->pntsv; b++) {
2947 bp1= nu->bp+b*nu->pntsu;
2952 while(bp1!=bp2 && a>0) {
2953 SWAP(BPoint, *bp1, *bp2);