7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/curve.c
38 #include <math.h> // floor
42 #include "MEM_guardedalloc.h"
44 #include "BLI_blenlib.h"
46 #include "BLI_utildefines.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_material_types.h"
51 /* for dereferencing pointers */
52 #include "DNA_key_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_vfont_types.h"
55 #include "DNA_object_types.h"
57 #include "BKE_animsys.h"
59 #include "BKE_curve.h"
60 #include "BKE_displist.h"
62 #include "BKE_global.h"
64 #include "BKE_library.h"
66 #include "BKE_object.h"
74 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
76 float *labda, float *mu, float *vec);
78 void unlink_curve(Curve *cu)
82 for(a=0; a<cu->totcol; a++) {
83 if(cu->mat[a]) cu->mat[a]->id.us--;
86 if(cu->vfont) cu->vfont->id.us--;
89 if(cu->vfontb) cu->vfontb->id.us--;
92 if(cu->vfonti) cu->vfonti->id.us--;
95 if(cu->vfontbi) cu->vfontbi->id.us--;
98 if(cu->key) cu->key->id.us--;
102 /* frees editcurve entirely */
103 void BKE_free_editfont(Curve *cu)
106 EditFont *ef= cu->editfont;
108 if(ef->oldstr) MEM_freeN(ef->oldstr);
109 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
110 if(ef->textbuf) MEM_freeN(ef->textbuf);
111 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
112 if(ef->copybuf) MEM_freeN(ef->copybuf);
113 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
120 /* don't free curve itself */
121 void free_curve(Curve *cu)
123 freeNurblist(&cu->nurb);
124 BLI_freelistN(&cu->bev);
125 freedisplist(&cu->disp);
126 BKE_free_editfont(cu);
128 free_curve_editNurb(cu);
130 BKE_free_animdata((ID *)cu);
132 if(cu->mat) MEM_freeN(cu->mat);
133 if(cu->str) MEM_freeN(cu->str);
134 if(cu->strinfo) MEM_freeN(cu->strinfo);
135 if(cu->bb) MEM_freeN(cu->bb);
136 if(cu->path) free_path(cu->path);
137 if(cu->tb) MEM_freeN(cu->tb);
140 Curve *add_curve(const char *name, int type)
144 cu= alloc_libblock(&G.main->curve, ID_CU, name);
146 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
147 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
149 cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
152 cu->spacing= cu->linedist= 1.0;
155 cu->texflag= CU_AUTOSPACE;
156 cu->smallcaps_scale= 0.75f;
157 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
159 cu->bb= unit_boundbox();
162 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
164 cu->str= MEM_mallocN(12, "str");
165 BLI_strncpy(cu->str, "Text", 12);
167 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
168 cu->totbox= cu->actbox= 1;
169 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
170 cu->tb[0].w = cu->tb[0].h = 0.0;
176 Curve *copy_curve(Curve *cu)
181 cun= copy_libblock(cu);
182 cun->nurb.first= cun->nurb.last= NULL;
183 duplicateNurblist( &(cun->nurb), &(cu->nurb));
185 cun->mat= MEM_dupallocN(cu->mat);
186 for(a=0; a<cun->totcol; a++) {
187 id_us_plus((ID *)cun->mat[a]);
190 cun->str= MEM_dupallocN(cu->str);
191 cun->strinfo= MEM_dupallocN(cu->strinfo);
192 cun->tb= MEM_dupallocN(cu->tb);
193 cun->bb= MEM_dupallocN(cu->bb);
195 cun->key= copy_key(cu->key);
196 if(cun->key) cun->key->from= (ID *)cun;
198 cun->disp.first= cun->disp.last= NULL;
199 cun->bev.first= cun->bev.last= NULL;
206 #if 0 // XXX old animation system
207 /* single user ipo too */
208 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
209 #endif // XXX old animation system
211 id_us_plus((ID *)cun->vfont);
212 id_us_plus((ID *)cun->vfontb);
213 id_us_plus((ID *)cun->vfonti);
214 id_us_plus((ID *)cun->vfontbi);
219 void make_local_curve(Curve *cu)
225 /* - when there are only lib users: don't do
226 * - when there are only local users: set flag
230 if(cu->id.lib==NULL) return;
232 if(cu->vfont) cu->vfont->id.lib= NULL;
233 if(cu->vfontb) cu->vfontb->id.lib= NULL;
234 if(cu->vfonti) cu->vfonti->id.lib= NULL;
235 if(cu->vfontbi) cu->vfontbi->id.lib= NULL;
239 cu->id.flag= LIB_LOCAL;
240 new_id(NULL, (ID *)cu, NULL);
244 ob= G.main->object.first;
247 if(ob->id.lib) lib= 1;
253 if(local && lib==0) {
255 cu->id.flag= LIB_LOCAL;
256 new_id(NULL, (ID *)cu, NULL);
258 else if(local && lib) {
262 ob= G.main->object.first;
266 if(ob->id.lib==NULL) {
277 short curve_type(Curve *cu)
283 for (nu= cu->nurb.first; nu; nu= nu->next) {
292 void test_curve_type(Object *ob)
294 ob->type = curve_type(ob->data);
297 void tex_space_curve(Curve *cu)
301 float *fp, min[3], max[3];
304 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
307 INIT_MINMAX(min, max);
312 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
313 else tot= dl->nr*dl->parts;
318 DO_MINMAX(fp, min, max);
325 min[0] = min[1] = min[2] = -1.0f;
326 max[0] = max[1] = max[2] = 1.0f;
329 boundbox_set_from_min_max(bb, min, max);
331 if(cu->texflag & CU_AUTOSPACE) {
332 mid_v3_v3v3(cu->loc, min, max);
333 cu->size[0]= (max[0]-min[0])/2.0f;
334 cu->size[1]= (max[1]-min[1])/2.0f;
335 cu->size[2]= (max[2]-min[2])/2.0f;
337 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
339 if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
340 else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
341 else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
343 if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
344 else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
345 else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
347 if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
348 else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
349 else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
355 int count_curveverts(ListBase *nurb)
362 if(nu->bezt) tot+= 3*nu->pntsu;
363 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
370 int count_curveverts_without_handles(ListBase *nurb)
377 if(nu->bezt) tot+= nu->pntsu;
378 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
385 /* **************** NURBS ROUTINES ******************** */
387 void freeNurb(Nurb *nu)
392 if(nu->bezt) MEM_freeN(nu->bezt);
394 if(nu->bp) MEM_freeN(nu->bp);
396 if(nu->knotsu) MEM_freeN(nu->knotsu);
398 if(nu->knotsv) MEM_freeN(nu->knotsv);
400 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
407 void freeNurblist(ListBase *lb)
419 lb->first= lb->last= NULL;
422 Nurb *duplicateNurb(Nurb *nu)
427 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
428 if(newnu==NULL) return NULL;
429 memcpy(newnu, nu, sizeof(Nurb));
433 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
434 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
437 len= nu->pntsu*nu->pntsv;
439 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
440 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
442 newnu->knotsu= newnu->knotsv= NULL;
447 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
448 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
451 if(nu->pntsv>1 && nu->knotsv) {
454 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
455 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
462 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
470 nun= duplicateNurb(nu);
471 BLI_addtail(lb1, nun);
477 void test2DNurb(Nurb *nu)
483 if((nu->flag & CU_2D)==0)
486 if(nu->type == CU_BEZIER) {
490 bezt->vec[0][2]= 0.0;
491 bezt->vec[1][2]= 0.0;
492 bezt->vec[2][2]= 0.0;
497 a= nu->pntsu*nu->pntsv;
506 void minmaxNurb(Nurb *nu, float *min, float *max)
512 if(nu->type == CU_BEZIER) {
516 DO_MINMAX(bezt->vec[0], min, max);
517 DO_MINMAX(bezt->vec[1], min, max);
518 DO_MINMAX(bezt->vec[2], min, max);
523 a= nu->pntsu*nu->pntsv;
526 DO_MINMAX(bp->vec, min, max);
532 /* be sure to call makeknots after this */
533 void addNurbPoints(Nurb *nu, int number)
537 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
540 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
544 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
546 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
553 void addNurbPointsBezier(Nurb *nu, int number)
555 BezTriple *tmp= nu->bezt;
557 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
560 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
564 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
566 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
573 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
576 static void calcknots(float *knots, short aantal, short order, short type)
577 /* knots: number of pnts NOT corrected for cyclic */
578 /* type; 0: uniform, 1: endpoints, 2: bezier */
594 if(a>=order && a<=aantal) k+= 1.0f;
598 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
609 if(a>=order && a<=aantal) k+= 0.5f;
614 printf("bez nurb curve order is not 3 or 4, should never happen\n");
619 static void makecyclicknots(float *knots, short pnts, short order)
620 /* pnts, order: number of pnts NOT corrected for cyclic */
624 if(knots==NULL) return;
628 /* do first long rows (order -1), remove identical knots at endpoints */
631 for(a=1; a<order2; a++) {
632 if(knots[b]!= knots[b-a]) break;
634 if(a==order2) knots[pnts+order-2]+= 1.0f;
638 c=pnts + order + order2;
639 for(a=pnts+order2; a<c; a++) {
640 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
647 static void makeknots(Nurb *nu, short uv)
649 if(nu->type == CU_NURBS) {
651 if(nu->knotsu) MEM_freeN(nu->knotsu);
652 if(check_valid_nurb_u(nu)) {
653 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
654 if(nu->flagu & CU_NURB_CYCLIC) {
655 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
656 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
658 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
661 else nu->knotsu= NULL;
664 if(nu->knotsv) MEM_freeN(nu->knotsv);
665 if(check_valid_nurb_v(nu)) {
666 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
667 if(nu->flagv & CU_NURB_CYCLIC) {
668 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
669 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
671 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
674 else nu->knotsv= NULL;
679 void nurbs_knot_calc_u(Nurb *nu)
684 void nurbs_knot_calc_v(Nurb *nu)
689 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
692 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
694 orderpluspnts= order+pnts;
695 opp2 = orderpluspnts-1;
697 /* this is for float inaccuracy */
698 if(t < knots[0]) t= knots[0];
699 else if(t > knots[opp2]) t= knots[opp2];
701 /* this part is order '1' */
703 for(i=0;i<opp2;i++) {
704 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
720 /* this is order 2,3,... */
721 for(j=2; j<=order; j++) {
723 if(i2+j>= orderpluspnts) i2= opp2-j;
725 for(i= i1; i<=i2; i++) {
727 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
731 if(basis[i+1] != 0.0f)
732 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
743 for(i=i1; i<=i2; i++) {
744 if(basis[i] > 0.0f) {
746 if(*start==1000) *start= i;
752 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
753 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
756 float *basisu, *basis, *basisv, *sum, *fp, *in;
757 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
758 int i, j, iofs, jofs, cycl, len, curu, curv;
759 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
761 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
763 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
764 if(nu->orderu>nu->pntsu) return;
765 if(nu->orderv>nu->pntsv) return;
766 if(coord_array==NULL) return;
768 /* allocate and initialize */
774 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
783 i= nu->pntsu*nu->pntsv;
786 if(bp->vec[3] != 1.0f) {
794 ustart= fp[nu->orderu-1];
795 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
796 else uend= fp[nu->pntsu];
797 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
799 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
802 vstart= fp[nu->orderv-1];
804 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
805 else vend= fp[nu->pntsv];
806 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
809 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
810 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
811 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
813 /* precalculation of basisv and jstart,jend */
814 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
820 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
825 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
832 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
845 for(j= jsta; j<=jen; j++) {
847 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
849 bp= nu->bp+ nu->pntsu*jofs+istart-1;
851 for(i= istart; i<=iend; i++, fp++) {
855 bp= nu->bp+ nu->pntsu*jofs+iofs;
860 *fp= basisu[i]*basis[j]*bp->vec[3];
863 else *fp= basisu[i]*basis[j];
869 for(j= jsta; j<=jen; j++) {
870 for(i= istart; i<=iend; i++, fp++) {
876 /* one! (1.0) real point now */
878 for(j= jsta; j<=jen; j++) {
880 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
882 bp= nu->bp+ nu->pntsu*jofs+istart-1;
884 for(i= istart; i<=iend; i++, fp++) {
888 bp= nu->bp+ nu->pntsu*jofs+iofs;
893 in[0]+= (*fp) * bp->vec[0];
894 in[1]+= (*fp) * bp->vec[1];
895 in[2]+= (*fp) * bp->vec[2];
904 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
915 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
916 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
917 * tilt_array and radius_array will be written to if valid */
920 float u, ustart, uend, ustep, sumdiv;
921 float *basisu, *sum, *fp;
922 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
923 int i, len, istart, iend, cycl;
925 if(nu->knotsu==NULL) return;
926 if(nu->orderu>nu->pntsu) return;
927 if(coord_array==NULL) return;
929 /* allocate and initialize */
932 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
934 resolu= (resolu*SEGMENTSU(nu));
942 ustart= fp[nu->orderu-1];
943 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
944 else uend= fp[nu->pntsu];
945 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
947 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
949 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
955 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
959 bp= nu->bp+ istart-1;
960 for(i= istart; i<=iend; i++, fp++) {
962 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
965 *fp= basisu[i]*bp->vec[3];
968 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
969 /* is normalizing needed? */
971 for(i= istart; i<=iend; i++, fp++) {
976 /* one! (1.0) real point */
978 bp= nu->bp+ istart-1;
979 for(i= istart; i<=iend; i++, fp++) {
981 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
986 coord_fp[0]+= (*fp) * bp->vec[0];
987 coord_fp[1]+= (*fp) * bp->vec[1];
988 coord_fp[2]+= (*fp) * bp->vec[2];
991 (*tilt_fp) += (*fp) * bp->alfa;
994 (*radius_fp) += (*fp) * bp->radius;
997 (*weight_fp) += (*fp) * bp->weight;
1002 coord_fp = (float *)(((char *)coord_fp) + stride);
1004 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1005 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1006 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1016 /* forward differencing method for bezier curve */
1017 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1019 float rt0,rt1,rt2,rt3,f;
1024 rt1= 3.0f*(q1-q0)/f;
1026 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1028 rt3= (q3-q0+3.0f*(q1-q2))/f;
1035 for(a=0; a<=it; a++) {
1037 p = (float *)(((char *)p)+stride);
1044 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1046 /* note that these are not purpendicular to the curve
1047 * they need to be rotated for this,
1049 * This could also be optimized like forward_diff_bezier */
1051 for(a=0; a<=it; a++) {
1052 float t = (float)a / (float)it;
1055 for(i=0; i<3; i++) {
1056 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1059 p = (float *)(((char *)p)+stride);
1063 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1065 float *make_orco_surf(Object *ob)
1067 /* Note: this function is used in convertblender only atm, so
1068 * suppose nonzero curve's render resolution should always be used */
1069 Curve *cu= ob->data;
1074 float *fp, *coord_array;
1076 /* first calculate the size of the datablock */
1079 /* as we want to avoid the seam in a cyclic nurbs
1080 texture wrapping, reserve extra orco data space to save these extra needed
1081 vertex based UV coordinates for the meridian vertices.
1082 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1083 the renderface/vert construction.
1085 See also convertblender.c: init_render_surf()
1088 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1089 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1091 sizeu = nu->pntsu*resolu;
1092 sizev = nu->pntsv*resolv;
1093 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1094 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1095 if(nu->pntsv>1) tot+= sizeu * sizev;
1099 /* makeNurbfaces wants zeros */
1100 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1104 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1105 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1108 sizeu = nu->pntsu*resolu;
1109 sizev = nu->pntsv*resolv;
1110 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1111 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1113 if(cu->flag & CU_UV_ORCO) {
1114 for(b=0; b< sizeu; b++) {
1115 for(a=0; a< sizev; a++) {
1117 if(sizev <2) fp[0]= 0.0f;
1118 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1120 if(sizeu <2) fp[1]= 0.0f;
1121 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1130 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1131 float *tdata= _tdata;
1133 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1135 for(b=0; b<sizeu; b++) {
1137 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1140 for(a=0; a<sizev; a++) {
1142 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1145 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1147 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1148 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1149 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1164 /* NOTE: This routine is tied to the order of vertex
1165 * built by displist and as passed to the renderer.
1167 float *make_orco_curve(Scene *scene, Object *ob)
1169 Curve *cu = ob->data;
1172 float *fp, *coord_array;
1173 ListBase disp = {NULL, NULL};
1175 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1178 for (dl=disp.first; dl; dl=dl->next) {
1179 if (dl->type==DL_INDEX3) {
1181 } else if (dl->type==DL_SURF) {
1182 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1183 if (dl->flag & DL_CYCL_U) {
1184 if (dl->flag & DL_CYCL_V)
1185 numVerts += (dl->parts+1)*(dl->nr+1);
1187 numVerts += dl->parts*(dl->nr+1);
1190 numVerts += dl->parts*dl->nr;
1194 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1195 for (dl=disp.first; dl; dl=dl->next) {
1196 if (dl->type==DL_INDEX3) {
1197 for (u=0; u<dl->nr; u++, fp+=3) {
1198 if (cu->flag & CU_UV_ORCO) {
1199 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1203 VECCOPY(fp, &dl->verts[u*3]);
1205 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1206 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1207 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1210 } else if (dl->type==DL_SURF) {
1211 int sizeu= dl->nr, sizev= dl->parts;
1213 /* exception as handled in convertblender.c too */
1214 if (dl->flag & DL_CYCL_U) {
1216 if (dl->flag & DL_CYCL_V)
1220 for (u=0; u<sizev; u++) {
1221 for (v=0; v<sizeu; v++,fp+=3) {
1222 if (cu->flag & CU_UV_ORCO) {
1223 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1224 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1228 int realv= v % dl->nr;
1229 int realu= u % dl->parts;
1231 vert= dl->verts + 3*(dl->nr*realu + realv);
1234 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1235 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1236 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1243 freedisplist(&disp);
1249 /* ***************** BEVEL ****************** */
1251 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1253 DispList *dl, *dlnew;
1255 float *fp, facx, facy, angle, dangle;
1259 disp->first = disp->last = NULL;
1261 /* if a font object is being edited, then do nothing */
1262 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1265 if (cu->bevobj->type!=OB_CURVE) return;
1267 bevcu= cu->bevobj->data;
1268 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1269 ListBase bevdisp= {NULL, NULL};
1270 facx= cu->bevobj->size[0];
1271 facy= cu->bevobj->size[1];
1274 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1277 dl= cu->bevobj->disp.first;
1279 makeDispListCurveTypes(scene, cu->bevobj, 0);
1280 dl= cu->bevobj->disp.first;
1285 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1286 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1288 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1289 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1291 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1293 BLI_addtail(disp, dlnew);
1295 nr= dlnew->parts*dlnew->nr;
1306 freedisplist(&bevdisp);
1309 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1312 else if(cu->ext2==0.0f) {
1313 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1314 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1315 BLI_addtail(disp, dl);
1318 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1327 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1329 nr= 4+ 2*cu->bevresol;
1331 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1332 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1333 BLI_addtail(disp, dl);
1336 dl->flag= DL_BACK_CURVE;
1341 dangle= (2.0f*(float)M_PI/(nr));
1342 angle= -(nr-1)*dangle;
1344 for(a=0; a<nr; a++) {
1346 fp[1]= (cosf(angle)*(cu->ext2));
1347 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1355 /* bevel now in three parts, for proper vertex normals */
1358 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1359 dnr= nr= 2+ cu->bevresol;
1360 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1361 nr= 3+ 2*cu->bevresol;
1363 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1364 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1365 BLI_addtail(disp, dl);
1368 dl->flag= DL_BACK_CURVE;
1373 dangle= (0.5*M_PI/(dnr-1));
1374 angle= -(nr-1)*dangle;
1376 for(a=0; a<nr; a++) {
1378 fp[1]= (float)(cosf(angle)*(cu->ext2));
1379 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1385 /* part 2, sidefaces */
1386 if(cu->ext1!=0.0f) {
1389 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1390 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1391 BLI_addtail(disp, dl);
1402 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1403 dl= MEM_dupallocN(dl);
1404 dl->verts= MEM_dupallocN(dl->verts);
1405 BLI_addtail(disp, dl);
1416 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1417 dnr= nr= 2+ cu->bevresol;
1418 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1419 nr= 3+ 2*cu->bevresol;
1421 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1422 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1423 BLI_addtail(disp, dl);
1425 dl->flag= DL_FRONT_CURVE;
1432 dangle= (0.5*M_PI/(dnr-1));
1434 for(a=0; a<nr; a++) {
1436 fp[1]= (float)(cosf(angle)*(cu->ext2));
1437 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1445 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1449 0: no intersection of segments
1450 1: exact intersection of segments
1451 2: cross-intersection of segments
1455 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1456 if(deler==0.0f) return -1;
1458 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1459 *labda= -(*labda/deler);
1461 deler= v3[coy]-v4[coy];
1463 deler=v3[cox]-v4[cox];
1464 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1466 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1468 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1469 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1471 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1472 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1479 static short bevelinside(BevList *bl1,BevList *bl2)
1481 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1482 /* returns '1' if correct hole */
1483 BevPoint *bevp, *prevbevp;
1484 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1485 int nr, links=0,rechts=0,mode;
1487 /* take first vertex of possible hole */
1489 bevp= (BevPoint *)(bl2+1);
1490 hvec1[0]= bevp->vec[0];
1491 hvec1[1]= bevp->vec[1];
1493 VECCOPY(hvec2,hvec1);
1496 /* test it with all edges of potential surounding poly */
1497 /* count number of transitions left-right */
1499 bevp= (BevPoint *)(bl1+1);
1501 prevbevp= bevp+(nr-1);
1504 min= prevbevp->vec[1];
1508 max= prevbevp->vec[1];
1511 if(min<=hvec1[1] && max>=hvec1[1]) {
1512 /* there's a transition, calc intersection point */
1513 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1514 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1515 only allow for one situation: we choose lab= 1.0
1517 if(mode >= 0 && lab != 0.0f) {
1518 if(vec[0]<hvec1[0]) links++;
1527 if( (links & 1) && (rechts & 1) ) return 1;
1538 static int vergxcobev(const void *a1, const void *a2)
1540 const struct bevelsort *x1=a1,*x2=a2;
1542 if( x1->left > x2->left ) return 1;
1543 else if( x1->left < x2->left) return -1;
1547 /* this function cannot be replaced with atan2, but why? */
1549 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1551 float t01, t02, x3, y3;
1553 t01= (float)sqrt(x1*x1+y1*y1);
1554 t02= (float)sqrt(x2*x2+y2*y2);
1555 if(t01==0.0f) t01= 1.0f;
1556 if(t02==0.0f) t02= 1.0f;
1564 if(fabs(t02)>=1.0) t02= .5*M_PI;
1565 else t02= (saacos(t02))/2.0f;
1567 t02= (float)sin(t02);
1568 if(t02==0.0f) t02= 1.0f;
1572 if(x3==0 && y3==0) {
1576 t01= (float)sqrt(x3*x3+y3*y3);
1586 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1588 BezTriple *pprev, *next, *last;
1589 float fac, dfac, t[4];
1592 if(tilt_array==NULL && radius_array==NULL)
1595 last= nu->bezt+(nu->pntsu-1);
1597 /* returns a point */
1598 if(prevbezt==nu->bezt) {
1599 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1600 else pprev= prevbezt;
1602 else pprev= prevbezt-1;
1606 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1612 dfac= 1.0f/(float)resolu;
1614 for(a=0; a<resolu; a++, fac+= dfac) {
1616 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1617 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1619 key_curve_position_weights(fac, t, nu->tilt_interp);
1620 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1623 tilt_array = (float *)(((char *)tilt_array) + stride);
1627 if (nu->radius_interp==KEY_CU_EASE) {
1628 /* Support 2.47 ease interp
1629 * Note! - this only takes the 2 points into account,
1630 * giving much more localized results to changes in radius, sometimes you want that */
1631 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1634 /* reuse interpolation from tilt if we can */
1635 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1636 key_curve_position_weights(fac, t, nu->radius_interp);
1638 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1641 radius_array = (float *)(((char *)radius_array) + stride);
1645 /* basic interpolation for now, could copy tilt interp too */
1646 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1648 weight_array = (float *)(((char *)weight_array) + stride);
1653 /* make_bevel_list_3D_* funcs, at a minimum these must
1654 * fill in the bezp->quat and bezp->dir values */
1656 /* correct non-cyclic cases by copying direction and rotation
1657 * values onto the first & last end-points */
1658 static void bevel_list_cyclic_fix_3D(BevList *bl)
1660 BevPoint *bevp, *bevp1;
1662 bevp= (BevPoint *)(bl+1);
1664 QUATCOPY(bevp->quat, bevp1->quat);
1665 VECCOPY(bevp->dir, bevp1->dir);
1666 VECCOPY(bevp->tan, bevp1->tan);
1667 bevp= (BevPoint *)(bl+1);
1670 QUATCOPY(bevp->quat, bevp1->quat);
1671 VECCOPY(bevp->dir, bevp1->dir);
1672 VECCOPY(bevp->tan, bevp1->tan);
1674 /* utility for make_bevel_list_3D_* funcs */
1675 static void bevel_list_calc_bisect(BevList *bl)
1677 BevPoint *bevp2, *bevp1, *bevp0;
1680 bevp2= (BevPoint *)(bl+1);
1681 bevp1= bevp2+(bl->nr-1);
1686 /* totally simple */
1687 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1694 static void bevel_list_flip_tangents(BevList *bl)
1696 BevPoint *bevp2, *bevp1, *bevp0;
1699 bevp2= (BevPoint *)(bl+1);
1700 bevp1= bevp2+(bl->nr-1);
1705 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1706 negate_v3(bevp1->tan);
1713 /* apply user tilt */
1714 static void bevel_list_apply_tilt(BevList *bl)
1716 BevPoint *bevp2, *bevp1;
1720 bevp2= (BevPoint *)(bl+1);
1721 bevp1= bevp2+(bl->nr-1);
1725 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1726 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1727 normalize_qt(bevp1->quat);
1733 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1734 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1736 BevPoint *bevp2, *bevp1, *bevp0;
1740 float bevp0_quat[4];
1743 for(a=0; a < smooth_iter; a++) {
1745 bevp2= (BevPoint *)(bl+1);
1746 bevp1= bevp2+(bl->nr-1);
1751 if(bl->poly== -1) { /* check its not cyclic */
1752 /* skip the first point */
1765 QUATCOPY(bevp0_quat, bevp0->quat);
1768 /* interpolate quats */
1769 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1770 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1773 mul_qt_v3(q, zaxis);
1774 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1775 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1778 QUATCOPY(bevp0_quat, bevp1->quat);
1779 mul_qt_qtqt(q, q2, q);
1780 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1781 normalize_qt(bevp1->quat);
1784 /* bevp0= bevp1; */ /* UNUSED */
1791 static void make_bevel_list_3D_zup(BevList *bl)
1793 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1796 bevp2= (BevPoint *)(bl+1);
1797 bevp1= bevp2+(bl->nr-1);
1802 /* totally simple */
1803 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1804 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1812 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1814 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1818 bevel_list_calc_bisect(bl);
1820 bevp2= (BevPoint *)(bl+1);
1821 bevp1= bevp2+(bl->nr-1);
1827 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1828 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1831 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1833 if(angle > 0.0f) { /* otherwise we can keep as is */
1835 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1836 axis_angle_to_quat(q, cross_tmp, angle);
1837 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1840 QUATCOPY(bevp1->quat, bevp0->quat);
1849 if(bl->poly != -1) { /* check for cyclic */
1851 /* Need to correct for the start/end points not matching
1852 * do this by calculating the tilt angle difference, then apply
1853 * the rotation gradually over the entire curve
1855 * note that the split is between last and second last, rather then first/last as youd expect.
1857 * real order is like this
1858 * 0,1,2,3,4 --> 1,2,3,4,0
1860 * this is why we compare last with second last
1862 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1864 BevPoint *bevp_first;
1865 BevPoint *bevp_last;
1868 bevp_first= (BevPoint *)(bl+1);
1869 bevp_first+= bl->nr-1;
1870 bevp_last = bevp_first;
1873 /* quats and vec's are normalized, should not need to re-normalize */
1874 mul_qt_v3(bevp_first->quat, vec_1);
1875 mul_qt_v3(bevp_last->quat, vec_2);
1876 normalize_v3(vec_1);
1877 normalize_v3(vec_2);
1879 /* align the vector, can avoid this and it looks 98% OK but
1880 * better to align the angle quat roll's before comparing */
1882 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1883 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1884 axis_angle_to_quat(q, cross_tmp, angle);
1885 mul_qt_v3(q, vec_2);
1888 angle= angle_normalized_v3v3(vec_1, vec_2);
1890 /* flip rotation if needs be */
1891 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1892 normalize_v3(cross_tmp);
1893 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90.0f/(float)(180.0/M_PI))
1896 bevp2= (BevPoint *)(bl+1);
1897 bevp1= bevp2+(bl->nr-1);
1902 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1904 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1905 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1914 static void make_bevel_list_3D_tangent(BevList *bl)
1916 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1919 float bevp0_tan[3], cross_tmp[3];
1921 bevel_list_calc_bisect(bl);
1922 if(bl->poly== -1) /* check its not cyclic */
1923 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1924 bevel_list_flip_tangents(bl);
1926 /* correct the tangents */
1927 bevp2= (BevPoint *)(bl+1);
1928 bevp1= bevp2+(bl->nr-1);
1934 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1935 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1936 normalize_v3(bevp1->tan);
1944 /* now for the real twist calc */
1945 bevp2= (BevPoint *)(bl+1);
1946 bevp1= bevp2+(bl->nr-1);
1949 VECCOPY(bevp0_tan, bevp0->tan);
1954 /* make perpendicular, modify tan in place, is ok */
1956 float zero[3] = {0,0,0};
1958 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1959 normalize_v3(cross_tmp);
1960 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1962 /* bevp0= bevp1; */ /* UNUSED */
1968 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1970 switch(twist_mode) {
1971 case CU_TWIST_TANGENT:
1972 make_bevel_list_3D_tangent(bl);
1974 case CU_TWIST_MINIMUM:
1975 make_bevel_list_3D_minimum_twist(bl);
1977 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1978 make_bevel_list_3D_zup(bl);
1981 if(bl->poly== -1) /* check its not cyclic */
1982 bevel_list_cyclic_fix_3D(bl);
1985 bevel_list_smooth(bl, smooth_iter);
1987 bevel_list_apply_tilt(bl);
1992 /* only for 2 points */
1993 static void make_bevel_list_segment_3D(BevList *bl)
1997 BevPoint *bevp2= (BevPoint *)(bl+1);
1998 BevPoint *bevp1= bevp2+1;
2000 /* simple quat/dir */
2001 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2002 normalize_v3(bevp1->dir);
2004 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2006 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2007 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2008 normalize_qt(bevp1->quat);
2009 VECCOPY(bevp2->dir, bevp1->dir);
2010 QUATCOPY(bevp2->quat, bevp1->quat);
2015 void makeBevelList(Object *ob)
2018 - convert all curves to polys, with indication of resol and flags for double-vertices
2019 - possibly; do a smart vertice removal (in case Nurb)
2020 - separate in individual blicks with BoundBox
2021 - AutoHole detection
2025 BezTriple *bezt, *prevbezt;
2027 BevList *bl, *blnew, *blnext;
2028 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2029 float min, inp, x1, x2, y1, y2;
2030 struct bevelsort *sortdata, *sd, *sd1;
2031 int a, b, nr, poly, resolu = 0, len = 0;
2032 int do_tilt, do_radius, do_weight;
2034 /* this function needs an object, because of tflag and upflag */
2037 /* do we need to calculate the radius for each point? */
2038 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2040 /* STEP 1: MAKE POLYS */
2042 BLI_freelistN(&(cu->bev));
2043 if(cu->editnurb && ob->type!=OB_FONT) {
2044 ListBase *nurbs= ED_curve_editnurbs(cu);
2046 } else nu= cu->nurb.first;
2050 /* check if we will calculate tilt data */
2051 do_tilt = CU_DO_TILT(cu, nu);
2052 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2055 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2056 * enforced in the UI but can go wrong possibly */
2057 if(!check_valid_nurb_u(nu)) {
2058 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2059 BLI_addtail(&(cu->bev), bl);
2062 if(G.rendering && cu->resolu_ren!=0)
2063 resolu= cu->resolu_ren;
2067 if(nu->type == CU_POLY) {
2069 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2070 BLI_addtail(&(cu->bev), bl);
2072 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2076 bevp= (BevPoint *)(bl+1);
2080 VECCOPY(bevp->vec, bp->vec);
2081 bevp->alfa= bp->alfa;
2082 bevp->radius= bp->radius;
2083 bevp->weight= bp->weight;
2084 bevp->split_tag= TRUE;
2089 else if(nu->type == CU_BEZIER) {
2091 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2092 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2093 BLI_addtail(&(cu->bev), bl);
2095 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2097 bevp= (BevPoint *)(bl+1);
2101 if(nu->flagu & CU_NURB_CYCLIC) {
2103 prevbezt= nu->bezt+(nu->pntsu-1);
2111 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2113 VECCOPY(bevp->vec, prevbezt->vec[1]);
2114 bevp->alfa= prevbezt->alfa;
2115 bevp->radius= prevbezt->radius;
2116 bevp->weight= prevbezt->weight;
2117 bevp->split_tag= TRUE;
2118 bevp->dupe_tag= FALSE;
2124 /* always do all three, to prevent data hanging around */
2127 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2128 for(j=0; j<3; j++) {
2129 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2130 bezt->vec[0][j], bezt->vec[1][j],
2131 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2134 /* if both arrays are NULL do nothiong */
2135 alfa_bezpart( prevbezt, bezt, nu,
2136 do_tilt ? &bevp->alfa : NULL,
2137 do_radius ? &bevp->radius : NULL,
2138 do_weight ? &bevp->weight : NULL,
2139 resolu, sizeof(BevPoint));
2142 if(cu->twist_mode==CU_TWIST_TANGENT) {
2143 forward_diff_bezier_cotangent(
2144 prevbezt->vec[1], prevbezt->vec[2],
2145 bezt->vec[0], bezt->vec[1],
2146 bevp->tan, resolu, sizeof(BevPoint));
2149 /* indicate with handlecodes double points */
2150 if(prevbezt->h1==prevbezt->h2) {
2151 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2154 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2155 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2164 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2165 VECCOPY(bevp->vec, prevbezt->vec[1]);
2166 bevp->alfa= prevbezt->alfa;
2167 bevp->radius= prevbezt->radius;
2168 bevp->weight= prevbezt->weight;
2172 else if(nu->type == CU_NURBS) {
2174 len= (resolu*SEGMENTSU(nu));
2176 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2177 BLI_addtail(&(cu->bev), bl);
2180 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2182 bevp= (BevPoint *)(bl+1);
2184 makeNurbcurve( nu, &bevp->vec[0],
2185 do_tilt ? &bevp->alfa : NULL,
2186 do_radius ? &bevp->radius : NULL,
2187 do_weight ? &bevp->weight : NULL,
2188 resolu, sizeof(BevPoint));
2195 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2198 if (bl->nr) { /* null bevel items come from single points */
2200 bevp1= (BevPoint *)(bl+1);
2201 bevp0= bevp1+(nr-1);
2204 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2205 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2206 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2207 bevp0->dupe_tag= TRUE;
2221 if(bl->nr && bl->dupe_nr) {
2222 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2223 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2224 memcpy(blnew, bl, sizeof(BevList));
2226 BLI_remlink(&(cu->bev), bl);
2227 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2228 bevp0= (BevPoint *)(bl+1);
2229 bevp1= (BevPoint *)(blnew+1);
2232 if(bevp0->dupe_tag==0) {
2233 memcpy(bevp1, bevp0, sizeof(BevPoint));
2245 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2249 if(bl->nr && bl->poly>=0) {
2258 /* find extreme left points, also test (turning) direction */
2260 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2266 bevp= (BevPoint *)(bl+1);
2269 if(min>bevp->vec[0]) {
2278 bevp= (BevPoint *)(bl+1);
2279 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2280 else bevp0= bevp1-1;
2281 bevp= bevp+ (bl->nr-1);
2282 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2283 else bevp2= bevp1+1;
2285 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]);
2287 if(inp > 0.0f) sd->dir= 1;
2295 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2298 for(a=1; a<poly; a++, sd++) {
2299 bl= sd->bl; /* is bl a hole? */
2300 sd1= sortdata+ (a-1);
2301 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2302 if(bevelinside(sd1->bl, bl)) {
2303 bl->hole= 1- sd1->bl->hole;
2309 /* turning direction */
2310 if((cu->flag & CU_3D)==0) {
2312 for(a=0; a<poly; a++, sd++) {
2313 if(sd->bl->hole==sd->dir) {
2315 bevp1= (BevPoint *)(bl+1);
2316 bevp2= bevp1+ (bl->nr-1);
2319 SWAP(BevPoint, *bevp1, *bevp2);
2326 MEM_freeN(sortdata);
2329 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2330 if((cu->flag & CU_3D)==0) {
2331 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2332 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2339 else if(bl->nr==2) { /* 2 pnt, treat separate */
2340 bevp2= (BevPoint *)(bl+1);
2343 x1= bevp1->vec[0]- bevp2->vec[0];
2344 y1= bevp1->vec[1]- bevp2->vec[1];
2346 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2347 bevp2->sina= bevp1->sina;
2348 bevp2->cosa= bevp1->cosa;
2350 /* fill in dir & quat */
2351 make_bevel_list_segment_3D(bl);
2354 bevp2= (BevPoint *)(bl+1);
2355 bevp1= bevp2+(bl->nr-1);
2360 x1= bevp1->vec[0]- bevp0->vec[0];
2361 x2= bevp1->vec[0]- bevp2->vec[0];
2362 y1= bevp1->vec[1]- bevp0->vec[1];
2363 y2= bevp1->vec[1]- bevp2->vec[1];
2365 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2367 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2368 * no need for tricky tilt calculation as with 3D curves */
2369 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2370 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2371 /* done with inline make_bevel_list_3D_zup */
2378 /* correct non-cyclic cases */
2380 bevp= (BevPoint *)(bl+1);
2382 bevp->sina= bevp1->sina;
2383 bevp->cosa= bevp1->cosa;
2384 bevp= (BevPoint *)(bl+1);
2387 bevp->sina= bevp1->sina;
2388 bevp->cosa= bevp1->cosa;
2390 /* correct for the dir/quat, see above why its needed */
2391 bevel_list_cyclic_fix_3D(bl);
2397 else { /* 3D Curves */
2404 else if(bl->nr==2) { /* 2 pnt, treat separate */
2405 make_bevel_list_segment_3D(bl);
2408 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2415 /* ****************** HANDLES ************** */
2419 * 0: nothing, 1:auto, 2:vector, 3:aligned
2422 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2423 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2425 float *p1,*p2,*p3, pt[3];
2426 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2428 if(bezt->h1==0 && bezt->h2==0) return;
2434 pt[0]= 2*p2[0]- p3[0];
2435 pt[1]= 2*p2[1]- p3[1];
2436 pt[2]= 2*p2[2]- p3[2];
2439 else p1= prev->vec[1];
2442 pt[0]= 2*p2[0]- p1[0];
2443 pt[1]= 2*p2[1]- p1[1];
2444 pt[2]= 2*p2[2]- p1[2];
2447 else p3= next->vec[1];
2454 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2461 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2463 if(len1==0.0f) len1=1.0f;
2464 if(len2==0.0f) len2=1.0f;
2467 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2468 vx= dx1/len2 + dx/len1;
2469 vy= dy1/len2 + dy/len1;
2470 vz= dz1/len2 + dz/len1;
2471 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2473 int leftviolate=0, rightviolate=0; /* for mode==2 */
2475 if(len1>5.0f*len2) len1= 5.0f*len2;
2476 if(len2>5.0f*len1) len2= 5.0f*len1;
2478 if(bezt->h1==HD_AUTO) {
2480 *(p2-3)= *p2-vx*len1;
2481 *(p2-2)= *(p2+1)-vy*len1;
2482 *(p2-1)= *(p2+2)-vz*len1;
2484 if(mode==2 && next && prev) { // keep horizontal if extrema
2485 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2486 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2487 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2488 bezt->vec[0][1]= bezt->vec[1][1];
2490 else { // handles should not be beyond y coord of two others
2491 if(ydiff1 <= 0.0f) {
2492 if(prev->vec[1][1] > bezt->vec[0][1]) {
2493 bezt->vec[0][1]= prev->vec[1][1];
2498 if(prev->vec[1][1] < bezt->vec[0][1]) {
2499 bezt->vec[0][1]= prev->vec[1][1];
2506 if(bezt->h2==HD_AUTO) {
2508 *(p2+3)= *p2+vx*len2;
2509 *(p2+4)= *(p2+1)+vy*len2;
2510 *(p2+5)= *(p2+2)+vz*len2;
2512 if(mode==2 && next && prev) { // keep horizontal if extrema
2513 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2514 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2515 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2516 bezt->vec[2][1]= bezt->vec[1][1];
2518 else { // handles should not be beyond y coord of two others
2519 if(ydiff1 <= 0.0f) {
2520 if(next->vec[1][1] < bezt->vec[2][1]) {
2521 bezt->vec[2][1]= next->vec[1][1];
2526 if(next->vec[1][1] > bezt->vec[2][1]) {
2527 bezt->vec[2][1]= next->vec[1][1];
2534 if(leftviolate || rightviolate) { /* align left handle */
2537 sub_v3_v3v3(h1, p2-3, p2);
2538 sub_v3_v3v3(h2, p2, p2+3);
2539 len1= normalize_v3(h1);
2540 len2= normalize_v3(h2);
2545 *(p2+3)= *(p2) - vz*len2*h1[0];
2546 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2547 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2550 *(p2-3)= *(p2) + vz*len1*h2[0];
2551 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2552 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2559 if(bezt->h1==HD_VECT) { /* vector */
2564 *(p2-2)= *(p2+1)-dy;
2565 *(p2-1)= *(p2+2)-dz;
2567 if(bezt->h2==HD_VECT) {
2572 *(p2+4)= *(p2+1)+dy1;
2573 *(p2+5)= *(p2+2)+dz1;
2576 len2= len_v3v3(p2, p2+3);
2577 len1= len_v3v3(p2, p2-3);
2578 if(len1==0.0f) len1= 1.0f;
2579 if(len2==0.0f) len2= 1.0f;
2581 if(bezt->f1 & SELECT) { /* order of calculation */
2582 if(bezt->h2==HD_ALIGN) { /* aligned */
2584 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2585 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2586 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2588 if(bezt->h1==HD_ALIGN) {
2590 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2591 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2592 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2596 if(bezt->h1==HD_ALIGN) {
2598 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2599 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2600 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2602 if(bezt->h2==HD_ALIGN) { /* aligned */
2604 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2605 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2606 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2611 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2613 BezTriple *bezt, *prev, *next;
2616 if(nu->type != CU_BEZIER) return;
2617 if(nu->pntsu<2) return;
2621 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2626 calchandleNurb(bezt, prev, next, 0);
2629 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2639 void testhandlesNurb(Nurb *nu)
2641 /* use when something has changed with handles.
2642 it treats all BezTriples with the following rules:
2643 PHASE 1: do types have to be altered?
2644 Auto handles: become aligned when selection status is NOT(000 || 111)
2645 Vector handles: become 'nothing' when (one half selected AND other not)
2646 PHASE 2: recalculate handles
2651 if(nu->type != CU_BEZIER) return;
2657 if(bezt->f1 & SELECT) flag++;
2658 if(bezt->f2 & SELECT) flag += 2;
2659 if(bezt->f3 & SELECT) flag += 4;
2661 if( !(flag==0 || flag==7) ) {
2662 if(bezt->h1==HD_AUTO) { /* auto */
2665 if(bezt->h2==HD_AUTO) { /* auto */
2669 if(bezt->h1==HD_VECT) { /* vector */
2670 if(flag < 4) bezt->h1= 0;
2672 if(bezt->h2==HD_VECT) { /* vector */
2673 if( flag > 3) bezt->h2= 0;
2679 calchandlesNurb(nu);
2682 void autocalchandlesNurb(Nurb *nu, int flag)
2684 /* checks handle coordinates and calculates type */
2686 BezTriple *bezt2, *bezt1, *bezt0;
2687 int i, align, leftsmall, rightsmall;
2689 if(nu==NULL || nu->bezt==NULL) return;
2692 bezt1 = bezt2 + (nu->pntsu-1);
2698 align= leftsmall= rightsmall= 0;
2701 if(flag==0 || (bezt1->f1 & flag) ) {
2703 /* distance too short: vectorhandle */
2704 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2709 /* aligned handle? */
2710 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2712 bezt1->h1= HD_ALIGN;
2714 /* or vector handle? */
2715 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2721 if(flag==0 || (bezt1->f3 & flag) ) {
2723 /* distance too short: vectorhandle */
2724 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2729 /* aligned handle? */
2730 if(align) bezt1->h2= HD_ALIGN;
2732 /* or vector handle? */
2733 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2738 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2739 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2741 /* undesired combination: */
2742 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2743 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2750 calchandlesNurb(nu);
2753 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2757 nu= editnurb->first;
2759 autocalchandlesNurb(nu, flag);
2764 void sethandlesNurb(ListBase *editnurb, short code)
2766 /* code==1: set autohandle */
2767 /* code==2: set vectorhandle */
2768 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2769 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2770 /* code==5: Set align, like 3 but no toggle */
2771 /* code==6: Clear align, like 3 but no toggle */
2776 if(code==1 || code==2) {
2777 nu= editnurb->first;
2779 if(nu->type == CU_BEZIER) {
2783 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2784 if(bezt->f1 & SELECT) bezt->h1= code;
2785 if(bezt->f3 & SELECT) bezt->h2= code;
2786 if(bezt->h1!=bezt->h2) {
2787 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2788 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2793 calchandlesNurb(nu);
2799 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2801 nu= editnurb->first;
2804 } else if (code == 6) {
2809 if(nu->type == CU_BEZIER) {
2813 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2814 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2824 nu= editnurb->first;
2826 if(nu->type == CU_BEZIER) {
2830 if(bezt->f1 & SELECT) bezt->h1= ok;
2831 if(bezt->f3 & SELECT) bezt->h2= ok;
2835 calchandlesNurb(nu);
2842 static void swapdata(void *adr1, void *adr2, int len)
2850 memcpy(adr, adr1, len);
2851 memcpy(adr1, adr2, len);
2852 memcpy(adr2, adr, len);
2857 adr= (char *)MEM_mallocN(len, "curve swap");
2858 memcpy(adr, adr1, len);
2859 memcpy(adr1, adr2, len);
2860 memcpy(adr2, adr, len);
2865 void switchdirectionNurb(Nurb *nu)
2867 BezTriple *bezt1, *bezt2;
2869 float *fp1, *fp2, *tempf;
2872 if(nu->pntsu==1 && nu->pntsv==1) return;
2874 if(nu->type == CU_BEZIER) {
2878 if(a & 1) a+= 1; /* if odd, also swap middle content */
2881 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2883 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2884 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2886 SWAP(char, bezt1->h1, bezt1->h2);
2887 SWAP(short, bezt1->f1, bezt1->f3);
2890 SWAP(char, bezt2->h1, bezt2->h2);
2891 SWAP(short, bezt2->f1, bezt2->f3);
2892 bezt1->alfa= -bezt1->alfa;
2893 bezt2->alfa= -bezt2->alfa;
2900 else if(nu->pntsv==1) {
2905 while(bp1!=bp2 && a>0) {
2906 SWAP(BPoint, *bp1, *bp2);
2908 bp1->alfa= -bp1->alfa;
2909 bp2->alfa= -bp2->alfa;
2913 if(nu->type == CU_NURBS) {
2914 /* no knots for too short paths */
2921 while(fp1!=fp2 && a>0) {
2922 SWAP(float, *fp1, *fp2);
2927 /* and make in increasing order again */
2930 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2932 fp2[0]= fabs(fp1[1]-fp1[0]);
2943 fp1[0]= fp1[-1]+fp2[0];