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_meshdata_types.h"
56 #include "DNA_object_types.h"
58 #include "BKE_animsys.h"
60 #include "BKE_curve.h"
61 #include "BKE_displist.h"
63 #include "BKE_global.h"
65 #include "BKE_library.h"
67 #include "BKE_object.h"
68 #include "BKE_material.h"
76 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
78 float *labda, float *mu, float *vec);
80 void unlink_curve(Curve *cu)
84 for(a=0; a<cu->totcol; a++) {
85 if(cu->mat[a]) cu->mat[a]->id.us--;
88 if(cu->vfont) cu->vfont->id.us--;
91 if(cu->vfontb) cu->vfontb->id.us--;
94 if(cu->vfonti) cu->vfonti->id.us--;
97 if(cu->vfontbi) cu->vfontbi->id.us--;
100 if(cu->key) cu->key->id.us--;
104 /* frees editcurve entirely */
105 void BKE_free_editfont(Curve *cu)
108 EditFont *ef= cu->editfont;
110 if(ef->oldstr) MEM_freeN(ef->oldstr);
111 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
112 if(ef->textbuf) MEM_freeN(ef->textbuf);
113 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
114 if(ef->copybuf) MEM_freeN(ef->copybuf);
115 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
122 /* don't free curve itself */
123 void free_curve(Curve *cu)
125 freeNurblist(&cu->nurb);
126 BLI_freelistN(&cu->bev);
127 freedisplist(&cu->disp);
128 BKE_free_editfont(cu);
130 free_curve_editNurb(cu);
132 BKE_free_animdata((ID *)cu);
134 if(cu->mat) MEM_freeN(cu->mat);
135 if(cu->str) MEM_freeN(cu->str);
136 if(cu->strinfo) MEM_freeN(cu->strinfo);
137 if(cu->bb) MEM_freeN(cu->bb);
138 if(cu->path) free_path(cu->path);
139 if(cu->tb) MEM_freeN(cu->tb);
142 Curve *add_curve(const char *name, int type)
146 cu= alloc_libblock(&G.main->curve, ID_CU, name);
148 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
149 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
151 cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
154 cu->spacing= cu->linedist= 1.0;
157 cu->texflag= CU_AUTOSPACE;
158 cu->smallcaps_scale= 0.75f;
159 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
161 cu->bb= unit_boundbox();
164 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
166 cu->str= MEM_mallocN(12, "str");
167 BLI_strncpy(cu->str, "Text", 12);
169 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
170 cu->totbox= cu->actbox= 1;
171 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
172 cu->tb[0].w = cu->tb[0].h = 0.0;
178 Curve *copy_curve(Curve *cu)
183 cun= copy_libblock(cu);
184 cun->nurb.first= cun->nurb.last= NULL;
185 duplicateNurblist( &(cun->nurb), &(cu->nurb));
187 cun->mat= MEM_dupallocN(cu->mat);
188 for(a=0; a<cun->totcol; a++) {
189 id_us_plus((ID *)cun->mat[a]);
192 cun->str= MEM_dupallocN(cu->str);
193 cun->strinfo= MEM_dupallocN(cu->strinfo);
194 cun->tb= MEM_dupallocN(cu->tb);
195 cun->bb= MEM_dupallocN(cu->bb);
197 cun->key= copy_key(cu->key);
198 if(cun->key) cun->key->from= (ID *)cun;
200 cun->disp.first= cun->disp.last= NULL;
201 cun->bev.first= cun->bev.last= NULL;
208 #if 0 // XXX old animation system
209 /* single user ipo too */
210 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
211 #endif // XXX old animation system
213 id_us_plus((ID *)cun->vfont);
214 id_us_plus((ID *)cun->vfontb);
215 id_us_plus((ID *)cun->vfonti);
216 id_us_plus((ID *)cun->vfontbi);
221 static void extern_local_curve(Curve *cu)
223 id_lib_extern((ID *)cu->vfont);
224 id_lib_extern((ID *)cu->vfontb);
225 id_lib_extern((ID *)cu->vfonti);
226 id_lib_extern((ID *)cu->vfontbi);
229 extern_local_matarar(cu->mat, cu->totcol);
233 void make_local_curve(Curve *cu)
239 /* - when there are only lib users: don't do
240 * - when there are only local users: set flag
244 if(cu->id.lib==NULL) return;
248 cu->id.flag= LIB_LOCAL;
250 new_id(&bmain->curve, (ID *)cu, NULL);
251 extern_local_curve(cu);
255 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
257 if(ob->id.lib) lib= 1;
262 if(local && lib==0) {
264 cu->id.flag= LIB_LOCAL;
266 new_id(&bmain->curve, (ID *)cu, NULL);
267 extern_local_curve(cu);
269 else if(local && lib) {
270 Curve *cun= copy_curve(cu);
273 for(ob= bmain->object.first; ob; ob= ob->id.next) {
275 if(ob->id.lib==NULL) {
285 short curve_type(Curve *cu)
291 for (nu= cu->nurb.first; nu; nu= nu->next) {
300 void test_curve_type(Object *ob)
302 ob->type = curve_type(ob->data);
305 void tex_space_curve(Curve *cu)
309 float *fp, min[3], max[3];
312 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
315 INIT_MINMAX(min, max);
320 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
321 else tot= dl->nr*dl->parts;
326 DO_MINMAX(fp, min, max);
333 min[0] = min[1] = min[2] = -1.0f;
334 max[0] = max[1] = max[2] = 1.0f;
337 boundbox_set_from_min_max(bb, min, max);
339 if(cu->texflag & CU_AUTOSPACE) {
340 mid_v3_v3v3(cu->loc, min, max);
341 cu->size[0]= (max[0]-min[0])/2.0f;
342 cu->size[1]= (max[1]-min[1])/2.0f;
343 cu->size[2]= (max[2]-min[2])/2.0f;
345 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
347 if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
348 else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
349 else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
351 if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
352 else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
353 else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
355 if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
356 else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
357 else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
363 int count_curveverts(ListBase *nurb)
370 if(nu->bezt) tot+= 3*nu->pntsu;
371 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
378 int count_curveverts_without_handles(ListBase *nurb)
385 if(nu->bezt) tot+= nu->pntsu;
386 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
393 /* **************** NURBS ROUTINES ******************** */
395 void freeNurb(Nurb *nu)
400 if(nu->bezt) MEM_freeN(nu->bezt);
402 if(nu->bp) MEM_freeN(nu->bp);
404 if(nu->knotsu) MEM_freeN(nu->knotsu);
406 if(nu->knotsv) MEM_freeN(nu->knotsv);
408 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
415 void freeNurblist(ListBase *lb)
427 lb->first= lb->last= NULL;
430 Nurb *duplicateNurb(Nurb *nu)
435 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
436 if(newnu==NULL) return NULL;
437 memcpy(newnu, nu, sizeof(Nurb));
441 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
442 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
445 len= nu->pntsu*nu->pntsv;
447 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
448 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
450 newnu->knotsu= newnu->knotsv= NULL;
455 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
456 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
459 if(nu->pntsv>1 && nu->knotsv) {
462 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
463 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
470 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
478 nun= duplicateNurb(nu);
479 BLI_addtail(lb1, nun);
485 void test2DNurb(Nurb *nu)
491 if((nu->flag & CU_2D)==0)
494 if(nu->type == CU_BEZIER) {
498 bezt->vec[0][2]= 0.0;
499 bezt->vec[1][2]= 0.0;
500 bezt->vec[2][2]= 0.0;
505 a= nu->pntsu*nu->pntsv;
514 void minmaxNurb(Nurb *nu, float *min, float *max)
520 if(nu->type == CU_BEZIER) {
524 DO_MINMAX(bezt->vec[0], min, max);
525 DO_MINMAX(bezt->vec[1], min, max);
526 DO_MINMAX(bezt->vec[2], min, max);
531 a= nu->pntsu*nu->pntsv;
534 DO_MINMAX(bp->vec, min, max);
540 /* be sure to call makeknots after this */
541 void addNurbPoints(Nurb *nu, int number)
545 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
548 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
552 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
554 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
561 void addNurbPointsBezier(Nurb *nu, int number)
563 BezTriple *tmp= nu->bezt;
565 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
568 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
572 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
574 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
581 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
584 static void calcknots(float *knots, const short pnts, const short order, const short flag)
586 /* knots: number of pnts NOT corrected for cyclic */
587 const int pnts_order= pnts + order;
591 switch(flag & (CU_NURB_ENDPOINT|CU_NURB_BEZIER)) {
592 case CU_NURB_ENDPOINT:
594 for(a=1; a <= pnts_order; a++) {
596 if(a >= order && a <= pnts) k+= 1.0f;
600 /* Warning, the order MUST be 2 or 4,
601 * if this is not enforced, the displist will be corrupt */
604 for(a=0; a < pnts_order; a++) {
611 for(a=0; a < pnts_order; a++) {
612 if(a >= order && a <= pnts) k+= 0.5f;
617 printf("bez nurb curve order is not 3 or 4, should never happen\n");
621 for(a=0; a < pnts_order; a++) {
628 static void makecyclicknots(float *knots, short pnts, short order)
629 /* pnts, order: number of pnts NOT corrected for cyclic */
633 if(knots==NULL) return;
637 /* do first long rows (order -1), remove identical knots at endpoints */
640 for(a=1; a<order2; a++) {
641 if(knots[b]!= knots[b-a]) break;
643 if(a==order2) knots[pnts+order-2]+= 1.0f;
647 c=pnts + order + order2;
648 for(a=pnts+order2; a<c; a++) {
649 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
656 static void makeknots(Nurb *nu, short uv)
658 if(nu->type == CU_NURBS) {
660 if(nu->knotsu) MEM_freeN(nu->knotsu);
661 if(check_valid_nurb_u(nu)) {
662 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
663 if(nu->flagu & CU_NURB_CYCLIC) {
664 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
665 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
667 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
670 else nu->knotsu= NULL;
673 if(nu->knotsv) MEM_freeN(nu->knotsv);
674 if(check_valid_nurb_v(nu)) {
675 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
676 if(nu->flagv & CU_NURB_CYCLIC) {
677 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
678 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
680 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
683 else nu->knotsv= NULL;
688 void nurbs_knot_calc_u(Nurb *nu)
693 void nurbs_knot_calc_v(Nurb *nu)
698 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
701 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
703 orderpluspnts= order+pnts;
704 opp2 = orderpluspnts-1;
706 /* this is for float inaccuracy */
707 if(t < knots[0]) t= knots[0];
708 else if(t > knots[opp2]) t= knots[opp2];
710 /* this part is order '1' */
712 for(i=0;i<opp2;i++) {
713 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
729 /* this is order 2,3,... */
730 for(j=2; j<=order; j++) {
732 if(i2+j>= orderpluspnts) i2= opp2-j;
734 for(i= i1; i<=i2; i++) {
736 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
740 if(basis[i+1] != 0.0f)
741 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
752 for(i=i1; i<=i2; i++) {
753 if(basis[i] > 0.0f) {
755 if(*start==1000) *start= i;
761 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
762 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
765 float *basisu, *basis, *basisv, *sum, *fp, *in;
766 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
767 int i, j, iofs, jofs, cycl, len, curu, curv;
768 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
770 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
772 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
773 if(nu->orderu>nu->pntsu) return;
774 if(nu->orderv>nu->pntsv) return;
775 if(coord_array==NULL) return;
777 /* allocate and initialize */
783 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
792 i= nu->pntsu*nu->pntsv;
795 if(bp->vec[3] != 1.0f) {
803 ustart= fp[nu->orderu-1];
804 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
805 else uend= fp[nu->pntsu];
806 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
808 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
811 vstart= fp[nu->orderv-1];
813 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
814 else vend= fp[nu->pntsv];
815 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
818 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
819 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
820 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
822 /* precalculation of basisv and jstart,jend */
823 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
829 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
834 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
841 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
854 for(j= jsta; j<=jen; j++) {
856 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
858 bp= nu->bp+ nu->pntsu*jofs+istart-1;
860 for(i= istart; i<=iend; i++, fp++) {
864 bp= nu->bp+ nu->pntsu*jofs+iofs;
869 *fp= basisu[i]*basis[j]*bp->vec[3];
872 else *fp= basisu[i]*basis[j];
878 for(j= jsta; j<=jen; j++) {
879 for(i= istart; i<=iend; i++, fp++) {
885 /* one! (1.0) real point now */
887 for(j= jsta; j<=jen; j++) {
889 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
891 bp= nu->bp+ nu->pntsu*jofs+istart-1;
893 for(i= istart; i<=iend; i++, fp++) {
897 bp= nu->bp+ nu->pntsu*jofs+iofs;
902 in[0]+= (*fp) * bp->vec[0];
903 in[1]+= (*fp) * bp->vec[1];
904 in[2]+= (*fp) * bp->vec[2];
913 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
924 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
925 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
926 * tilt_array and radius_array will be written to if valid */
929 float u, ustart, uend, ustep, sumdiv;
930 float *basisu, *sum, *fp;
931 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
932 int i, len, istart, iend, cycl;
934 if(nu->knotsu==NULL) return;
935 if(nu->orderu>nu->pntsu) return;
936 if(coord_array==NULL) return;
938 /* allocate and initialize */
941 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
943 resolu= (resolu*SEGMENTSU(nu));
951 ustart= fp[nu->orderu-1];
952 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
953 else uend= fp[nu->pntsu];
954 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
956 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
958 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
964 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
968 bp= nu->bp+ istart-1;
969 for(i= istart; i<=iend; i++, fp++) {
971 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
974 *fp= basisu[i]*bp->vec[3];
977 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
978 /* is normalizing needed? */
980 for(i= istart; i<=iend; i++, fp++) {
985 /* one! (1.0) real point */
987 bp= nu->bp+ istart-1;
988 for(i= istart; i<=iend; i++, fp++) {
990 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
995 coord_fp[0]+= (*fp) * bp->vec[0];
996 coord_fp[1]+= (*fp) * bp->vec[1];
997 coord_fp[2]+= (*fp) * bp->vec[2];
1000 (*tilt_fp) += (*fp) * bp->alfa;
1003 (*radius_fp) += (*fp) * bp->radius;
1006 (*weight_fp) += (*fp) * bp->weight;
1011 coord_fp = (float *)(((char *)coord_fp) + stride);
1013 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1014 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1015 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1025 /* forward differencing method for bezier curve */
1026 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1028 float rt0,rt1,rt2,rt3,f;
1033 rt1= 3.0f*(q1-q0)/f;
1035 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1037 rt3= (q3-q0+3.0f*(q1-q2))/f;
1044 for(a=0; a<=it; a++) {
1046 p = (float *)(((char *)p)+stride);
1053 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1055 /* note that these are not purpendicular to the curve
1056 * they need to be rotated for this,
1058 * This could also be optimized like forward_diff_bezier */
1060 for(a=0; a<=it; a++) {
1061 float t = (float)a / (float)it;
1064 for(i=0; i<3; i++) {
1065 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1068 p = (float *)(((char *)p)+stride);
1072 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1074 float *make_orco_surf(Object *ob)
1076 /* Note: this function is used in convertblender only atm, so
1077 * suppose nonzero curve's render resolution should always be used */
1078 Curve *cu= ob->data;
1083 float *fp, *coord_array;
1085 /* first calculate the size of the datablock */
1088 /* as we want to avoid the seam in a cyclic nurbs
1089 texture wrapping, reserve extra orco data space to save these extra needed
1090 vertex based UV coordinates for the meridian vertices.
1091 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1092 the renderface/vert construction.
1094 See also convertblender.c: init_render_surf()
1097 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1098 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1100 sizeu = nu->pntsu*resolu;
1101 sizev = nu->pntsv*resolv;
1102 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1103 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1104 if(nu->pntsv>1) tot+= sizeu * sizev;
1108 /* makeNurbfaces wants zeros */
1109 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1113 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1114 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1117 sizeu = nu->pntsu*resolu;
1118 sizev = nu->pntsv*resolv;
1119 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1120 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1122 if(cu->flag & CU_UV_ORCO) {
1123 for(b=0; b< sizeu; b++) {
1124 for(a=0; a< sizev; a++) {
1126 if(sizev <2) fp[0]= 0.0f;
1127 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1129 if(sizeu <2) fp[1]= 0.0f;
1130 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1139 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1140 float *tdata= _tdata;
1142 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1144 for(b=0; b<sizeu; b++) {
1146 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1149 for(a=0; a<sizev; a++) {
1151 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1154 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1156 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1157 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1158 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1173 /* NOTE: This routine is tied to the order of vertex
1174 * built by displist and as passed to the renderer.
1176 float *make_orco_curve(Scene *scene, Object *ob)
1178 Curve *cu = ob->data;
1181 float *fp, *coord_array;
1182 ListBase disp = {NULL, NULL};
1184 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1187 for (dl=disp.first; dl; dl=dl->next) {
1188 if (dl->type==DL_INDEX3) {
1190 } else if (dl->type==DL_SURF) {
1191 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1192 if (dl->flag & DL_CYCL_U) {
1193 if (dl->flag & DL_CYCL_V)
1194 numVerts += (dl->parts+1)*(dl->nr+1);
1196 numVerts += dl->parts*(dl->nr+1);
1199 numVerts += dl->parts*dl->nr;
1203 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1204 for (dl=disp.first; dl; dl=dl->next) {
1205 if (dl->type==DL_INDEX3) {
1206 for (u=0; u<dl->nr; u++, fp+=3) {
1207 if (cu->flag & CU_UV_ORCO) {
1208 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1212 VECCOPY(fp, &dl->verts[u*3]);
1214 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1215 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1216 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1219 } else if (dl->type==DL_SURF) {
1220 int sizeu= dl->nr, sizev= dl->parts;
1222 /* exception as handled in convertblender.c too */
1223 if (dl->flag & DL_CYCL_U) {
1225 if (dl->flag & DL_CYCL_V)
1229 for (u=0; u<sizev; u++) {
1230 for (v=0; v<sizeu; v++,fp+=3) {
1231 if (cu->flag & CU_UV_ORCO) {
1232 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1233 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1237 int realv= v % dl->nr;
1238 int realu= u % dl->parts;
1240 vert= dl->verts + 3*(dl->nr*realu + realv);
1243 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1244 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1245 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1252 freedisplist(&disp);
1258 /* ***************** BEVEL ****************** */
1260 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1262 DispList *dl, *dlnew;
1264 float *fp, facx, facy, angle, dangle;
1268 disp->first = disp->last = NULL;
1270 /* if a font object is being edited, then do nothing */
1271 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1274 if (cu->bevobj->type!=OB_CURVE) return;
1276 bevcu= cu->bevobj->data;
1277 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1278 ListBase bevdisp= {NULL, NULL};
1279 facx= cu->bevobj->size[0];
1280 facy= cu->bevobj->size[1];
1283 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1286 dl= cu->bevobj->disp.first;
1288 makeDispListCurveTypes(scene, cu->bevobj, 0);
1289 dl= cu->bevobj->disp.first;
1294 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1295 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1297 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1298 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1300 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1302 BLI_addtail(disp, dlnew);
1304 nr= dlnew->parts*dlnew->nr;
1315 freedisplist(&bevdisp);
1318 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1321 else if(cu->ext2==0.0f) {
1322 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1323 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1324 BLI_addtail(disp, dl);
1327 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1336 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1338 nr= 4+ 2*cu->bevresol;
1340 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1341 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1342 BLI_addtail(disp, dl);
1345 dl->flag= DL_BACK_CURVE;
1350 dangle= (2.0f*(float)M_PI/(nr));
1351 angle= -(nr-1)*dangle;
1353 for(a=0; a<nr; a++) {
1355 fp[1]= (cosf(angle)*(cu->ext2));
1356 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1364 /* bevel now in three parts, for proper vertex normals */
1367 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1368 dnr= nr= 2+ cu->bevresol;
1369 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1370 nr= 3+ 2*cu->bevresol;
1372 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1373 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1374 BLI_addtail(disp, dl);
1377 dl->flag= DL_BACK_CURVE;
1382 dangle= (0.5*M_PI/(dnr-1));
1383 angle= -(nr-1)*dangle;
1385 for(a=0; a<nr; a++) {
1387 fp[1]= (float)(cosf(angle)*(cu->ext2));
1388 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1394 /* part 2, sidefaces */
1395 if(cu->ext1!=0.0f) {
1398 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1399 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1400 BLI_addtail(disp, dl);
1411 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1412 dl= MEM_dupallocN(dl);
1413 dl->verts= MEM_dupallocN(dl->verts);
1414 BLI_addtail(disp, dl);
1425 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1426 dnr= nr= 2+ cu->bevresol;
1427 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1428 nr= 3+ 2*cu->bevresol;
1430 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1431 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1432 BLI_addtail(disp, dl);
1434 dl->flag= DL_FRONT_CURVE;
1441 dangle= (0.5*M_PI/(dnr-1));
1443 for(a=0; a<nr; a++) {
1445 fp[1]= (float)(cosf(angle)*(cu->ext2));
1446 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1454 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1458 0: no intersection of segments
1459 1: exact intersection of segments
1460 2: cross-intersection of segments
1464 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1465 if(deler==0.0f) return -1;
1467 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1468 *labda= -(*labda/deler);
1470 deler= v3[coy]-v4[coy];
1472 deler=v3[cox]-v4[cox];
1473 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1475 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1477 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1478 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1480 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1481 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1488 static short bevelinside(BevList *bl1,BevList *bl2)
1490 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1491 /* returns '1' if correct hole */
1492 BevPoint *bevp, *prevbevp;
1493 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1494 int nr, links=0,rechts=0,mode;
1496 /* take first vertex of possible hole */
1498 bevp= (BevPoint *)(bl2+1);
1499 hvec1[0]= bevp->vec[0];
1500 hvec1[1]= bevp->vec[1];
1502 VECCOPY(hvec2,hvec1);
1505 /* test it with all edges of potential surounding poly */
1506 /* count number of transitions left-right */
1508 bevp= (BevPoint *)(bl1+1);
1510 prevbevp= bevp+(nr-1);
1513 min= prevbevp->vec[1];
1517 max= prevbevp->vec[1];
1520 if(min<=hvec1[1] && max>=hvec1[1]) {
1521 /* there's a transition, calc intersection point */
1522 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1523 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1524 only allow for one situation: we choose lab= 1.0
1526 if(mode >= 0 && lab != 0.0f) {
1527 if(vec[0]<hvec1[0]) links++;
1536 if( (links & 1) && (rechts & 1) ) return 1;
1547 static int vergxcobev(const void *a1, const void *a2)
1549 const struct bevelsort *x1=a1,*x2=a2;
1551 if( x1->left > x2->left ) return 1;
1552 else if( x1->left < x2->left) return -1;
1556 /* this function cannot be replaced with atan2, but why? */
1558 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1560 float t01, t02, x3, y3;
1562 t01= (float)sqrt(x1*x1+y1*y1);
1563 t02= (float)sqrt(x2*x2+y2*y2);
1564 if(t01==0.0f) t01= 1.0f;
1565 if(t02==0.0f) t02= 1.0f;
1573 if(fabs(t02)>=1.0) t02= .5*M_PI;
1574 else t02= (saacos(t02))/2.0f;
1576 t02= (float)sin(t02);
1577 if(t02==0.0f) t02= 1.0f;
1581 if(x3==0 && y3==0) {
1585 t01= (float)sqrt(x3*x3+y3*y3);
1595 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1597 BezTriple *pprev, *next, *last;
1598 float fac, dfac, t[4];
1601 if(tilt_array==NULL && radius_array==NULL)
1604 last= nu->bezt+(nu->pntsu-1);
1606 /* returns a point */
1607 if(prevbezt==nu->bezt) {
1608 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1609 else pprev= prevbezt;
1611 else pprev= prevbezt-1;
1615 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1621 dfac= 1.0f/(float)resolu;
1623 for(a=0; a<resolu; a++, fac+= dfac) {
1625 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1626 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1628 key_curve_position_weights(fac, t, nu->tilt_interp);
1629 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1632 tilt_array = (float *)(((char *)tilt_array) + stride);
1636 if (nu->radius_interp==KEY_CU_EASE) {
1637 /* Support 2.47 ease interp
1638 * Note! - this only takes the 2 points into account,
1639 * giving much more localized results to changes in radius, sometimes you want that */
1640 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1643 /* reuse interpolation from tilt if we can */
1644 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1645 key_curve_position_weights(fac, t, nu->radius_interp);
1647 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1650 radius_array = (float *)(((char *)radius_array) + stride);
1654 /* basic interpolation for now, could copy tilt interp too */
1655 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1657 weight_array = (float *)(((char *)weight_array) + stride);
1662 /* make_bevel_list_3D_* funcs, at a minimum these must
1663 * fill in the bezp->quat and bezp->dir values */
1665 /* correct non-cyclic cases by copying direction and rotation
1666 * values onto the first & last end-points */
1667 static void bevel_list_cyclic_fix_3D(BevList *bl)
1669 BevPoint *bevp, *bevp1;
1671 bevp= (BevPoint *)(bl+1);
1673 QUATCOPY(bevp->quat, bevp1->quat);
1674 VECCOPY(bevp->dir, bevp1->dir);
1675 VECCOPY(bevp->tan, bevp1->tan);
1676 bevp= (BevPoint *)(bl+1);
1679 QUATCOPY(bevp->quat, bevp1->quat);
1680 VECCOPY(bevp->dir, bevp1->dir);
1681 VECCOPY(bevp->tan, bevp1->tan);
1683 /* utility for make_bevel_list_3D_* funcs */
1684 static void bevel_list_calc_bisect(BevList *bl)
1686 BevPoint *bevp2, *bevp1, *bevp0;
1689 bevp2= (BevPoint *)(bl+1);
1690 bevp1= bevp2+(bl->nr-1);
1695 /* totally simple */
1696 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1703 static void bevel_list_flip_tangents(BevList *bl)
1705 BevPoint *bevp2, *bevp1, *bevp0;
1708 bevp2= (BevPoint *)(bl+1);
1709 bevp1= bevp2+(bl->nr-1);
1714 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1715 negate_v3(bevp1->tan);
1722 /* apply user tilt */
1723 static void bevel_list_apply_tilt(BevList *bl)
1725 BevPoint *bevp2, *bevp1;
1729 bevp2= (BevPoint *)(bl+1);
1730 bevp1= bevp2+(bl->nr-1);
1734 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1735 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1736 normalize_qt(bevp1->quat);
1742 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1743 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1745 BevPoint *bevp2, *bevp1, *bevp0;
1749 float bevp0_quat[4];
1752 for(a=0; a < smooth_iter; a++) {
1754 bevp2= (BevPoint *)(bl+1);
1755 bevp1= bevp2+(bl->nr-1);
1760 if(bl->poly== -1) { /* check its not cyclic */
1761 /* skip the first point */
1774 QUATCOPY(bevp0_quat, bevp0->quat);
1777 /* interpolate quats */
1778 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1779 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1782 mul_qt_v3(q, zaxis);
1783 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1784 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1787 QUATCOPY(bevp0_quat, bevp1->quat);
1788 mul_qt_qtqt(q, q2, q);
1789 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1790 normalize_qt(bevp1->quat);
1793 /* bevp0= bevp1; */ /* UNUSED */
1800 static void make_bevel_list_3D_zup(BevList *bl)
1802 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1805 bevp2= (BevPoint *)(bl+1);
1806 bevp1= bevp2+(bl->nr-1);
1811 /* totally simple */
1812 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1813 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1821 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1823 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1827 bevel_list_calc_bisect(bl);
1829 bevp2= (BevPoint *)(bl+1);
1830 bevp1= bevp2+(bl->nr-1);
1836 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1837 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1840 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1842 if(angle > 0.0f) { /* otherwise we can keep as is */
1844 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1845 axis_angle_to_quat(q, cross_tmp, angle);
1846 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1849 QUATCOPY(bevp1->quat, bevp0->quat);
1858 if(bl->poly != -1) { /* check for cyclic */
1860 /* Need to correct for the start/end points not matching
1861 * do this by calculating the tilt angle difference, then apply
1862 * the rotation gradually over the entire curve
1864 * note that the split is between last and second last, rather than first/last as youd expect.
1866 * real order is like this
1867 * 0,1,2,3,4 --> 1,2,3,4,0
1869 * this is why we compare last with second last
1871 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1873 BevPoint *bevp_first;
1874 BevPoint *bevp_last;
1877 bevp_first= (BevPoint *)(bl+1);
1878 bevp_first+= bl->nr-1;
1879 bevp_last = bevp_first;
1882 /* quats and vec's are normalized, should not need to re-normalize */
1883 mul_qt_v3(bevp_first->quat, vec_1);
1884 mul_qt_v3(bevp_last->quat, vec_2);
1885 normalize_v3(vec_1);
1886 normalize_v3(vec_2);
1888 /* align the vector, can avoid this and it looks 98% OK but
1889 * better to align the angle quat roll's before comparing */
1891 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1892 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1893 axis_angle_to_quat(q, cross_tmp, angle);
1894 mul_qt_v3(q, vec_2);
1897 angle= angle_normalized_v3v3(vec_1, vec_2);
1899 /* flip rotation if needs be */
1900 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1901 normalize_v3(cross_tmp);
1902 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90.0f/(float)(180.0/M_PI))
1905 bevp2= (BevPoint *)(bl+1);
1906 bevp1= bevp2+(bl->nr-1);
1911 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1913 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1914 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1923 static void make_bevel_list_3D_tangent(BevList *bl)
1925 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1928 float bevp0_tan[3], cross_tmp[3];
1930 bevel_list_calc_bisect(bl);
1931 if(bl->poly== -1) /* check its not cyclic */
1932 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1933 bevel_list_flip_tangents(bl);
1935 /* correct the tangents */
1936 bevp2= (BevPoint *)(bl+1);
1937 bevp1= bevp2+(bl->nr-1);
1943 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1944 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1945 normalize_v3(bevp1->tan);
1953 /* now for the real twist calc */
1954 bevp2= (BevPoint *)(bl+1);
1955 bevp1= bevp2+(bl->nr-1);
1958 VECCOPY(bevp0_tan, bevp0->tan);
1963 /* make perpendicular, modify tan in place, is ok */
1965 float zero[3] = {0,0,0};
1967 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1968 normalize_v3(cross_tmp);
1969 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1971 /* bevp0= bevp1; */ /* UNUSED */
1977 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1979 switch(twist_mode) {
1980 case CU_TWIST_TANGENT:
1981 make_bevel_list_3D_tangent(bl);
1983 case CU_TWIST_MINIMUM:
1984 make_bevel_list_3D_minimum_twist(bl);
1986 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1987 make_bevel_list_3D_zup(bl);
1990 if(bl->poly== -1) /* check its not cyclic */
1991 bevel_list_cyclic_fix_3D(bl);
1994 bevel_list_smooth(bl, smooth_iter);
1996 bevel_list_apply_tilt(bl);
2001 /* only for 2 points */
2002 static void make_bevel_list_segment_3D(BevList *bl)
2006 BevPoint *bevp2= (BevPoint *)(bl+1);
2007 BevPoint *bevp1= bevp2+1;
2009 /* simple quat/dir */
2010 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2011 normalize_v3(bevp1->dir);
2013 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2015 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2016 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2017 normalize_qt(bevp1->quat);
2018 VECCOPY(bevp2->dir, bevp1->dir);
2019 QUATCOPY(bevp2->quat, bevp1->quat);
2024 void makeBevelList(Object *ob)
2027 - convert all curves to polys, with indication of resol and flags for double-vertices
2028 - possibly; do a smart vertice removal (in case Nurb)
2029 - separate in individual blicks with BoundBox
2030 - AutoHole detection
2034 BezTriple *bezt, *prevbezt;
2036 BevList *bl, *blnew, *blnext;
2037 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2038 float min, inp, x1, x2, y1, y2;
2039 struct bevelsort *sortdata, *sd, *sd1;
2040 int a, b, nr, poly, resolu = 0, len = 0;
2041 int do_tilt, do_radius, do_weight;
2043 /* this function needs an object, because of tflag and upflag */
2046 /* do we need to calculate the radius for each point? */
2047 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2049 /* STEP 1: MAKE POLYS */
2051 BLI_freelistN(&(cu->bev));
2052 if(cu->editnurb && ob->type!=OB_FONT) {
2053 ListBase *nurbs= ED_curve_editnurbs(cu);
2055 } else nu= cu->nurb.first;
2059 /* check if we will calculate tilt data */
2060 do_tilt = CU_DO_TILT(cu, nu);
2061 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2064 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2065 * enforced in the UI but can go wrong possibly */
2066 if(!check_valid_nurb_u(nu)) {
2067 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2068 BLI_addtail(&(cu->bev), bl);
2071 if(G.rendering && cu->resolu_ren!=0)
2072 resolu= cu->resolu_ren;
2076 if(nu->type == CU_POLY) {
2078 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2079 BLI_addtail(&(cu->bev), bl);
2081 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2085 bevp= (BevPoint *)(bl+1);
2089 VECCOPY(bevp->vec, bp->vec);
2090 bevp->alfa= bp->alfa;
2091 bevp->radius= bp->radius;
2092 bevp->weight= bp->weight;
2093 bevp->split_tag= TRUE;
2098 else if(nu->type == CU_BEZIER) {
2100 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2101 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2102 BLI_addtail(&(cu->bev), bl);
2104 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2106 bevp= (BevPoint *)(bl+1);
2110 if(nu->flagu & CU_NURB_CYCLIC) {
2112 prevbezt= nu->bezt+(nu->pntsu-1);
2120 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2122 VECCOPY(bevp->vec, prevbezt->vec[1]);
2123 bevp->alfa= prevbezt->alfa;
2124 bevp->radius= prevbezt->radius;
2125 bevp->weight= prevbezt->weight;
2126 bevp->split_tag= TRUE;
2127 bevp->dupe_tag= FALSE;
2133 /* always do all three, to prevent data hanging around */
2136 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2137 for(j=0; j<3; j++) {
2138 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2139 bezt->vec[0][j], bezt->vec[1][j],
2140 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2143 /* if both arrays are NULL do nothiong */
2144 alfa_bezpart( prevbezt, bezt, nu,
2145 do_tilt ? &bevp->alfa : NULL,
2146 do_radius ? &bevp->radius : NULL,
2147 do_weight ? &bevp->weight : NULL,
2148 resolu, sizeof(BevPoint));
2151 if(cu->twist_mode==CU_TWIST_TANGENT) {
2152 forward_diff_bezier_cotangent(
2153 prevbezt->vec[1], prevbezt->vec[2],
2154 bezt->vec[0], bezt->vec[1],
2155 bevp->tan, resolu, sizeof(BevPoint));
2158 /* indicate with handlecodes double points */
2159 if(prevbezt->h1==prevbezt->h2) {
2160 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2163 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2164 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2173 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2174 VECCOPY(bevp->vec, prevbezt->vec[1]);
2175 bevp->alfa= prevbezt->alfa;
2176 bevp->radius= prevbezt->radius;
2177 bevp->weight= prevbezt->weight;
2181 else if(nu->type == CU_NURBS) {
2183 len= (resolu*SEGMENTSU(nu));
2185 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2186 BLI_addtail(&(cu->bev), bl);
2189 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2191 bevp= (BevPoint *)(bl+1);
2193 makeNurbcurve( nu, &bevp->vec[0],
2194 do_tilt ? &bevp->alfa : NULL,
2195 do_radius ? &bevp->radius : NULL,
2196 do_weight ? &bevp->weight : NULL,
2197 resolu, sizeof(BevPoint));
2204 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2207 if (bl->nr) { /* null bevel items come from single points */
2209 bevp1= (BevPoint *)(bl+1);
2210 bevp0= bevp1+(nr-1);
2213 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2214 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2215 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2216 bevp0->dupe_tag= TRUE;
2230 if(bl->nr && bl->dupe_nr) {
2231 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2232 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2233 memcpy(blnew, bl, sizeof(BevList));
2235 BLI_remlink(&(cu->bev), bl);
2236 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2237 bevp0= (BevPoint *)(bl+1);
2238 bevp1= (BevPoint *)(blnew+1);
2241 if(bevp0->dupe_tag==0) {
2242 memcpy(bevp1, bevp0, sizeof(BevPoint));
2254 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2258 if(bl->nr && bl->poly>=0) {
2267 /* find extreme left points, also test (turning) direction */
2269 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2275 bevp= (BevPoint *)(bl+1);
2278 if(min>bevp->vec[0]) {
2287 bevp= (BevPoint *)(bl+1);
2288 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2289 else bevp0= bevp1-1;
2290 bevp= bevp+ (bl->nr-1);
2291 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2292 else bevp2= bevp1+1;
2294 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]);
2296 if(inp > 0.0f) sd->dir= 1;
2304 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2307 for(a=1; a<poly; a++, sd++) {
2308 bl= sd->bl; /* is bl a hole? */
2309 sd1= sortdata+ (a-1);
2310 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2311 if(bevelinside(sd1->bl, bl)) {
2312 bl->hole= 1- sd1->bl->hole;
2318 /* turning direction */
2319 if((cu->flag & CU_3D)==0) {
2321 for(a=0; a<poly; a++, sd++) {
2322 if(sd->bl->hole==sd->dir) {
2324 bevp1= (BevPoint *)(bl+1);
2325 bevp2= bevp1+ (bl->nr-1);
2328 SWAP(BevPoint, *bevp1, *bevp2);
2335 MEM_freeN(sortdata);
2338 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2339 if((cu->flag & CU_3D)==0) {
2340 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2341 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2348 else if(bl->nr==2) { /* 2 pnt, treat separate */
2349 bevp2= (BevPoint *)(bl+1);
2352 x1= bevp1->vec[0]- bevp2->vec[0];
2353 y1= bevp1->vec[1]- bevp2->vec[1];
2355 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2356 bevp2->sina= bevp1->sina;
2357 bevp2->cosa= bevp1->cosa;
2359 /* fill in dir & quat */
2360 make_bevel_list_segment_3D(bl);
2363 bevp2= (BevPoint *)(bl+1);
2364 bevp1= bevp2+(bl->nr-1);
2369 x1= bevp1->vec[0]- bevp0->vec[0];
2370 x2= bevp1->vec[0]- bevp2->vec[0];
2371 y1= bevp1->vec[1]- bevp0->vec[1];
2372 y2= bevp1->vec[1]- bevp2->vec[1];
2374 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2376 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2377 * no need for tricky tilt calculation as with 3D curves */
2378 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2379 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2380 /* done with inline make_bevel_list_3D_zup */
2387 /* correct non-cyclic cases */
2389 bevp= (BevPoint *)(bl+1);
2391 bevp->sina= bevp1->sina;
2392 bevp->cosa= bevp1->cosa;
2393 bevp= (BevPoint *)(bl+1);
2396 bevp->sina= bevp1->sina;
2397 bevp->cosa= bevp1->cosa;
2399 /* correct for the dir/quat, see above why its needed */
2400 bevel_list_cyclic_fix_3D(bl);
2406 else { /* 3D Curves */
2413 else if(bl->nr==2) { /* 2 pnt, treat separate */
2414 make_bevel_list_segment_3D(bl);
2417 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2424 /* ****************** HANDLES ************** */
2428 * 0: nothing, 1:auto, 2:vector, 3:aligned
2431 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2432 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2434 float *p1,*p2,*p3, pt[3];
2435 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2436 const float eps= 1e-5;
2438 if(bezt->h1==0 && bezt->h2==0) return;
2444 pt[0]= 2*p2[0]- p3[0];
2445 pt[1]= 2*p2[1]- p3[1];
2446 pt[2]= 2*p2[2]- p3[2];
2449 else p1= prev->vec[1];
2452 pt[0]= 2*p2[0]- p1[0];
2453 pt[1]= 2*p2[1]- p1[1];
2454 pt[2]= 2*p2[2]- p1[2];
2457 else p3= next->vec[1];
2464 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2471 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2473 if(len1==0.0f) len1=1.0f;
2474 if(len2==0.0f) len2=1.0f;
2477 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2478 vx= dx1/len2 + dx/len1;
2479 vy= dy1/len2 + dy/len1;
2480 vz= dz1/len2 + dz/len1;
2481 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2483 int leftviolate=0, rightviolate=0; /* for mode==2 */
2485 if(len1>5.0f*len2) len1= 5.0f*len2;
2486 if(len2>5.0f*len1) len2= 5.0f*len1;
2488 if(bezt->h1==HD_AUTO) {
2490 *(p2-3)= *p2-vx*len1;
2491 *(p2-2)= *(p2+1)-vy*len1;
2492 *(p2-1)= *(p2+2)-vz*len1;
2494 if(mode==2 && next && prev) { // keep horizontal if extrema
2495 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2496 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2497 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2498 bezt->vec[0][1]= bezt->vec[1][1];
2500 else { // handles should not be beyond y coord of two others
2501 if(ydiff1 <= 0.0f) {
2502 if(prev->vec[1][1] > bezt->vec[0][1]) {
2503 bezt->vec[0][1]= prev->vec[1][1];
2508 if(prev->vec[1][1] < bezt->vec[0][1]) {
2509 bezt->vec[0][1]= prev->vec[1][1];
2516 if(bezt->h2==HD_AUTO) {
2518 *(p2+3)= *p2+vx*len2;
2519 *(p2+4)= *(p2+1)+vy*len2;
2520 *(p2+5)= *(p2+2)+vz*len2;
2522 if(mode==2 && next && prev) { // keep horizontal if extrema
2523 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2524 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2525 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2526 bezt->vec[2][1]= bezt->vec[1][1];
2528 else { // handles should not be beyond y coord of two others
2529 if(ydiff1 <= 0.0f) {
2530 if(next->vec[1][1] < bezt->vec[2][1]) {
2531 bezt->vec[2][1]= next->vec[1][1];
2536 if(next->vec[1][1] > bezt->vec[2][1]) {
2537 bezt->vec[2][1]= next->vec[1][1];
2544 if(leftviolate || rightviolate) { /* align left handle */
2547 sub_v3_v3v3(h1, p2-3, p2);
2548 sub_v3_v3v3(h2, p2, p2+3);
2549 len1= normalize_v3(h1);
2550 len2= normalize_v3(h2);
2555 *(p2+3)= *(p2) - vz*len2*h1[0];
2556 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2557 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2560 *(p2-3)= *(p2) + vz*len1*h2[0];
2561 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2562 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2569 if(bezt->h1==HD_VECT) { /* vector */
2574 *(p2-2)= *(p2+1)-dy;
2575 *(p2-1)= *(p2+2)-dz;
2577 if(bezt->h2==HD_VECT) {
2582 *(p2+4)= *(p2+1)+dy1;
2583 *(p2+5)= *(p2+2)+dz1;
2586 len2= len_v3v3(p2, p2+3);
2587 len1= len_v3v3(p2, p2-3);
2588 if(len1==0.0f) len1= 1.0f;
2589 if(len2==0.0f) len2= 1.0f;
2591 if(bezt->f1 & SELECT) { /* order of calculation */
2592 if(bezt->h2==HD_ALIGN) { /* aligned */
2595 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2596 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2597 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2600 if(bezt->h1==HD_ALIGN) {
2603 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2604 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2605 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2610 if(bezt->h1==HD_ALIGN) {
2613 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2614 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2615 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2618 if(bezt->h2==HD_ALIGN) { /* aligned */
2621 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2622 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2623 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2629 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2631 BezTriple *bezt, *prev, *next;
2634 if(nu->type != CU_BEZIER) return;
2635 if(nu->pntsu<2) return;
2639 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2644 calchandleNurb(bezt, prev, next, 0);
2647 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2657 void testhandlesNurb(Nurb *nu)
2659 /* use when something has changed with handles.
2660 it treats all BezTriples with the following rules:
2661 PHASE 1: do types have to be altered?
2662 Auto handles: become aligned when selection status is NOT(000 || 111)
2663 Vector handles: become 'nothing' when (one half selected AND other not)
2664 PHASE 2: recalculate handles
2669 if(nu->type != CU_BEZIER) return;
2675 if(bezt->f1 & SELECT) flag++;
2676 if(bezt->f2 & SELECT) flag += 2;
2677 if(bezt->f3 & SELECT) flag += 4;
2679 if( !(flag==0 || flag==7) ) {
2680 if(bezt->h1==HD_AUTO) { /* auto */
2683 if(bezt->h2==HD_AUTO) { /* auto */
2687 if(bezt->h1==HD_VECT) { /* vector */
2688 if(flag < 4) bezt->h1= 0;
2690 if(bezt->h2==HD_VECT) { /* vector */
2691 if( flag > 3) bezt->h2= 0;
2697 calchandlesNurb(nu);
2700 void autocalchandlesNurb(Nurb *nu, int flag)
2702 /* checks handle coordinates and calculates type */
2704 BezTriple *bezt2, *bezt1, *bezt0;
2705 int i, align, leftsmall, rightsmall;
2707 if(nu==NULL || nu->bezt==NULL) return;
2710 bezt1 = bezt2 + (nu->pntsu-1);
2716 align= leftsmall= rightsmall= 0;
2719 if(flag==0 || (bezt1->f1 & flag) ) {
2721 /* distance too short: vectorhandle */
2722 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2727 /* aligned handle? */
2728 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2730 bezt1->h1= HD_ALIGN;
2732 /* or vector handle? */
2733 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2739 if(flag==0 || (bezt1->f3 & flag) ) {
2741 /* distance too short: vectorhandle */
2742 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2747 /* aligned handle? */
2748 if(align) bezt1->h2= HD_ALIGN;
2750 /* or vector handle? */
2751 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2756 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2757 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2759 /* undesired combination: */
2760 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2761 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2768 calchandlesNurb(nu);
2771 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2775 nu= editnurb->first;
2777 autocalchandlesNurb(nu, flag);
2782 void sethandlesNurb(ListBase *editnurb, short code)
2784 /* code==1: set autohandle */
2785 /* code==2: set vectorhandle */
2786 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2787 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2788 /* code==5: Set align, like 3 but no toggle */
2789 /* code==6: Clear align, like 3 but no toggle */
2794 if(code==1 || code==2) {
2795 nu= editnurb->first;
2797 if(nu->type == CU_BEZIER) {
2801 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2802 if(bezt->f1 & SELECT) bezt->h1= code;
2803 if(bezt->f3 & SELECT) bezt->h2= code;
2804 if(bezt->h1!=bezt->h2) {
2805 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2806 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2811 calchandlesNurb(nu);
2817 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2819 nu= editnurb->first;
2822 } else if (code == 6) {
2827 if(nu->type == CU_BEZIER) {
2831 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2832 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2842 nu= editnurb->first;
2844 if(nu->type == CU_BEZIER) {
2848 if(bezt->f1 & SELECT) bezt->h1= ok;
2849 if(bezt->f3 & SELECT) bezt->h2= ok;
2853 calchandlesNurb(nu);
2860 static void swapdata(void *adr1, void *adr2, int len)
2868 memcpy(adr, adr1, len);
2869 memcpy(adr1, adr2, len);
2870 memcpy(adr2, adr, len);
2875 adr= (char *)MEM_mallocN(len, "curve swap");
2876 memcpy(adr, adr1, len);
2877 memcpy(adr1, adr2, len);
2878 memcpy(adr2, adr, len);
2883 void switchdirectionNurb(Nurb *nu)
2885 BezTriple *bezt1, *bezt2;
2887 float *fp1, *fp2, *tempf;
2890 if(nu->pntsu==1 && nu->pntsv==1) return;
2892 if(nu->type == CU_BEZIER) {
2896 if(a & 1) a+= 1; /* if odd, also swap middle content */
2899 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2901 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2902 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2904 SWAP(char, bezt1->h1, bezt1->h2);
2905 SWAP(short, bezt1->f1, bezt1->f3);
2908 SWAP(char, bezt2->h1, bezt2->h2);
2909 SWAP(short, bezt2->f1, bezt2->f3);
2910 bezt1->alfa= -bezt1->alfa;
2911 bezt2->alfa= -bezt2->alfa;
2918 else if(nu->pntsv==1) {
2923 while(bp1!=bp2 && a>0) {
2924 SWAP(BPoint, *bp1, *bp2);
2926 bp1->alfa= -bp1->alfa;
2927 bp2->alfa= -bp2->alfa;
2931 if(nu->type == CU_NURBS) {
2932 /* no knots for too short paths */
2939 while(fp1!=fp2 && a>0) {
2940 SWAP(float, *fp1, *fp2);
2945 /* and make in increasing order again */
2948 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2950 fp2[0]= fabs(fp1[1]-fp1[0]);