7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 #include <math.h> // floor
37 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_material_types.h"
44 /* for dereferencing pointers */
45 #include "DNA_key_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_vfont_types.h"
48 #include "DNA_object_types.h"
50 #include "BKE_animsys.h"
52 #include "BKE_curve.h"
53 #include "BKE_displist.h"
55 #include "BKE_global.h"
57 #include "BKE_library.h"
59 #include "BKE_object.h"
60 #include "BKE_utildefines.h" // VECCOPY
67 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
69 float *labda, float *mu, float *vec);
71 void unlink_curve(Curve *cu)
75 for(a=0; a<cu->totcol; a++) {
76 if(cu->mat[a]) cu->mat[a]->id.us--;
79 if(cu->vfont) cu->vfont->id.us--;
81 if(cu->key) cu->key->id.us--;
85 /* frees editcurve entirely */
86 void BKE_free_editfont(Curve *cu)
89 EditFont *ef= cu->editfont;
91 if(ef->oldstr) MEM_freeN(ef->oldstr);
92 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
93 if(ef->textbuf) MEM_freeN(ef->textbuf);
94 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
95 if(ef->copybuf) MEM_freeN(ef->copybuf);
96 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
103 /* don't free curve itself */
104 void free_curve(Curve *cu)
106 freeNurblist(&cu->nurb);
107 BLI_freelistN(&cu->bev);
108 freedisplist(&cu->disp);
109 BKE_free_editfont(cu);
111 free_curve_editNurb(cu);
113 BKE_free_animdata((ID *)cu);
115 if(cu->mat) MEM_freeN(cu->mat);
116 if(cu->str) MEM_freeN(cu->str);
117 if(cu->strinfo) MEM_freeN(cu->strinfo);
118 if(cu->bb) MEM_freeN(cu->bb);
119 if(cu->path) free_path(cu->path);
120 if(cu->tb) MEM_freeN(cu->tb);
123 Curve *add_curve(char *name, int type)
127 cu= alloc_libblock(&G.main->curve, ID_CU, name);
129 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
130 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
132 cu->resolu= cu->resolv= 12;
135 cu->spacing= cu->linedist= 1.0;
138 cu->texflag= CU_AUTOSPACE;
139 cu->smallcaps_scale= 0.75f;
140 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
142 cu->bb= unit_boundbox();
145 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
147 cu->str= MEM_mallocN(12, "str");
148 strcpy(cu->str, "Text");
150 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
151 cu->totbox= cu->actbox= 1;
152 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
153 cu->tb[0].w = cu->tb[0].h = 0.0;
159 Curve *copy_curve(Curve *cu)
164 cun= copy_libblock(cu);
165 cun->nurb.first= cun->nurb.last= 0;
166 duplicateNurblist( &(cun->nurb), &(cu->nurb));
168 cun->mat= MEM_dupallocN(cu->mat);
169 for(a=0; a<cun->totcol; a++) {
170 id_us_plus((ID *)cun->mat[a]);
173 cun->str= MEM_dupallocN(cu->str);
174 cun->strinfo= MEM_dupallocN(cu->strinfo);
175 cun->tb= MEM_dupallocN(cu->tb);
176 cun->bb= MEM_dupallocN(cu->bb);
178 cun->key= copy_key(cu->key);
179 if(cun->key) cun->key->from= (ID *)cun;
181 cun->disp.first= cun->disp.last= 0;
182 cun->bev.first= cun->bev.last= 0;
188 #if 0 // XXX old animation system
189 /* single user ipo too */
190 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
191 #endif // XXX old animation system
193 id_us_plus((ID *)cun->vfont);
194 id_us_plus((ID *)cun->vfontb);
195 id_us_plus((ID *)cun->vfonti);
196 id_us_plus((ID *)cun->vfontbi);
201 void make_local_curve(Curve *cu)
207 /* - when there are only lib users: don't do
208 * - when there are only local users: set flag
212 if(cu->id.lib==0) return;
214 if(cu->vfont) cu->vfont->id.lib= 0;
218 cu->id.flag= LIB_LOCAL;
219 new_id(0, (ID *)cu, 0);
223 ob= G.main->object.first;
226 if(ob->id.lib) lib= 1;
232 if(local && lib==0) {
234 cu->id.flag= LIB_LOCAL;
235 new_id(0, (ID *)cu, 0);
237 else if(local && lib) {
241 ob= G.main->object.first;
256 short curve_type(Curve *cu)
262 for (nu= cu->nurb.first; nu; nu= nu->next) {
271 void test_curve_type(Object *ob)
273 ob->type = curve_type(ob->data);
276 void tex_space_curve(Curve *cu)
280 float *fp, min[3], max[3];
283 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
286 INIT_MINMAX(min, max);
291 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
292 else tot= dl->nr*dl->parts;
297 DO_MINMAX(fp, min, max);
304 min[0] = min[1] = min[2] = -1.0f;
305 max[0] = max[1] = max[2] = 1.0f;
308 boundbox_set_from_min_max(bb, min, max);
310 if(cu->texflag & CU_AUTOSPACE) {
311 mid_v3_v3v3(cu->loc, min, max);
312 cu->size[0]= (max[0]-min[0])/2.0f;
313 cu->size[1]= (max[1]-min[1])/2.0f;
314 cu->size[2]= (max[2]-min[2])/2.0f;
316 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
318 if(cu->size[0]==0.0) cu->size[0]= 1.0;
319 else if(cu->size[0]>0.0 && cu->size[0]<0.00001) cu->size[0]= 0.00001;
320 else if(cu->size[0]<0.0 && cu->size[0]> -0.00001) cu->size[0]= -0.00001;
322 if(cu->size[1]==0.0) cu->size[1]= 1.0;
323 else if(cu->size[1]>0.0 && cu->size[1]<0.00001) cu->size[1]= 0.00001;
324 else if(cu->size[1]<0.0 && cu->size[1]> -0.00001) cu->size[1]= -0.00001;
326 if(cu->size[2]==0.0) cu->size[2]= 1.0;
327 else if(cu->size[2]>0.0 && cu->size[2]<0.00001) cu->size[2]= 0.00001;
328 else if(cu->size[2]<0.0 && cu->size[2]> -0.00001) cu->size[2]= -0.00001;
334 int count_curveverts(ListBase *nurb)
341 if(nu->bezt) tot+= 3*nu->pntsu;
342 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
349 int count_curveverts_without_handles(ListBase *nurb)
356 if(nu->bezt) tot+= nu->pntsu;
357 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
364 /* **************** NURBS ROUTINES ******************** */
366 void freeNurb(Nurb *nu)
371 if(nu->bezt) MEM_freeN(nu->bezt);
373 if(nu->bp) MEM_freeN(nu->bp);
375 if(nu->knotsu) MEM_freeN(nu->knotsu);
377 if(nu->knotsv) MEM_freeN(nu->knotsv);
379 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
386 void freeNurblist(ListBase *lb)
398 lb->first= lb->last= 0;
401 Nurb *duplicateNurb(Nurb *nu)
406 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
407 if(newnu==0) return 0;
408 memcpy(newnu, nu, sizeof(Nurb));
412 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
413 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
416 len= nu->pntsu*nu->pntsv;
418 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
419 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
421 newnu->knotsu= newnu->knotsv= NULL;
426 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
427 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
430 if(nu->pntsv>1 && nu->knotsv) {
433 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
434 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
441 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
449 nun= duplicateNurb(nu);
450 BLI_addtail(lb1, nun);
456 void test2DNurb(Nurb *nu)
462 if((nu->flag & CU_2D)==0)
465 if(nu->type == CU_BEZIER) {
469 bezt->vec[0][2]= 0.0;
470 bezt->vec[1][2]= 0.0;
471 bezt->vec[2][2]= 0.0;
476 a= nu->pntsu*nu->pntsv;
485 void minmaxNurb(Nurb *nu, float *min, float *max)
491 if(nu->type == CU_BEZIER) {
495 DO_MINMAX(bezt->vec[0], min, max);
496 DO_MINMAX(bezt->vec[1], min, max);
497 DO_MINMAX(bezt->vec[2], min, max);
502 a= nu->pntsu*nu->pntsv;
505 DO_MINMAX(bp->vec, min, max);
511 /* be sure to call makeknots after this */
512 void addNurbPoints(Nurb *nu, int number)
516 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
519 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
523 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
525 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
532 void addNurbPointsBezier(Nurb *nu, int number)
534 BezTriple *tmp= nu->bezt;
536 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
539 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
543 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
545 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
552 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
555 static void calcknots(float *knots, short aantal, short order, short type)
556 /* knots: number of pnts NOT corrected for cyclic */
557 /* type; 0: uniform, 1: endpoints, 2: bezier */
573 if(a>=order && a<=aantal) k+= 1.0;
577 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
581 knots[a]= (float)floor(k);
588 if(a>=order && a<=aantal) k+= (0.5);
589 knots[a]= (float)floor(k);
593 printf("bez nurb curve order is not 3 or 4, should never happen\n");
598 static void makecyclicknots(float *knots, short pnts, short order)
599 /* pnts, order: number of pnts NOT corrected for cyclic */
607 /* do first long rows (order -1), remove identical knots at endpoints */
610 for(a=1; a<order2; a++) {
611 if(knots[b]!= knots[b-a]) break;
613 if(a==order2) knots[pnts+order-2]+= 1.0;
617 c=pnts + order + order2;
618 for(a=pnts+order2; a<c; a++) {
619 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
626 void makeknots(Nurb *nu, short uv)
628 if(nu->type == CU_NURBS) {
630 if(nu->knotsu) MEM_freeN(nu->knotsu);
631 if(check_valid_nurb_u(nu)) {
632 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
633 if(nu->flagu & CU_NURB_CYCLIC) {
634 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
635 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
637 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
640 else nu->knotsu= NULL;
643 if(nu->knotsv) MEM_freeN(nu->knotsv);
644 if(check_valid_nurb_v(nu)) {
645 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
646 if(nu->flagv & CU_NURB_CYCLIC) {
647 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
648 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
650 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
653 else nu->knotsv= NULL;
658 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
661 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
663 orderpluspnts= order+pnts;
664 opp2 = orderpluspnts-1;
666 /* this is for float inaccuracy */
667 if(t < knots[0]) t= knots[0];
668 else if(t > knots[opp2]) t= knots[opp2];
670 /* this part is order '1' */
672 for(i=0;i<opp2;i++) {
673 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
689 /* this is order 2,3,... */
690 for(j=2; j<=order; j++) {
692 if(i2+j>= orderpluspnts) i2= opp2-j;
694 for(i= i1; i<=i2; i++) {
696 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
701 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
712 for(i=i1; i<=i2; i++) {
715 if(*start==1000) *start= i;
721 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride)
722 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
725 float *basisu, *basis, *basisv, *sum, *fp, *in;
726 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
727 int i, j, iofs, jofs, cycl, len, resolu, resolv;
728 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
730 int totu = nu->pntsu*nu->resolu, totv = nu->pntsv*nu->resolv;
732 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
733 if(nu->orderu>nu->pntsu) return;
734 if(nu->orderv>nu->pntsv) return;
735 if(coord_array==NULL) return;
737 /* allocate and initialize */
743 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
752 i= nu->pntsu*nu->pntsv;
755 if(bp->vec[3]!=1.0) {
763 ustart= fp[nu->orderu-1];
764 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
765 else uend= fp[nu->pntsu];
766 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
768 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
771 vstart= fp[nu->orderv-1];
773 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
774 else vend= fp[nu->pntsv];
775 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
778 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
779 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
780 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
782 /* precalculation of basisv and jstart,jend */
783 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
789 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+resolv, jend+resolv);
794 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
801 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
807 jsta= jstart[resolv];
814 for(j= jsta; j<=jen; j++) {
816 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
818 bp= nu->bp+ nu->pntsu*jofs+istart-1;
820 for(i= istart; i<=iend; i++, fp++) {
824 bp= nu->bp+ nu->pntsu*jofs+iofs;
829 *fp= basisu[i]*basis[j]*bp->vec[3];
832 else *fp= basisu[i]*basis[j];
838 for(j= jsta; j<=jen; j++) {
839 for(i= istart; i<=iend; i++, fp++) {
845 /* one! (1.0) real point now */
847 for(j= jsta; j<=jen; j++) {
849 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
851 bp= nu->bp+ nu->pntsu*jofs+istart-1;
853 for(i= istart; i<=iend; i++, fp++) {
857 bp= nu->bp+ nu->pntsu*jofs+iofs;
862 in[0]+= (*fp) * bp->vec[0];
863 in[1]+= (*fp) * bp->vec[1];
864 in[2]+= (*fp) * bp->vec[2];
873 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
884 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
885 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
886 * tilt_array and radius_array will be written to if valid */
889 float u, ustart, uend, ustep, sumdiv;
890 float *basisu, *sum, *fp;
891 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
892 int i, len, istart, iend, cycl;
894 if(nu->knotsu==NULL) return;
895 if(nu->orderu>nu->pntsu) return;
896 if(coord_array==0) return;
898 /* allocate and initialize */
901 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
903 resolu= (resolu*SEGMENTSU(nu));
911 ustart= fp[nu->orderu-1];
912 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
913 else uend= fp[nu->pntsu];
914 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
916 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
918 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
924 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
928 bp= nu->bp+ istart-1;
929 for(i= istart; i<=iend; i++, fp++) {
931 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
934 *fp= basisu[i]*bp->vec[3];
937 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
938 /* is normalizing needed? */
940 for(i= istart; i<=iend; i++, fp++) {
945 /* one! (1.0) real point */
947 bp= nu->bp+ istart-1;
948 for(i= istart; i<=iend; i++, fp++) {
950 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
955 coord_fp[0]+= (*fp) * bp->vec[0];
956 coord_fp[1]+= (*fp) * bp->vec[1];
957 coord_fp[2]+= (*fp) * bp->vec[2];
960 (*tilt_fp) += (*fp) * bp->alfa;
963 (*radius_fp) += (*fp) * bp->radius;
966 (*weight_fp) += (*fp) * bp->weight;
971 coord_fp = (float *)(((char *)coord_fp) + stride);
973 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
974 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
975 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
985 /* forward differencing method for bezier curve */
986 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
988 float rt0,rt1,rt2,rt3,f;
995 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
997 rt3= (q3-q0+3.0f*(q1-q2))/f;
1004 for(a=0; a<=it; a++) {
1006 p = (float *)(((char *)p)+stride);
1013 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1015 /* note that these are not purpendicular to the curve
1016 * they need to be rotated for this,
1018 * This could also be optimized like forward_diff_bezier */
1020 for(a=0; a<=it; a++) {
1021 float t = (float)a / (float)it;
1024 for(i=0; i<3; i++) {
1025 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1028 p = (float *)(((char *)p)+stride);
1032 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1034 float *make_orco_surf(Object *ob)
1036 Curve *cu= ob->data;
1040 float *fp, *coord_array;
1042 /* first calculate the size of the datablock */
1045 /* as we want to avoid the seam in a cyclic nurbs
1046 texture wrapping, reserve extra orco data space to save these extra needed
1047 vertex based UV coordinates for the meridian vertices.
1048 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1049 the renderface/vert construction.
1051 See also convertblender.c: init_render_surf()
1054 sizeu = nu->pntsu*nu->resolu;
1055 sizev = nu->pntsv*nu->resolv;
1056 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1057 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1058 if(nu->pntsv>1) tot+= sizeu * sizev;
1062 /* makeNurbfaces wants zeros */
1063 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1068 sizeu = nu->pntsu*nu->resolu;
1069 sizev = nu->pntsv*nu->resolv;
1070 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1071 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1073 if(cu->flag & CU_UV_ORCO) {
1074 for(b=0; b< sizeu; b++) {
1075 for(a=0; a< sizev; a++) {
1077 if(sizev <2) fp[0]= 0.0f;
1078 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1080 if(sizeu <2) fp[1]= 0.0f;
1081 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1090 float *_tdata= MEM_callocN((nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv) *3*sizeof(float), "temp data");
1091 float *tdata= _tdata;
1093 makeNurbfaces(nu, tdata, 0);
1095 for(b=0; b<sizeu; b++) {
1097 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1100 for(a=0; a<sizev; a++) {
1102 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1105 tdata = _tdata + 3 * (use_b * (nu->pntsv*nu->resolv) + use_a);
1107 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1108 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1109 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1124 /* NOTE: This routine is tied to the order of vertex
1125 * built by displist and as passed to the renderer.
1127 float *make_orco_curve(Scene *scene, Object *ob)
1129 Curve *cu = ob->data;
1132 float *fp, *coord_array;
1133 ListBase disp = {NULL, NULL};
1135 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1138 for (dl=disp.first; dl; dl=dl->next) {
1139 if (dl->type==DL_INDEX3) {
1141 } else if (dl->type==DL_SURF) {
1142 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1143 if (dl->flag & DL_CYCL_U) {
1144 if (dl->flag & DL_CYCL_V)
1145 numVerts += (dl->parts+1)*(dl->nr+1);
1147 numVerts += dl->parts*(dl->nr+1);
1150 numVerts += dl->parts*dl->nr;
1154 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1155 for (dl=disp.first; dl; dl=dl->next) {
1156 if (dl->type==DL_INDEX3) {
1157 for (u=0; u<dl->nr; u++, fp+=3) {
1158 if (cu->flag & CU_UV_ORCO) {
1159 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1163 VECCOPY(fp, &dl->verts[u*3]);
1165 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1166 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1167 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1170 } else if (dl->type==DL_SURF) {
1171 int sizeu= dl->nr, sizev= dl->parts;
1173 /* exception as handled in convertblender.c too */
1174 if (dl->flag & DL_CYCL_U) {
1176 if (dl->flag & DL_CYCL_V)
1180 for (u=0; u<sizev; u++) {
1181 for (v=0; v<sizeu; v++,fp+=3) {
1182 if (cu->flag & CU_UV_ORCO) {
1183 fp[0]= 2.0f*u/(dl->parts-1) - 1.0f;
1184 fp[1]= 2.0f*v/(dl->nr-1) - 1.0f;
1188 int realv= v % dl->nr;
1189 int realu= u % dl->parts;
1191 vert= dl->verts + 3*(dl->nr*realu + realv);
1194 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1195 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1196 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1203 freedisplist(&disp);
1209 /* ***************** BEVEL ****************** */
1211 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1213 DispList *dl, *dlnew;
1215 float *fp, facx, facy, angle, dangle;
1219 disp->first = disp->last = NULL;
1221 /* if a font object is being edited, then do nothing */
1222 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1225 if (cu->bevobj->type!=OB_CURVE) return;
1227 bevcu= cu->bevobj->data;
1228 if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1229 ListBase bevdisp= {NULL, NULL};
1230 facx= cu->bevobj->size[0];
1231 facy= cu->bevobj->size[1];
1234 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1237 dl= bevcu->disp.first;
1239 makeDispListCurveTypes(scene, cu->bevobj, 0);
1240 dl= bevcu->disp.first;
1245 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1246 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1248 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1249 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1251 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1253 BLI_addtail(disp, dlnew);
1255 nr= dlnew->parts*dlnew->nr;
1266 freedisplist(&bevdisp);
1269 else if(cu->ext1==0.0 && cu->ext2==0.0) {
1272 else if(cu->ext2==0.0) {
1273 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1274 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1275 BLI_addtail(disp, dl);
1278 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1287 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1289 nr= 4+ 2*cu->bevresol;
1291 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1292 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1293 BLI_addtail(disp, dl);
1296 dl->flag= DL_BACK_CURVE;
1301 dangle= (2.0f*M_PI/(nr));
1302 angle= -(nr-1)*dangle;
1304 for(a=0; a<nr; a++) {
1306 fp[1]= (float)(cos(angle)*(cu->ext2));
1307 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1315 /* bevel now in three parts, for proper vertex normals */
1318 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1319 dnr= nr= 2+ cu->bevresol;
1320 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1321 nr= 3+ 2*cu->bevresol;
1323 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1324 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1325 BLI_addtail(disp, dl);
1328 dl->flag= DL_BACK_CURVE;
1333 dangle= (0.5*M_PI/(dnr-1));
1334 angle= -(nr-1)*dangle;
1336 for(a=0; a<nr; a++) {
1338 fp[1]= (float)(cos(angle)*(cu->ext2));
1339 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1345 /* part 2, sidefaces */
1349 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1350 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1351 BLI_addtail(disp, dl);
1362 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1363 dl= MEM_dupallocN(dl);
1364 dl->verts= MEM_dupallocN(dl->verts);
1365 BLI_addtail(disp, dl);
1376 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1377 dnr= nr= 2+ cu->bevresol;
1378 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1379 nr= 3+ 2*cu->bevresol;
1381 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1382 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1383 BLI_addtail(disp, dl);
1385 dl->flag= DL_FRONT_CURVE;
1392 dangle= (0.5*M_PI/(dnr-1));
1394 for(a=0; a<nr; a++) {
1396 fp[1]= (float)(cos(angle)*(cu->ext2));
1397 fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1405 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1409 0: no intersection of segments
1410 1: exact intersection of segments
1411 2: cross-intersection of segments
1415 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1416 if(deler==0.0) return -1;
1418 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1419 *labda= -(*labda/deler);
1421 deler= v3[coy]-v4[coy];
1423 deler=v3[cox]-v4[cox];
1424 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1426 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1428 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1429 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1431 if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1432 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1439 static short bevelinside(BevList *bl1,BevList *bl2)
1441 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1442 /* returns '1' if correct hole */
1443 BevPoint *bevp, *prevbevp;
1444 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1445 int nr, links=0,rechts=0,mode;
1447 /* take first vertex of possible hole */
1449 bevp= (BevPoint *)(bl2+1);
1450 hvec1[0]= bevp->vec[0];
1451 hvec1[1]= bevp->vec[1];
1453 VECCOPY(hvec2,hvec1);
1456 /* test it with all edges of potential surounding poly */
1457 /* count number of transitions left-right */
1459 bevp= (BevPoint *)(bl1+1);
1461 prevbevp= bevp+(nr-1);
1464 min= prevbevp->vec[1];
1468 max= prevbevp->vec[1];
1471 if(min<=hvec1[1] && max>=hvec1[1]) {
1472 /* there's a transition, calc intersection point */
1473 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1474 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1475 only allow for one situation: we choose lab= 1.0
1477 if(mode>=0 && lab!=0.0) {
1478 if(vec[0]<hvec1[0]) links++;
1487 if( (links & 1) && (rechts & 1) ) return 1;
1498 static int vergxcobev(const void *a1, const void *a2)
1500 const struct bevelsort *x1=a1,*x2=a2;
1502 if( x1->left > x2->left ) return 1;
1503 else if( x1->left < x2->left) return -1;
1507 /* this function cannot be replaced with atan2, but why? */
1509 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1511 float t01, t02, x3, y3;
1513 t01= (float)sqrt(x1*x1+y1*y1);
1514 t02= (float)sqrt(x2*x2+y2*y2);
1515 if(t01==0.0) t01= 1.0;
1516 if(t02==0.0) t02= 1.0;
1524 if(fabs(t02)>=1.0) t02= .5*M_PI;
1525 else t02= (saacos(t02))/2.0f;
1527 t02= (float)sin(t02);
1528 if(t02==0.0) t02= 1.0;
1532 if(x3==0 && y3==0) {
1536 t01= (float)sqrt(x3*x3+y3*y3);
1546 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1548 BezTriple *pprev, *next, *last;
1549 float fac, dfac, t[4];
1552 if(tilt_array==NULL && radius_array==NULL)
1555 last= nu->bezt+(nu->pntsu-1);
1557 /* returns a point */
1558 if(prevbezt==nu->bezt) {
1559 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1560 else pprev= prevbezt;
1562 else pprev= prevbezt-1;
1566 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1572 dfac= 1.0f/(float)resolu;
1574 for(a=0; a<resolu; a++, fac+= dfac) {
1576 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1577 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1579 key_curve_position_weights(fac, t, nu->tilt_interp);
1580 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1583 tilt_array = (float *)(((char *)tilt_array) + stride);
1587 if (nu->radius_interp==KEY_CU_EASE) {
1588 /* Support 2.47 ease interp
1589 * Note! - this only takes the 2 points into account,
1590 * giving much more localized results to changes in radius, sometimes you want that */
1591 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1594 /* reuse interpolation from tilt if we can */
1595 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1596 key_curve_position_weights(fac, t, nu->radius_interp);
1598 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1601 radius_array = (float *)(((char *)radius_array) + stride);
1605 /* basic interpolation for now, could copy tilt interp too */
1606 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1608 weight_array = (float *)(((char *)weight_array) + stride);
1613 /* make_bevel_list_3D_* funcs, at a minimum these must
1614 * fill in the bezp->quat and bezp->dir values */
1616 /* correct non-cyclic cases by copying direction and rotation
1617 * values onto the first & last end-points */
1618 static void bevel_list_cyclic_fix_3D(BevList *bl)
1620 BevPoint *bevp, *bevp1;
1622 bevp= (BevPoint *)(bl+1);
1624 QUATCOPY(bevp->quat, bevp1->quat);
1625 VECCOPY(bevp->dir, bevp1->dir);
1626 VECCOPY(bevp->tan, bevp1->tan);
1627 bevp= (BevPoint *)(bl+1);
1630 QUATCOPY(bevp->quat, bevp1->quat);
1631 VECCOPY(bevp->dir, bevp1->dir);
1632 VECCOPY(bevp->tan, bevp1->tan);
1634 /* utility for make_bevel_list_3D_* funcs */
1635 static void bevel_list_calc_bisect(BevList *bl)
1637 BevPoint *bevp2, *bevp1, *bevp0;
1640 bevp2= (BevPoint *)(bl+1);
1641 bevp1= bevp2+(bl->nr-1);
1646 /* totally simple */
1647 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1654 static void bevel_list_flip_tangents(BevList *bl)
1656 BevPoint *bevp2, *bevp1, *bevp0;
1659 bevp2= (BevPoint *)(bl+1);
1660 bevp1= bevp2+(bl->nr-1);
1665 if(RAD2DEG(angle_v2v2(bevp0->tan, bevp1->tan)) > 90)
1666 negate_v3(bevp1->tan);
1673 /* apply user tilt */
1674 static void bevel_list_apply_tilt(BevList *bl)
1676 BevPoint *bevp2, *bevp1, *bevp0;
1680 bevp2= (BevPoint *)(bl+1);
1681 bevp1= bevp2+(bl->nr-1);
1686 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1687 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1688 normalize_qt(bevp1->quat);
1695 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1696 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1698 BevPoint *bevp2, *bevp1, *bevp0;
1702 float bevp0_quat[4];
1705 for(a=0; a < smooth_iter; a++) {
1707 bevp2= (BevPoint *)(bl+1);
1708 bevp1= bevp2+(bl->nr-1);
1713 if(bl->poly== -1) { /* check its not cyclic */
1714 /* skip the first point */
1727 QUATCOPY(bevp0_quat, bevp0->quat);
1730 /* interpolate quats */
1731 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1732 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1735 mul_qt_v3(q, zaxis);
1736 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1737 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1740 QUATCOPY(bevp0_quat, bevp1->quat);
1741 mul_qt_qtqt(q, q2, q);
1742 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1743 normalize_qt(bevp1->quat);
1753 static void make_bevel_list_3D_zup(BevList *bl)
1755 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1758 bevp2= (BevPoint *)(bl+1);
1759 bevp1= bevp2+(bl->nr-1);
1764 /* totally simple */
1765 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1766 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1774 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1776 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1782 bevel_list_calc_bisect(bl);
1784 bevp2= (BevPoint *)(bl+1);
1785 bevp1= bevp2+(bl->nr-1);
1791 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1792 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1795 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1797 if(angle > 0.0f) { /* otherwise we can keep as is */
1798 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1799 axis_angle_to_quat(q, cross_tmp, angle);
1800 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1803 QUATCOPY(bevp1->quat, bevp0->quat);
1812 if(bl->poly != -1) { /* check for cyclic */
1814 /* Need to correct for the start/end points not matching
1815 * do this by calculating the tilt angle difference, then apply
1816 * the rotation gradually over the entire curve
1818 * note that the split is between last and second last, rather then first/last as youd expect.
1820 * real order is like this
1821 * 0,1,2,3,4 --> 1,2,3,4,0
1823 * this is why we compare last with second last
1825 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1827 BevPoint *bevp_first;
1828 BevPoint *bevp_last;
1831 bevp_first= (BevPoint *)(bl+1);
1832 bevp_first+= bl->nr-1;
1833 bevp_last = bevp_first;
1836 /* quats and vec's are normalized, should not need to re-normalize */
1837 mul_qt_v3(bevp_first->quat, vec_1);
1838 mul_qt_v3(bevp_last->quat, vec_2);
1839 normalize_v3(vec_1);
1840 normalize_v3(vec_2);
1842 /* align the vector, can avoid this and it looks 98% OK but
1843 * better to align the angle quat roll's before comparing */
1845 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1846 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1847 axis_angle_to_quat(q, cross_tmp, angle);
1848 mul_qt_v3(q, vec_2);
1851 angle= angle_normalized_v3v3(vec_1, vec_2);
1853 /* flip rotation if needs be */
1854 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1855 normalize_v3(cross_tmp);
1856 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90/(180.0/M_PI))
1859 bevp2= (BevPoint *)(bl+1);
1860 bevp1= bevp2+(bl->nr-1);
1865 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1867 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1868 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1877 static void make_bevel_list_3D_tangent(BevList *bl)
1879 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1882 float bevp0_tan[3], cross_tmp[3];
1884 bevel_list_calc_bisect(bl);
1885 if(bl->poly== -1) /* check its not cyclic */
1886 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1887 bevel_list_flip_tangents(bl);
1889 /* correct the tangents */
1890 bevp2= (BevPoint *)(bl+1);
1891 bevp1= bevp2+(bl->nr-1);
1897 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1898 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1899 normalize_v3(bevp1->tan);
1907 /* now for the real twist calc */
1908 bevp2= (BevPoint *)(bl+1);
1909 bevp1= bevp2+(bl->nr-1);
1912 VECCOPY(bevp0_tan, bevp0->tan);
1917 /* make perpendicular, modify tan in place, is ok */
1919 float zero[3] = {0,0,0};
1921 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1922 normalize_v3(cross_tmp);
1923 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1931 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1933 switch(twist_mode) {
1934 case CU_TWIST_TANGENT:
1935 make_bevel_list_3D_tangent(bl);
1937 case CU_TWIST_MINIMUM:
1938 make_bevel_list_3D_minimum_twist(bl);
1940 default: /* CU_TWIST_Z_UP default, pre 2.49c */
1941 make_bevel_list_3D_zup(bl);
1944 if(bl->poly== -1) /* check its not cyclic */
1945 bevel_list_cyclic_fix_3D(bl);
1948 bevel_list_smooth(bl, smooth_iter);
1950 bevel_list_apply_tilt(bl);
1955 /* only for 2 points */
1956 static void make_bevel_list_segment_3D(BevList *bl)
1960 BevPoint *bevp2= (BevPoint *)(bl+1);
1961 BevPoint *bevp1= bevp2+1;
1963 /* simple quat/dir */
1964 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
1965 normalize_v3(bevp1->dir);
1967 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1969 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1970 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1971 normalize_qt(bevp1->quat);
1972 VECCOPY(bevp2->dir, bevp1->dir);
1973 QUATCOPY(bevp2->quat, bevp1->quat);
1978 void makeBevelList(Object *ob)
1981 - convert all curves to polys, with indication of resol and flags for double-vertices
1982 - possibly; do a smart vertice removal (in case Nurb)
1983 - separate in individual blicks with BoundBox
1984 - AutoHole detection
1988 BezTriple *bezt, *prevbezt;
1990 BevList *bl, *blnew, *blnext;
1991 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
1992 float min, inp, x1, x2, y1, y2;
1993 struct bevelsort *sortdata, *sd, *sd1;
1994 int a, b, nr, poly, resolu = 0, len = 0;
1995 int do_tilt, do_radius, do_weight;
1997 /* this function needs an object, because of tflag and upflag */
2000 /* do we need to calculate the radius for each point? */
2001 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2003 /* STEP 1: MAKE POLYS */
2005 BLI_freelistN(&(cu->bev));
2006 if(cu->editnurb && ob->type!=OB_FONT) {
2007 ListBase *nurbs= ED_curve_editnurbs(cu);
2009 } else nu= cu->nurb.first;
2013 /* check if we will calculate tilt data */
2014 do_tilt = CU_DO_TILT(cu, nu);
2015 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2018 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2019 * enforced in the UI but can go wrong possibly */
2020 if(!check_valid_nurb_u(nu)) {
2021 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2022 BLI_addtail(&(cu->bev), bl);
2025 if(G.rendering && cu->resolu_ren!=0)
2026 resolu= cu->resolu_ren;
2030 if(nu->type == CU_POLY) {
2032 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2033 BLI_addtail(&(cu->bev), bl);
2035 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2039 bevp= (BevPoint *)(bl+1);
2043 VECCOPY(bevp->vec, bp->vec);
2044 bevp->alfa= bp->alfa;
2045 bevp->radius= bp->radius;
2046 bevp->weight= bp->weight;
2047 bevp->split_tag= TRUE;
2052 else if(nu->type == CU_BEZIER) {
2054 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2055 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2056 BLI_addtail(&(cu->bev), bl);
2058 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2060 bevp= (BevPoint *)(bl+1);
2064 if(nu->flagu & CU_NURB_CYCLIC) {
2066 prevbezt= nu->bezt+(nu->pntsu-1);
2074 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2076 VECCOPY(bevp->vec, prevbezt->vec[1]);
2077 bevp->alfa= prevbezt->alfa;
2078 bevp->radius= prevbezt->radius;
2079 bevp->weight= prevbezt->weight;
2080 bevp->split_tag= TRUE;
2081 bevp->dupe_tag= FALSE;
2087 /* always do all three, to prevent data hanging around */
2090 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2091 for(j=0; j<3; j++) {
2092 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2093 bezt->vec[0][j], bezt->vec[1][j],
2094 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2097 /* if both arrays are NULL do nothiong */
2098 alfa_bezpart( prevbezt, bezt, nu,
2099 do_tilt ? &bevp->alfa : NULL,
2100 do_radius ? &bevp->radius : NULL,
2101 do_weight ? &bevp->weight : NULL,
2102 resolu, sizeof(BevPoint));
2105 if(cu->twist_mode==CU_TWIST_TANGENT) {
2106 forward_diff_bezier_cotangent(
2107 prevbezt->vec[1], prevbezt->vec[2],
2108 bezt->vec[0], bezt->vec[1],
2109 bevp->tan, resolu, sizeof(BevPoint));
2112 /* indicate with handlecodes double points */
2113 if(prevbezt->h1==prevbezt->h2) {
2114 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2117 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2118 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2127 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2128 VECCOPY(bevp->vec, prevbezt->vec[1]);
2129 bevp->alfa= prevbezt->alfa;
2130 bevp->radius= prevbezt->radius;
2131 bevp->weight= prevbezt->weight;
2135 else if(nu->type == CU_NURBS) {
2137 len= (resolu*SEGMENTSU(nu));
2139 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2140 BLI_addtail(&(cu->bev), bl);
2143 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2145 bevp= (BevPoint *)(bl+1);
2147 makeNurbcurve( nu, &bevp->vec[0],
2148 do_tilt ? &bevp->alfa : NULL,
2149 do_radius ? &bevp->radius : NULL,
2150 do_weight ? &bevp->weight : NULL,
2151 resolu, sizeof(BevPoint));
2158 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2161 if (bl->nr) { /* null bevel items come from single points */
2163 bevp1= (BevPoint *)(bl+1);
2164 bevp0= bevp1+(nr-1);
2167 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2168 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2169 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2170 bevp0->dupe_tag= TRUE;
2184 if(bl->nr && bl->dupe_nr) {
2185 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2186 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2187 memcpy(blnew, bl, sizeof(BevList));
2189 BLI_remlink(&(cu->bev), bl);
2190 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2191 bevp0= (BevPoint *)(bl+1);
2192 bevp1= (BevPoint *)(blnew+1);
2195 if(bevp0->dupe_tag==0) {
2196 memcpy(bevp1, bevp0, sizeof(BevPoint));
2208 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2212 if(bl->nr && bl->poly>=0) {
2221 /* find extreme left points, also test (turning) direction */
2223 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2229 bevp= (BevPoint *)(bl+1);
2232 if(min>bevp->vec[0]) {
2241 bevp= (BevPoint *)(bl+1);
2242 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2243 else bevp0= bevp1-1;
2244 bevp= bevp+ (bl->nr-1);
2245 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2246 else bevp2= bevp1+1;
2248 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]);
2250 if(inp>0.0) sd->dir= 1;
2258 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2261 for(a=1; a<poly; a++, sd++) {
2262 bl= sd->bl; /* is bl a hole? */
2263 sd1= sortdata+ (a-1);
2264 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2265 if(bevelinside(sd1->bl, bl)) {
2266 bl->hole= 1- sd1->bl->hole;
2272 /* turning direction */
2273 if((cu->flag & CU_3D)==0) {
2275 for(a=0; a<poly; a++, sd++) {
2276 if(sd->bl->hole==sd->dir) {
2278 bevp1= (BevPoint *)(bl+1);
2279 bevp2= bevp1+ (bl->nr-1);
2282 SWAP(BevPoint, *bevp1, *bevp2);
2289 MEM_freeN(sortdata);
2292 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2293 if((cu->flag & CU_3D)==0) {
2294 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2295 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2302 else if(bl->nr==2) { /* 2 pnt, treat separate */
2303 bevp2= (BevPoint *)(bl+1);
2306 x1= bevp1->vec[0]- bevp2->vec[0];
2307 y1= bevp1->vec[1]- bevp2->vec[1];
2309 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2310 bevp2->sina= bevp1->sina;
2311 bevp2->cosa= bevp1->cosa;
2313 /* fill in dir & quat */
2314 make_bevel_list_segment_3D(bl);
2317 bevp2= (BevPoint *)(bl+1);
2318 bevp1= bevp2+(bl->nr-1);
2323 x1= bevp1->vec[0]- bevp0->vec[0];
2324 x2= bevp1->vec[0]- bevp2->vec[0];
2325 y1= bevp1->vec[1]- bevp0->vec[1];
2326 y2= bevp1->vec[1]- bevp2->vec[1];
2328 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2330 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2331 * no need for tricky tilt calculation as with 3D curves */
2332 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2333 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2334 /* done with inline make_bevel_list_3D_zup */
2341 /* correct non-cyclic cases */
2343 bevp= (BevPoint *)(bl+1);
2345 bevp->sina= bevp1->sina;
2346 bevp->cosa= bevp1->cosa;
2347 bevp= (BevPoint *)(bl+1);
2350 bevp->sina= bevp1->sina;
2351 bevp->cosa= bevp1->cosa;
2353 /* correct for the dir/quat, see above why its needed */
2354 bevel_list_cyclic_fix_3D(bl);
2360 else { /* 3D Curves */
2367 else if(bl->nr==2) { /* 2 pnt, treat separate */
2368 make_bevel_list_segment_3D(bl);
2371 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2378 /* ****************** HANDLES ************** */
2382 * 0: nothing, 1:auto, 2:vector, 3:aligned
2385 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2386 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2388 float *p1,*p2,*p3, pt[3];
2389 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2391 if(bezt->h1==0 && bezt->h2==0) return;
2397 pt[0]= 2*p2[0]- p3[0];
2398 pt[1]= 2*p2[1]- p3[1];
2399 pt[2]= 2*p2[2]- p3[2];
2402 else p1= prev->vec[1];
2405 pt[0]= 2*p2[0]- p1[0];
2406 pt[1]= 2*p2[1]- p1[1];
2407 pt[2]= 2*p2[2]- p1[2];
2410 else p3= next->vec[1];
2417 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2424 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2426 if(len1==0.0f) len1=1.0f;
2427 if(len2==0.0f) len2=1.0f;
2430 if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) { /* auto */
2431 vx= dx1/len2 + dx/len1;
2432 vy= dy1/len2 + dy/len1;
2433 vz= dz1/len2 + dz/len1;
2434 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2436 int leftviolate=0, rightviolate=0; /* for mode==2 */
2438 if(len1>5.0f*len2) len1= 5.0f*len2;
2439 if(len2>5.0f*len1) len2= 5.0f*len1;
2441 if(bezt->h1==HD_AUTO) {
2443 *(p2-3)= *p2-vx*len1;
2444 *(p2-2)= *(p2+1)-vy*len1;
2445 *(p2-1)= *(p2+2)-vz*len1;
2447 if(mode==2 && next && prev) { // keep horizontal if extrema
2448 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2449 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2450 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2451 bezt->vec[0][1]= bezt->vec[1][1];
2453 else { // handles should not be beyond y coord of two others
2455 if(prev->vec[1][1] > bezt->vec[0][1]) {
2456 bezt->vec[0][1]= prev->vec[1][1];
2461 if(prev->vec[1][1] < bezt->vec[0][1]) {
2462 bezt->vec[0][1]= prev->vec[1][1];
2469 if(bezt->h2==HD_AUTO) {
2471 *(p2+3)= *p2+vx*len2;
2472 *(p2+4)= *(p2+1)+vy*len2;
2473 *(p2+5)= *(p2+2)+vz*len2;
2475 if(mode==2 && next && prev) { // keep horizontal if extrema
2476 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2477 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2478 if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2479 bezt->vec[2][1]= bezt->vec[1][1];
2481 else { // handles should not be beyond y coord of two others
2483 if(next->vec[1][1] < bezt->vec[2][1]) {
2484 bezt->vec[2][1]= next->vec[1][1];
2489 if(next->vec[1][1] > bezt->vec[2][1]) {
2490 bezt->vec[2][1]= next->vec[1][1];
2497 if(leftviolate || rightviolate) { /* align left handle */
2500 sub_v3_v3v3(h1, p2-3, p2);
2501 sub_v3_v3v3(h2, p2, p2+3);
2502 len1= normalize_v3(h1);
2503 len2= normalize_v3(h2);
2508 *(p2+3)= *(p2) - vz*len2*h1[0];
2509 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2510 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2513 *(p2-3)= *(p2) + vz*len1*h2[0];
2514 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2515 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2522 if(bezt->h1==HD_VECT) { /* vector */
2527 *(p2-2)= *(p2+1)-dy;
2528 *(p2-1)= *(p2+2)-dz;
2530 if(bezt->h2==HD_VECT) {
2535 *(p2+4)= *(p2+1)+dy1;
2536 *(p2+5)= *(p2+2)+dz1;
2539 len2= len_v3v3(p2, p2+3);
2540 len1= len_v3v3(p2, p2-3);
2541 if(len1==0.0) len1=1.0;
2542 if(len2==0.0) len2=1.0;
2544 if(bezt->f1 & SELECT) { /* order of calculation */
2545 if(bezt->h2==HD_ALIGN) { /* aligned */
2547 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2548 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2549 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2551 if(bezt->h1==HD_ALIGN) {
2553 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2554 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2555 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2559 if(bezt->h1==HD_ALIGN) {
2561 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2562 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2563 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2565 if(bezt->h2==HD_ALIGN) { /* aligned */
2567 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2568 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2569 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2574 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2576 BezTriple *bezt, *prev, *next;
2579 if(nu->type != CU_BEZIER) return;
2580 if(nu->pntsu<2) return;
2584 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2589 calchandleNurb(bezt, prev, next, 0);
2592 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2602 void testhandlesNurb(Nurb *nu)
2604 /* use when something has changed with handles.
2605 it treats all BezTriples with the following rules:
2606 PHASE 1: do types have to be altered?
2607 Auto handles: become aligned when selection status is NOT(000 || 111)
2608 Vector handles: become 'nothing' when (one half selected AND other not)
2609 PHASE 2: recalculate handles
2614 if(nu->type != CU_BEZIER) return;
2620 if(bezt->f1 & SELECT) flag++;
2621 if(bezt->f2 & SELECT) flag += 2;
2622 if(bezt->f3 & SELECT) flag += 4;
2624 if( !(flag==0 || flag==7) ) {
2625 if(bezt->h1==HD_AUTO) { /* auto */
2628 if(bezt->h2==HD_AUTO) { /* auto */
2632 if(bezt->h1==HD_VECT) { /* vector */
2633 if(flag < 4) bezt->h1= 0;
2635 if(bezt->h2==HD_VECT) { /* vector */
2636 if( flag > 3) bezt->h2= 0;
2642 calchandlesNurb(nu);
2645 void autocalchandlesNurb(Nurb *nu, int flag)
2647 /* checks handle coordinates and calculates type */
2649 BezTriple *bezt2, *bezt1, *bezt0;
2650 int i, align, leftsmall, rightsmall;
2652 if(nu==0 || nu->bezt==0) return;
2655 bezt1 = bezt2 + (nu->pntsu-1);
2661 align= leftsmall= rightsmall= 0;
2664 if(flag==0 || (bezt1->f1 & flag) ) {
2666 /* distance too short: vectorhandle */
2667 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2672 /* aligned handle? */
2673 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2675 bezt1->h1= HD_ALIGN;
2677 /* or vector handle? */
2678 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2684 if(flag==0 || (bezt1->f3 & flag) ) {
2686 /* distance too short: vectorhandle */
2687 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2692 /* aligned handle? */
2693 if(align) bezt1->h2= HD_ALIGN;
2695 /* or vector handle? */
2696 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2701 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2702 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2704 /* undesired combination: */
2705 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2706 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2713 calchandlesNurb(nu);
2716 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2720 nu= editnurb->first;
2722 autocalchandlesNurb(nu, flag);
2727 void sethandlesNurb(ListBase *editnurb, short code)
2729 /* code==1: set autohandle */
2730 /* code==2: set vectorhandle */
2731 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2732 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2733 /* code==5: Set align, like 3 but no toggle */
2734 /* code==6: Clear align, like 3 but no toggle */
2739 if(code==1 || code==2) {
2740 nu= editnurb->first;
2742 if(nu->type == CU_BEZIER) {
2746 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2747 if(bezt->f1 & SELECT) bezt->h1= code;
2748 if(bezt->f3 & SELECT) bezt->h2= code;
2749 if(bezt->h1!=bezt->h2) {
2750 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2751 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2756 calchandlesNurb(nu);
2762 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2764 nu= editnurb->first;
2767 } else if (code == 6) {
2772 if(nu->type == CU_BEZIER) {
2776 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2777 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2787 nu= editnurb->first;
2789 if(nu->type == CU_BEZIER) {
2793 if(bezt->f1 & SELECT) bezt->h1= ok;
2794 if(bezt->f3 & SELECT) bezt->h2= ok;
2798 calchandlesNurb(nu);
2805 static void swapdata(void *adr1, void *adr2, int len)
2813 memcpy(adr, adr1, len);
2814 memcpy(adr1, adr2, len);
2815 memcpy(adr2, adr, len);
2820 adr= (char *)MEM_mallocN(len, "curve swap");
2821 memcpy(adr, adr1, len);
2822 memcpy(adr1, adr2, len);
2823 memcpy(adr2, adr, len);
2828 void switchdirectionNurb(Nurb *nu)
2830 BezTriple *bezt1, *bezt2;
2832 float *fp1, *fp2, *tempf;
2835 if(nu->pntsu==1 && nu->pntsv==1) return;
2837 if(nu->type == CU_BEZIER) {
2841 if(a & 1) a+= 1; /* if odd, also swap middle content */
2844 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2846 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2847 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2849 SWAP(char, bezt1->h1, bezt1->h2);
2850 SWAP(short, bezt1->f1, bezt1->f3);
2853 SWAP(char, bezt2->h1, bezt2->h2);
2854 SWAP(short, bezt2->f1, bezt2->f3);
2855 bezt1->alfa= -bezt1->alfa;
2856 bezt2->alfa= -bezt2->alfa;
2863 else if(nu->pntsv==1) {
2868 while(bp1!=bp2 && a>0) {
2869 SWAP(BPoint, *bp1, *bp2);
2871 bp1->alfa= -bp1->alfa;
2872 bp2->alfa= -bp2->alfa;
2876 if(nu->type == CU_NURBS) {
2882 while(fp1!=fp2 && a>0) {
2883 SWAP(float, *fp1, *fp2);
2888 /* and make in increasing order again */
2891 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2893 fp2[0]= fabs(fp1[1]-fp1[0]);
2904 fp1[0]= fp1[-1]+fp2[0];
2913 for(b=0; b<nu->pntsv; b++) {
2915 bp1= nu->bp+b*nu->pntsu;
2920 while(bp1!=bp2 && a>0) {
2921 SWAP(BPoint, *bp1, *bp2);
2931 float (*curve_getVertexCos(Curve *cu, ListBase *lb, int *numVerts_r))[3]
2933 int i, numVerts = *numVerts_r = count_curveverts(lb);
2934 float *co, (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "cu_vcos");
2938 for (nu=lb->first; nu; nu=nu->next) {
2939 if (nu->type == CU_BEZIER) {
2940 BezTriple *bezt = nu->bezt;
2942 for (i=0; i<nu->pntsu; i++,bezt++) {
2943 VECCOPY(co, bezt->vec[0]); co+=3;
2944 VECCOPY(co, bezt->vec[1]); co+=3;
2945 VECCOPY(co, bezt->vec[2]); co+=3;
2948 BPoint *bp = nu->bp;