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, const short pnts, const short order, const short flag)
585 /* knots: number of pnts NOT corrected for cyclic */
586 const int pnts_order= pnts + order;
590 switch(flag & (CU_NURB_ENDPOINT|CU_NURB_BEZIER)) {
591 case CU_NURB_ENDPOINT:
593 for(a=1; a <= pnts_order; a++) {
595 if(a >= order && a <= pnts) k+= 1.0f;
599 /* Warning, the order MUST be 2 or 4,
600 * if this is not enforced, the displist will be corrupt */
603 for(a=0; a < pnts_order; a++) {
610 for(a=0; a < pnts_order; a++) {
611 if(a >= order && a <= pnts) k+= 0.5f;
616 printf("bez nurb curve order is not 3 or 4, should never happen\n");
620 for(a=0; a < pnts_order; a++) {
627 static void makecyclicknots(float *knots, short pnts, short order)
628 /* pnts, order: number of pnts NOT corrected for cyclic */
632 if(knots==NULL) return;
636 /* do first long rows (order -1), remove identical knots at endpoints */
639 for(a=1; a<order2; a++) {
640 if(knots[b]!= knots[b-a]) break;
642 if(a==order2) knots[pnts+order-2]+= 1.0f;
646 c=pnts + order + order2;
647 for(a=pnts+order2; a<c; a++) {
648 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
655 static void makeknots(Nurb *nu, short uv)
657 if(nu->type == CU_NURBS) {
659 if(nu->knotsu) MEM_freeN(nu->knotsu);
660 if(check_valid_nurb_u(nu)) {
661 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
662 if(nu->flagu & CU_NURB_CYCLIC) {
663 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
664 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
666 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
669 else nu->knotsu= NULL;
672 if(nu->knotsv) MEM_freeN(nu->knotsv);
673 if(check_valid_nurb_v(nu)) {
674 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
675 if(nu->flagv & CU_NURB_CYCLIC) {
676 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
677 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
679 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
682 else nu->knotsv= NULL;
687 void nurbs_knot_calc_u(Nurb *nu)
692 void nurbs_knot_calc_v(Nurb *nu)
697 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
700 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
702 orderpluspnts= order+pnts;
703 opp2 = orderpluspnts-1;
705 /* this is for float inaccuracy */
706 if(t < knots[0]) t= knots[0];
707 else if(t > knots[opp2]) t= knots[opp2];
709 /* this part is order '1' */
711 for(i=0;i<opp2;i++) {
712 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
728 /* this is order 2,3,... */
729 for(j=2; j<=order; j++) {
731 if(i2+j>= orderpluspnts) i2= opp2-j;
733 for(i= i1; i<=i2; i++) {
735 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
739 if(basis[i+1] != 0.0f)
740 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
751 for(i=i1; i<=i2; i++) {
752 if(basis[i] > 0.0f) {
754 if(*start==1000) *start= i;
760 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
761 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
764 float *basisu, *basis, *basisv, *sum, *fp, *in;
765 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
766 int i, j, iofs, jofs, cycl, len, curu, curv;
767 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
769 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
771 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
772 if(nu->orderu>nu->pntsu) return;
773 if(nu->orderv>nu->pntsv) return;
774 if(coord_array==NULL) return;
776 /* allocate and initialize */
782 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
791 i= nu->pntsu*nu->pntsv;
794 if(bp->vec[3] != 1.0f) {
802 ustart= fp[nu->orderu-1];
803 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
804 else uend= fp[nu->pntsu];
805 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
807 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
810 vstart= fp[nu->orderv-1];
812 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
813 else vend= fp[nu->pntsv];
814 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
817 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
818 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
819 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
821 /* precalculation of basisv and jstart,jend */
822 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
828 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
833 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
840 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
853 for(j= jsta; j<=jen; j++) {
855 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
857 bp= nu->bp+ nu->pntsu*jofs+istart-1;
859 for(i= istart; i<=iend; i++, fp++) {
863 bp= nu->bp+ nu->pntsu*jofs+iofs;
868 *fp= basisu[i]*basis[j]*bp->vec[3];
871 else *fp= basisu[i]*basis[j];
877 for(j= jsta; j<=jen; j++) {
878 for(i= istart; i<=iend; i++, fp++) {
884 /* one! (1.0) real point now */
886 for(j= jsta; j<=jen; j++) {
888 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
890 bp= nu->bp+ nu->pntsu*jofs+istart-1;
892 for(i= istart; i<=iend; i++, fp++) {
896 bp= nu->bp+ nu->pntsu*jofs+iofs;
901 in[0]+= (*fp) * bp->vec[0];
902 in[1]+= (*fp) * bp->vec[1];
903 in[2]+= (*fp) * bp->vec[2];
912 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
923 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
924 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
925 * tilt_array and radius_array will be written to if valid */
928 float u, ustart, uend, ustep, sumdiv;
929 float *basisu, *sum, *fp;
930 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
931 int i, len, istart, iend, cycl;
933 if(nu->knotsu==NULL) return;
934 if(nu->orderu>nu->pntsu) return;
935 if(coord_array==NULL) return;
937 /* allocate and initialize */
940 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
942 resolu= (resolu*SEGMENTSU(nu));
950 ustart= fp[nu->orderu-1];
951 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
952 else uend= fp[nu->pntsu];
953 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
955 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
957 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
963 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
967 bp= nu->bp+ istart-1;
968 for(i= istart; i<=iend; i++, fp++) {
970 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
973 *fp= basisu[i]*bp->vec[3];
976 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
977 /* is normalizing needed? */
979 for(i= istart; i<=iend; i++, fp++) {
984 /* one! (1.0) real point */
986 bp= nu->bp+ istart-1;
987 for(i= istart; i<=iend; i++, fp++) {
989 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
994 coord_fp[0]+= (*fp) * bp->vec[0];
995 coord_fp[1]+= (*fp) * bp->vec[1];
996 coord_fp[2]+= (*fp) * bp->vec[2];
999 (*tilt_fp) += (*fp) * bp->alfa;
1002 (*radius_fp) += (*fp) * bp->radius;
1005 (*weight_fp) += (*fp) * bp->weight;
1010 coord_fp = (float *)(((char *)coord_fp) + stride);
1012 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1013 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1014 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1024 /* forward differencing method for bezier curve */
1025 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1027 float rt0,rt1,rt2,rt3,f;
1032 rt1= 3.0f*(q1-q0)/f;
1034 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1036 rt3= (q3-q0+3.0f*(q1-q2))/f;
1043 for(a=0; a<=it; a++) {
1045 p = (float *)(((char *)p)+stride);
1052 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1054 /* note that these are not purpendicular to the curve
1055 * they need to be rotated for this,
1057 * This could also be optimized like forward_diff_bezier */
1059 for(a=0; a<=it; a++) {
1060 float t = (float)a / (float)it;
1063 for(i=0; i<3; i++) {
1064 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1067 p = (float *)(((char *)p)+stride);
1071 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1073 float *make_orco_surf(Object *ob)
1075 /* Note: this function is used in convertblender only atm, so
1076 * suppose nonzero curve's render resolution should always be used */
1077 Curve *cu= ob->data;
1082 float *fp, *coord_array;
1084 /* first calculate the size of the datablock */
1087 /* as we want to avoid the seam in a cyclic nurbs
1088 texture wrapping, reserve extra orco data space to save these extra needed
1089 vertex based UV coordinates for the meridian vertices.
1090 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1091 the renderface/vert construction.
1093 See also convertblender.c: init_render_surf()
1096 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1097 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1099 sizeu = nu->pntsu*resolu;
1100 sizev = nu->pntsv*resolv;
1101 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1102 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1103 if(nu->pntsv>1) tot+= sizeu * sizev;
1107 /* makeNurbfaces wants zeros */
1108 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1112 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1113 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1116 sizeu = nu->pntsu*resolu;
1117 sizev = nu->pntsv*resolv;
1118 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1119 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1121 if(cu->flag & CU_UV_ORCO) {
1122 for(b=0; b< sizeu; b++) {
1123 for(a=0; a< sizev; a++) {
1125 if(sizev <2) fp[0]= 0.0f;
1126 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1128 if(sizeu <2) fp[1]= 0.0f;
1129 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1138 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1139 float *tdata= _tdata;
1141 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1143 for(b=0; b<sizeu; b++) {
1145 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1148 for(a=0; a<sizev; a++) {
1150 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1153 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1155 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1156 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1157 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1172 /* NOTE: This routine is tied to the order of vertex
1173 * built by displist and as passed to the renderer.
1175 float *make_orco_curve(Scene *scene, Object *ob)
1177 Curve *cu = ob->data;
1180 float *fp, *coord_array;
1181 ListBase disp = {NULL, NULL};
1183 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1186 for (dl=disp.first; dl; dl=dl->next) {
1187 if (dl->type==DL_INDEX3) {
1189 } else if (dl->type==DL_SURF) {
1190 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1191 if (dl->flag & DL_CYCL_U) {
1192 if (dl->flag & DL_CYCL_V)
1193 numVerts += (dl->parts+1)*(dl->nr+1);
1195 numVerts += dl->parts*(dl->nr+1);
1198 numVerts += dl->parts*dl->nr;
1202 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1203 for (dl=disp.first; dl; dl=dl->next) {
1204 if (dl->type==DL_INDEX3) {
1205 for (u=0; u<dl->nr; u++, fp+=3) {
1206 if (cu->flag & CU_UV_ORCO) {
1207 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1211 VECCOPY(fp, &dl->verts[u*3]);
1213 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1214 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1215 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1218 } else if (dl->type==DL_SURF) {
1219 int sizeu= dl->nr, sizev= dl->parts;
1221 /* exception as handled in convertblender.c too */
1222 if (dl->flag & DL_CYCL_U) {
1224 if (dl->flag & DL_CYCL_V)
1228 for (u=0; u<sizev; u++) {
1229 for (v=0; v<sizeu; v++,fp+=3) {
1230 if (cu->flag & CU_UV_ORCO) {
1231 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1232 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1236 int realv= v % dl->nr;
1237 int realu= u % dl->parts;
1239 vert= dl->verts + 3*(dl->nr*realu + realv);
1242 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1243 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1244 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1251 freedisplist(&disp);
1257 /* ***************** BEVEL ****************** */
1259 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1261 DispList *dl, *dlnew;
1263 float *fp, facx, facy, angle, dangle;
1267 disp->first = disp->last = NULL;
1269 /* if a font object is being edited, then do nothing */
1270 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1273 if (cu->bevobj->type!=OB_CURVE) return;
1275 bevcu= cu->bevobj->data;
1276 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1277 ListBase bevdisp= {NULL, NULL};
1278 facx= cu->bevobj->size[0];
1279 facy= cu->bevobj->size[1];
1282 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1285 dl= cu->bevobj->disp.first;
1287 makeDispListCurveTypes(scene, cu->bevobj, 0);
1288 dl= cu->bevobj->disp.first;
1293 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1294 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1296 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1297 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1299 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1301 BLI_addtail(disp, dlnew);
1303 nr= dlnew->parts*dlnew->nr;
1314 freedisplist(&bevdisp);
1317 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1320 else if(cu->ext2==0.0f) {
1321 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1322 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1323 BLI_addtail(disp, dl);
1326 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1335 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1337 nr= 4+ 2*cu->bevresol;
1339 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1340 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1341 BLI_addtail(disp, dl);
1344 dl->flag= DL_BACK_CURVE;
1349 dangle= (2.0f*(float)M_PI/(nr));
1350 angle= -(nr-1)*dangle;
1352 for(a=0; a<nr; a++) {
1354 fp[1]= (cosf(angle)*(cu->ext2));
1355 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1363 /* bevel now in three parts, for proper vertex normals */
1366 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1367 dnr= nr= 2+ cu->bevresol;
1368 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1369 nr= 3+ 2*cu->bevresol;
1371 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1372 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1373 BLI_addtail(disp, dl);
1376 dl->flag= DL_BACK_CURVE;
1381 dangle= (0.5*M_PI/(dnr-1));
1382 angle= -(nr-1)*dangle;
1384 for(a=0; a<nr; a++) {
1386 fp[1]= (float)(cosf(angle)*(cu->ext2));
1387 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1393 /* part 2, sidefaces */
1394 if(cu->ext1!=0.0f) {
1397 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1398 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1399 BLI_addtail(disp, dl);
1410 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1411 dl= MEM_dupallocN(dl);
1412 dl->verts= MEM_dupallocN(dl->verts);
1413 BLI_addtail(disp, dl);
1424 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1425 dnr= nr= 2+ cu->bevresol;
1426 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1427 nr= 3+ 2*cu->bevresol;
1429 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1430 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1431 BLI_addtail(disp, dl);
1433 dl->flag= DL_FRONT_CURVE;
1440 dangle= (0.5*M_PI/(dnr-1));
1442 for(a=0; a<nr; a++) {
1444 fp[1]= (float)(cosf(angle)*(cu->ext2));
1445 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1453 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1457 0: no intersection of segments
1458 1: exact intersection of segments
1459 2: cross-intersection of segments
1463 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1464 if(deler==0.0f) return -1;
1466 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1467 *labda= -(*labda/deler);
1469 deler= v3[coy]-v4[coy];
1471 deler=v3[cox]-v4[cox];
1472 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1474 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1476 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1477 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1479 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1480 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1487 static short bevelinside(BevList *bl1,BevList *bl2)
1489 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1490 /* returns '1' if correct hole */
1491 BevPoint *bevp, *prevbevp;
1492 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1493 int nr, links=0,rechts=0,mode;
1495 /* take first vertex of possible hole */
1497 bevp= (BevPoint *)(bl2+1);
1498 hvec1[0]= bevp->vec[0];
1499 hvec1[1]= bevp->vec[1];
1501 VECCOPY(hvec2,hvec1);
1504 /* test it with all edges of potential surounding poly */
1505 /* count number of transitions left-right */
1507 bevp= (BevPoint *)(bl1+1);
1509 prevbevp= bevp+(nr-1);
1512 min= prevbevp->vec[1];
1516 max= prevbevp->vec[1];
1519 if(min<=hvec1[1] && max>=hvec1[1]) {
1520 /* there's a transition, calc intersection point */
1521 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1522 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1523 only allow for one situation: we choose lab= 1.0
1525 if(mode >= 0 && lab != 0.0f) {
1526 if(vec[0]<hvec1[0]) links++;
1535 if( (links & 1) && (rechts & 1) ) return 1;
1546 static int vergxcobev(const void *a1, const void *a2)
1548 const struct bevelsort *x1=a1,*x2=a2;
1550 if( x1->left > x2->left ) return 1;
1551 else if( x1->left < x2->left) return -1;
1555 /* this function cannot be replaced with atan2, but why? */
1557 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1559 float t01, t02, x3, y3;
1561 t01= (float)sqrt(x1*x1+y1*y1);
1562 t02= (float)sqrt(x2*x2+y2*y2);
1563 if(t01==0.0f) t01= 1.0f;
1564 if(t02==0.0f) t02= 1.0f;
1572 if(fabs(t02)>=1.0) t02= .5*M_PI;
1573 else t02= (saacos(t02))/2.0f;
1575 t02= (float)sin(t02);
1576 if(t02==0.0f) t02= 1.0f;
1580 if(x3==0 && y3==0) {
1584 t01= (float)sqrt(x3*x3+y3*y3);
1594 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1596 BezTriple *pprev, *next, *last;
1597 float fac, dfac, t[4];
1600 if(tilt_array==NULL && radius_array==NULL)
1603 last= nu->bezt+(nu->pntsu-1);
1605 /* returns a point */
1606 if(prevbezt==nu->bezt) {
1607 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1608 else pprev= prevbezt;
1610 else pprev= prevbezt-1;
1614 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1620 dfac= 1.0f/(float)resolu;
1622 for(a=0; a<resolu; a++, fac+= dfac) {
1624 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1625 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1627 key_curve_position_weights(fac, t, nu->tilt_interp);
1628 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1631 tilt_array = (float *)(((char *)tilt_array) + stride);
1635 if (nu->radius_interp==KEY_CU_EASE) {
1636 /* Support 2.47 ease interp
1637 * Note! - this only takes the 2 points into account,
1638 * giving much more localized results to changes in radius, sometimes you want that */
1639 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1642 /* reuse interpolation from tilt if we can */
1643 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1644 key_curve_position_weights(fac, t, nu->radius_interp);
1646 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1649 radius_array = (float *)(((char *)radius_array) + stride);
1653 /* basic interpolation for now, could copy tilt interp too */
1654 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1656 weight_array = (float *)(((char *)weight_array) + stride);
1661 /* make_bevel_list_3D_* funcs, at a minimum these must
1662 * fill in the bezp->quat and bezp->dir values */
1664 /* correct non-cyclic cases by copying direction and rotation
1665 * values onto the first & last end-points */
1666 static void bevel_list_cyclic_fix_3D(BevList *bl)
1668 BevPoint *bevp, *bevp1;
1670 bevp= (BevPoint *)(bl+1);
1672 QUATCOPY(bevp->quat, bevp1->quat);
1673 VECCOPY(bevp->dir, bevp1->dir);
1674 VECCOPY(bevp->tan, bevp1->tan);
1675 bevp= (BevPoint *)(bl+1);
1678 QUATCOPY(bevp->quat, bevp1->quat);
1679 VECCOPY(bevp->dir, bevp1->dir);
1680 VECCOPY(bevp->tan, bevp1->tan);
1682 /* utility for make_bevel_list_3D_* funcs */
1683 static void bevel_list_calc_bisect(BevList *bl)
1685 BevPoint *bevp2, *bevp1, *bevp0;
1688 bevp2= (BevPoint *)(bl+1);
1689 bevp1= bevp2+(bl->nr-1);
1694 /* totally simple */
1695 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1702 static void bevel_list_flip_tangents(BevList *bl)
1704 BevPoint *bevp2, *bevp1, *bevp0;
1707 bevp2= (BevPoint *)(bl+1);
1708 bevp1= bevp2+(bl->nr-1);
1713 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1714 negate_v3(bevp1->tan);
1721 /* apply user tilt */
1722 static void bevel_list_apply_tilt(BevList *bl)
1724 BevPoint *bevp2, *bevp1;
1728 bevp2= (BevPoint *)(bl+1);
1729 bevp1= bevp2+(bl->nr-1);
1733 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1734 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1735 normalize_qt(bevp1->quat);
1741 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1742 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1744 BevPoint *bevp2, *bevp1, *bevp0;
1748 float bevp0_quat[4];
1751 for(a=0; a < smooth_iter; a++) {
1753 bevp2= (BevPoint *)(bl+1);
1754 bevp1= bevp2+(bl->nr-1);
1759 if(bl->poly== -1) { /* check its not cyclic */
1760 /* skip the first point */
1773 QUATCOPY(bevp0_quat, bevp0->quat);
1776 /* interpolate quats */
1777 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1778 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1781 mul_qt_v3(q, zaxis);
1782 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1783 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1786 QUATCOPY(bevp0_quat, bevp1->quat);
1787 mul_qt_qtqt(q, q2, q);
1788 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1789 normalize_qt(bevp1->quat);
1792 /* bevp0= bevp1; */ /* UNUSED */
1799 static void make_bevel_list_3D_zup(BevList *bl)
1801 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1804 bevp2= (BevPoint *)(bl+1);
1805 bevp1= bevp2+(bl->nr-1);
1810 /* totally simple */
1811 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1812 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1820 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1822 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1826 bevel_list_calc_bisect(bl);
1828 bevp2= (BevPoint *)(bl+1);
1829 bevp1= bevp2+(bl->nr-1);
1835 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1836 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1839 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1841 if(angle > 0.0f) { /* otherwise we can keep as is */
1843 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1844 axis_angle_to_quat(q, cross_tmp, angle);
1845 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1848 QUATCOPY(bevp1->quat, bevp0->quat);
1857 if(bl->poly != -1) { /* check for cyclic */
1859 /* Need to correct for the start/end points not matching
1860 * do this by calculating the tilt angle difference, then apply
1861 * the rotation gradually over the entire curve
1863 * note that the split is between last and second last, rather than first/last as youd expect.
1865 * real order is like this
1866 * 0,1,2,3,4 --> 1,2,3,4,0
1868 * this is why we compare last with second last
1870 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1872 BevPoint *bevp_first;
1873 BevPoint *bevp_last;
1876 bevp_first= (BevPoint *)(bl+1);
1877 bevp_first+= bl->nr-1;
1878 bevp_last = bevp_first;
1881 /* quats and vec's are normalized, should not need to re-normalize */
1882 mul_qt_v3(bevp_first->quat, vec_1);
1883 mul_qt_v3(bevp_last->quat, vec_2);
1884 normalize_v3(vec_1);
1885 normalize_v3(vec_2);
1887 /* align the vector, can avoid this and it looks 98% OK but
1888 * better to align the angle quat roll's before comparing */
1890 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1891 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1892 axis_angle_to_quat(q, cross_tmp, angle);
1893 mul_qt_v3(q, vec_2);
1896 angle= angle_normalized_v3v3(vec_1, vec_2);
1898 /* flip rotation if needs be */
1899 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1900 normalize_v3(cross_tmp);
1901 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90.0f/(float)(180.0/M_PI))
1904 bevp2= (BevPoint *)(bl+1);
1905 bevp1= bevp2+(bl->nr-1);
1910 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1912 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1913 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1922 static void make_bevel_list_3D_tangent(BevList *bl)
1924 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1927 float bevp0_tan[3], cross_tmp[3];
1929 bevel_list_calc_bisect(bl);
1930 if(bl->poly== -1) /* check its not cyclic */
1931 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1932 bevel_list_flip_tangents(bl);
1934 /* correct the tangents */
1935 bevp2= (BevPoint *)(bl+1);
1936 bevp1= bevp2+(bl->nr-1);
1942 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1943 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1944 normalize_v3(bevp1->tan);
1952 /* now for the real twist calc */
1953 bevp2= (BevPoint *)(bl+1);
1954 bevp1= bevp2+(bl->nr-1);
1957 VECCOPY(bevp0_tan, bevp0->tan);
1962 /* make perpendicular, modify tan in place, is ok */
1964 float zero[3] = {0,0,0};
1966 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1967 normalize_v3(cross_tmp);
1968 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1970 /* bevp0= bevp1; */ /* UNUSED */
1976 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1978 switch(twist_mode) {
1979 case CU_TWIST_TANGENT:
1980 make_bevel_list_3D_tangent(bl);
1982 case CU_TWIST_MINIMUM:
1983 make_bevel_list_3D_minimum_twist(bl);
1985 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1986 make_bevel_list_3D_zup(bl);
1989 if(bl->poly== -1) /* check its not cyclic */
1990 bevel_list_cyclic_fix_3D(bl);
1993 bevel_list_smooth(bl, smooth_iter);
1995 bevel_list_apply_tilt(bl);
2000 /* only for 2 points */
2001 static void make_bevel_list_segment_3D(BevList *bl)
2005 BevPoint *bevp2= (BevPoint *)(bl+1);
2006 BevPoint *bevp1= bevp2+1;
2008 /* simple quat/dir */
2009 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2010 normalize_v3(bevp1->dir);
2012 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2014 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2015 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2016 normalize_qt(bevp1->quat);
2017 VECCOPY(bevp2->dir, bevp1->dir);
2018 QUATCOPY(bevp2->quat, bevp1->quat);
2023 void makeBevelList(Object *ob)
2026 - convert all curves to polys, with indication of resol and flags for double-vertices
2027 - possibly; do a smart vertice removal (in case Nurb)
2028 - separate in individual blicks with BoundBox
2029 - AutoHole detection
2033 BezTriple *bezt, *prevbezt;
2035 BevList *bl, *blnew, *blnext;
2036 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2037 float min, inp, x1, x2, y1, y2;
2038 struct bevelsort *sortdata, *sd, *sd1;
2039 int a, b, nr, poly, resolu = 0, len = 0;
2040 int do_tilt, do_radius, do_weight;
2042 /* this function needs an object, because of tflag and upflag */
2045 /* do we need to calculate the radius for each point? */
2046 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2048 /* STEP 1: MAKE POLYS */
2050 BLI_freelistN(&(cu->bev));
2051 if(cu->editnurb && ob->type!=OB_FONT) {
2052 ListBase *nurbs= ED_curve_editnurbs(cu);
2054 } else nu= cu->nurb.first;
2058 /* check if we will calculate tilt data */
2059 do_tilt = CU_DO_TILT(cu, nu);
2060 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2063 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2064 * enforced in the UI but can go wrong possibly */
2065 if(!check_valid_nurb_u(nu)) {
2066 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2067 BLI_addtail(&(cu->bev), bl);
2070 if(G.rendering && cu->resolu_ren!=0)
2071 resolu= cu->resolu_ren;
2075 if(nu->type == CU_POLY) {
2077 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2078 BLI_addtail(&(cu->bev), bl);
2080 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2084 bevp= (BevPoint *)(bl+1);
2088 VECCOPY(bevp->vec, bp->vec);
2089 bevp->alfa= bp->alfa;
2090 bevp->radius= bp->radius;
2091 bevp->weight= bp->weight;
2092 bevp->split_tag= TRUE;
2097 else if(nu->type == CU_BEZIER) {
2099 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2100 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2101 BLI_addtail(&(cu->bev), bl);
2103 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2105 bevp= (BevPoint *)(bl+1);
2109 if(nu->flagu & CU_NURB_CYCLIC) {
2111 prevbezt= nu->bezt+(nu->pntsu-1);
2119 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2121 VECCOPY(bevp->vec, prevbezt->vec[1]);
2122 bevp->alfa= prevbezt->alfa;
2123 bevp->radius= prevbezt->radius;
2124 bevp->weight= prevbezt->weight;
2125 bevp->split_tag= TRUE;
2126 bevp->dupe_tag= FALSE;
2132 /* always do all three, to prevent data hanging around */
2135 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2136 for(j=0; j<3; j++) {
2137 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2138 bezt->vec[0][j], bezt->vec[1][j],
2139 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2142 /* if both arrays are NULL do nothiong */
2143 alfa_bezpart( prevbezt, bezt, nu,
2144 do_tilt ? &bevp->alfa : NULL,
2145 do_radius ? &bevp->radius : NULL,
2146 do_weight ? &bevp->weight : NULL,
2147 resolu, sizeof(BevPoint));
2150 if(cu->twist_mode==CU_TWIST_TANGENT) {
2151 forward_diff_bezier_cotangent(
2152 prevbezt->vec[1], prevbezt->vec[2],
2153 bezt->vec[0], bezt->vec[1],
2154 bevp->tan, resolu, sizeof(BevPoint));
2157 /* indicate with handlecodes double points */
2158 if(prevbezt->h1==prevbezt->h2) {
2159 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2162 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2163 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2172 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2173 VECCOPY(bevp->vec, prevbezt->vec[1]);
2174 bevp->alfa= prevbezt->alfa;
2175 bevp->radius= prevbezt->radius;
2176 bevp->weight= prevbezt->weight;
2180 else if(nu->type == CU_NURBS) {
2182 len= (resolu*SEGMENTSU(nu));
2184 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2185 BLI_addtail(&(cu->bev), bl);
2188 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2190 bevp= (BevPoint *)(bl+1);
2192 makeNurbcurve( nu, &bevp->vec[0],
2193 do_tilt ? &bevp->alfa : NULL,
2194 do_radius ? &bevp->radius : NULL,
2195 do_weight ? &bevp->weight : NULL,
2196 resolu, sizeof(BevPoint));
2203 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2206 if (bl->nr) { /* null bevel items come from single points */
2208 bevp1= (BevPoint *)(bl+1);
2209 bevp0= bevp1+(nr-1);
2212 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2213 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2214 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2215 bevp0->dupe_tag= TRUE;
2229 if(bl->nr && bl->dupe_nr) {
2230 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2231 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2232 memcpy(blnew, bl, sizeof(BevList));
2234 BLI_remlink(&(cu->bev), bl);
2235 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2236 bevp0= (BevPoint *)(bl+1);
2237 bevp1= (BevPoint *)(blnew+1);
2240 if(bevp0->dupe_tag==0) {
2241 memcpy(bevp1, bevp0, sizeof(BevPoint));
2253 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2257 if(bl->nr && bl->poly>=0) {
2266 /* find extreme left points, also test (turning) direction */
2268 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2274 bevp= (BevPoint *)(bl+1);
2277 if(min>bevp->vec[0]) {
2286 bevp= (BevPoint *)(bl+1);
2287 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2288 else bevp0= bevp1-1;
2289 bevp= bevp+ (bl->nr-1);
2290 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2291 else bevp2= bevp1+1;
2293 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]);
2295 if(inp > 0.0f) sd->dir= 1;
2303 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2306 for(a=1; a<poly; a++, sd++) {
2307 bl= sd->bl; /* is bl a hole? */
2308 sd1= sortdata+ (a-1);
2309 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2310 if(bevelinside(sd1->bl, bl)) {
2311 bl->hole= 1- sd1->bl->hole;
2317 /* turning direction */
2318 if((cu->flag & CU_3D)==0) {
2320 for(a=0; a<poly; a++, sd++) {
2321 if(sd->bl->hole==sd->dir) {
2323 bevp1= (BevPoint *)(bl+1);
2324 bevp2= bevp1+ (bl->nr-1);
2327 SWAP(BevPoint, *bevp1, *bevp2);
2334 MEM_freeN(sortdata);
2337 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2338 if((cu->flag & CU_3D)==0) {
2339 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2340 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2347 else if(bl->nr==2) { /* 2 pnt, treat separate */
2348 bevp2= (BevPoint *)(bl+1);
2351 x1= bevp1->vec[0]- bevp2->vec[0];
2352 y1= bevp1->vec[1]- bevp2->vec[1];
2354 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2355 bevp2->sina= bevp1->sina;
2356 bevp2->cosa= bevp1->cosa;
2358 /* fill in dir & quat */
2359 make_bevel_list_segment_3D(bl);
2362 bevp2= (BevPoint *)(bl+1);
2363 bevp1= bevp2+(bl->nr-1);
2368 x1= bevp1->vec[0]- bevp0->vec[0];
2369 x2= bevp1->vec[0]- bevp2->vec[0];
2370 y1= bevp1->vec[1]- bevp0->vec[1];
2371 y2= bevp1->vec[1]- bevp2->vec[1];
2373 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2375 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2376 * no need for tricky tilt calculation as with 3D curves */
2377 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2378 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2379 /* done with inline make_bevel_list_3D_zup */
2386 /* correct non-cyclic cases */
2388 bevp= (BevPoint *)(bl+1);
2390 bevp->sina= bevp1->sina;
2391 bevp->cosa= bevp1->cosa;
2392 bevp= (BevPoint *)(bl+1);
2395 bevp->sina= bevp1->sina;
2396 bevp->cosa= bevp1->cosa;
2398 /* correct for the dir/quat, see above why its needed */
2399 bevel_list_cyclic_fix_3D(bl);
2405 else { /* 3D Curves */
2412 else if(bl->nr==2) { /* 2 pnt, treat separate */
2413 make_bevel_list_segment_3D(bl);
2416 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2423 /* ****************** HANDLES ************** */
2427 * 0: nothing, 1:auto, 2:vector, 3:aligned
2430 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2431 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2433 float *p1,*p2,*p3, pt[3];
2434 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2435 const float eps= 1e-5;
2437 if(bezt->h1==0 && bezt->h2==0) return;
2443 pt[0]= 2*p2[0]- p3[0];
2444 pt[1]= 2*p2[1]- p3[1];
2445 pt[2]= 2*p2[2]- p3[2];
2448 else p1= prev->vec[1];
2451 pt[0]= 2*p2[0]- p1[0];
2452 pt[1]= 2*p2[1]- p1[1];
2453 pt[2]= 2*p2[2]- p1[2];
2456 else p3= next->vec[1];
2463 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2470 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2472 if(len1==0.0f) len1=1.0f;
2473 if(len2==0.0f) len2=1.0f;
2476 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2477 vx= dx1/len2 + dx/len1;
2478 vy= dy1/len2 + dy/len1;
2479 vz= dz1/len2 + dz/len1;
2480 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2482 int leftviolate=0, rightviolate=0; /* for mode==2 */
2484 if(len1>5.0f*len2) len1= 5.0f*len2;
2485 if(len2>5.0f*len1) len2= 5.0f*len1;
2487 if(bezt->h1==HD_AUTO) {
2489 *(p2-3)= *p2-vx*len1;
2490 *(p2-2)= *(p2+1)-vy*len1;
2491 *(p2-1)= *(p2+2)-vz*len1;
2493 if(mode==2 && next && prev) { // keep horizontal if extrema
2494 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2495 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2496 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2497 bezt->vec[0][1]= bezt->vec[1][1];
2499 else { // handles should not be beyond y coord of two others
2500 if(ydiff1 <= 0.0f) {
2501 if(prev->vec[1][1] > bezt->vec[0][1]) {
2502 bezt->vec[0][1]= prev->vec[1][1];
2507 if(prev->vec[1][1] < bezt->vec[0][1]) {
2508 bezt->vec[0][1]= prev->vec[1][1];
2515 if(bezt->h2==HD_AUTO) {
2517 *(p2+3)= *p2+vx*len2;
2518 *(p2+4)= *(p2+1)+vy*len2;
2519 *(p2+5)= *(p2+2)+vz*len2;
2521 if(mode==2 && next && prev) { // keep horizontal if extrema
2522 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2523 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2524 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2525 bezt->vec[2][1]= bezt->vec[1][1];
2527 else { // handles should not be beyond y coord of two others
2528 if(ydiff1 <= 0.0f) {
2529 if(next->vec[1][1] < bezt->vec[2][1]) {
2530 bezt->vec[2][1]= next->vec[1][1];
2535 if(next->vec[1][1] > bezt->vec[2][1]) {
2536 bezt->vec[2][1]= next->vec[1][1];
2543 if(leftviolate || rightviolate) { /* align left handle */
2546 sub_v3_v3v3(h1, p2-3, p2);
2547 sub_v3_v3v3(h2, p2, p2+3);
2548 len1= normalize_v3(h1);
2549 len2= normalize_v3(h2);
2554 *(p2+3)= *(p2) - vz*len2*h1[0];
2555 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2556 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2559 *(p2-3)= *(p2) + vz*len1*h2[0];
2560 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2561 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2568 if(bezt->h1==HD_VECT) { /* vector */
2573 *(p2-2)= *(p2+1)-dy;
2574 *(p2-1)= *(p2+2)-dz;
2576 if(bezt->h2==HD_VECT) {
2581 *(p2+4)= *(p2+1)+dy1;
2582 *(p2+5)= *(p2+2)+dz1;
2585 len2= len_v3v3(p2, p2+3);
2586 len1= len_v3v3(p2, p2-3);
2587 if(len1==0.0f) len1= 1.0f;
2588 if(len2==0.0f) len2= 1.0f;
2590 if(bezt->f1 & SELECT) { /* order of calculation */
2591 if(bezt->h2==HD_ALIGN) { /* aligned */
2594 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2595 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2596 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2599 if(bezt->h1==HD_ALIGN) {
2602 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2603 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2604 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2609 if(bezt->h1==HD_ALIGN) {
2612 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2613 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2614 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2617 if(bezt->h2==HD_ALIGN) { /* aligned */
2620 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2621 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2622 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2628 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2630 BezTriple *bezt, *prev, *next;
2633 if(nu->type != CU_BEZIER) return;
2634 if(nu->pntsu<2) return;
2638 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2643 calchandleNurb(bezt, prev, next, 0);
2646 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2656 void testhandlesNurb(Nurb *nu)
2658 /* use when something has changed with handles.
2659 it treats all BezTriples with the following rules:
2660 PHASE 1: do types have to be altered?
2661 Auto handles: become aligned when selection status is NOT(000 || 111)
2662 Vector handles: become 'nothing' when (one half selected AND other not)
2663 PHASE 2: recalculate handles
2668 if(nu->type != CU_BEZIER) return;
2674 if(bezt->f1 & SELECT) flag++;
2675 if(bezt->f2 & SELECT) flag += 2;
2676 if(bezt->f3 & SELECT) flag += 4;
2678 if( !(flag==0 || flag==7) ) {
2679 if(bezt->h1==HD_AUTO) { /* auto */
2682 if(bezt->h2==HD_AUTO) { /* auto */
2686 if(bezt->h1==HD_VECT) { /* vector */
2687 if(flag < 4) bezt->h1= 0;
2689 if(bezt->h2==HD_VECT) { /* vector */
2690 if( flag > 3) bezt->h2= 0;
2696 calchandlesNurb(nu);
2699 void autocalchandlesNurb(Nurb *nu, int flag)
2701 /* checks handle coordinates and calculates type */
2703 BezTriple *bezt2, *bezt1, *bezt0;
2704 int i, align, leftsmall, rightsmall;
2706 if(nu==NULL || nu->bezt==NULL) return;
2709 bezt1 = bezt2 + (nu->pntsu-1);
2715 align= leftsmall= rightsmall= 0;
2718 if(flag==0 || (bezt1->f1 & flag) ) {
2720 /* distance too short: vectorhandle */
2721 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2726 /* aligned handle? */
2727 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2729 bezt1->h1= HD_ALIGN;
2731 /* or vector handle? */
2732 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2738 if(flag==0 || (bezt1->f3 & flag) ) {
2740 /* distance too short: vectorhandle */
2741 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2746 /* aligned handle? */
2747 if(align) bezt1->h2= HD_ALIGN;
2749 /* or vector handle? */
2750 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2755 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2756 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2758 /* undesired combination: */
2759 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2760 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2767 calchandlesNurb(nu);
2770 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2774 nu= editnurb->first;
2776 autocalchandlesNurb(nu, flag);
2781 void sethandlesNurb(ListBase *editnurb, short code)
2783 /* code==1: set autohandle */
2784 /* code==2: set vectorhandle */
2785 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2786 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2787 /* code==5: Set align, like 3 but no toggle */
2788 /* code==6: Clear align, like 3 but no toggle */
2793 if(code==1 || code==2) {
2794 nu= editnurb->first;
2796 if(nu->type == CU_BEZIER) {
2800 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2801 if(bezt->f1 & SELECT) bezt->h1= code;
2802 if(bezt->f3 & SELECT) bezt->h2= code;
2803 if(bezt->h1!=bezt->h2) {
2804 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2805 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2810 calchandlesNurb(nu);
2816 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2818 nu= editnurb->first;
2821 } else if (code == 6) {
2826 if(nu->type == CU_BEZIER) {
2830 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2831 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2841 nu= editnurb->first;
2843 if(nu->type == CU_BEZIER) {
2847 if(bezt->f1 & SELECT) bezt->h1= ok;
2848 if(bezt->f3 & SELECT) bezt->h2= ok;
2852 calchandlesNurb(nu);
2859 static void swapdata(void *adr1, void *adr2, int len)
2867 memcpy(adr, adr1, len);
2868 memcpy(adr1, adr2, len);
2869 memcpy(adr2, adr, len);
2874 adr= (char *)MEM_mallocN(len, "curve swap");
2875 memcpy(adr, adr1, len);
2876 memcpy(adr1, adr2, len);
2877 memcpy(adr2, adr, len);
2882 void switchdirectionNurb(Nurb *nu)
2884 BezTriple *bezt1, *bezt2;
2886 float *fp1, *fp2, *tempf;
2889 if(nu->pntsu==1 && nu->pntsv==1) return;
2891 if(nu->type == CU_BEZIER) {
2895 if(a & 1) a+= 1; /* if odd, also swap middle content */
2898 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2900 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2901 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2903 SWAP(char, bezt1->h1, bezt1->h2);
2904 SWAP(short, bezt1->f1, bezt1->f3);
2907 SWAP(char, bezt2->h1, bezt2->h2);
2908 SWAP(short, bezt2->f1, bezt2->f3);
2909 bezt1->alfa= -bezt1->alfa;
2910 bezt2->alfa= -bezt2->alfa;
2917 else if(nu->pntsv==1) {
2922 while(bp1!=bp2 && a>0) {
2923 SWAP(BPoint, *bp1, *bp2);
2925 bp1->alfa= -bp1->alfa;
2926 bp2->alfa= -bp2->alfa;
2930 if(nu->type == CU_NURBS) {
2931 /* no knots for too short paths */
2938 while(fp1!=fp2 && a>0) {
2939 SWAP(float, *fp1, *fp2);
2944 /* and make in increasing order again */
2947 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");