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"
67 #include "BKE_material.h"
75 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
77 float *labda, float *mu, float *vec);
79 void unlink_curve(Curve *cu)
83 for(a=0; a<cu->totcol; a++) {
84 if(cu->mat[a]) cu->mat[a]->id.us--;
87 if(cu->vfont) cu->vfont->id.us--;
90 if(cu->vfontb) cu->vfontb->id.us--;
93 if(cu->vfonti) cu->vfonti->id.us--;
96 if(cu->vfontbi) cu->vfontbi->id.us--;
99 if(cu->key) cu->key->id.us--;
103 /* frees editcurve entirely */
104 void BKE_free_editfont(Curve *cu)
107 EditFont *ef= cu->editfont;
109 if(ef->oldstr) MEM_freeN(ef->oldstr);
110 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
111 if(ef->textbuf) MEM_freeN(ef->textbuf);
112 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
113 if(ef->copybuf) MEM_freeN(ef->copybuf);
114 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
121 /* don't free curve itself */
122 void free_curve(Curve *cu)
124 freeNurblist(&cu->nurb);
125 BLI_freelistN(&cu->bev);
126 freedisplist(&cu->disp);
127 BKE_free_editfont(cu);
129 free_curve_editNurb(cu);
131 BKE_free_animdata((ID *)cu);
133 if(cu->mat) MEM_freeN(cu->mat);
134 if(cu->str) MEM_freeN(cu->str);
135 if(cu->strinfo) MEM_freeN(cu->strinfo);
136 if(cu->bb) MEM_freeN(cu->bb);
137 if(cu->path) free_path(cu->path);
138 if(cu->tb) MEM_freeN(cu->tb);
141 Curve *add_curve(const char *name, int type)
145 cu= alloc_libblock(&G.main->curve, ID_CU, name);
147 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
148 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
150 cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
153 cu->spacing= cu->linedist= 1.0;
156 cu->texflag= CU_AUTOSPACE;
157 cu->smallcaps_scale= 0.75f;
158 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
160 cu->bb= unit_boundbox();
163 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
165 cu->str= MEM_mallocN(12, "str");
166 BLI_strncpy(cu->str, "Text", 12);
168 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
169 cu->totbox= cu->actbox= 1;
170 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
171 cu->tb[0].w = cu->tb[0].h = 0.0;
177 Curve *copy_curve(Curve *cu)
182 cun= copy_libblock(cu);
183 cun->nurb.first= cun->nurb.last= NULL;
184 duplicateNurblist( &(cun->nurb), &(cu->nurb));
186 cun->mat= MEM_dupallocN(cu->mat);
187 for(a=0; a<cun->totcol; a++) {
188 id_us_plus((ID *)cun->mat[a]);
191 cun->str= MEM_dupallocN(cu->str);
192 cun->strinfo= MEM_dupallocN(cu->strinfo);
193 cun->tb= MEM_dupallocN(cu->tb);
194 cun->bb= MEM_dupallocN(cu->bb);
196 cun->key= copy_key(cu->key);
197 if(cun->key) cun->key->from= (ID *)cun;
199 cun->disp.first= cun->disp.last= NULL;
200 cun->bev.first= cun->bev.last= NULL;
207 #if 0 // XXX old animation system
208 /* single user ipo too */
209 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
210 #endif // XXX old animation system
212 id_us_plus((ID *)cun->vfont);
213 id_us_plus((ID *)cun->vfontb);
214 id_us_plus((ID *)cun->vfonti);
215 id_us_plus((ID *)cun->vfontbi);
220 static void extern_local_curve(Curve *cu)
222 id_lib_extern((ID *)cu->vfont);
223 id_lib_extern((ID *)cu->vfontb);
224 id_lib_extern((ID *)cu->vfonti);
225 id_lib_extern((ID *)cu->vfontbi);
228 extern_local_matarar(cu->mat, cu->totcol);
232 void make_local_curve(Curve *cu)
238 /* - when there are only lib users: don't do
239 * - when there are only local users: set flag
243 if(cu->id.lib==NULL) return;
247 cu->id.flag= LIB_LOCAL;
249 new_id(&bmain->curve, (ID *)cu, NULL);
250 extern_local_curve(cu);
254 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
256 if(ob->id.lib) lib= 1;
261 if(local && lib==0) {
263 cu->id.flag= LIB_LOCAL;
265 new_id(&bmain->curve, (ID *)cu, NULL);
266 extern_local_curve(cu);
268 else if(local && lib) {
269 Curve *cun= copy_curve(cu);
272 for(ob= bmain->object.first; ob; ob= ob->id.next) {
274 if(ob->id.lib==NULL) {
284 short curve_type(Curve *cu)
290 for (nu= cu->nurb.first; nu; nu= nu->next) {
299 void test_curve_type(Object *ob)
301 ob->type = curve_type(ob->data);
304 void tex_space_curve(Curve *cu)
308 float *fp, min[3], max[3];
311 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
314 INIT_MINMAX(min, max);
319 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
320 else tot= dl->nr*dl->parts;
325 DO_MINMAX(fp, min, max);
332 min[0] = min[1] = min[2] = -1.0f;
333 max[0] = max[1] = max[2] = 1.0f;
336 boundbox_set_from_min_max(bb, min, max);
338 if(cu->texflag & CU_AUTOSPACE) {
339 mid_v3_v3v3(cu->loc, min, max);
340 cu->size[0]= (max[0]-min[0])/2.0f;
341 cu->size[1]= (max[1]-min[1])/2.0f;
342 cu->size[2]= (max[2]-min[2])/2.0f;
344 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
346 if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
347 else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
348 else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
350 if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
351 else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
352 else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
354 if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
355 else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
356 else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
362 int count_curveverts(ListBase *nurb)
369 if(nu->bezt) tot+= 3*nu->pntsu;
370 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
377 int count_curveverts_without_handles(ListBase *nurb)
384 if(nu->bezt) tot+= nu->pntsu;
385 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
392 /* **************** NURBS ROUTINES ******************** */
394 void freeNurb(Nurb *nu)
399 if(nu->bezt) MEM_freeN(nu->bezt);
401 if(nu->bp) MEM_freeN(nu->bp);
403 if(nu->knotsu) MEM_freeN(nu->knotsu);
405 if(nu->knotsv) MEM_freeN(nu->knotsv);
407 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
414 void freeNurblist(ListBase *lb)
426 lb->first= lb->last= NULL;
429 Nurb *duplicateNurb(Nurb *nu)
434 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
435 if(newnu==NULL) return NULL;
436 memcpy(newnu, nu, sizeof(Nurb));
440 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
441 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
444 len= nu->pntsu*nu->pntsv;
446 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
447 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
449 newnu->knotsu= newnu->knotsv= NULL;
454 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
455 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
458 if(nu->pntsv>1 && nu->knotsv) {
461 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
462 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
469 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
477 nun= duplicateNurb(nu);
478 BLI_addtail(lb1, nun);
484 void test2DNurb(Nurb *nu)
490 if((nu->flag & CU_2D)==0)
493 if(nu->type == CU_BEZIER) {
497 bezt->vec[0][2]= 0.0;
498 bezt->vec[1][2]= 0.0;
499 bezt->vec[2][2]= 0.0;
504 a= nu->pntsu*nu->pntsv;
513 void minmaxNurb(Nurb *nu, float *min, float *max)
519 if(nu->type == CU_BEZIER) {
523 DO_MINMAX(bezt->vec[0], min, max);
524 DO_MINMAX(bezt->vec[1], min, max);
525 DO_MINMAX(bezt->vec[2], min, max);
530 a= nu->pntsu*nu->pntsv;
533 DO_MINMAX(bp->vec, min, max);
539 /* be sure to call makeknots after this */
540 void addNurbPoints(Nurb *nu, int number)
544 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
547 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
551 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
553 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
560 void addNurbPointsBezier(Nurb *nu, int number)
562 BezTriple *tmp= nu->bezt;
564 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
567 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
571 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
573 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
580 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
583 static void calcknots(float *knots, short aantal, short order, short type)
584 /* knots: number of pnts NOT corrected for cyclic */
585 /* type; 0: uniform, 1: endpoints, 2: bezier */
601 if(a>=order && a<=aantal) k+= 1.0f;
605 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
616 if(a>=order && a<=aantal) k+= 0.5f;
621 printf("bez nurb curve order is not 3 or 4, should never happen\n");
626 static void makecyclicknots(float *knots, short pnts, short order)
627 /* pnts, order: number of pnts NOT corrected for cyclic */
631 if(knots==NULL) return;
635 /* do first long rows (order -1), remove identical knots at endpoints */
638 for(a=1; a<order2; a++) {
639 if(knots[b]!= knots[b-a]) break;
641 if(a==order2) knots[pnts+order-2]+= 1.0f;
645 c=pnts + order + order2;
646 for(a=pnts+order2; a<c; a++) {
647 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
654 static void makeknots(Nurb *nu, short uv)
656 if(nu->type == CU_NURBS) {
658 if(nu->knotsu) MEM_freeN(nu->knotsu);
659 if(check_valid_nurb_u(nu)) {
660 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
661 if(nu->flagu & CU_NURB_CYCLIC) {
662 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
663 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
665 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
668 else nu->knotsu= NULL;
671 if(nu->knotsv) MEM_freeN(nu->knotsv);
672 if(check_valid_nurb_v(nu)) {
673 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
674 if(nu->flagv & CU_NURB_CYCLIC) {
675 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
676 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
678 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
681 else nu->knotsv= NULL;
686 void nurbs_knot_calc_u(Nurb *nu)
691 void nurbs_knot_calc_v(Nurb *nu)
696 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
699 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
701 orderpluspnts= order+pnts;
702 opp2 = orderpluspnts-1;
704 /* this is for float inaccuracy */
705 if(t < knots[0]) t= knots[0];
706 else if(t > knots[opp2]) t= knots[opp2];
708 /* this part is order '1' */
710 for(i=0;i<opp2;i++) {
711 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
727 /* this is order 2,3,... */
728 for(j=2; j<=order; j++) {
730 if(i2+j>= orderpluspnts) i2= opp2-j;
732 for(i= i1; i<=i2; i++) {
734 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
738 if(basis[i+1] != 0.0f)
739 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
750 for(i=i1; i<=i2; i++) {
751 if(basis[i] > 0.0f) {
753 if(*start==1000) *start= i;
759 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
760 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
763 float *basisu, *basis, *basisv, *sum, *fp, *in;
764 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
765 int i, j, iofs, jofs, cycl, len, curu, curv;
766 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
768 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
770 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
771 if(nu->orderu>nu->pntsu) return;
772 if(nu->orderv>nu->pntsv) return;
773 if(coord_array==NULL) return;
775 /* allocate and initialize */
781 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
790 i= nu->pntsu*nu->pntsv;
793 if(bp->vec[3] != 1.0f) {
801 ustart= fp[nu->orderu-1];
802 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
803 else uend= fp[nu->pntsu];
804 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
806 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
809 vstart= fp[nu->orderv-1];
811 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
812 else vend= fp[nu->pntsv];
813 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
816 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
817 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
818 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
820 /* precalculation of basisv and jstart,jend */
821 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
827 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
832 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
839 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
852 for(j= jsta; j<=jen; j++) {
854 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
856 bp= nu->bp+ nu->pntsu*jofs+istart-1;
858 for(i= istart; i<=iend; i++, fp++) {
862 bp= nu->bp+ nu->pntsu*jofs+iofs;
867 *fp= basisu[i]*basis[j]*bp->vec[3];
870 else *fp= basisu[i]*basis[j];
876 for(j= jsta; j<=jen; j++) {
877 for(i= istart; i<=iend; i++, fp++) {
883 /* one! (1.0) real point now */
885 for(j= jsta; j<=jen; j++) {
887 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
889 bp= nu->bp+ nu->pntsu*jofs+istart-1;
891 for(i= istart; i<=iend; i++, fp++) {
895 bp= nu->bp+ nu->pntsu*jofs+iofs;
900 in[0]+= (*fp) * bp->vec[0];
901 in[1]+= (*fp) * bp->vec[1];
902 in[2]+= (*fp) * bp->vec[2];
911 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
922 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
923 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
924 * tilt_array and radius_array will be written to if valid */
927 float u, ustart, uend, ustep, sumdiv;
928 float *basisu, *sum, *fp;
929 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
930 int i, len, istart, iend, cycl;
932 if(nu->knotsu==NULL) return;
933 if(nu->orderu>nu->pntsu) return;
934 if(coord_array==NULL) return;
936 /* allocate and initialize */
939 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
941 resolu= (resolu*SEGMENTSU(nu));
949 ustart= fp[nu->orderu-1];
950 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
951 else uend= fp[nu->pntsu];
952 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
954 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
956 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
962 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
966 bp= nu->bp+ istart-1;
967 for(i= istart; i<=iend; i++, fp++) {
969 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
972 *fp= basisu[i]*bp->vec[3];
975 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
976 /* is normalizing needed? */
978 for(i= istart; i<=iend; i++, fp++) {
983 /* one! (1.0) real point */
985 bp= nu->bp+ istart-1;
986 for(i= istart; i<=iend; i++, fp++) {
988 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
993 coord_fp[0]+= (*fp) * bp->vec[0];
994 coord_fp[1]+= (*fp) * bp->vec[1];
995 coord_fp[2]+= (*fp) * bp->vec[2];
998 (*tilt_fp) += (*fp) * bp->alfa;
1001 (*radius_fp) += (*fp) * bp->radius;
1004 (*weight_fp) += (*fp) * bp->weight;
1009 coord_fp = (float *)(((char *)coord_fp) + stride);
1011 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1012 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1013 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1023 /* forward differencing method for bezier curve */
1024 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1026 float rt0,rt1,rt2,rt3,f;
1031 rt1= 3.0f*(q1-q0)/f;
1033 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1035 rt3= (q3-q0+3.0f*(q1-q2))/f;
1042 for(a=0; a<=it; a++) {
1044 p = (float *)(((char *)p)+stride);
1051 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1053 /* note that these are not purpendicular to the curve
1054 * they need to be rotated for this,
1056 * This could also be optimized like forward_diff_bezier */
1058 for(a=0; a<=it; a++) {
1059 float t = (float)a / (float)it;
1062 for(i=0; i<3; i++) {
1063 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1066 p = (float *)(((char *)p)+stride);
1070 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1072 float *make_orco_surf(Object *ob)
1074 /* Note: this function is used in convertblender only atm, so
1075 * suppose nonzero curve's render resolution should always be used */
1076 Curve *cu= ob->data;
1081 float *fp, *coord_array;
1083 /* first calculate the size of the datablock */
1086 /* as we want to avoid the seam in a cyclic nurbs
1087 texture wrapping, reserve extra orco data space to save these extra needed
1088 vertex based UV coordinates for the meridian vertices.
1089 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1090 the renderface/vert construction.
1092 See also convertblender.c: init_render_surf()
1095 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1096 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1098 sizeu = nu->pntsu*resolu;
1099 sizev = nu->pntsv*resolv;
1100 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1101 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1102 if(nu->pntsv>1) tot+= sizeu * sizev;
1106 /* makeNurbfaces wants zeros */
1107 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1111 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1112 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1115 sizeu = nu->pntsu*resolu;
1116 sizev = nu->pntsv*resolv;
1117 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1118 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1120 if(cu->flag & CU_UV_ORCO) {
1121 for(b=0; b< sizeu; b++) {
1122 for(a=0; a< sizev; a++) {
1124 if(sizev <2) fp[0]= 0.0f;
1125 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1127 if(sizeu <2) fp[1]= 0.0f;
1128 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1137 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1138 float *tdata= _tdata;
1140 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1142 for(b=0; b<sizeu; b++) {
1144 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1147 for(a=0; a<sizev; a++) {
1149 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1152 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1154 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1155 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1156 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1171 /* NOTE: This routine is tied to the order of vertex
1172 * built by displist and as passed to the renderer.
1174 float *make_orco_curve(Scene *scene, Object *ob)
1176 Curve *cu = ob->data;
1179 float *fp, *coord_array;
1180 ListBase disp = {NULL, NULL};
1182 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1185 for (dl=disp.first; dl; dl=dl->next) {
1186 if (dl->type==DL_INDEX3) {
1188 } else if (dl->type==DL_SURF) {
1189 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1190 if (dl->flag & DL_CYCL_U) {
1191 if (dl->flag & DL_CYCL_V)
1192 numVerts += (dl->parts+1)*(dl->nr+1);
1194 numVerts += dl->parts*(dl->nr+1);
1197 numVerts += dl->parts*dl->nr;
1201 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1202 for (dl=disp.first; dl; dl=dl->next) {
1203 if (dl->type==DL_INDEX3) {
1204 for (u=0; u<dl->nr; u++, fp+=3) {
1205 if (cu->flag & CU_UV_ORCO) {
1206 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1210 VECCOPY(fp, &dl->verts[u*3]);
1212 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1213 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1214 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1217 } else if (dl->type==DL_SURF) {
1218 int sizeu= dl->nr, sizev= dl->parts;
1220 /* exception as handled in convertblender.c too */
1221 if (dl->flag & DL_CYCL_U) {
1223 if (dl->flag & DL_CYCL_V)
1227 for (u=0; u<sizev; u++) {
1228 for (v=0; v<sizeu; v++,fp+=3) {
1229 if (cu->flag & CU_UV_ORCO) {
1230 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1231 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1235 int realv= v % dl->nr;
1236 int realu= u % dl->parts;
1238 vert= dl->verts + 3*(dl->nr*realu + realv);
1241 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1242 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1243 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1250 freedisplist(&disp);
1256 /* ***************** BEVEL ****************** */
1258 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1260 DispList *dl, *dlnew;
1262 float *fp, facx, facy, angle, dangle;
1266 disp->first = disp->last = NULL;
1268 /* if a font object is being edited, then do nothing */
1269 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1272 if (cu->bevobj->type!=OB_CURVE) return;
1274 bevcu= cu->bevobj->data;
1275 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1276 ListBase bevdisp= {NULL, NULL};
1277 facx= cu->bevobj->size[0];
1278 facy= cu->bevobj->size[1];
1281 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1284 dl= cu->bevobj->disp.first;
1286 makeDispListCurveTypes(scene, cu->bevobj, 0);
1287 dl= cu->bevobj->disp.first;
1292 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1293 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1295 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1296 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1298 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1300 BLI_addtail(disp, dlnew);
1302 nr= dlnew->parts*dlnew->nr;
1313 freedisplist(&bevdisp);
1316 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1319 else if(cu->ext2==0.0f) {
1320 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1321 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1322 BLI_addtail(disp, dl);
1325 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1334 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1336 nr= 4+ 2*cu->bevresol;
1338 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1339 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1340 BLI_addtail(disp, dl);
1343 dl->flag= DL_BACK_CURVE;
1348 dangle= (2.0f*(float)M_PI/(nr));
1349 angle= -(nr-1)*dangle;
1351 for(a=0; a<nr; a++) {
1353 fp[1]= (cosf(angle)*(cu->ext2));
1354 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1362 /* bevel now in three parts, for proper vertex normals */
1365 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1366 dnr= nr= 2+ cu->bevresol;
1367 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1368 nr= 3+ 2*cu->bevresol;
1370 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1371 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1372 BLI_addtail(disp, dl);
1375 dl->flag= DL_BACK_CURVE;
1380 dangle= (0.5*M_PI/(dnr-1));
1381 angle= -(nr-1)*dangle;
1383 for(a=0; a<nr; a++) {
1385 fp[1]= (float)(cosf(angle)*(cu->ext2));
1386 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1392 /* part 2, sidefaces */
1393 if(cu->ext1!=0.0f) {
1396 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1397 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1398 BLI_addtail(disp, dl);
1409 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1410 dl= MEM_dupallocN(dl);
1411 dl->verts= MEM_dupallocN(dl->verts);
1412 BLI_addtail(disp, dl);
1423 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1424 dnr= nr= 2+ cu->bevresol;
1425 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1426 nr= 3+ 2*cu->bevresol;
1428 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1429 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1430 BLI_addtail(disp, dl);
1432 dl->flag= DL_FRONT_CURVE;
1439 dangle= (0.5*M_PI/(dnr-1));
1441 for(a=0; a<nr; a++) {
1443 fp[1]= (float)(cosf(angle)*(cu->ext2));
1444 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1452 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1456 0: no intersection of segments
1457 1: exact intersection of segments
1458 2: cross-intersection of segments
1462 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1463 if(deler==0.0f) return -1;
1465 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1466 *labda= -(*labda/deler);
1468 deler= v3[coy]-v4[coy];
1470 deler=v3[cox]-v4[cox];
1471 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1473 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1475 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1476 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1478 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1479 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1486 static short bevelinside(BevList *bl1,BevList *bl2)
1488 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1489 /* returns '1' if correct hole */
1490 BevPoint *bevp, *prevbevp;
1491 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1492 int nr, links=0,rechts=0,mode;
1494 /* take first vertex of possible hole */
1496 bevp= (BevPoint *)(bl2+1);
1497 hvec1[0]= bevp->vec[0];
1498 hvec1[1]= bevp->vec[1];
1500 VECCOPY(hvec2,hvec1);
1503 /* test it with all edges of potential surounding poly */
1504 /* count number of transitions left-right */
1506 bevp= (BevPoint *)(bl1+1);
1508 prevbevp= bevp+(nr-1);
1511 min= prevbevp->vec[1];
1515 max= prevbevp->vec[1];
1518 if(min<=hvec1[1] && max>=hvec1[1]) {
1519 /* there's a transition, calc intersection point */
1520 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1521 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1522 only allow for one situation: we choose lab= 1.0
1524 if(mode >= 0 && lab != 0.0f) {
1525 if(vec[0]<hvec1[0]) links++;
1534 if( (links & 1) && (rechts & 1) ) return 1;
1545 static int vergxcobev(const void *a1, const void *a2)
1547 const struct bevelsort *x1=a1,*x2=a2;
1549 if( x1->left > x2->left ) return 1;
1550 else if( x1->left < x2->left) return -1;
1554 /* this function cannot be replaced with atan2, but why? */
1556 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1558 float t01, t02, x3, y3;
1560 t01= (float)sqrt(x1*x1+y1*y1);
1561 t02= (float)sqrt(x2*x2+y2*y2);
1562 if(t01==0.0f) t01= 1.0f;
1563 if(t02==0.0f) t02= 1.0f;
1571 if(fabs(t02)>=1.0) t02= .5*M_PI;
1572 else t02= (saacos(t02))/2.0f;
1574 t02= (float)sin(t02);
1575 if(t02==0.0f) t02= 1.0f;
1579 if(x3==0 && y3==0) {
1583 t01= (float)sqrt(x3*x3+y3*y3);
1593 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1595 BezTriple *pprev, *next, *last;
1596 float fac, dfac, t[4];
1599 if(tilt_array==NULL && radius_array==NULL)
1602 last= nu->bezt+(nu->pntsu-1);
1604 /* returns a point */
1605 if(prevbezt==nu->bezt) {
1606 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1607 else pprev= prevbezt;
1609 else pprev= prevbezt-1;
1613 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1619 dfac= 1.0f/(float)resolu;
1621 for(a=0; a<resolu; a++, fac+= dfac) {
1623 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1624 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1626 key_curve_position_weights(fac, t, nu->tilt_interp);
1627 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1630 tilt_array = (float *)(((char *)tilt_array) + stride);
1634 if (nu->radius_interp==KEY_CU_EASE) {
1635 /* Support 2.47 ease interp
1636 * Note! - this only takes the 2 points into account,
1637 * giving much more localized results to changes in radius, sometimes you want that */
1638 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1641 /* reuse interpolation from tilt if we can */
1642 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1643 key_curve_position_weights(fac, t, nu->radius_interp);
1645 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1648 radius_array = (float *)(((char *)radius_array) + stride);
1652 /* basic interpolation for now, could copy tilt interp too */
1653 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1655 weight_array = (float *)(((char *)weight_array) + stride);
1660 /* make_bevel_list_3D_* funcs, at a minimum these must
1661 * fill in the bezp->quat and bezp->dir values */
1663 /* correct non-cyclic cases by copying direction and rotation
1664 * values onto the first & last end-points */
1665 static void bevel_list_cyclic_fix_3D(BevList *bl)
1667 BevPoint *bevp, *bevp1;
1669 bevp= (BevPoint *)(bl+1);
1671 QUATCOPY(bevp->quat, bevp1->quat);
1672 VECCOPY(bevp->dir, bevp1->dir);
1673 VECCOPY(bevp->tan, bevp1->tan);
1674 bevp= (BevPoint *)(bl+1);
1677 QUATCOPY(bevp->quat, bevp1->quat);
1678 VECCOPY(bevp->dir, bevp1->dir);
1679 VECCOPY(bevp->tan, bevp1->tan);
1681 /* utility for make_bevel_list_3D_* funcs */
1682 static void bevel_list_calc_bisect(BevList *bl)
1684 BevPoint *bevp2, *bevp1, *bevp0;
1687 bevp2= (BevPoint *)(bl+1);
1688 bevp1= bevp2+(bl->nr-1);
1693 /* totally simple */
1694 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1701 static void bevel_list_flip_tangents(BevList *bl)
1703 BevPoint *bevp2, *bevp1, *bevp0;
1706 bevp2= (BevPoint *)(bl+1);
1707 bevp1= bevp2+(bl->nr-1);
1712 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1713 negate_v3(bevp1->tan);
1720 /* apply user tilt */
1721 static void bevel_list_apply_tilt(BevList *bl)
1723 BevPoint *bevp2, *bevp1;
1727 bevp2= (BevPoint *)(bl+1);
1728 bevp1= bevp2+(bl->nr-1);
1732 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1733 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1734 normalize_qt(bevp1->quat);
1740 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1741 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1743 BevPoint *bevp2, *bevp1, *bevp0;
1747 float bevp0_quat[4];
1750 for(a=0; a < smooth_iter; a++) {
1752 bevp2= (BevPoint *)(bl+1);
1753 bevp1= bevp2+(bl->nr-1);
1758 if(bl->poly== -1) { /* check its not cyclic */
1759 /* skip the first point */
1772 QUATCOPY(bevp0_quat, bevp0->quat);
1775 /* interpolate quats */
1776 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1777 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1780 mul_qt_v3(q, zaxis);
1781 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1782 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1785 QUATCOPY(bevp0_quat, bevp1->quat);
1786 mul_qt_qtqt(q, q2, q);
1787 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1788 normalize_qt(bevp1->quat);
1791 /* bevp0= bevp1; */ /* UNUSED */
1798 static void make_bevel_list_3D_zup(BevList *bl)
1800 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1803 bevp2= (BevPoint *)(bl+1);
1804 bevp1= bevp2+(bl->nr-1);
1809 /* totally simple */
1810 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1811 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1819 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1821 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1825 bevel_list_calc_bisect(bl);
1827 bevp2= (BevPoint *)(bl+1);
1828 bevp1= bevp2+(bl->nr-1);
1834 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1835 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1838 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1840 if(angle > 0.0f) { /* otherwise we can keep as is */
1842 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1843 axis_angle_to_quat(q, cross_tmp, angle);
1844 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1847 QUATCOPY(bevp1->quat, bevp0->quat);
1856 if(bl->poly != -1) { /* check for cyclic */
1858 /* Need to correct for the start/end points not matching
1859 * do this by calculating the tilt angle difference, then apply
1860 * the rotation gradually over the entire curve
1862 * note that the split is between last and second last, rather than first/last as youd expect.
1864 * real order is like this
1865 * 0,1,2,3,4 --> 1,2,3,4,0
1867 * this is why we compare last with second last
1869 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1871 BevPoint *bevp_first;
1872 BevPoint *bevp_last;
1875 bevp_first= (BevPoint *)(bl+1);
1876 bevp_first+= bl->nr-1;
1877 bevp_last = bevp_first;
1880 /* quats and vec's are normalized, should not need to re-normalize */
1881 mul_qt_v3(bevp_first->quat, vec_1);
1882 mul_qt_v3(bevp_last->quat, vec_2);
1883 normalize_v3(vec_1);
1884 normalize_v3(vec_2);
1886 /* align the vector, can avoid this and it looks 98% OK but
1887 * better to align the angle quat roll's before comparing */
1889 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1890 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1891 axis_angle_to_quat(q, cross_tmp, angle);
1892 mul_qt_v3(q, vec_2);
1895 angle= angle_normalized_v3v3(vec_1, vec_2);
1897 /* flip rotation if needs be */
1898 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1899 normalize_v3(cross_tmp);
1900 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90.0f/(float)(180.0/M_PI))
1903 bevp2= (BevPoint *)(bl+1);
1904 bevp1= bevp2+(bl->nr-1);
1909 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1911 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1912 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1921 static void make_bevel_list_3D_tangent(BevList *bl)
1923 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1926 float bevp0_tan[3], cross_tmp[3];
1928 bevel_list_calc_bisect(bl);
1929 if(bl->poly== -1) /* check its not cyclic */
1930 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1931 bevel_list_flip_tangents(bl);
1933 /* correct the tangents */
1934 bevp2= (BevPoint *)(bl+1);
1935 bevp1= bevp2+(bl->nr-1);
1941 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1942 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1943 normalize_v3(bevp1->tan);
1951 /* now for the real twist calc */
1952 bevp2= (BevPoint *)(bl+1);
1953 bevp1= bevp2+(bl->nr-1);
1956 VECCOPY(bevp0_tan, bevp0->tan);
1961 /* make perpendicular, modify tan in place, is ok */
1963 float zero[3] = {0,0,0};
1965 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1966 normalize_v3(cross_tmp);
1967 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1969 /* bevp0= bevp1; */ /* UNUSED */
1975 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1977 switch(twist_mode) {
1978 case CU_TWIST_TANGENT:
1979 make_bevel_list_3D_tangent(bl);
1981 case CU_TWIST_MINIMUM:
1982 make_bevel_list_3D_minimum_twist(bl);
1984 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1985 make_bevel_list_3D_zup(bl);
1988 if(bl->poly== -1) /* check its not cyclic */
1989 bevel_list_cyclic_fix_3D(bl);
1992 bevel_list_smooth(bl, smooth_iter);
1994 bevel_list_apply_tilt(bl);
1999 /* only for 2 points */
2000 static void make_bevel_list_segment_3D(BevList *bl)
2004 BevPoint *bevp2= (BevPoint *)(bl+1);
2005 BevPoint *bevp1= bevp2+1;
2007 /* simple quat/dir */
2008 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2009 normalize_v3(bevp1->dir);
2011 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2013 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2014 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2015 normalize_qt(bevp1->quat);
2016 VECCOPY(bevp2->dir, bevp1->dir);
2017 QUATCOPY(bevp2->quat, bevp1->quat);
2022 void makeBevelList(Object *ob)
2025 - convert all curves to polys, with indication of resol and flags for double-vertices
2026 - possibly; do a smart vertice removal (in case Nurb)
2027 - separate in individual blicks with BoundBox
2028 - AutoHole detection
2032 BezTriple *bezt, *prevbezt;
2034 BevList *bl, *blnew, *blnext;
2035 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2036 float min, inp, x1, x2, y1, y2;
2037 struct bevelsort *sortdata, *sd, *sd1;
2038 int a, b, nr, poly, resolu = 0, len = 0;
2039 int do_tilt, do_radius, do_weight;
2041 /* this function needs an object, because of tflag and upflag */
2044 /* do we need to calculate the radius for each point? */
2045 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2047 /* STEP 1: MAKE POLYS */
2049 BLI_freelistN(&(cu->bev));
2050 if(cu->editnurb && ob->type!=OB_FONT) {
2051 ListBase *nurbs= ED_curve_editnurbs(cu);
2053 } else nu= cu->nurb.first;
2057 /* check if we will calculate tilt data */
2058 do_tilt = CU_DO_TILT(cu, nu);
2059 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2062 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2063 * enforced in the UI but can go wrong possibly */
2064 if(!check_valid_nurb_u(nu)) {
2065 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2066 BLI_addtail(&(cu->bev), bl);
2069 if(G.rendering && cu->resolu_ren!=0)
2070 resolu= cu->resolu_ren;
2074 if(nu->type == CU_POLY) {
2076 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2077 BLI_addtail(&(cu->bev), bl);
2079 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2083 bevp= (BevPoint *)(bl+1);
2087 VECCOPY(bevp->vec, bp->vec);
2088 bevp->alfa= bp->alfa;
2089 bevp->radius= bp->radius;
2090 bevp->weight= bp->weight;
2091 bevp->split_tag= TRUE;
2096 else if(nu->type == CU_BEZIER) {
2098 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2099 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2100 BLI_addtail(&(cu->bev), bl);
2102 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2104 bevp= (BevPoint *)(bl+1);
2108 if(nu->flagu & CU_NURB_CYCLIC) {
2110 prevbezt= nu->bezt+(nu->pntsu-1);
2118 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2120 VECCOPY(bevp->vec, prevbezt->vec[1]);
2121 bevp->alfa= prevbezt->alfa;
2122 bevp->radius= prevbezt->radius;
2123 bevp->weight= prevbezt->weight;
2124 bevp->split_tag= TRUE;
2125 bevp->dupe_tag= FALSE;
2131 /* always do all three, to prevent data hanging around */
2134 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2135 for(j=0; j<3; j++) {
2136 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2137 bezt->vec[0][j], bezt->vec[1][j],
2138 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2141 /* if both arrays are NULL do nothiong */
2142 alfa_bezpart( prevbezt, bezt, nu,
2143 do_tilt ? &bevp->alfa : NULL,
2144 do_radius ? &bevp->radius : NULL,
2145 do_weight ? &bevp->weight : NULL,
2146 resolu, sizeof(BevPoint));
2149 if(cu->twist_mode==CU_TWIST_TANGENT) {
2150 forward_diff_bezier_cotangent(
2151 prevbezt->vec[1], prevbezt->vec[2],
2152 bezt->vec[0], bezt->vec[1],
2153 bevp->tan, resolu, sizeof(BevPoint));
2156 /* indicate with handlecodes double points */
2157 if(prevbezt->h1==prevbezt->h2) {
2158 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2161 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2162 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2171 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2172 VECCOPY(bevp->vec, prevbezt->vec[1]);
2173 bevp->alfa= prevbezt->alfa;
2174 bevp->radius= prevbezt->radius;
2175 bevp->weight= prevbezt->weight;
2179 else if(nu->type == CU_NURBS) {
2181 len= (resolu*SEGMENTSU(nu));
2183 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2184 BLI_addtail(&(cu->bev), bl);
2187 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2189 bevp= (BevPoint *)(bl+1);
2191 makeNurbcurve( nu, &bevp->vec[0],
2192 do_tilt ? &bevp->alfa : NULL,
2193 do_radius ? &bevp->radius : NULL,
2194 do_weight ? &bevp->weight : NULL,
2195 resolu, sizeof(BevPoint));
2202 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2205 if (bl->nr) { /* null bevel items come from single points */
2207 bevp1= (BevPoint *)(bl+1);
2208 bevp0= bevp1+(nr-1);
2211 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2212 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2213 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2214 bevp0->dupe_tag= TRUE;
2228 if(bl->nr && bl->dupe_nr) {
2229 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2230 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2231 memcpy(blnew, bl, sizeof(BevList));
2233 BLI_remlink(&(cu->bev), bl);
2234 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2235 bevp0= (BevPoint *)(bl+1);
2236 bevp1= (BevPoint *)(blnew+1);
2239 if(bevp0->dupe_tag==0) {
2240 memcpy(bevp1, bevp0, sizeof(BevPoint));
2252 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2256 if(bl->nr && bl->poly>=0) {
2265 /* find extreme left points, also test (turning) direction */
2267 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2273 bevp= (BevPoint *)(bl+1);
2276 if(min>bevp->vec[0]) {
2285 bevp= (BevPoint *)(bl+1);
2286 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2287 else bevp0= bevp1-1;
2288 bevp= bevp+ (bl->nr-1);
2289 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2290 else bevp2= bevp1+1;
2292 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]);
2294 if(inp > 0.0f) sd->dir= 1;
2302 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2305 for(a=1; a<poly; a++, sd++) {
2306 bl= sd->bl; /* is bl a hole? */
2307 sd1= sortdata+ (a-1);
2308 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2309 if(bevelinside(sd1->bl, bl)) {
2310 bl->hole= 1- sd1->bl->hole;
2316 /* turning direction */
2317 if((cu->flag & CU_3D)==0) {
2319 for(a=0; a<poly; a++, sd++) {
2320 if(sd->bl->hole==sd->dir) {
2322 bevp1= (BevPoint *)(bl+1);
2323 bevp2= bevp1+ (bl->nr-1);
2326 SWAP(BevPoint, *bevp1, *bevp2);
2333 MEM_freeN(sortdata);
2336 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2337 if((cu->flag & CU_3D)==0) {
2338 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2339 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2346 else if(bl->nr==2) { /* 2 pnt, treat separate */
2347 bevp2= (BevPoint *)(bl+1);
2350 x1= bevp1->vec[0]- bevp2->vec[0];
2351 y1= bevp1->vec[1]- bevp2->vec[1];
2353 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2354 bevp2->sina= bevp1->sina;
2355 bevp2->cosa= bevp1->cosa;
2357 /* fill in dir & quat */
2358 make_bevel_list_segment_3D(bl);
2361 bevp2= (BevPoint *)(bl+1);
2362 bevp1= bevp2+(bl->nr-1);
2367 x1= bevp1->vec[0]- bevp0->vec[0];
2368 x2= bevp1->vec[0]- bevp2->vec[0];
2369 y1= bevp1->vec[1]- bevp0->vec[1];
2370 y2= bevp1->vec[1]- bevp2->vec[1];
2372 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2374 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2375 * no need for tricky tilt calculation as with 3D curves */
2376 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2377 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2378 /* done with inline make_bevel_list_3D_zup */
2385 /* correct non-cyclic cases */
2387 bevp= (BevPoint *)(bl+1);
2389 bevp->sina= bevp1->sina;
2390 bevp->cosa= bevp1->cosa;
2391 bevp= (BevPoint *)(bl+1);
2394 bevp->sina= bevp1->sina;
2395 bevp->cosa= bevp1->cosa;
2397 /* correct for the dir/quat, see above why its needed */
2398 bevel_list_cyclic_fix_3D(bl);
2404 else { /* 3D Curves */
2411 else if(bl->nr==2) { /* 2 pnt, treat separate */
2412 make_bevel_list_segment_3D(bl);
2415 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2422 /* ****************** HANDLES ************** */
2426 * 0: nothing, 1:auto, 2:vector, 3:aligned
2429 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2430 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2432 float *p1,*p2,*p3, pt[3];
2433 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2434 const float eps= 1e-5;
2436 if(bezt->h1==0 && bezt->h2==0) return;
2442 pt[0]= 2*p2[0]- p3[0];
2443 pt[1]= 2*p2[1]- p3[1];
2444 pt[2]= 2*p2[2]- p3[2];
2447 else p1= prev->vec[1];
2450 pt[0]= 2*p2[0]- p1[0];
2451 pt[1]= 2*p2[1]- p1[1];
2452 pt[2]= 2*p2[2]- p1[2];
2455 else p3= next->vec[1];
2462 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2469 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2471 if(len1==0.0f) len1=1.0f;
2472 if(len2==0.0f) len2=1.0f;
2475 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2476 vx= dx1/len2 + dx/len1;
2477 vy= dy1/len2 + dy/len1;
2478 vz= dz1/len2 + dz/len1;
2479 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2481 int leftviolate=0, rightviolate=0; /* for mode==2 */
2483 if(len1>5.0f*len2) len1= 5.0f*len2;
2484 if(len2>5.0f*len1) len2= 5.0f*len1;
2486 if(bezt->h1==HD_AUTO) {
2488 *(p2-3)= *p2-vx*len1;
2489 *(p2-2)= *(p2+1)-vy*len1;
2490 *(p2-1)= *(p2+2)-vz*len1;
2492 if(mode==2 && next && prev) { // keep horizontal if extrema
2493 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2494 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2495 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2496 bezt->vec[0][1]= bezt->vec[1][1];
2498 else { // handles should not be beyond y coord of two others
2499 if(ydiff1 <= 0.0f) {
2500 if(prev->vec[1][1] > bezt->vec[0][1]) {
2501 bezt->vec[0][1]= prev->vec[1][1];
2506 if(prev->vec[1][1] < bezt->vec[0][1]) {
2507 bezt->vec[0][1]= prev->vec[1][1];
2514 if(bezt->h2==HD_AUTO) {
2516 *(p2+3)= *p2+vx*len2;
2517 *(p2+4)= *(p2+1)+vy*len2;
2518 *(p2+5)= *(p2+2)+vz*len2;
2520 if(mode==2 && next && prev) { // keep horizontal if extrema
2521 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2522 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2523 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2524 bezt->vec[2][1]= bezt->vec[1][1];
2526 else { // handles should not be beyond y coord of two others
2527 if(ydiff1 <= 0.0f) {
2528 if(next->vec[1][1] < bezt->vec[2][1]) {
2529 bezt->vec[2][1]= next->vec[1][1];
2534 if(next->vec[1][1] > bezt->vec[2][1]) {
2535 bezt->vec[2][1]= next->vec[1][1];
2542 if(leftviolate || rightviolate) { /* align left handle */
2545 sub_v3_v3v3(h1, p2-3, p2);
2546 sub_v3_v3v3(h2, p2, p2+3);
2547 len1= normalize_v3(h1);
2548 len2= normalize_v3(h2);
2553 *(p2+3)= *(p2) - vz*len2*h1[0];
2554 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2555 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2558 *(p2-3)= *(p2) + vz*len1*h2[0];
2559 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2560 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2567 if(bezt->h1==HD_VECT) { /* vector */
2572 *(p2-2)= *(p2+1)-dy;
2573 *(p2-1)= *(p2+2)-dz;
2575 if(bezt->h2==HD_VECT) {
2580 *(p2+4)= *(p2+1)+dy1;
2581 *(p2+5)= *(p2+2)+dz1;
2584 len2= len_v3v3(p2, p2+3);
2585 len1= len_v3v3(p2, p2-3);
2586 if(len1==0.0f) len1= 1.0f;
2587 if(len2==0.0f) len2= 1.0f;
2589 if(bezt->f1 & SELECT) { /* order of calculation */
2590 if(bezt->h2==HD_ALIGN) { /* aligned */
2593 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2594 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2595 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2598 if(bezt->h1==HD_ALIGN) {
2601 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2602 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2603 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2608 if(bezt->h1==HD_ALIGN) {
2611 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2612 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2613 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2616 if(bezt->h2==HD_ALIGN) { /* aligned */
2619 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2620 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2621 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2627 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2629 BezTriple *bezt, *prev, *next;
2632 if(nu->type != CU_BEZIER) return;
2633 if(nu->pntsu<2) return;
2637 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2642 calchandleNurb(bezt, prev, next, 0);
2645 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2655 void testhandlesNurb(Nurb *nu)
2657 /* use when something has changed with handles.
2658 it treats all BezTriples with the following rules:
2659 PHASE 1: do types have to be altered?
2660 Auto handles: become aligned when selection status is NOT(000 || 111)
2661 Vector handles: become 'nothing' when (one half selected AND other not)
2662 PHASE 2: recalculate handles
2667 if(nu->type != CU_BEZIER) return;
2673 if(bezt->f1 & SELECT) flag++;
2674 if(bezt->f2 & SELECT) flag += 2;
2675 if(bezt->f3 & SELECT) flag += 4;
2677 if( !(flag==0 || flag==7) ) {
2678 if(bezt->h1==HD_AUTO) { /* auto */
2681 if(bezt->h2==HD_AUTO) { /* auto */
2685 if(bezt->h1==HD_VECT) { /* vector */
2686 if(flag < 4) bezt->h1= 0;
2688 if(bezt->h2==HD_VECT) { /* vector */
2689 if( flag > 3) bezt->h2= 0;
2695 calchandlesNurb(nu);
2698 void autocalchandlesNurb(Nurb *nu, int flag)
2700 /* checks handle coordinates and calculates type */
2702 BezTriple *bezt2, *bezt1, *bezt0;
2703 int i, align, leftsmall, rightsmall;
2705 if(nu==NULL || nu->bezt==NULL) return;
2708 bezt1 = bezt2 + (nu->pntsu-1);
2714 align= leftsmall= rightsmall= 0;
2717 if(flag==0 || (bezt1->f1 & flag) ) {
2719 /* distance too short: vectorhandle */
2720 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2725 /* aligned handle? */
2726 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2728 bezt1->h1= HD_ALIGN;
2730 /* or vector handle? */
2731 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2737 if(flag==0 || (bezt1->f3 & flag) ) {
2739 /* distance too short: vectorhandle */
2740 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2745 /* aligned handle? */
2746 if(align) bezt1->h2= HD_ALIGN;
2748 /* or vector handle? */
2749 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2754 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2755 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2757 /* undesired combination: */
2758 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2759 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2766 calchandlesNurb(nu);
2769 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2773 nu= editnurb->first;
2775 autocalchandlesNurb(nu, flag);
2780 void sethandlesNurb(ListBase *editnurb, short code)
2782 /* code==1: set autohandle */
2783 /* code==2: set vectorhandle */
2784 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2785 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2786 /* code==5: Set align, like 3 but no toggle */
2787 /* code==6: Clear align, like 3 but no toggle */
2792 if(code==1 || code==2) {
2793 nu= editnurb->first;
2795 if(nu->type == CU_BEZIER) {
2799 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2800 if(bezt->f1 & SELECT) bezt->h1= code;
2801 if(bezt->f3 & SELECT) bezt->h2= code;
2802 if(bezt->h1!=bezt->h2) {
2803 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2804 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2809 calchandlesNurb(nu);
2815 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2817 nu= editnurb->first;
2820 } else if (code == 6) {
2825 if(nu->type == CU_BEZIER) {
2829 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2830 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2840 nu= editnurb->first;
2842 if(nu->type == CU_BEZIER) {
2846 if(bezt->f1 & SELECT) bezt->h1= ok;
2847 if(bezt->f3 & SELECT) bezt->h2= ok;
2851 calchandlesNurb(nu);
2858 static void swapdata(void *adr1, void *adr2, int len)
2866 memcpy(adr, adr1, len);
2867 memcpy(adr1, adr2, len);
2868 memcpy(adr2, adr, len);
2873 adr= (char *)MEM_mallocN(len, "curve swap");
2874 memcpy(adr, adr1, len);
2875 memcpy(adr1, adr2, len);
2876 memcpy(adr2, adr, len);
2881 void switchdirectionNurb(Nurb *nu)
2883 BezTriple *bezt1, *bezt2;
2885 float *fp1, *fp2, *tempf;
2888 if(nu->pntsu==1 && nu->pntsv==1) return;
2890 if(nu->type == CU_BEZIER) {
2894 if(a & 1) a+= 1; /* if odd, also swap middle content */
2897 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2899 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2900 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2902 SWAP(char, bezt1->h1, bezt1->h2);
2903 SWAP(short, bezt1->f1, bezt1->f3);
2906 SWAP(char, bezt2->h1, bezt2->h2);
2907 SWAP(short, bezt2->f1, bezt2->f3);
2908 bezt1->alfa= -bezt1->alfa;
2909 bezt2->alfa= -bezt2->alfa;
2916 else if(nu->pntsv==1) {
2921 while(bp1!=bp2 && a>0) {
2922 SWAP(BPoint, *bp1, *bp2);
2924 bp1->alfa= -bp1->alfa;
2925 bp2->alfa= -bp2->alfa;
2929 if(nu->type == CU_NURBS) {
2930 /* no knots for too short paths */
2937 while(fp1!=fp2 && a>0) {
2938 SWAP(float, *fp1, *fp2);
2943 /* and make in increasing order again */
2946 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2948 fp2[0]= fabs(fp1[1]-fp1[0]);