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 strcpy(cu->str, "Text");
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)
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, resolu, resolv;
751 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
753 int totu = nu->pntsu*nu->resolu, totv = nu->pntsv*nu->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+resolv, jend+resolv);
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);
830 jsta= jstart[resolv];
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 Curve *cu= ob->data;
1063 float *fp, *coord_array;
1065 /* first calculate the size of the datablock */
1068 /* as we want to avoid the seam in a cyclic nurbs
1069 texture wrapping, reserve extra orco data space to save these extra needed
1070 vertex based UV coordinates for the meridian vertices.
1071 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1072 the renderface/vert construction.
1074 See also convertblender.c: init_render_surf()
1077 sizeu = nu->pntsu*nu->resolu;
1078 sizev = nu->pntsv*nu->resolv;
1079 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1080 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1081 if(nu->pntsv>1) tot+= sizeu * sizev;
1085 /* makeNurbfaces wants zeros */
1086 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1091 sizeu = nu->pntsu*nu->resolu;
1092 sizev = nu->pntsv*nu->resolv;
1093 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1094 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1096 if(cu->flag & CU_UV_ORCO) {
1097 for(b=0; b< sizeu; b++) {
1098 for(a=0; a< sizev; a++) {
1100 if(sizev <2) fp[0]= 0.0f;
1101 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1103 if(sizeu <2) fp[1]= 0.0f;
1104 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1113 float *_tdata= MEM_callocN((nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv) *3*sizeof(float), "temp data");
1114 float *tdata= _tdata;
1116 makeNurbfaces(nu, tdata, 0);
1118 for(b=0; b<sizeu; b++) {
1120 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1123 for(a=0; a<sizev; a++) {
1125 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1128 tdata = _tdata + 3 * (use_b * (nu->pntsv*nu->resolv) + use_a);
1130 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1131 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1132 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1147 /* NOTE: This routine is tied to the order of vertex
1148 * built by displist and as passed to the renderer.
1150 float *make_orco_curve(Scene *scene, Object *ob)
1152 Curve *cu = ob->data;
1155 float *fp, *coord_array;
1156 ListBase disp = {NULL, NULL};
1158 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1161 for (dl=disp.first; dl; dl=dl->next) {
1162 if (dl->type==DL_INDEX3) {
1164 } else if (dl->type==DL_SURF) {
1165 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1166 if (dl->flag & DL_CYCL_U) {
1167 if (dl->flag & DL_CYCL_V)
1168 numVerts += (dl->parts+1)*(dl->nr+1);
1170 numVerts += dl->parts*(dl->nr+1);
1173 numVerts += dl->parts*dl->nr;
1177 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1178 for (dl=disp.first; dl; dl=dl->next) {
1179 if (dl->type==DL_INDEX3) {
1180 for (u=0; u<dl->nr; u++, fp+=3) {
1181 if (cu->flag & CU_UV_ORCO) {
1182 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1186 VECCOPY(fp, &dl->verts[u*3]);
1188 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1189 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1190 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1193 } else if (dl->type==DL_SURF) {
1194 int sizeu= dl->nr, sizev= dl->parts;
1196 /* exception as handled in convertblender.c too */
1197 if (dl->flag & DL_CYCL_U) {
1199 if (dl->flag & DL_CYCL_V)
1203 for (u=0; u<sizev; u++) {
1204 for (v=0; v<sizeu; v++,fp+=3) {
1205 if (cu->flag & CU_UV_ORCO) {
1206 fp[0]= 2.0f*u/(dl->parts-1) - 1.0f;
1207 fp[1]= 2.0f*v/(dl->nr-1) - 1.0f;
1211 int realv= v % dl->nr;
1212 int realu= u % dl->parts;
1214 vert= dl->verts + 3*(dl->nr*realu + realv);
1217 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1218 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1219 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1226 freedisplist(&disp);
1232 /* ***************** BEVEL ****************** */
1234 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1236 DispList *dl, *dlnew;
1238 float *fp, facx, facy, angle, dangle;
1242 disp->first = disp->last = NULL;
1244 /* if a font object is being edited, then do nothing */
1245 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1248 if (cu->bevobj->type!=OB_CURVE) return;
1250 bevcu= cu->bevobj->data;
1251 if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1252 ListBase bevdisp= {NULL, NULL};
1253 facx= cu->bevobj->size[0];
1254 facy= cu->bevobj->size[1];
1257 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1260 dl= bevcu->disp.first;
1262 makeDispListCurveTypes(scene, cu->bevobj, 0);
1263 dl= bevcu->disp.first;
1268 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1269 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1271 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1272 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1274 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1276 BLI_addtail(disp, dlnew);
1278 nr= dlnew->parts*dlnew->nr;
1289 freedisplist(&bevdisp);
1292 else if(cu->ext1==0.0 && cu->ext2==0.0) {
1295 else if(cu->ext2==0.0) {
1296 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1297 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1298 BLI_addtail(disp, dl);
1301 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1310 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1312 nr= 4+ 2*cu->bevresol;
1314 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1315 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1316 BLI_addtail(disp, dl);
1319 dl->flag= DL_BACK_CURVE;
1324 dangle= (2.0f*M_PI/(nr));
1325 angle= -(nr-1)*dangle;
1327 for(a=0; a<nr; a++) {
1329 fp[1]= (float)(cos(angle)*(cu->ext2));
1330 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1338 /* bevel now in three parts, for proper vertex normals */
1341 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1342 dnr= nr= 2+ cu->bevresol;
1343 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1344 nr= 3+ 2*cu->bevresol;
1346 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1347 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1348 BLI_addtail(disp, dl);
1351 dl->flag= DL_BACK_CURVE;
1356 dangle= (0.5*M_PI/(dnr-1));
1357 angle= -(nr-1)*dangle;
1359 for(a=0; a<nr; a++) {
1361 fp[1]= (float)(cos(angle)*(cu->ext2));
1362 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1368 /* part 2, sidefaces */
1372 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1373 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1374 BLI_addtail(disp, dl);
1385 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1386 dl= MEM_dupallocN(dl);
1387 dl->verts= MEM_dupallocN(dl->verts);
1388 BLI_addtail(disp, dl);
1399 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1400 dnr= nr= 2+ cu->bevresol;
1401 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1402 nr= 3+ 2*cu->bevresol;
1404 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1405 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1406 BLI_addtail(disp, dl);
1408 dl->flag= DL_FRONT_CURVE;
1415 dangle= (0.5*M_PI/(dnr-1));
1417 for(a=0; a<nr; a++) {
1419 fp[1]= (float)(cos(angle)*(cu->ext2));
1420 fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1428 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1432 0: no intersection of segments
1433 1: exact intersection of segments
1434 2: cross-intersection of segments
1438 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1439 if(deler==0.0) return -1;
1441 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1442 *labda= -(*labda/deler);
1444 deler= v3[coy]-v4[coy];
1446 deler=v3[cox]-v4[cox];
1447 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1449 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1451 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1452 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1454 if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1455 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1462 static short bevelinside(BevList *bl1,BevList *bl2)
1464 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1465 /* returns '1' if correct hole */
1466 BevPoint *bevp, *prevbevp;
1467 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1468 int nr, links=0,rechts=0,mode;
1470 /* take first vertex of possible hole */
1472 bevp= (BevPoint *)(bl2+1);
1473 hvec1[0]= bevp->vec[0];
1474 hvec1[1]= bevp->vec[1];
1476 VECCOPY(hvec2,hvec1);
1479 /* test it with all edges of potential surounding poly */
1480 /* count number of transitions left-right */
1482 bevp= (BevPoint *)(bl1+1);
1484 prevbevp= bevp+(nr-1);
1487 min= prevbevp->vec[1];
1491 max= prevbevp->vec[1];
1494 if(min<=hvec1[1] && max>=hvec1[1]) {
1495 /* there's a transition, calc intersection point */
1496 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1497 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1498 only allow for one situation: we choose lab= 1.0
1500 if(mode>=0 && lab!=0.0) {
1501 if(vec[0]<hvec1[0]) links++;
1510 if( (links & 1) && (rechts & 1) ) return 1;
1521 static int vergxcobev(const void *a1, const void *a2)
1523 const struct bevelsort *x1=a1,*x2=a2;
1525 if( x1->left > x2->left ) return 1;
1526 else if( x1->left < x2->left) return -1;
1530 /* this function cannot be replaced with atan2, but why? */
1532 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1534 float t01, t02, x3, y3;
1536 t01= (float)sqrt(x1*x1+y1*y1);
1537 t02= (float)sqrt(x2*x2+y2*y2);
1538 if(t01==0.0) t01= 1.0;
1539 if(t02==0.0) t02= 1.0;
1547 if(fabs(t02)>=1.0) t02= .5*M_PI;
1548 else t02= (saacos(t02))/2.0f;
1550 t02= (float)sin(t02);
1551 if(t02==0.0) t02= 1.0;
1555 if(x3==0 && y3==0) {
1559 t01= (float)sqrt(x3*x3+y3*y3);
1569 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1571 BezTriple *pprev, *next, *last;
1572 float fac, dfac, t[4];
1575 if(tilt_array==NULL && radius_array==NULL)
1578 last= nu->bezt+(nu->pntsu-1);
1580 /* returns a point */
1581 if(prevbezt==nu->bezt) {
1582 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1583 else pprev= prevbezt;
1585 else pprev= prevbezt-1;
1589 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1595 dfac= 1.0f/(float)resolu;
1597 for(a=0; a<resolu; a++, fac+= dfac) {
1599 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1600 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1602 key_curve_position_weights(fac, t, nu->tilt_interp);
1603 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1606 tilt_array = (float *)(((char *)tilt_array) + stride);
1610 if (nu->radius_interp==KEY_CU_EASE) {
1611 /* Support 2.47 ease interp
1612 * Note! - this only takes the 2 points into account,
1613 * giving much more localized results to changes in radius, sometimes you want that */
1614 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1617 /* reuse interpolation from tilt if we can */
1618 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1619 key_curve_position_weights(fac, t, nu->radius_interp);
1621 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1624 radius_array = (float *)(((char *)radius_array) + stride);
1628 /* basic interpolation for now, could copy tilt interp too */
1629 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1631 weight_array = (float *)(((char *)weight_array) + stride);
1636 /* make_bevel_list_3D_* funcs, at a minimum these must
1637 * fill in the bezp->quat and bezp->dir values */
1639 /* correct non-cyclic cases by copying direction and rotation
1640 * values onto the first & last end-points */
1641 static void bevel_list_cyclic_fix_3D(BevList *bl)
1643 BevPoint *bevp, *bevp1;
1645 bevp= (BevPoint *)(bl+1);
1647 QUATCOPY(bevp->quat, bevp1->quat);
1648 VECCOPY(bevp->dir, bevp1->dir);
1649 VECCOPY(bevp->tan, bevp1->tan);
1650 bevp= (BevPoint *)(bl+1);
1653 QUATCOPY(bevp->quat, bevp1->quat);
1654 VECCOPY(bevp->dir, bevp1->dir);
1655 VECCOPY(bevp->tan, bevp1->tan);
1657 /* utility for make_bevel_list_3D_* funcs */
1658 static void bevel_list_calc_bisect(BevList *bl)
1660 BevPoint *bevp2, *bevp1, *bevp0;
1663 bevp2= (BevPoint *)(bl+1);
1664 bevp1= bevp2+(bl->nr-1);
1669 /* totally simple */
1670 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1677 static void bevel_list_flip_tangents(BevList *bl)
1679 BevPoint *bevp2, *bevp1, *bevp0;
1682 bevp2= (BevPoint *)(bl+1);
1683 bevp1= bevp2+(bl->nr-1);
1688 if(RAD2DEG(angle_v2v2(bevp0->tan, bevp1->tan)) > 90)
1689 negate_v3(bevp1->tan);
1696 /* apply user tilt */
1697 static void bevel_list_apply_tilt(BevList *bl)
1699 BevPoint *bevp2, *bevp1, *bevp0;
1703 bevp2= (BevPoint *)(bl+1);
1704 bevp1= bevp2+(bl->nr-1);
1709 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1710 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1711 normalize_qt(bevp1->quat);
1718 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1719 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1721 BevPoint *bevp2, *bevp1, *bevp0;
1725 float bevp0_quat[4];
1728 for(a=0; a < smooth_iter; a++) {
1730 bevp2= (BevPoint *)(bl+1);
1731 bevp1= bevp2+(bl->nr-1);
1736 if(bl->poly== -1) { /* check its not cyclic */
1737 /* skip the first point */
1750 QUATCOPY(bevp0_quat, bevp0->quat);
1753 /* interpolate quats */
1754 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1755 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1758 mul_qt_v3(q, zaxis);
1759 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1760 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1763 QUATCOPY(bevp0_quat, bevp1->quat);
1764 mul_qt_qtqt(q, q2, q);
1765 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1766 normalize_qt(bevp1->quat);
1776 static void make_bevel_list_3D_zup(BevList *bl)
1778 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1781 bevp2= (BevPoint *)(bl+1);
1782 bevp1= bevp2+(bl->nr-1);
1787 /* totally simple */
1788 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1789 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1797 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1799 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1805 bevel_list_calc_bisect(bl);
1807 bevp2= (BevPoint *)(bl+1);
1808 bevp1= bevp2+(bl->nr-1);
1814 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1815 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1818 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1820 if(angle > 0.0f) { /* otherwise we can keep as is */
1821 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1822 axis_angle_to_quat(q, cross_tmp, angle);
1823 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1826 QUATCOPY(bevp1->quat, bevp0->quat);
1835 if(bl->poly != -1) { /* check for cyclic */
1837 /* Need to correct for the start/end points not matching
1838 * do this by calculating the tilt angle difference, then apply
1839 * the rotation gradually over the entire curve
1841 * note that the split is between last and second last, rather then first/last as youd expect.
1843 * real order is like this
1844 * 0,1,2,3,4 --> 1,2,3,4,0
1846 * this is why we compare last with second last
1848 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1850 BevPoint *bevp_first;
1851 BevPoint *bevp_last;
1854 bevp_first= (BevPoint *)(bl+1);
1855 bevp_first+= bl->nr-1;
1856 bevp_last = bevp_first;
1859 /* quats and vec's are normalized, should not need to re-normalize */
1860 mul_qt_v3(bevp_first->quat, vec_1);
1861 mul_qt_v3(bevp_last->quat, vec_2);
1862 normalize_v3(vec_1);
1863 normalize_v3(vec_2);
1865 /* align the vector, can avoid this and it looks 98% OK but
1866 * better to align the angle quat roll's before comparing */
1868 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1869 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1870 axis_angle_to_quat(q, cross_tmp, angle);
1871 mul_qt_v3(q, vec_2);
1874 angle= angle_normalized_v3v3(vec_1, vec_2);
1876 /* flip rotation if needs be */
1877 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1878 normalize_v3(cross_tmp);
1879 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90/(180.0/M_PI))
1882 bevp2= (BevPoint *)(bl+1);
1883 bevp1= bevp2+(bl->nr-1);
1888 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1890 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1891 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1900 static void make_bevel_list_3D_tangent(BevList *bl)
1902 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1905 float bevp0_tan[3], cross_tmp[3];
1907 bevel_list_calc_bisect(bl);
1908 if(bl->poly== -1) /* check its not cyclic */
1909 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1910 bevel_list_flip_tangents(bl);
1912 /* correct the tangents */
1913 bevp2= (BevPoint *)(bl+1);
1914 bevp1= bevp2+(bl->nr-1);
1920 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1921 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1922 normalize_v3(bevp1->tan);
1930 /* now for the real twist calc */
1931 bevp2= (BevPoint *)(bl+1);
1932 bevp1= bevp2+(bl->nr-1);
1935 VECCOPY(bevp0_tan, bevp0->tan);
1940 /* make perpendicular, modify tan in place, is ok */
1942 float zero[3] = {0,0,0};
1944 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1945 normalize_v3(cross_tmp);
1946 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1954 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1956 switch(twist_mode) {
1957 case CU_TWIST_TANGENT:
1958 make_bevel_list_3D_tangent(bl);
1960 case CU_TWIST_MINIMUM:
1961 make_bevel_list_3D_minimum_twist(bl);
1963 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1964 make_bevel_list_3D_zup(bl);
1967 if(bl->poly== -1) /* check its not cyclic */
1968 bevel_list_cyclic_fix_3D(bl);
1971 bevel_list_smooth(bl, smooth_iter);
1973 bevel_list_apply_tilt(bl);
1978 /* only for 2 points */
1979 static void make_bevel_list_segment_3D(BevList *bl)
1983 BevPoint *bevp2= (BevPoint *)(bl+1);
1984 BevPoint *bevp1= bevp2+1;
1986 /* simple quat/dir */
1987 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
1988 normalize_v3(bevp1->dir);
1990 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1992 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1993 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1994 normalize_qt(bevp1->quat);
1995 VECCOPY(bevp2->dir, bevp1->dir);
1996 QUATCOPY(bevp2->quat, bevp1->quat);
2001 void makeBevelList(Object *ob)
2004 - convert all curves to polys, with indication of resol and flags for double-vertices
2005 - possibly; do a smart vertice removal (in case Nurb)
2006 - separate in individual blicks with BoundBox
2007 - AutoHole detection
2011 BezTriple *bezt, *prevbezt;
2013 BevList *bl, *blnew, *blnext;
2014 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2015 float min, inp, x1, x2, y1, y2;
2016 struct bevelsort *sortdata, *sd, *sd1;
2017 int a, b, nr, poly, resolu = 0, len = 0;
2018 int do_tilt, do_radius, do_weight;
2020 /* this function needs an object, because of tflag and upflag */
2023 /* do we need to calculate the radius for each point? */
2024 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2026 /* STEP 1: MAKE POLYS */
2028 BLI_freelistN(&(cu->bev));
2029 if(cu->editnurb && ob->type!=OB_FONT) {
2030 ListBase *nurbs= ED_curve_editnurbs(cu);
2032 } else nu= cu->nurb.first;
2036 /* check if we will calculate tilt data */
2037 do_tilt = CU_DO_TILT(cu, nu);
2038 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2041 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2042 * enforced in the UI but can go wrong possibly */
2043 if(!check_valid_nurb_u(nu)) {
2044 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2045 BLI_addtail(&(cu->bev), bl);
2048 if(G.rendering && cu->resolu_ren!=0)
2049 resolu= cu->resolu_ren;
2053 if(nu->type == CU_POLY) {
2055 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2056 BLI_addtail(&(cu->bev), bl);
2058 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2062 bevp= (BevPoint *)(bl+1);
2066 VECCOPY(bevp->vec, bp->vec);
2067 bevp->alfa= bp->alfa;
2068 bevp->radius= bp->radius;
2069 bevp->weight= bp->weight;
2070 bevp->split_tag= TRUE;
2075 else if(nu->type == CU_BEZIER) {
2077 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2078 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2079 BLI_addtail(&(cu->bev), bl);
2081 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2083 bevp= (BevPoint *)(bl+1);
2087 if(nu->flagu & CU_NURB_CYCLIC) {
2089 prevbezt= nu->bezt+(nu->pntsu-1);
2097 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2099 VECCOPY(bevp->vec, prevbezt->vec[1]);
2100 bevp->alfa= prevbezt->alfa;
2101 bevp->radius= prevbezt->radius;
2102 bevp->weight= prevbezt->weight;
2103 bevp->split_tag= TRUE;
2104 bevp->dupe_tag= FALSE;
2110 /* always do all three, to prevent data hanging around */
2113 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2114 for(j=0; j<3; j++) {
2115 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2116 bezt->vec[0][j], bezt->vec[1][j],
2117 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2120 /* if both arrays are NULL do nothiong */
2121 alfa_bezpart( prevbezt, bezt, nu,
2122 do_tilt ? &bevp->alfa : NULL,
2123 do_radius ? &bevp->radius : NULL,
2124 do_weight ? &bevp->weight : NULL,
2125 resolu, sizeof(BevPoint));
2128 if(cu->twist_mode==CU_TWIST_TANGENT) {
2129 forward_diff_bezier_cotangent(
2130 prevbezt->vec[1], prevbezt->vec[2],
2131 bezt->vec[0], bezt->vec[1],
2132 bevp->tan, resolu, sizeof(BevPoint));
2135 /* indicate with handlecodes double points */
2136 if(prevbezt->h1==prevbezt->h2) {
2137 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2140 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2141 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2150 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2151 VECCOPY(bevp->vec, prevbezt->vec[1]);
2152 bevp->alfa= prevbezt->alfa;
2153 bevp->radius= prevbezt->radius;
2154 bevp->weight= prevbezt->weight;
2158 else if(nu->type == CU_NURBS) {
2160 len= (resolu*SEGMENTSU(nu));
2162 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2163 BLI_addtail(&(cu->bev), bl);
2166 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2168 bevp= (BevPoint *)(bl+1);
2170 makeNurbcurve( nu, &bevp->vec[0],
2171 do_tilt ? &bevp->alfa : NULL,
2172 do_radius ? &bevp->radius : NULL,
2173 do_weight ? &bevp->weight : NULL,
2174 resolu, sizeof(BevPoint));
2181 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2184 if (bl->nr) { /* null bevel items come from single points */
2186 bevp1= (BevPoint *)(bl+1);
2187 bevp0= bevp1+(nr-1);
2190 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2191 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2192 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2193 bevp0->dupe_tag= TRUE;
2207 if(bl->nr && bl->dupe_nr) {
2208 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2209 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2210 memcpy(blnew, bl, sizeof(BevList));
2212 BLI_remlink(&(cu->bev), bl);
2213 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2214 bevp0= (BevPoint *)(bl+1);
2215 bevp1= (BevPoint *)(blnew+1);
2218 if(bevp0->dupe_tag==0) {
2219 memcpy(bevp1, bevp0, sizeof(BevPoint));
2231 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2235 if(bl->nr && bl->poly>=0) {
2244 /* find extreme left points, also test (turning) direction */
2246 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2252 bevp= (BevPoint *)(bl+1);
2255 if(min>bevp->vec[0]) {
2264 bevp= (BevPoint *)(bl+1);
2265 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2266 else bevp0= bevp1-1;
2267 bevp= bevp+ (bl->nr-1);
2268 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2269 else bevp2= bevp1+1;
2271 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]);
2273 if(inp>0.0) sd->dir= 1;
2281 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2284 for(a=1; a<poly; a++, sd++) {
2285 bl= sd->bl; /* is bl a hole? */
2286 sd1= sortdata+ (a-1);
2287 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2288 if(bevelinside(sd1->bl, bl)) {
2289 bl->hole= 1- sd1->bl->hole;
2295 /* turning direction */
2296 if((cu->flag & CU_3D)==0) {
2298 for(a=0; a<poly; a++, sd++) {
2299 if(sd->bl->hole==sd->dir) {
2301 bevp1= (BevPoint *)(bl+1);
2302 bevp2= bevp1+ (bl->nr-1);
2305 SWAP(BevPoint, *bevp1, *bevp2);
2312 MEM_freeN(sortdata);
2315 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2316 if((cu->flag & CU_3D)==0) {
2317 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2318 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2325 else if(bl->nr==2) { /* 2 pnt, treat separate */
2326 bevp2= (BevPoint *)(bl+1);
2329 x1= bevp1->vec[0]- bevp2->vec[0];
2330 y1= bevp1->vec[1]- bevp2->vec[1];
2332 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2333 bevp2->sina= bevp1->sina;
2334 bevp2->cosa= bevp1->cosa;
2336 /* fill in dir & quat */
2337 make_bevel_list_segment_3D(bl);
2340 bevp2= (BevPoint *)(bl+1);
2341 bevp1= bevp2+(bl->nr-1);
2346 x1= bevp1->vec[0]- bevp0->vec[0];
2347 x2= bevp1->vec[0]- bevp2->vec[0];
2348 y1= bevp1->vec[1]- bevp0->vec[1];
2349 y2= bevp1->vec[1]- bevp2->vec[1];
2351 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2353 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2354 * no need for tricky tilt calculation as with 3D curves */
2355 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2356 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2357 /* done with inline make_bevel_list_3D_zup */
2364 /* correct non-cyclic cases */
2366 bevp= (BevPoint *)(bl+1);
2368 bevp->sina= bevp1->sina;
2369 bevp->cosa= bevp1->cosa;
2370 bevp= (BevPoint *)(bl+1);
2373 bevp->sina= bevp1->sina;
2374 bevp->cosa= bevp1->cosa;
2376 /* correct for the dir/quat, see above why its needed */
2377 bevel_list_cyclic_fix_3D(bl);
2383 else { /* 3D Curves */
2390 else if(bl->nr==2) { /* 2 pnt, treat separate */
2391 make_bevel_list_segment_3D(bl);
2394 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2401 /* ****************** HANDLES ************** */
2405 * 0: nothing, 1:auto, 2:vector, 3:aligned
2408 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2409 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2411 float *p1,*p2,*p3, pt[3];
2412 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2414 if(bezt->h1==0 && bezt->h2==0) return;
2420 pt[0]= 2*p2[0]- p3[0];
2421 pt[1]= 2*p2[1]- p3[1];
2422 pt[2]= 2*p2[2]- p3[2];
2425 else p1= prev->vec[1];
2428 pt[0]= 2*p2[0]- p1[0];
2429 pt[1]= 2*p2[1]- p1[1];
2430 pt[2]= 2*p2[2]- p1[2];
2433 else p3= next->vec[1];
2440 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2447 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2449 if(len1==0.0f) len1=1.0f;
2450 if(len2==0.0f) len2=1.0f;
2453 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2454 vx= dx1/len2 + dx/len1;
2455 vy= dy1/len2 + dy/len1;
2456 vz= dz1/len2 + dz/len1;
2457 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2459 int leftviolate=0, rightviolate=0; /* for mode==2 */
2461 if(len1>5.0f*len2) len1= 5.0f*len2;
2462 if(len2>5.0f*len1) len2= 5.0f*len1;
2464 if(bezt->h1==HD_AUTO) {
2466 *(p2-3)= *p2-vx*len1;
2467 *(p2-2)= *(p2+1)-vy*len1;
2468 *(p2-1)= *(p2+2)-vz*len1;
2470 if(mode==2 && next && prev) { // keep horizontal if extrema
2471 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2472 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2473 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2474 bezt->vec[0][1]= bezt->vec[1][1];
2476 else { // handles should not be beyond y coord of two others
2478 if(prev->vec[1][1] > bezt->vec[0][1]) {
2479 bezt->vec[0][1]= prev->vec[1][1];
2484 if(prev->vec[1][1] < bezt->vec[0][1]) {
2485 bezt->vec[0][1]= prev->vec[1][1];
2492 if(bezt->h2==HD_AUTO) {
2494 *(p2+3)= *p2+vx*len2;
2495 *(p2+4)= *(p2+1)+vy*len2;
2496 *(p2+5)= *(p2+2)+vz*len2;
2498 if(mode==2 && next && prev) { // keep horizontal if extrema
2499 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2500 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2501 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2502 bezt->vec[2][1]= bezt->vec[1][1];
2504 else { // handles should not be beyond y coord of two others
2506 if(next->vec[1][1] < bezt->vec[2][1]) {
2507 bezt->vec[2][1]= next->vec[1][1];
2512 if(next->vec[1][1] > bezt->vec[2][1]) {
2513 bezt->vec[2][1]= next->vec[1][1];
2520 if(leftviolate || rightviolate) { /* align left handle */
2523 sub_v3_v3v3(h1, p2-3, p2);
2524 sub_v3_v3v3(h2, p2, p2+3);
2525 len1= normalize_v3(h1);
2526 len2= normalize_v3(h2);
2531 *(p2+3)= *(p2) - vz*len2*h1[0];
2532 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2533 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2536 *(p2-3)= *(p2) + vz*len1*h2[0];
2537 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2538 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2545 if(bezt->h1==HD_VECT) { /* vector */
2550 *(p2-2)= *(p2+1)-dy;
2551 *(p2-1)= *(p2+2)-dz;
2553 if(bezt->h2==HD_VECT) {
2558 *(p2+4)= *(p2+1)+dy1;
2559 *(p2+5)= *(p2+2)+dz1;
2562 len2= len_v3v3(p2, p2+3);
2563 len1= len_v3v3(p2, p2-3);
2564 if(len1==0.0) len1=1.0;
2565 if(len2==0.0) len2=1.0;
2567 if(bezt->f1 & SELECT) { /* order of calculation */
2568 if(bezt->h2==HD_ALIGN) { /* aligned */
2570 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2571 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2572 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2574 if(bezt->h1==HD_ALIGN) {
2576 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2577 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2578 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2582 if(bezt->h1==HD_ALIGN) {
2584 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2585 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2586 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2588 if(bezt->h2==HD_ALIGN) { /* aligned */
2590 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2591 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2592 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2597 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2599 BezTriple *bezt, *prev, *next;
2602 if(nu->type != CU_BEZIER) return;
2603 if(nu->pntsu<2) return;
2607 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2612 calchandleNurb(bezt, prev, next, 0);
2615 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2625 void testhandlesNurb(Nurb *nu)
2627 /* use when something has changed with handles.
2628 it treats all BezTriples with the following rules:
2629 PHASE 1: do types have to be altered?
2630 Auto handles: become aligned when selection status is NOT(000 || 111)
2631 Vector handles: become 'nothing' when (one half selected AND other not)
2632 PHASE 2: recalculate handles
2637 if(nu->type != CU_BEZIER) return;
2643 if(bezt->f1 & SELECT) flag++;
2644 if(bezt->f2 & SELECT) flag += 2;
2645 if(bezt->f3 & SELECT) flag += 4;
2647 if( !(flag==0 || flag==7) ) {
2648 if(bezt->h1==HD_AUTO) { /* auto */
2651 if(bezt->h2==HD_AUTO) { /* auto */
2655 if(bezt->h1==HD_VECT) { /* vector */
2656 if(flag < 4) bezt->h1= 0;
2658 if(bezt->h2==HD_VECT) { /* vector */
2659 if( flag > 3) bezt->h2= 0;
2665 calchandlesNurb(nu);
2668 void autocalchandlesNurb(Nurb *nu, int flag)
2670 /* checks handle coordinates and calculates type */
2672 BezTriple *bezt2, *bezt1, *bezt0;
2673 int i, align, leftsmall, rightsmall;
2675 if(nu==0 || nu->bezt==0) return;
2678 bezt1 = bezt2 + (nu->pntsu-1);
2684 align= leftsmall= rightsmall= 0;
2687 if(flag==0 || (bezt1->f1 & flag) ) {
2689 /* distance too short: vectorhandle */
2690 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2695 /* aligned handle? */
2696 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2698 bezt1->h1= HD_ALIGN;
2700 /* or vector handle? */
2701 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2707 if(flag==0 || (bezt1->f3 & flag) ) {
2709 /* distance too short: vectorhandle */
2710 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2715 /* aligned handle? */
2716 if(align) bezt1->h2= HD_ALIGN;
2718 /* or vector handle? */
2719 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2724 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2725 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2727 /* undesired combination: */
2728 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2729 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2736 calchandlesNurb(nu);
2739 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2743 nu= editnurb->first;
2745 autocalchandlesNurb(nu, flag);
2750 void sethandlesNurb(ListBase *editnurb, short code)
2752 /* code==1: set autohandle */
2753 /* code==2: set vectorhandle */
2754 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2755 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2756 /* code==5: Set align, like 3 but no toggle */
2757 /* code==6: Clear align, like 3 but no toggle */
2762 if(code==1 || code==2) {
2763 nu= editnurb->first;
2765 if(nu->type == CU_BEZIER) {
2769 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2770 if(bezt->f1 & SELECT) bezt->h1= code;
2771 if(bezt->f3 & SELECT) bezt->h2= code;
2772 if(bezt->h1!=bezt->h2) {
2773 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2774 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2779 calchandlesNurb(nu);
2785 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2787 nu= editnurb->first;
2790 } else if (code == 6) {
2795 if(nu->type == CU_BEZIER) {
2799 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2800 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2810 nu= editnurb->first;
2812 if(nu->type == CU_BEZIER) {
2816 if(bezt->f1 & SELECT) bezt->h1= ok;
2817 if(bezt->f3 & SELECT) bezt->h2= ok;
2821 calchandlesNurb(nu);
2828 static void swapdata(void *adr1, void *adr2, int len)
2836 memcpy(adr, adr1, len);
2837 memcpy(adr1, adr2, len);
2838 memcpy(adr2, adr, len);
2843 adr= (char *)MEM_mallocN(len, "curve swap");
2844 memcpy(adr, adr1, len);
2845 memcpy(adr1, adr2, len);
2846 memcpy(adr2, adr, len);
2851 void switchdirectionNurb(Nurb *nu)
2853 BezTriple *bezt1, *bezt2;
2855 float *fp1, *fp2, *tempf;
2858 if(nu->pntsu==1 && nu->pntsv==1) return;
2860 if(nu->type == CU_BEZIER) {
2864 if(a & 1) a+= 1; /* if odd, also swap middle content */
2867 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2869 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2870 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2872 SWAP(char, bezt1->h1, bezt1->h2);
2873 SWAP(short, bezt1->f1, bezt1->f3);
2876 SWAP(char, bezt2->h1, bezt2->h2);
2877 SWAP(short, bezt2->f1, bezt2->f3);
2878 bezt1->alfa= -bezt1->alfa;
2879 bezt2->alfa= -bezt2->alfa;
2886 else if(nu->pntsv==1) {
2891 while(bp1!=bp2 && a>0) {
2892 SWAP(BPoint, *bp1, *bp2);
2894 bp1->alfa= -bp1->alfa;
2895 bp2->alfa= -bp2->alfa;
2899 if(nu->type == CU_NURBS) {
2905 while(fp1!=fp2 && a>0) {
2906 SWAP(float, *fp1, *fp2);
2911 /* and make in increasing order again */
2914 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2916 fp2[0]= fabs(fp1[1]-fp1[0]);
2927 fp1[0]= fp1[-1]+fp2[0];
2936 for(b=0; b<nu->pntsv; b++) {
2938 bp1= nu->bp+b*nu->pntsu;
2943 while(bp1!=bp2 && a>0) {
2944 SWAP(BPoint, *bp1, *bp2);
2954 float (*curve_getVertexCos(Curve *UNUSED(cu), ListBase *lb, int *numVerts_r))[3]