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"
47 #include "BLI_ghash.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_material_types.h"
52 /* for dereferencing pointers */
53 #include "DNA_key_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_vfont_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"
73 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4,
75 float *labda, float *mu, float *vec);
77 void unlink_curve(Curve *cu)
81 for(a=0; a<cu->totcol; a++) {
82 if(cu->mat[a]) cu->mat[a]->id.us--;
85 if(cu->vfont) cu->vfont->id.us--;
88 if(cu->vfontb) cu->vfontb->id.us--;
91 if(cu->vfonti) cu->vfonti->id.us--;
94 if(cu->vfontbi) cu->vfontbi->id.us--;
97 if(cu->key) cu->key->id.us--;
101 /* frees editcurve entirely */
102 void BKE_free_editfont(Curve *cu)
105 EditFont *ef= cu->editfont;
107 if(ef->oldstr) MEM_freeN(ef->oldstr);
108 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
109 if(ef->textbuf) MEM_freeN(ef->textbuf);
110 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
111 if(ef->copybuf) MEM_freeN(ef->copybuf);
112 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
119 void free_curve_editNurb_keyIndex(EditNurb *editnurb)
121 if (!editnurb->keyindex) {
124 BLI_ghash_free(editnurb->keyindex, NULL, (GHashValFreeFP)MEM_freeN);
125 editnurb->keyindex= NULL;
128 void free_curve_editNurb (Curve *cu)
131 freeNurblist(&cu->editnurb->nurbs);
132 free_curve_editNurb_keyIndex(cu->editnurb);
133 MEM_freeN(cu->editnurb);
138 /* don't free curve itself */
139 void free_curve(Curve *cu)
141 freeNurblist(&cu->nurb);
142 BLI_freelistN(&cu->bev);
143 freedisplist(&cu->disp);
144 BKE_free_editfont(cu);
146 free_curve_editNurb(cu);
148 BKE_free_animdata((ID *)cu);
150 if(cu->mat) MEM_freeN(cu->mat);
151 if(cu->str) MEM_freeN(cu->str);
152 if(cu->strinfo) MEM_freeN(cu->strinfo);
153 if(cu->bb) MEM_freeN(cu->bb);
154 if(cu->path) free_path(cu->path);
155 if(cu->tb) MEM_freeN(cu->tb);
158 Curve *add_curve(const char *name, int type)
162 cu= alloc_libblock(&G.main->curve, ID_CU, name);
164 cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
165 cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
167 cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
170 cu->spacing= cu->linedist= 1.0;
173 cu->texflag= CU_AUTOSPACE;
174 cu->smallcaps_scale= 0.75f;
175 cu->twist_mode= CU_TWIST_MINIMUM; // XXX: this one seems to be the best one in most cases, at least for curve deform...
177 cu->bb= unit_boundbox();
180 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
182 cu->str= MEM_mallocN(12, "str");
183 BLI_strncpy(cu->str, "Text", 12);
185 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
186 cu->totbox= cu->actbox= 1;
187 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
188 cu->tb[0].w = cu->tb[0].h = 0.0;
194 Curve *copy_curve(Curve *cu)
199 cun= copy_libblock(cu);
200 cun->nurb.first= cun->nurb.last= NULL;
201 duplicateNurblist( &(cun->nurb), &(cu->nurb));
203 cun->mat= MEM_dupallocN(cu->mat);
204 for(a=0; a<cun->totcol; a++) {
205 id_us_plus((ID *)cun->mat[a]);
208 cun->str= MEM_dupallocN(cu->str);
209 cun->strinfo= MEM_dupallocN(cu->strinfo);
210 cun->tb= MEM_dupallocN(cu->tb);
211 cun->bb= MEM_dupallocN(cu->bb);
213 cun->key= copy_key(cu->key);
214 if(cun->key) cun->key->from= (ID *)cun;
216 cun->disp.first= cun->disp.last= NULL;
217 cun->bev.first= cun->bev.last= NULL;
224 #if 0 // XXX old animation system
225 /* single user ipo too */
226 if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
227 #endif // XXX old animation system
229 id_us_plus((ID *)cun->vfont);
230 id_us_plus((ID *)cun->vfontb);
231 id_us_plus((ID *)cun->vfonti);
232 id_us_plus((ID *)cun->vfontbi);
237 static void extern_local_curve(Curve *cu)
239 id_lib_extern((ID *)cu->vfont);
240 id_lib_extern((ID *)cu->vfontb);
241 id_lib_extern((ID *)cu->vfonti);
242 id_lib_extern((ID *)cu->vfontbi);
245 extern_local_matarar(cu->mat, cu->totcol);
249 void make_local_curve(Curve *cu)
255 /* - when there are only lib users: don't do
256 * - when there are only local users: set flag
260 if(cu->id.lib==NULL) return;
264 cu->id.flag= LIB_LOCAL;
266 new_id(&bmain->curve, (ID *)cu, NULL);
267 extern_local_curve(cu);
271 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
273 if(ob->id.lib) lib= 1;
278 if(local && lib==0) {
280 cu->id.flag= LIB_LOCAL;
282 new_id(&bmain->curve, (ID *)cu, NULL);
283 extern_local_curve(cu);
285 else if(local && lib) {
286 Curve *cun= copy_curve(cu);
289 for(ob= bmain->object.first; ob; ob= ob->id.next) {
291 if(ob->id.lib==NULL) {
301 /* Get list of nurbs from editnurbs structure */
302 ListBase *curve_editnurbs(Curve *cu)
305 return &cu->editnurb->nurbs;
311 short curve_type(Curve *cu)
317 for (nu= cu->nurb.first; nu; nu= nu->next) {
326 void test_curve_type(Object *ob)
328 ob->type = curve_type(ob->data);
331 void tex_space_curve(Curve *cu)
335 float *fp, min[3], max[3];
338 if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
341 INIT_MINMAX(min, max);
346 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
347 else tot= dl->nr*dl->parts;
352 DO_MINMAX(fp, min, max);
359 min[0] = min[1] = min[2] = -1.0f;
360 max[0] = max[1] = max[2] = 1.0f;
363 boundbox_set_from_min_max(bb, min, max);
365 if(cu->texflag & CU_AUTOSPACE) {
366 mid_v3_v3v3(cu->loc, min, max);
367 cu->size[0]= (max[0]-min[0])/2.0f;
368 cu->size[1]= (max[1]-min[1])/2.0f;
369 cu->size[2]= (max[2]-min[2])/2.0f;
371 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0f;
373 if(cu->size[0]==0.0f) cu->size[0]= 1.0f;
374 else if(cu->size[0]>0.0f && cu->size[0]<0.00001f) cu->size[0]= 0.00001f;
375 else if(cu->size[0]<0.0f && cu->size[0]> -0.00001f) cu->size[0]= -0.00001f;
377 if(cu->size[1]==0.0f) cu->size[1]= 1.0f;
378 else if(cu->size[1]>0.0f && cu->size[1]<0.00001f) cu->size[1]= 0.00001f;
379 else if(cu->size[1]<0.0f && cu->size[1]> -0.00001f) cu->size[1]= -0.00001f;
381 if(cu->size[2]==0.0f) cu->size[2]= 1.0f;
382 else if(cu->size[2]>0.0f && cu->size[2]<0.00001f) cu->size[2]= 0.00001f;
383 else if(cu->size[2]<0.0f && cu->size[2]> -0.00001f) cu->size[2]= -0.00001f;
388 int count_curveverts(ListBase *nurb)
395 if(nu->bezt) tot+= 3*nu->pntsu;
396 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
403 int count_curveverts_without_handles(ListBase *nurb)
410 if(nu->bezt) tot+= nu->pntsu;
411 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
418 /* **************** NURBS ROUTINES ******************** */
420 void freeNurb(Nurb *nu)
425 if(nu->bezt) MEM_freeN(nu->bezt);
427 if(nu->bp) MEM_freeN(nu->bp);
429 if(nu->knotsu) MEM_freeN(nu->knotsu);
431 if(nu->knotsv) MEM_freeN(nu->knotsv);
433 /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
440 void freeNurblist(ListBase *lb)
452 lb->first= lb->last= NULL;
455 Nurb *duplicateNurb(Nurb *nu)
460 newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
461 if(newnu==NULL) return NULL;
462 memcpy(newnu, nu, sizeof(Nurb));
466 (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
467 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
470 len= nu->pntsu*nu->pntsv;
472 (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
473 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
475 newnu->knotsu= newnu->knotsv= NULL;
480 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
481 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
484 if(nu->pntsv>1 && nu->knotsv) {
487 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
488 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
495 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
503 nun= duplicateNurb(nu);
504 BLI_addtail(lb1, nun);
510 void test2DNurb(Nurb *nu)
516 if((nu->flag & CU_2D)==0)
519 if(nu->type == CU_BEZIER) {
523 bezt->vec[0][2]= 0.0;
524 bezt->vec[1][2]= 0.0;
525 bezt->vec[2][2]= 0.0;
530 a= nu->pntsu*nu->pntsv;
539 void minmaxNurb(Nurb *nu, float *min, float *max)
545 if(nu->type == CU_BEZIER) {
549 DO_MINMAX(bezt->vec[0], min, max);
550 DO_MINMAX(bezt->vec[1], min, max);
551 DO_MINMAX(bezt->vec[2], min, max);
556 a= nu->pntsu*nu->pntsv;
559 DO_MINMAX(bp->vec, min, max);
565 /* be sure to call makeknots after this */
566 void addNurbPoints(Nurb *nu, int number)
570 nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
573 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
577 memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
579 for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
586 void addNurbPointsBezier(Nurb *nu, int number)
588 BezTriple *tmp= nu->bezt;
590 nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
593 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
597 memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
599 for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
606 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
609 static void calcknots(float *knots, const short pnts, const short order, const short flag)
611 /* knots: number of pnts NOT corrected for cyclic */
612 const int pnts_order= pnts + order;
616 switch(flag & (CU_NURB_ENDPOINT|CU_NURB_BEZIER)) {
617 case CU_NURB_ENDPOINT:
619 for(a=1; a <= pnts_order; a++) {
621 if(a >= order && a <= pnts) k+= 1.0f;
625 /* Warning, the order MUST be 2 or 4,
626 * if this is not enforced, the displist will be corrupt */
629 for(a=0; a < pnts_order; a++) {
636 for(a=0; a < pnts_order; a++) {
637 if(a >= order && a <= pnts) k+= 0.5f;
642 printf("bez nurb curve order is not 3 or 4, should never happen\n");
646 for(a=0; a < pnts_order; a++) {
653 static void makecyclicknots(float *knots, short pnts, short order)
654 /* pnts, order: number of pnts NOT corrected for cyclic */
658 if(knots==NULL) return;
662 /* do first long rows (order -1), remove identical knots at endpoints */
665 for(a=1; a<order2; a++) {
666 if(knots[b]!= knots[b-a]) break;
668 if(a==order2) knots[pnts+order-2]+= 1.0f;
672 c=pnts + order + order2;
673 for(a=pnts+order2; a<c; a++) {
674 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
681 static void makeknots(Nurb *nu, short uv)
683 if(nu->type == CU_NURBS) {
685 if(nu->knotsu) MEM_freeN(nu->knotsu);
686 if(check_valid_nurb_u(nu)) {
687 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
688 if(nu->flagu & CU_NURB_CYCLIC) {
689 calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
690 makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
692 calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
695 else nu->knotsu= NULL;
698 if(nu->knotsv) MEM_freeN(nu->knotsv);
699 if(check_valid_nurb_v(nu)) {
700 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
701 if(nu->flagv & CU_NURB_CYCLIC) {
702 calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
703 makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
705 calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
708 else nu->knotsv= NULL;
713 void nurbs_knot_calc_u(Nurb *nu)
718 void nurbs_knot_calc_v(Nurb *nu)
723 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
726 int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
728 orderpluspnts= order+pnts;
729 opp2 = orderpluspnts-1;
731 /* this is for float inaccuracy */
732 if(t < knots[0]) t= knots[0];
733 else if(t > knots[opp2]) t= knots[opp2];
735 /* this part is order '1' */
737 for(i=0;i<opp2;i++) {
738 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
754 /* this is order 2,3,... */
755 for(j=2; j<=order; j++) {
757 if(i2+j>= orderpluspnts) i2= opp2-j;
759 for(i= i1; i<=i2; i++) {
761 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
765 if(basis[i+1] != 0.0f)
766 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
777 for(i=i1; i<=i2; i++) {
778 if(basis[i] > 0.0f) {
780 if(*start==1000) *start= i;
786 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
787 /* coord_array has to be 3*4*resolu*resolv in size, and zero-ed */
790 float *basisu, *basis, *basisv, *sum, *fp, *in;
791 float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
792 int i, j, iofs, jofs, cycl, len, curu, curv;
793 int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
795 int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
797 if(nu->knotsu==NULL || nu->knotsv==NULL) return;
798 if(nu->orderu>nu->pntsu) return;
799 if(nu->orderv>nu->pntsv) return;
800 if(coord_array==NULL) return;
802 /* allocate and initialize */
808 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
817 i= nu->pntsu*nu->pntsv;
820 if(bp->vec[3] != 1.0f) {
828 ustart= fp[nu->orderu-1];
829 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
830 else uend= fp[nu->pntsu];
831 ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
833 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
836 vstart= fp[nu->orderv-1];
838 if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
839 else vend= fp[nu->pntsv];
840 vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
843 basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
844 jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
845 jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
847 /* precalculation of basisv and jstart,jend */
848 if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1;
854 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
859 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
866 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
879 for(j= jsta; j<=jen; j++) {
881 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
883 bp= nu->bp+ nu->pntsu*jofs+istart-1;
885 for(i= istart; i<=iend; i++, fp++) {
889 bp= nu->bp+ nu->pntsu*jofs+iofs;
894 *fp= basisu[i]*basis[j]*bp->vec[3];
897 else *fp= basisu[i]*basis[j];
903 for(j= jsta; j<=jen; j++) {
904 for(i= istart; i<=iend; i++, fp++) {
910 /* one! (1.0) real point now */
912 for(j= jsta; j<=jen; j++) {
914 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
916 bp= nu->bp+ nu->pntsu*jofs+istart-1;
918 for(i= istart; i<=iend; i++, fp++) {
922 bp= nu->bp+ nu->pntsu*jofs+iofs;
927 in[0]+= (*fp) * bp->vec[0];
928 in[1]+= (*fp) * bp->vec[1];
929 in[2]+= (*fp) * bp->vec[2];
938 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
949 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
950 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
951 * tilt_array and radius_array will be written to if valid */
954 float u, ustart, uend, ustep, sumdiv;
955 float *basisu, *sum, *fp;
956 float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
957 int i, len, istart, iend, cycl;
959 if(nu->knotsu==NULL) return;
960 if(nu->orderu>nu->pntsu) return;
961 if(coord_array==NULL) return;
963 /* allocate and initialize */
966 sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
968 resolu= (resolu*SEGMENTSU(nu));
976 ustart= fp[nu->orderu-1];
977 if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
978 else uend= fp[nu->pntsu];
979 ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
981 basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
983 if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1;
989 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
993 bp= nu->bp+ istart-1;
994 for(i= istart; i<=iend; i++, fp++) {
996 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
999 *fp= basisu[i]*bp->vec[3];
1002 if(sumdiv != 0.0f) if(sumdiv < 0.999f || sumdiv > 1.001f) {
1003 /* is normalizing needed? */
1005 for(i= istart; i<=iend; i++, fp++) {
1010 /* one! (1.0) real point */
1012 bp= nu->bp+ istart-1;
1013 for(i= istart; i<=iend; i++, fp++) {
1015 if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
1020 coord_fp[0]+= (*fp) * bp->vec[0];
1021 coord_fp[1]+= (*fp) * bp->vec[1];
1022 coord_fp[2]+= (*fp) * bp->vec[2];
1025 (*tilt_fp) += (*fp) * bp->alfa;
1028 (*radius_fp) += (*fp) * bp->radius;
1031 (*weight_fp) += (*fp) * bp->weight;
1036 coord_fp = (float *)(((char *)coord_fp) + stride);
1038 if (tilt_fp) tilt_fp = (float *)(((char *)tilt_fp) + stride);
1039 if (radius_fp) radius_fp = (float *)(((char *)radius_fp) + stride);
1040 if (weight_fp) weight_fp = (float *)(((char *)weight_fp) + stride);
1050 /* forward differencing method for bezier curve */
1051 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1053 float rt0,rt1,rt2,rt3,f;
1058 rt1= 3.0f*(q1-q0)/f;
1060 rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1062 rt3= (q3-q0+3.0f*(q1-q2))/f;
1069 for(a=0; a<=it; a++) {
1071 p = (float *)(((char *)p)+stride);
1078 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1080 /* note that these are not purpendicular to the curve
1081 * they need to be rotated for this,
1083 * This could also be optimized like forward_diff_bezier */
1085 for(a=0; a<=it; a++) {
1086 float t = (float)a / (float)it;
1089 for(i=0; i<3; i++) {
1090 p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1093 p = (float *)(((char *)p)+stride);
1097 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1099 float *make_orco_surf(Object *ob)
1101 /* Note: this function is used in convertblender only atm, so
1102 * suppose nonzero curve's render resolution should always be used */
1103 Curve *cu= ob->data;
1108 float *fp, *coord_array;
1110 /* first calculate the size of the datablock */
1113 /* as we want to avoid the seam in a cyclic nurbs
1114 texture wrapping, reserve extra orco data space to save these extra needed
1115 vertex based UV coordinates for the meridian vertices.
1116 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1117 the renderface/vert construction.
1119 See also convertblender.c: init_render_surf()
1122 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1123 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1125 sizeu = nu->pntsu*resolu;
1126 sizev = nu->pntsv*resolv;
1127 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1128 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1129 if(nu->pntsv>1) tot+= sizeu * sizev;
1133 /* makeNurbfaces wants zeros */
1134 fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1138 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1139 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1142 sizeu = nu->pntsu*resolu;
1143 sizev = nu->pntsv*resolv;
1144 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1145 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1147 if(cu->flag & CU_UV_ORCO) {
1148 for(b=0; b< sizeu; b++) {
1149 for(a=0; a< sizev; a++) {
1151 if(sizev <2) fp[0]= 0.0f;
1152 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1154 if(sizeu <2) fp[1]= 0.0f;
1155 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1164 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1165 float *tdata= _tdata;
1167 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1169 for(b=0; b<sizeu; b++) {
1171 if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1174 for(a=0; a<sizev; a++) {
1176 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1179 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1181 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1182 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1183 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1198 /* NOTE: This routine is tied to the order of vertex
1199 * built by displist and as passed to the renderer.
1201 float *make_orco_curve(Scene *scene, Object *ob)
1203 Curve *cu = ob->data;
1206 float *fp, *coord_array;
1207 ListBase disp = {NULL, NULL};
1209 makeDispListCurveTypes_forOrco(scene, ob, &disp);
1212 for (dl=disp.first; dl; dl=dl->next) {
1213 if (dl->type==DL_INDEX3) {
1215 } else if (dl->type==DL_SURF) {
1216 /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1217 if (dl->flag & DL_CYCL_U) {
1218 if (dl->flag & DL_CYCL_V)
1219 numVerts += (dl->parts+1)*(dl->nr+1);
1221 numVerts += dl->parts*(dl->nr+1);
1224 numVerts += dl->parts*dl->nr;
1228 fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1229 for (dl=disp.first; dl; dl=dl->next) {
1230 if (dl->type==DL_INDEX3) {
1231 for (u=0; u<dl->nr; u++, fp+=3) {
1232 if (cu->flag & CU_UV_ORCO) {
1233 fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1237 VECCOPY(fp, &dl->verts[u*3]);
1239 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1240 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1241 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1244 } else if (dl->type==DL_SURF) {
1245 int sizeu= dl->nr, sizev= dl->parts;
1247 /* exception as handled in convertblender.c too */
1248 if (dl->flag & DL_CYCL_U) {
1250 if (dl->flag & DL_CYCL_V)
1254 for (u=0; u<sizev; u++) {
1255 for (v=0; v<sizeu; v++,fp+=3) {
1256 if (cu->flag & CU_UV_ORCO) {
1257 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1258 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1262 int realv= v % dl->nr;
1263 int realu= u % dl->parts;
1265 vert= dl->verts + 3*(dl->nr*realu + realv);
1268 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1269 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1270 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1277 freedisplist(&disp);
1283 /* ***************** BEVEL ****************** */
1285 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1287 DispList *dl, *dlnew;
1289 float *fp, facx, facy, angle, dangle;
1293 disp->first = disp->last = NULL;
1295 /* if a font object is being edited, then do nothing */
1296 // XXX if( ob == obedit && ob->type == OB_FONT ) return;
1299 if (cu->bevobj->type!=OB_CURVE) return;
1301 bevcu= cu->bevobj->data;
1302 if(bevcu->ext1==0.0f && bevcu->ext2==0.0f) {
1303 ListBase bevdisp= {NULL, NULL};
1304 facx= cu->bevobj->size[0];
1305 facy= cu->bevobj->size[1];
1308 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1311 dl= cu->bevobj->disp.first;
1313 makeDispListCurveTypes(scene, cu->bevobj, 0);
1314 dl= cu->bevobj->disp.first;
1319 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1320 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1322 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1323 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1325 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1327 BLI_addtail(disp, dlnew);
1329 nr= dlnew->parts*dlnew->nr;
1340 freedisplist(&bevdisp);
1343 else if(cu->ext1==0.0f && cu->ext2==0.0f) {
1346 else if(cu->ext2==0.0f) {
1347 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1348 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1349 BLI_addtail(disp, dl);
1352 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1361 else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f) { // we make a full round bevel in that case
1363 nr= 4+ 2*cu->bevresol;
1365 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1366 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1367 BLI_addtail(disp, dl);
1370 dl->flag= DL_BACK_CURVE;
1375 dangle= (2.0f*(float)M_PI/(nr));
1376 angle= -(nr-1)*dangle;
1378 for(a=0; a<nr; a++) {
1380 fp[1]= (cosf(angle)*(cu->ext2));
1381 fp[2]= (sinf(angle)*(cu->ext2)) - cu->ext1;
1389 /* bevel now in three parts, for proper vertex normals */
1392 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1393 dnr= nr= 2+ cu->bevresol;
1394 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1395 nr= 3+ 2*cu->bevresol;
1397 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1398 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1399 BLI_addtail(disp, dl);
1402 dl->flag= DL_BACK_CURVE;
1407 dangle= (0.5*M_PI/(dnr-1));
1408 angle= -(nr-1)*dangle;
1410 for(a=0; a<nr; a++) {
1412 fp[1]= (float)(cosf(angle)*(cu->ext2));
1413 fp[2]= (float)(sinf(angle)*(cu->ext2)) - cu->ext1;
1419 /* part 2, sidefaces */
1420 if(cu->ext1!=0.0f) {
1423 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1424 dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1425 BLI_addtail(disp, dl);
1436 if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1437 dl= MEM_dupallocN(dl);
1438 dl->verts= MEM_dupallocN(dl->verts);
1439 BLI_addtail(disp, dl);
1450 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1451 dnr= nr= 2+ cu->bevresol;
1452 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1453 nr= 3+ 2*cu->bevresol;
1455 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1456 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1457 BLI_addtail(disp, dl);
1459 dl->flag= DL_FRONT_CURVE;
1466 dangle= (0.5*M_PI/(dnr-1));
1468 for(a=0; a<nr; a++) {
1470 fp[1]= (float)(cosf(angle)*(cu->ext2));
1471 fp[2]= (float)(sinf(angle)*(cu->ext2)) + cu->ext1;
1479 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1483 0: no intersection of segments
1484 1: exact intersection of segments
1485 2: cross-intersection of segments
1489 deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1490 if(deler==0.0f) return -1;
1492 *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1493 *labda= -(*labda/deler);
1495 deler= v3[coy]-v4[coy];
1497 deler=v3[cox]-v4[cox];
1498 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1500 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1502 vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1503 vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1505 if(*labda>=0.0f && *labda<=1.0f && *mu>=0.0f && *mu<=1.0f) {
1506 if(*labda==0.0f || *labda==1.0f || *mu==0.0f || *mu==1.0f) return 1;
1513 static short bevelinside(BevList *bl1,BevList *bl2)
1515 /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1516 /* returns '1' if correct hole */
1517 BevPoint *bevp, *prevbevp;
1518 float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1519 int nr, links=0,rechts=0,mode;
1521 /* take first vertex of possible hole */
1523 bevp= (BevPoint *)(bl2+1);
1524 hvec1[0]= bevp->vec[0];
1525 hvec1[1]= bevp->vec[1];
1527 VECCOPY(hvec2,hvec1);
1530 /* test it with all edges of potential surounding poly */
1531 /* count number of transitions left-right */
1533 bevp= (BevPoint *)(bl1+1);
1535 prevbevp= bevp+(nr-1);
1538 min= prevbevp->vec[1];
1542 max= prevbevp->vec[1];
1545 if(min<=hvec1[1] && max>=hvec1[1]) {
1546 /* there's a transition, calc intersection point */
1547 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1548 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1549 only allow for one situation: we choose lab= 1.0
1551 if(mode >= 0 && lab != 0.0f) {
1552 if(vec[0]<hvec1[0]) links++;
1561 if( (links & 1) && (rechts & 1) ) return 1;
1572 static int vergxcobev(const void *a1, const void *a2)
1574 const struct bevelsort *x1=a1,*x2=a2;
1576 if( x1->left > x2->left ) return 1;
1577 else if( x1->left < x2->left) return -1;
1581 /* this function cannot be replaced with atan2, but why? */
1583 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1585 float t01, t02, x3, y3;
1587 t01= (float)sqrt(x1*x1+y1*y1);
1588 t02= (float)sqrt(x2*x2+y2*y2);
1589 if(t01==0.0f) t01= 1.0f;
1590 if(t02==0.0f) t02= 1.0f;
1598 if(fabs(t02)>=1.0) t02= .5*M_PI;
1599 else t02= (saacos(t02))/2.0f;
1601 t02= (float)sin(t02);
1602 if(t02==0.0f) t02= 1.0f;
1606 if(x3==0 && y3==0) {
1610 t01= (float)sqrt(x3*x3+y3*y3);
1620 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1622 BezTriple *pprev, *next, *last;
1623 float fac, dfac, t[4];
1626 if(tilt_array==NULL && radius_array==NULL)
1629 last= nu->bezt+(nu->pntsu-1);
1631 /* returns a point */
1632 if(prevbezt==nu->bezt) {
1633 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1634 else pprev= prevbezt;
1636 else pprev= prevbezt-1;
1640 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1646 dfac= 1.0f/(float)resolu;
1648 for(a=0; a<resolu; a++, fac+= dfac) {
1650 if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1651 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1653 key_curve_position_weights(fac, t, nu->tilt_interp);
1654 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1657 tilt_array = (float *)(((char *)tilt_array) + stride);
1661 if (nu->radius_interp==KEY_CU_EASE) {
1662 /* Support 2.47 ease interp
1663 * Note! - this only takes the 2 points into account,
1664 * giving much more localized results to changes in radius, sometimes you want that */
1665 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1668 /* reuse interpolation from tilt if we can */
1669 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1670 key_curve_position_weights(fac, t, nu->radius_interp);
1672 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1675 radius_array = (float *)(((char *)radius_array) + stride);
1679 /* basic interpolation for now, could copy tilt interp too */
1680 *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1682 weight_array = (float *)(((char *)weight_array) + stride);
1687 /* make_bevel_list_3D_* funcs, at a minimum these must
1688 * fill in the bezp->quat and bezp->dir values */
1690 /* correct non-cyclic cases by copying direction and rotation
1691 * values onto the first & last end-points */
1692 static void bevel_list_cyclic_fix_3D(BevList *bl)
1694 BevPoint *bevp, *bevp1;
1696 bevp= (BevPoint *)(bl+1);
1698 QUATCOPY(bevp->quat, bevp1->quat);
1699 VECCOPY(bevp->dir, bevp1->dir);
1700 VECCOPY(bevp->tan, bevp1->tan);
1701 bevp= (BevPoint *)(bl+1);
1704 QUATCOPY(bevp->quat, bevp1->quat);
1705 VECCOPY(bevp->dir, bevp1->dir);
1706 VECCOPY(bevp->tan, bevp1->tan);
1708 /* utility for make_bevel_list_3D_* funcs */
1709 static void bevel_list_calc_bisect(BevList *bl)
1711 BevPoint *bevp2, *bevp1, *bevp0;
1714 bevp2= (BevPoint *)(bl+1);
1715 bevp1= bevp2+(bl->nr-1);
1720 /* totally simple */
1721 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1728 static void bevel_list_flip_tangents(BevList *bl)
1730 BevPoint *bevp2, *bevp1, *bevp0;
1733 bevp2= (BevPoint *)(bl+1);
1734 bevp1= bevp2+(bl->nr-1);
1739 if(RAD2DEGF(angle_v2v2(bevp0->tan, bevp1->tan)) > 90.0f)
1740 negate_v3(bevp1->tan);
1747 /* apply user tilt */
1748 static void bevel_list_apply_tilt(BevList *bl)
1750 BevPoint *bevp2, *bevp1;
1754 bevp2= (BevPoint *)(bl+1);
1755 bevp1= bevp2+(bl->nr-1);
1759 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1760 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1761 normalize_qt(bevp1->quat);
1767 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1768 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1770 BevPoint *bevp2, *bevp1, *bevp0;
1774 float bevp0_quat[4];
1777 for(a=0; a < smooth_iter; a++) {
1779 bevp2= (BevPoint *)(bl+1);
1780 bevp1= bevp2+(bl->nr-1);
1785 if(bl->poly== -1) { /* check its not cyclic */
1786 /* skip the first point */
1799 QUATCOPY(bevp0_quat, bevp0->quat);
1802 /* interpolate quats */
1803 float zaxis[3] = {0,0,1}, cross[3], q2[4];
1804 interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1807 mul_qt_v3(q, zaxis);
1808 cross_v3_v3v3(cross, zaxis, bevp1->dir);
1809 axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1812 QUATCOPY(bevp0_quat, bevp1->quat);
1813 mul_qt_qtqt(q, q2, q);
1814 interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1815 normalize_qt(bevp1->quat);
1818 /* bevp0= bevp1; */ /* UNUSED */
1825 static void make_bevel_list_3D_zup(BevList *bl)
1827 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1830 bevp2= (BevPoint *)(bl+1);
1831 bevp1= bevp2+(bl->nr-1);
1836 /* totally simple */
1837 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1838 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1846 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1848 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1852 bevel_list_calc_bisect(bl);
1854 bevp2= (BevPoint *)(bl+1);
1855 bevp1= bevp2+(bl->nr-1);
1861 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1862 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1865 float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1867 if(angle > 0.0f) { /* otherwise we can keep as is */
1869 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1870 axis_angle_to_quat(q, cross_tmp, angle);
1871 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1874 QUATCOPY(bevp1->quat, bevp0->quat);
1883 if(bl->poly != -1) { /* check for cyclic */
1885 /* Need to correct for the start/end points not matching
1886 * do this by calculating the tilt angle difference, then apply
1887 * the rotation gradually over the entire curve
1889 * note that the split is between last and second last, rather than first/last as youd expect.
1891 * real order is like this
1892 * 0,1,2,3,4 --> 1,2,3,4,0
1894 * this is why we compare last with second last
1896 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1898 BevPoint *bevp_first;
1899 BevPoint *bevp_last;
1902 bevp_first= (BevPoint *)(bl+1);
1903 bevp_first+= bl->nr-1;
1904 bevp_last = bevp_first;
1907 /* quats and vec's are normalized, should not need to re-normalize */
1908 mul_qt_v3(bevp_first->quat, vec_1);
1909 mul_qt_v3(bevp_last->quat, vec_2);
1910 normalize_v3(vec_1);
1911 normalize_v3(vec_2);
1913 /* align the vector, can avoid this and it looks 98% OK but
1914 * better to align the angle quat roll's before comparing */
1916 cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1917 angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1918 axis_angle_to_quat(q, cross_tmp, angle);
1919 mul_qt_v3(q, vec_2);
1922 angle= angle_normalized_v3v3(vec_1, vec_2);
1924 /* flip rotation if needs be */
1925 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1926 normalize_v3(cross_tmp);
1927 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
1930 bevp2= (BevPoint *)(bl+1);
1931 bevp1= bevp2+(bl->nr-1);
1936 ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1938 axis_angle_to_quat(q, bevp1->dir, ang_fac);
1939 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1948 static void make_bevel_list_3D_tangent(BevList *bl)
1950 BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1953 float bevp0_tan[3], cross_tmp[3];
1955 bevel_list_calc_bisect(bl);
1956 if(bl->poly== -1) /* check its not cyclic */
1957 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1958 bevel_list_flip_tangents(bl);
1960 /* correct the tangents */
1961 bevp2= (BevPoint *)(bl+1);
1962 bevp1= bevp2+(bl->nr-1);
1968 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1969 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1970 normalize_v3(bevp1->tan);
1978 /* now for the real twist calc */
1979 bevp2= (BevPoint *)(bl+1);
1980 bevp1= bevp2+(bl->nr-1);
1983 VECCOPY(bevp0_tan, bevp0->tan);
1988 /* make perpendicular, modify tan in place, is ok */
1990 float zero[3] = {0,0,0};
1992 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1993 normalize_v3(cross_tmp);
1994 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1996 /* bevp0= bevp1; */ /* UNUSED */
2002 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
2004 switch(twist_mode) {
2005 case CU_TWIST_TANGENT:
2006 make_bevel_list_3D_tangent(bl);
2008 case CU_TWIST_MINIMUM:
2009 make_bevel_list_3D_minimum_twist(bl);
2011 default: /* CU_TWIST_Z_UP default, pre 2.49c */
2012 make_bevel_list_3D_zup(bl);
2015 if(bl->poly== -1) /* check its not cyclic */
2016 bevel_list_cyclic_fix_3D(bl);
2019 bevel_list_smooth(bl, smooth_iter);
2021 bevel_list_apply_tilt(bl);
2026 /* only for 2 points */
2027 static void make_bevel_list_segment_3D(BevList *bl)
2031 BevPoint *bevp2= (BevPoint *)(bl+1);
2032 BevPoint *bevp1= bevp2+1;
2034 /* simple quat/dir */
2035 sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
2036 normalize_v3(bevp1->dir);
2038 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2040 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2041 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2042 normalize_qt(bevp1->quat);
2043 VECCOPY(bevp2->dir, bevp1->dir);
2044 QUATCOPY(bevp2->quat, bevp1->quat);
2049 void makeBevelList(Object *ob)
2052 - convert all curves to polys, with indication of resol and flags for double-vertices
2053 - possibly; do a smart vertice removal (in case Nurb)
2054 - separate in individual blicks with BoundBox
2055 - AutoHole detection
2059 BezTriple *bezt, *prevbezt;
2061 BevList *bl, *blnew, *blnext;
2062 BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2063 float min, inp, x1, x2, y1, y2;
2064 struct bevelsort *sortdata, *sd, *sd1;
2065 int a, b, nr, poly, resolu = 0, len = 0;
2066 int do_tilt, do_radius, do_weight;
2068 /* this function needs an object, because of tflag and upflag */
2071 /* do we need to calculate the radius for each point? */
2072 /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2074 /* STEP 1: MAKE POLYS */
2076 BLI_freelistN(&(cu->bev));
2077 if(cu->editnurb && ob->type!=OB_FONT) {
2078 ListBase *nurbs= curve_editnurbs(cu);
2080 } else nu= cu->nurb.first;
2084 /* check if we will calculate tilt data */
2085 do_tilt = CU_DO_TILT(cu, nu);
2086 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2089 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2090 * enforced in the UI but can go wrong possibly */
2091 if(!check_valid_nurb_u(nu)) {
2092 bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2093 BLI_addtail(&(cu->bev), bl);
2096 if(G.rendering && cu->resolu_ren!=0)
2097 resolu= cu->resolu_ren;
2101 if(nu->type == CU_POLY) {
2103 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2104 BLI_addtail(&(cu->bev), bl);
2106 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2110 bevp= (BevPoint *)(bl+1);
2114 VECCOPY(bevp->vec, bp->vec);
2115 bevp->alfa= bp->alfa;
2116 bevp->radius= bp->radius;
2117 bevp->weight= bp->weight;
2118 bevp->split_tag= TRUE;
2123 else if(nu->type == CU_BEZIER) {
2125 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1; /* in case last point is not cyclic */
2126 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2127 BLI_addtail(&(cu->bev), bl);
2129 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2131 bevp= (BevPoint *)(bl+1);
2135 if(nu->flagu & CU_NURB_CYCLIC) {
2137 prevbezt= nu->bezt+(nu->pntsu-1);
2145 if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2147 VECCOPY(bevp->vec, prevbezt->vec[1]);
2148 bevp->alfa= prevbezt->alfa;
2149 bevp->radius= prevbezt->radius;
2150 bevp->weight= prevbezt->weight;
2151 bevp->split_tag= TRUE;
2152 bevp->dupe_tag= FALSE;
2158 /* always do all three, to prevent data hanging around */
2161 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2162 for(j=0; j<3; j++) {
2163 forward_diff_bezier( prevbezt->vec[1][j], prevbezt->vec[2][j],
2164 bezt->vec[0][j], bezt->vec[1][j],
2165 &(bevp->vec[j]), resolu, sizeof(BevPoint));
2168 /* if both arrays are NULL do nothiong */
2169 alfa_bezpart( prevbezt, bezt, nu,
2170 do_tilt ? &bevp->alfa : NULL,
2171 do_radius ? &bevp->radius : NULL,
2172 do_weight ? &bevp->weight : NULL,
2173 resolu, sizeof(BevPoint));
2176 if(cu->twist_mode==CU_TWIST_TANGENT) {
2177 forward_diff_bezier_cotangent(
2178 prevbezt->vec[1], prevbezt->vec[2],
2179 bezt->vec[0], bezt->vec[1],
2180 bevp->tan, resolu, sizeof(BevPoint));
2183 /* indicate with handlecodes double points */
2184 if(prevbezt->h1==prevbezt->h2) {
2185 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2188 if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2189 else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2198 if((nu->flagu & CU_NURB_CYCLIC)==0) { /* not cyclic: endpoint */
2199 VECCOPY(bevp->vec, prevbezt->vec[1]);
2200 bevp->alfa= prevbezt->alfa;
2201 bevp->radius= prevbezt->radius;
2202 bevp->weight= prevbezt->weight;
2206 else if(nu->type == CU_NURBS) {
2208 len= (resolu*SEGMENTSU(nu));
2210 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2211 BLI_addtail(&(cu->bev), bl);
2214 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2216 bevp= (BevPoint *)(bl+1);
2218 makeNurbcurve( nu, &bevp->vec[0],
2219 do_tilt ? &bevp->alfa : NULL,
2220 do_radius ? &bevp->radius : NULL,
2221 do_weight ? &bevp->weight : NULL,
2222 resolu, sizeof(BevPoint));
2229 /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2232 if (bl->nr) { /* null bevel items come from single points */
2234 bevp1= (BevPoint *)(bl+1);
2235 bevp0= bevp1+(nr-1);
2238 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2239 if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2240 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2241 bevp0->dupe_tag= TRUE;
2255 if(bl->nr && bl->dupe_nr) {
2256 nr= bl->nr- bl->dupe_nr+1; /* +1 because vectorbezier sets flag too */
2257 blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2258 memcpy(blnew, bl, sizeof(BevList));
2260 BLI_remlink(&(cu->bev), bl);
2261 BLI_insertlinkbefore(&(cu->bev),blnext,blnew); /* to make sure bevlijst is tuned with nurblist */
2262 bevp0= (BevPoint *)(bl+1);
2263 bevp1= (BevPoint *)(blnew+1);
2266 if(bevp0->dupe_tag==0) {
2267 memcpy(bevp1, bevp0, sizeof(BevPoint));
2279 /* STEP 3: POLYS COUNT AND AUTOHOLE */
2283 if(bl->nr && bl->poly>=0) {
2292 /* find extreme left points, also test (turning) direction */
2294 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2300 bevp= (BevPoint *)(bl+1);
2303 if(min>bevp->vec[0]) {
2312 bevp= (BevPoint *)(bl+1);
2313 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2314 else bevp0= bevp1-1;
2315 bevp= bevp+ (bl->nr-1);
2316 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2317 else bevp2= bevp1+1;
2319 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]);
2321 if(inp > 0.0f) sd->dir= 1;
2329 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2332 for(a=1; a<poly; a++, sd++) {
2333 bl= sd->bl; /* is bl a hole? */
2334 sd1= sortdata+ (a-1);
2335 for(b=a-1; b>=0; b--, sd1--) { /* all polys to the left */
2336 if(bevelinside(sd1->bl, bl)) {
2337 bl->hole= 1- sd1->bl->hole;
2343 /* turning direction */
2344 if((cu->flag & CU_3D)==0) {
2346 for(a=0; a<poly; a++, sd++) {
2347 if(sd->bl->hole==sd->dir) {
2349 bevp1= (BevPoint *)(bl+1);
2350 bevp2= bevp1+ (bl->nr-1);
2353 SWAP(BevPoint, *bevp1, *bevp2);
2360 MEM_freeN(sortdata);
2363 /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2364 if((cu->flag & CU_3D)==0) {
2365 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2366 * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2373 else if(bl->nr==2) { /* 2 pnt, treat separate */
2374 bevp2= (BevPoint *)(bl+1);
2377 x1= bevp1->vec[0]- bevp2->vec[0];
2378 y1= bevp1->vec[1]- bevp2->vec[1];
2380 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2381 bevp2->sina= bevp1->sina;
2382 bevp2->cosa= bevp1->cosa;
2384 /* fill in dir & quat */
2385 make_bevel_list_segment_3D(bl);
2388 bevp2= (BevPoint *)(bl+1);
2389 bevp1= bevp2+(bl->nr-1);
2394 x1= bevp1->vec[0]- bevp0->vec[0];
2395 x2= bevp1->vec[0]- bevp2->vec[0];
2396 y1= bevp1->vec[1]- bevp0->vec[1];
2397 y2= bevp1->vec[1]- bevp2->vec[1];
2399 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2401 /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2402 * no need for tricky tilt calculation as with 3D curves */
2403 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2404 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2405 /* done with inline make_bevel_list_3D_zup */
2412 /* correct non-cyclic cases */
2414 bevp= (BevPoint *)(bl+1);
2416 bevp->sina= bevp1->sina;
2417 bevp->cosa= bevp1->cosa;
2418 bevp= (BevPoint *)(bl+1);
2421 bevp->sina= bevp1->sina;
2422 bevp->cosa= bevp1->cosa;
2424 /* correct for the dir/quat, see above why its needed */
2425 bevel_list_cyclic_fix_3D(bl);
2431 else { /* 3D Curves */
2438 else if(bl->nr==2) { /* 2 pnt, treat separate */
2439 make_bevel_list_segment_3D(bl);
2442 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2449 /* ****************** HANDLES ************** */
2453 * 0: nothing, 1:auto, 2:vector, 3:aligned
2456 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2457 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2459 float *p1,*p2,*p3, pt[3];
2460 float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2461 const float eps= 1e-5;
2463 if(bezt->h1==0 && bezt->h2==0) return;
2469 pt[0]= 2*p2[0]- p3[0];
2470 pt[1]= 2*p2[1]- p3[1];
2471 pt[2]= 2*p2[2]- p3[2];
2474 else p1= prev->vec[1];
2477 pt[0]= 2*p2[0]- p1[0];
2478 pt[1]= 2*p2[1]- p1[1];
2479 pt[2]= 2*p2[2]- p1[2];
2482 else p3= next->vec[1];
2489 else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2496 else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2498 if(len1==0.0f) len1=1.0f;
2499 if(len2==0.0f) len2=1.0f;
2502 if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM) || ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) { /* auto */
2503 vx= dx1/len2 + dx/len1;
2504 vy= dy1/len2 + dy/len1;
2505 vz= dz1/len2 + dz/len1;
2506 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2508 int leftviolate=0, rightviolate=0; /* for mode==2 */
2510 if(len1>5.0f*len2) len1= 5.0f*len2;
2511 if(len2>5.0f*len1) len2= 5.0f*len1;
2513 if(ELEM(bezt->h1,HD_AUTO,HD_AUTO_ANIM)) {
2515 *(p2-3)= *p2-vx*len1;
2516 *(p2-2)= *(p2+1)-vy*len1;
2517 *(p2-1)= *(p2+2)-vz*len1;
2519 if((bezt->h1==HD_AUTO_ANIM) && next && prev) { // keep horizontal if extrema
2520 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2521 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2522 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2523 bezt->vec[0][1]= bezt->vec[1][1];
2525 else { // handles should not be beyond y coord of two others
2526 if(ydiff1 <= 0.0f) {
2527 if(prev->vec[1][1] > bezt->vec[0][1]) {
2528 bezt->vec[0][1]= prev->vec[1][1];
2533 if(prev->vec[1][1] < bezt->vec[0][1]) {
2534 bezt->vec[0][1]= prev->vec[1][1];
2541 if(ELEM(bezt->h2,HD_AUTO,HD_AUTO_ANIM)) {
2543 *(p2+3)= *p2+vx*len2;
2544 *(p2+4)= *(p2+1)+vy*len2;
2545 *(p2+5)= *(p2+2)+vz*len2;
2547 if((bezt->h2==HD_AUTO_ANIM) && next && prev) { // keep horizontal if extrema
2548 float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2549 float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2550 if( (ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f) ) {
2551 bezt->vec[2][1]= bezt->vec[1][1];
2553 else { // handles should not be beyond y coord of two others
2554 if(ydiff1 <= 0.0f) {
2555 if(next->vec[1][1] < bezt->vec[2][1]) {
2556 bezt->vec[2][1]= next->vec[1][1];
2561 if(next->vec[1][1] > bezt->vec[2][1]) {
2562 bezt->vec[2][1]= next->vec[1][1];
2569 if(leftviolate || rightviolate) { /* align left handle */
2572 sub_v3_v3v3(h1, p2-3, p2);
2573 sub_v3_v3v3(h2, p2, p2+3);
2574 len1= normalize_v3(h1);
2575 len2= normalize_v3(h2);
2577 vz= dot_v3v3(h1, h2);
2580 *(p2+3)= *(p2) - vz*len2*h1[0];
2581 *(p2+4)= *(p2+1) - vz*len2*h1[1];
2582 *(p2+5)= *(p2+2) - vz*len2*h1[2];
2585 *(p2-3)= *(p2) + vz*len1*h2[0];
2586 *(p2-2)= *(p2+1) + vz*len1*h2[1];
2587 *(p2-1)= *(p2+2) + vz*len1*h2[2];
2594 if(bezt->h1==HD_VECT) { /* vector */
2599 *(p2-2)= *(p2+1)-dy;
2600 *(p2-1)= *(p2+2)-dz;
2602 if(bezt->h2==HD_VECT) {
2607 *(p2+4)= *(p2+1)+dy1;
2608 *(p2+5)= *(p2+2)+dz1;
2611 len2= len_v3v3(p2, p2+3);
2612 len1= len_v3v3(p2, p2-3);
2613 if(len1==0.0f) len1= 1.0f;
2614 if(len2==0.0f) len2= 1.0f;
2616 if(bezt->f1 & SELECT) { /* order of calculation */
2617 if(bezt->h2==HD_ALIGN) { /* aligned */
2620 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2621 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2622 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2625 if(bezt->h1==HD_ALIGN) {
2628 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2629 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2630 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2635 if(bezt->h1==HD_ALIGN) {
2638 p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2639 p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2640 p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2643 if(bezt->h2==HD_ALIGN) { /* aligned */
2646 p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2647 p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2648 p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2654 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2656 BezTriple *bezt, *prev, *next;
2659 if(nu->type != CU_BEZIER) return;
2660 if(nu->pntsu<2) return;
2664 if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2669 calchandleNurb(bezt, prev, next, 0);
2672 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2682 void testhandlesNurb(Nurb *nu)
2684 /* use when something has changed with handles.
2685 it treats all BezTriples with the following rules:
2686 PHASE 1: do types have to be altered?
2687 Auto handles: become aligned when selection status is NOT(000 || 111)
2688 Vector handles: become 'nothing' when (one half selected AND other not)
2689 PHASE 2: recalculate handles
2694 if(nu->type != CU_BEZIER) return;
2700 if(bezt->f1 & SELECT) flag++;
2701 if(bezt->f2 & SELECT) flag += 2;
2702 if(bezt->f3 & SELECT) flag += 4;
2704 if( !(flag==0 || flag==7) ) {
2705 if(ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
2708 if(ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
2712 if(bezt->h1==HD_VECT) { /* vector */
2713 if(flag < 4) bezt->h1= 0;
2715 if(bezt->h2==HD_VECT) { /* vector */
2716 if( flag > 3) bezt->h2= 0;
2722 calchandlesNurb(nu);
2725 void autocalchandlesNurb(Nurb *nu, int flag)
2727 /* checks handle coordinates and calculates type */
2729 BezTriple *bezt2, *bezt1, *bezt0;
2730 int i, align, leftsmall, rightsmall;
2732 if(nu==NULL || nu->bezt==NULL) return;
2735 bezt1 = bezt2 + (nu->pntsu-1);
2741 align= leftsmall= rightsmall= 0;
2744 if(flag==0 || (bezt1->f1 & flag) ) {
2746 /* distance too short: vectorhandle */
2747 if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001f) {
2752 /* aligned handle? */
2753 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001f) {
2755 bezt1->h1= HD_ALIGN;
2757 /* or vector handle? */
2758 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001f)
2764 if(flag==0 || (bezt1->f3 & flag) ) {
2766 /* distance too short: vectorhandle */
2767 if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001f) {
2772 /* aligned handle? */
2773 if(align) bezt1->h2= HD_ALIGN;
2775 /* or vector handle? */
2776 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001f)
2781 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2782 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2784 /* undesired combination: */
2785 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2786 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2793 calchandlesNurb(nu);
2796 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2800 nu= editnurb->first;
2802 autocalchandlesNurb(nu, flag);
2807 void sethandlesNurb(ListBase *editnurb, short code)
2809 /* code==1: set autohandle */
2810 /* code==2: set vectorhandle */
2811 /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2812 /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2813 /* code==5: Set align, like 3 but no toggle */
2814 /* code==6: Clear align, like 3 but no toggle */
2819 if(code==1 || code==2) {
2820 nu= editnurb->first;
2822 if(nu->type == CU_BEZIER) {
2826 if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2827 if(bezt->f1 & SELECT) bezt->h1= code;
2828 if(bezt->f3 & SELECT) bezt->h2= code;
2829 if(bezt->h1!=bezt->h2) {
2830 if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2831 if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2836 calchandlesNurb(nu);
2842 /* there is 1 handle not FREE: FREE it all, else make ALIGNED */
2844 nu= editnurb->first;
2847 } else if (code == 6) {
2852 if(nu->type == CU_BEZIER) {
2856 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2857 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2867 nu= editnurb->first;
2869 if(nu->type == CU_BEZIER) {
2873 if(bezt->f1 & SELECT) bezt->h1= ok;
2874 if(bezt->f3 & SELECT) bezt->h2= ok;
2878 calchandlesNurb(nu);
2885 static void swapdata(void *adr1, void *adr2, int len)
2893 memcpy(adr, adr1, len);
2894 memcpy(adr1, adr2, len);
2895 memcpy(adr2, adr, len);
2900 adr= (char *)MEM_mallocN(len, "curve swap");
2901 memcpy(adr, adr1, len);
2902 memcpy(adr1, adr2, len);
2903 memcpy(adr2, adr, len);
2908 void switchdirectionNurb(Nurb *nu)
2910 BezTriple *bezt1, *bezt2;
2912 float *fp1, *fp2, *tempf;
2915 if(nu->pntsu==1 && nu->pntsv==1) return;
2917 if(nu->type == CU_BEZIER) {
2921 if(a & 1) a+= 1; /* if odd, also swap middle content */
2924 if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2926 swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2927 if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2929 SWAP(char, bezt1->h1, bezt1->h2);
2930 SWAP(short, bezt1->f1, bezt1->f3);
2933 SWAP(char, bezt2->h1, bezt2->h2);
2934 SWAP(short, bezt2->f1, bezt2->f3);
2935 bezt1->alfa= -bezt1->alfa;
2936 bezt2->alfa= -bezt2->alfa;
2943 else if(nu->pntsv==1) {
2948 while(bp1!=bp2 && a>0) {
2949 SWAP(BPoint, *bp1, *bp2);
2951 bp1->alfa= -bp1->alfa;
2952 bp2->alfa= -bp2->alfa;
2956 if(nu->type == CU_NURBS) {
2957 /* no knots for too short paths */