fix for the rna curve interpolation enum, 'ease' was using the same value as Bezier.
[blender-staging.git] / source / blender / blenkernel / intern / curve.c
1
2 /*  curve.c 
3  * 
4  *  
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
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.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include <math.h>  // floor
34 #include <string.h>
35 #include <stdlib.h>  
36
37 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"  
39 #include "BLI_math.h"  
40
41 #include "DNA_curve_types.h"  
42 #include "DNA_material_types.h"  
43
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"
49
50 #include "BKE_animsys.h"
51 #include "BKE_anim.h"  
52 #include "BKE_curve.h"  
53 #include "BKE_displist.h"  
54 #include "BKE_font.h" 
55 #include "BKE_global.h" 
56 #include "BKE_key.h"  
57 #include "BKE_library.h"  
58 #include "BKE_main.h"  
59 #include "BKE_object.h"  
60 #include "BKE_utildefines.h"  // VECCOPY
61
62 #include "ED_curve.h"
63
64 /* globals */
65
66 /* local */
67 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, 
68                            short cox, short coy, 
69                            float *labda, float *mu, float *vec);
70
71 void unlink_curve(Curve *cu)
72 {
73         int a;
74         
75         for(a=0; a<cu->totcol; a++) {
76                 if(cu->mat[a]) cu->mat[a]->id.us--;
77                 cu->mat[a]= 0;
78         }
79         if(cu->vfont) cu->vfont->id.us--; 
80         cu->vfont= 0;
81         if(cu->key) cu->key->id.us--;
82         cu->key= 0;
83 }
84
85 /* frees editcurve entirely */
86 void BKE_free_editfont(Curve *cu)
87 {
88         if(cu->editfont) {
89                 EditFont *ef= cu->editfont;
90                 
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);
97                 
98                 MEM_freeN(ef);
99                 cu->editfont= NULL;
100         }
101 }
102
103 /* don't free curve itself */
104 void free_curve(Curve *cu)
105 {
106         freeNurblist(&cu->nurb);
107         BLI_freelistN(&cu->bev);
108         freedisplist(&cu->disp);
109         BKE_free_editfont(cu);
110
111         free_curve_editNurb(cu);
112         unlink_curve(cu);
113         BKE_free_animdata((ID *)cu);
114         
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);
121 }
122
123 Curve *add_curve(char *name, int type)
124 {
125         Curve *cu;
126
127         cu= alloc_libblock(&G.main->curve, ID_CU, name);
128         
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;
131         cu->pathlen= 100;
132         cu->resolu= cu->resolv= 12;
133         cu->width= 1.0;
134         cu->wordspace = 1.0;
135         cu->spacing= cu->linedist= 1.0;
136         cu->fsize= 1.0;
137         cu->ulheight = 0.05;    
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...
141         
142         cu->bb= unit_boundbox();
143         
144         if(type==OB_FONT) {
145                 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
146                 cu->vfont->id.us+=4;
147                 cu->str= MEM_mallocN(12, "str");
148                 strcpy(cu->str, "Text");
149                 cu->len= cu->pos= 4;
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;
154         }
155         
156         return cu;
157 }
158
159 Curve *copy_curve(Curve *cu)
160 {
161         Curve *cun;
162         int a;
163         
164         cun= copy_libblock(cu);
165         cun->nurb.first= cun->nurb.last= 0;
166         duplicateNurblist( &(cun->nurb), &(cu->nurb));
167
168         cun->mat= MEM_dupallocN(cu->mat);
169         for(a=0; a<cun->totcol; a++) {
170                 id_us_plus((ID *)cun->mat[a]);
171         }
172         
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);
177         
178         cun->key= copy_key(cu->key);
179         if(cun->key) cun->key->from= (ID *)cun;
180         
181         cun->disp.first= cun->disp.last= 0;
182         cun->bev.first= cun->bev.last= 0;
183         cun->path= 0;
184
185         cun->editnurb= NULL;
186         cun->editfont= NULL;
187
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
192
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);
197         
198         return cun;
199 }
200
201 void make_local_curve(Curve *cu)
202 {
203         Object *ob = 0;
204         Curve *cun;
205         int local=0, lib=0;
206         
207         /* - when there are only lib users: don't do
208          * - when there are only local users: set flag
209          * - mixed: do a copy
210          */
211         
212         if(cu->id.lib==0) return;
213         
214         if(cu->vfont) cu->vfont->id.lib= 0;
215         
216         if(cu->id.us==1) {
217                 cu->id.lib= 0;
218                 cu->id.flag= LIB_LOCAL;
219                 new_id(0, (ID *)cu, 0);
220                 return;
221         }
222         
223         ob= G.main->object.first;
224         while(ob) {
225                 if(ob->data==cu) {
226                         if(ob->id.lib) lib= 1;
227                         else local= 1;
228                 }
229                 ob= ob->id.next;
230         }
231         
232         if(local && lib==0) {
233                 cu->id.lib= 0;
234                 cu->id.flag= LIB_LOCAL;
235                 new_id(0, (ID *)cu, 0);
236         }
237         else if(local && lib) {
238                 cun= copy_curve(cu);
239                 cun->id.us= 0;
240                 
241                 ob= G.main->object.first;
242                 while(ob) {
243                         if(ob->data==cu) {
244                                 
245                                 if(ob->id.lib==0) {
246                                         ob->data= cun;
247                                         cun->id.us++;
248                                         cu->id.us--;
249                                 }
250                         }
251                         ob= ob->id.next;
252                 }
253         }
254 }
255
256 short curve_type(Curve *cu)
257 {
258         Nurb *nu;
259         if(cu->vfont) {
260                 return OB_FONT;
261         }
262         for (nu= cu->nurb.first; nu; nu= nu->next) {
263                 if(nu->pntsv>1) {
264                         return OB_SURF;
265                 }
266         }
267         
268         return OB_CURVE;
269 }
270
271 void test_curve_type(Object *ob)
272 {       
273         ob->type = curve_type(ob->data);
274 }
275
276 void tex_space_curve(Curve *cu)
277 {
278         DispList *dl;
279         BoundBox *bb;
280         float *fp, min[3], max[3];
281         int tot, doit= 0;
282         
283         if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
284         bb= cu->bb;
285         
286         INIT_MINMAX(min, max);
287
288         dl= cu->disp.first;
289         while(dl) {
290                 
291                 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
292                 else tot= dl->nr*dl->parts;
293                 
294                 if(tot) doit= 1;
295                 fp= dl->verts;
296                 while(tot--) {
297                         DO_MINMAX(fp, min, max);
298                         fp += 3;
299                 }
300                 dl= dl->next;
301         }
302
303         if(!doit) {
304                 min[0] = min[1] = min[2] = -1.0f;
305                 max[0] = max[1] = max[2] = 1.0f;
306         }
307
308         boundbox_set_from_min_max(bb, min, max);
309
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;
315
316                 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
317
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;
321         
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;
325         
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;
329
330         }
331 }
332
333
334 int count_curveverts(ListBase *nurb)
335 {
336         Nurb *nu;
337         int tot=0;
338         
339         nu= nurb->first;
340         while(nu) {
341                 if(nu->bezt) tot+= 3*nu->pntsu;
342                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
343                 
344                 nu= nu->next;
345         }
346         return tot;
347 }
348
349 int count_curveverts_without_handles(ListBase *nurb)
350 {
351         Nurb *nu;
352         int tot=0;
353         
354         nu= nurb->first;
355         while(nu) {
356                 if(nu->bezt) tot+= nu->pntsu;
357                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
358                 
359                 nu= nu->next;
360         }
361         return tot;
362 }
363
364 /* **************** NURBS ROUTINES ******************** */
365
366 void freeNurb(Nurb *nu)
367 {
368
369         if(nu==0) return;
370
371         if(nu->bezt) MEM_freeN(nu->bezt);
372         nu->bezt= 0;
373         if(nu->bp) MEM_freeN(nu->bp);
374         nu->bp= 0;
375         if(nu->knotsu) MEM_freeN(nu->knotsu);
376         nu->knotsu= NULL;
377         if(nu->knotsv) MEM_freeN(nu->knotsv);
378         nu->knotsv= NULL;
379         /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
380
381         MEM_freeN(nu);
382
383 }
384
385
386 void freeNurblist(ListBase *lb)
387 {
388         Nurb *nu, *next;
389
390         if(lb==0) return;
391
392         nu= lb->first;
393         while(nu) {
394                 next= nu->next;
395                 freeNurb(nu);
396                 nu= next;
397         }
398         lb->first= lb->last= 0;
399 }
400
401 Nurb *duplicateNurb(Nurb *nu)
402 {
403         Nurb *newnu;
404         int len;
405
406         newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
407         if(newnu==0) return 0;
408         memcpy(newnu, nu, sizeof(Nurb));
409
410         if(nu->bezt) {
411                 newnu->bezt=
412                         (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
413                 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
414         }
415         else {
416                 len= nu->pntsu*nu->pntsv;
417                 newnu->bp=
418                         (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
419                 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
420                 
421                 newnu->knotsu= newnu->knotsv= NULL;
422                 
423                 if(nu->knotsu) {
424                         len= KNOTSU(nu);
425                         if(len) {
426                                 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
427                                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
428                         }
429                 }
430                 if(nu->pntsv>1 && nu->knotsv) {
431                         len= KNOTSV(nu);
432                         if(len) {
433                                 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
434                                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
435                         }
436                 }
437         }
438         return newnu;
439 }
440
441 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
442 {
443         Nurb *nu, *nun;
444         
445         freeNurblist(lb1);
446         
447         nu= lb2->first;
448         while(nu) {
449                 nun= duplicateNurb(nu);
450                 BLI_addtail(lb1, nun);
451                 
452                 nu= nu->next;
453         }
454 }
455
456 void test2DNurb(Nurb *nu)
457 {
458         BezTriple *bezt;
459         BPoint *bp;
460         int a;
461         
462         if((nu->flag & CU_2D)==0)
463                 return;
464
465         if(nu->type == CU_BEZIER) {
466                 a= nu->pntsu;
467                 bezt= nu->bezt;
468                 while(a--) {
469                         bezt->vec[0][2]= 0.0; 
470                         bezt->vec[1][2]= 0.0; 
471                         bezt->vec[2][2]= 0.0;
472                         bezt++;
473                 }
474         }
475         else {
476                 a= nu->pntsu*nu->pntsv;
477                 bp= nu->bp;
478                 while(a--) {
479                         bp->vec[2]= 0.0;
480                         bp++;
481                 }
482         }
483 }
484
485 void minmaxNurb(Nurb *nu, float *min, float *max)
486 {
487         BezTriple *bezt;
488         BPoint *bp;
489         int a;
490
491         if(nu->type == CU_BEZIER) {
492                 a= nu->pntsu;
493                 bezt= nu->bezt;
494                 while(a--) {
495                         DO_MINMAX(bezt->vec[0], min, max);
496                         DO_MINMAX(bezt->vec[1], min, max);
497                         DO_MINMAX(bezt->vec[2], min, max);
498                         bezt++;
499                 }
500         }
501         else {
502                 a= nu->pntsu*nu->pntsv;
503                 bp= nu->bp;
504                 while(a--) {
505                         DO_MINMAX(bp->vec, min, max);
506                         bp++;
507                 }
508         }
509 }
510
511 /* be sure to call makeknots after this */
512 void addNurbPoints(Nurb *nu, int number)
513 {
514         BPoint *tmp= nu->bp;
515         int i;
516         nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
517
518         if(tmp) {
519                 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
520                 MEM_freeN(tmp);
521         }
522
523         memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
524
525         for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
526                 tmp->radius= 1.0f;
527         }
528
529         nu->pntsu += number;
530 }
531
532 void addNurbPointsBezier(Nurb *nu, int number)
533 {
534         BezTriple *tmp= nu->bezt;
535         int i;
536         nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
537
538         if(tmp) {
539                 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
540                 MEM_freeN(tmp);
541         }
542
543         memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
544
545         for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
546                 tmp->radius= 1.0f;
547         }
548
549         nu->pntsu += number;
550 }
551
552 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
553
554
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 */
558 {
559         float k;
560         int a, t;
561
562                 t = aantal+order;
563         if(type==0) {
564
565                 for(a=0;a<t;a++) {
566                         knots[a]= (float)a;
567                 }
568         }
569         else if(type==1) {
570                 k= 0.0;
571                 for(a=1;a<=t;a++) {
572                         knots[a-1]= k;
573                         if(a>=order && a<=aantal) k+= 1.0;
574                 }
575         }
576         else if(type==2) {
577                 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
578                 if(order==4) {
579                         k= 0.34;
580                         for(a=0;a<t;a++) {
581                                 knots[a]= (float)floor(k);
582                                 k+= (1.0/3.0);
583                         }
584                 }
585                 else if(order==3) {
586                         k= 0.6;
587                         for(a=0;a<t;a++) {
588                                 if(a>=order && a<=aantal) k+= (0.5);
589                                 knots[a]= (float)floor(k);
590                         }
591                 }
592                 else {
593                         printf("bez nurb curve order is not 3 or 4, should never happen\n");
594                 }
595         }
596 }
597
598 static void makecyclicknots(float *knots, short pnts, short order)
599 /* pnts, order: number of pnts NOT corrected for cyclic */
600 {
601         int a, b, order2, c;
602
603         if(knots==0) return;
604
605         order2=order-1;
606
607         /* do first long rows (order -1), remove identical knots at endpoints */
608         if(order>2) {
609                 b= pnts+order2;
610                 for(a=1; a<order2; a++) {
611                         if(knots[b]!= knots[b-a]) break;
612                 }
613                 if(a==order2) knots[pnts+order-2]+= 1.0;
614         }
615
616         b= order;
617                 c=pnts + order + order2;
618         for(a=pnts+order2; a<c; a++) {
619                 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
620                 b--;
621         }
622 }
623
624
625
626 void makeknots(Nurb *nu, short uv)
627 {
628         if(nu->type == CU_NURBS) {
629                 if(uv == 1) {
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);
636                                 } else {
637                                         calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
638                                 }
639                         }
640                         else nu->knotsu= NULL;
641                 
642                 } else if(uv == 2) {
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);
649                                 } else {
650                                         calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
651                                 }
652                         }
653                         else nu->knotsv= NULL;
654                 }
655         }
656 }
657
658 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
659 {
660         float d, e;
661         int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
662
663         orderpluspnts= order+pnts;
664                 opp2 = orderpluspnts-1;
665
666         /* this is for float inaccuracy */
667         if(t < knots[0]) t= knots[0];
668         else if(t > knots[opp2]) t= knots[opp2];
669
670         /* this part is order '1' */
671                 o2 = order + 1;
672         for(i=0;i<opp2;i++) {
673                 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
674                         basis[i]= 1.0;
675                         i1= i-o2;
676                         if(i1<0) i1= 0;
677                         i2= i;
678                         i++;
679                         while(i<opp2) {
680                                 basis[i]= 0.0;
681                                 i++;
682                         }
683                         break;
684                 }
685                 else basis[i]= 0.0;
686         }
687         basis[i]= 0.0;
688         
689         /* this is order 2,3,... */
690         for(j=2; j<=order; j++) {
691
692                 if(i2+j>= orderpluspnts) i2= opp2-j;
693
694                 for(i= i1; i<=i2; i++) {
695                         if(basis[i]!=0.0)
696                                 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
697                         else
698                                 d= 0.0;
699
700                         if(basis[i+1]!=0.0)
701                                 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
702                         else
703                                 e= 0.0;
704
705                         basis[i]= d+e;
706                 }
707         }
708
709         *start= 1000;
710         *end= 0;
711
712         for(i=i1; i<=i2; i++) {
713                 if(basis[i]>0.0) {
714                         *end= i;
715                         if(*start==1000) *start= i;
716                 }
717         }
718 }
719
720
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 */
723 {
724         BPoint *bp;
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;
729         
730         int totu = nu->pntsu*nu->resolu, totv = nu->pntsv*nu->resolv;
731         
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;
736         
737         /* allocate and initialize */
738         len = totu * totv;
739         if(len==0) return;
740         
741
742         
743         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
744         
745         len= totu*totv;
746         if(len==0) {
747                 MEM_freeN(sum);
748                 return;
749         }
750
751         bp= nu->bp;
752         i= nu->pntsu*nu->pntsv;
753         ratcomp=0;
754         while(i--) {
755                 if(bp->vec[3]!=1.0) {
756                         ratcomp= 1;
757                         break;
758                 }
759                 bp++;
760         }
761         
762         fp= nu->knotsu;
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);
767         
768         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
769
770         fp= nu->knotsv;
771         vstart= fp[nu->orderv-1];
772         
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);
776         
777         len= KNOTSV(nu);
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");
781
782         /* precalculation of basisv and jstart,jend */
783         if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1; 
784         else cycl= 0;
785         v= vstart;
786         basis= basisv;
787         resolv= totv;
788         while(resolv--) {
789                 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+resolv, jend+resolv);
790                 basis+= KNOTSV(nu);
791                 v+= vstep;
792         }
793
794         if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
795         else cycl= 0;
796         in= coord_array;
797         u= ustart;
798         resolu= totu;
799         while(resolu--) {
800
801                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
802
803                 basis= basisv;
804                 resolv= totv;
805                 while(resolv--) {
806
807                         jsta= jstart[resolv];
808                         jen= jend[resolv];
809
810                         /* calculate sum */
811                         sumdiv= 0.0;
812                         fp= sum;
813
814                         for(j= jsta; j<=jen; j++) {
815
816                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
817                                 else jofs= j;
818                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
819
820                                 for(i= istart; i<=iend; i++, fp++) {
821
822                                         if(i>= nu->pntsu) {
823                                                 iofs= i- nu->pntsu;
824                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
825                                         }
826                                         else bp++;
827
828                                         if(ratcomp) {
829                                                 *fp= basisu[i]*basis[j]*bp->vec[3];
830                                                 sumdiv+= *fp;
831                                         }
832                                         else *fp= basisu[i]*basis[j];
833                                 }
834                         }
835                 
836                         if(ratcomp) {
837                                 fp= sum;
838                                 for(j= jsta; j<=jen; j++) {
839                                         for(i= istart; i<=iend; i++, fp++) {
840                                                 *fp/= sumdiv;
841                                         }
842                                 }
843                         }
844
845                         /* one! (1.0) real point now */
846                         fp= sum;
847                         for(j= jsta; j<=jen; j++) {
848
849                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
850                                 else jofs= j;
851                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
852
853                                 for(i= istart; i<=iend; i++, fp++) {
854
855                                         if(i>= nu->pntsu) {
856                                                 iofs= i- nu->pntsu;
857                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
858                                         }
859                                         else bp++;
860
861                                         if(*fp!=0.0) {
862                                                 in[0]+= (*fp) * bp->vec[0];
863                                                 in[1]+= (*fp) * bp->vec[1];
864                                                 in[2]+= (*fp) * bp->vec[2];
865                                         }
866                                 }
867                         }
868
869                         in+=3;
870                         basis+= KNOTSV(nu);
871                 }
872                 u+= ustep;
873                 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
874         }
875
876         /* free */
877         MEM_freeN(sum);
878         MEM_freeN(basisu);
879         MEM_freeN(basisv);
880         MEM_freeN(jstart);
881         MEM_freeN(jend);
882 }
883
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 */
887 {
888         BPoint *bp;
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;
893
894         if(nu->knotsu==NULL) return;
895         if(nu->orderu>nu->pntsu) return;
896         if(coord_array==0) return;
897
898         /* allocate and initialize */
899         len= nu->pntsu;
900         if(len==0) return;
901         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
902         
903         resolu= (resolu*SEGMENTSU(nu));
904         
905         if(resolu==0) {
906                 MEM_freeN(sum);
907                 return;
908         }
909
910         fp= nu->knotsu;
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));
915         
916         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
917
918         if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
919         else cycl= 0;
920
921         u= ustart;
922         while(resolu--) {
923
924                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
925                 /* calc sum */
926                 sumdiv= 0.0;
927                 fp= sum;
928                 bp= nu->bp+ istart-1;
929                 for(i= istart; i<=iend; i++, fp++) {
930
931                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
932                         else bp++;
933
934                         *fp= basisu[i]*bp->vec[3];
935                         sumdiv+= *fp;
936                 }
937                 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
938                         /* is normalizing needed? */
939                         fp= sum;
940                         for(i= istart; i<=iend; i++, fp++) {
941                                 *fp/= sumdiv;
942                         }
943                 }
944
945                 /* one! (1.0) real point */
946                 fp= sum;
947                 bp= nu->bp+ istart-1;
948                 for(i= istart; i<=iend; i++, fp++) {
949
950                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
951                         else bp++;
952
953                         if(*fp!=0.0) {
954                                 
955                                 coord_fp[0]+= (*fp) * bp->vec[0];
956                                 coord_fp[1]+= (*fp) * bp->vec[1];
957                                 coord_fp[2]+= (*fp) * bp->vec[2];
958                                 
959                                 if (tilt_fp)
960                                         (*tilt_fp) += (*fp) * bp->alfa;
961                                 
962                                 if (radius_fp)
963                                         (*radius_fp) += (*fp) * bp->radius;
964
965                                 if (weight_fp)
966                                         (*weight_fp) += (*fp) * bp->weight;
967                                 
968                         }
969                 }
970
971                 coord_fp = (float *)(((char *)coord_fp) + stride);
972                 
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);
976                 
977                 u+= ustep;
978         }
979
980         /* free */
981         MEM_freeN(sum);
982         MEM_freeN(basisu);
983 }
984
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)
987 {
988         float rt0,rt1,rt2,rt3,f;
989         int a;
990
991         f= (float)it;
992         rt0= q0;
993         rt1= 3.0f*(q1-q0)/f;
994         f*= f;
995         rt2= 3.0f*(q0-2.0f*q1+q2)/f;
996         f*= it;
997         rt3= (q3-q0+3.0f*(q1-q2))/f;
998         
999           q0= rt0;
1000         q1= rt1+rt2+rt3;
1001         q2= 2*rt2+6*rt3;
1002         q3= 6*rt3;
1003   
1004           for(a=0; a<=it; a++) {
1005                 *p= q0;
1006                 p = (float *)(((char *)p)+stride);
1007                 q0+= q1;
1008                  q1+= q2;
1009                  q2+= q3;
1010          }
1011 }
1012
1013 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1014 {
1015         /* note that these are not purpendicular to the curve
1016          * they need to be rotated for this,
1017          *
1018          * This could also be optimized like forward_diff_bezier */
1019         int a;
1020           for(a=0; a<=it; a++) {
1021                 float t = (float)a / (float)it;
1022
1023                 int i;
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];
1026                 }
1027                 normalize_v3(p);
1028                 p = (float *)(((char *)p)+stride);
1029          }
1030 }
1031
1032 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1033
1034 float *make_orco_surf(Object *ob)
1035 {
1036         Curve *cu= ob->data;
1037         Nurb *nu;
1038         int a, b, tot=0;
1039         int sizeu, sizev;
1040         float *fp, *coord_array;
1041         
1042         /* first calculate the size of the datablock */
1043         nu= cu->nurb.first;
1044         while(nu) {
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.
1050                 
1051                 See also convertblender.c: init_render_surf()
1052                 */
1053                 
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;
1059                 
1060                 nu= nu->next;
1061         }
1062         /* makeNurbfaces wants zeros */
1063         fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1064         
1065         nu= cu->nurb.first;
1066         while(nu) {
1067                 if(nu->pntsv>1) {
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++;
1072                         
1073                         if(cu->flag & CU_UV_ORCO) {
1074                                 for(b=0; b< sizeu; b++) {
1075                                         for(a=0; a< sizev; a++) {
1076                                                 
1077                                                 if(sizev <2) fp[0]= 0.0f;
1078                                                 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1079                                                 
1080                                                 if(sizeu <2) fp[1]= 0.0f;
1081                                                 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1082                                                 
1083                                                 fp[2]= 0.0;
1084                                                 
1085                                                 fp+= 3;
1086                                         }
1087                                 }
1088                         }
1089                         else {
1090                                 float *_tdata= MEM_callocN((nu->pntsu*nu->resolu) * (nu->pntsv*nu->resolv) *3*sizeof(float), "temp data");
1091                                 float *tdata= _tdata;
1092                                 
1093                                 makeNurbfaces(nu, tdata, 0);
1094                                 
1095                                 for(b=0; b<sizeu; b++) {
1096                                         int use_b= b;
1097                                         if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1098                                                 use_b= 0;
1099                                         
1100                                         for(a=0; a<sizev; a++) {
1101                                                 int use_a= a;
1102                                                 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1103                                                         use_a= 0;
1104                                                 
1105                                                 tdata = _tdata + 3 * (use_b * (nu->pntsv*nu->resolv) + use_a);
1106                                                 
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];
1110                                                 fp+= 3;
1111                                         }
1112                                 }
1113                                 
1114                                 MEM_freeN(_tdata);
1115                         }
1116                 }
1117                 nu= nu->next;
1118         }
1119         
1120         return coord_array;
1121 }
1122
1123
1124         /* NOTE: This routine is tied to the order of vertex
1125          * built by displist and as passed to the renderer.
1126          */
1127 float *make_orco_curve(Scene *scene, Object *ob)
1128 {
1129         Curve *cu = ob->data;
1130         DispList *dl;
1131         int u, v, numVerts;
1132         float *fp, *coord_array;
1133         ListBase disp = {NULL, NULL};
1134
1135         makeDispListCurveTypes_forOrco(scene, ob, &disp);
1136
1137         numVerts = 0;
1138         for (dl=disp.first; dl; dl=dl->next) {
1139                 if (dl->type==DL_INDEX3) {
1140                         numVerts += dl->nr;
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);
1146                                 else
1147                                         numVerts += dl->parts*(dl->nr+1);
1148                         }
1149                         else
1150                                 numVerts += dl->parts*dl->nr;
1151                 }
1152         }
1153
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;
1160                                         fp[1]= 0.0;
1161                                         fp[2]= 0.0;
1162                                 } else {
1163                                         VECCOPY(fp, &dl->verts[u*3]);
1164
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];
1168                                 }
1169                         }
1170                 } else if (dl->type==DL_SURF) {
1171                         int sizeu= dl->nr, sizev= dl->parts;
1172                         
1173                         /* exception as handled in convertblender.c too */
1174                         if (dl->flag & DL_CYCL_U) {
1175                                 sizeu++;
1176                                 if (dl->flag & DL_CYCL_V)
1177                                         sizev++;
1178                         }
1179                         
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;
1185                                                 fp[2]= 0.0;
1186                                         } else {
1187                                                 float *vert;
1188                                                 int realv= v % dl->nr;
1189                                                 int realu= u % dl->parts;
1190                                                 
1191                                                 vert= dl->verts + 3*(dl->nr*realu + realv);
1192                                                 VECCOPY(fp, vert);
1193
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];
1197                                         }
1198                                 }
1199                         }
1200                 }
1201         }
1202
1203         freedisplist(&disp);
1204
1205         return coord_array;
1206 }
1207
1208
1209 /* ***************** BEVEL ****************** */
1210
1211 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1212 {
1213         DispList *dl, *dlnew;
1214         Curve *bevcu, *cu;
1215         float *fp, facx, facy, angle, dangle;
1216         int nr, a;
1217
1218         cu= ob->data;
1219         disp->first = disp->last = NULL;
1220
1221         /* if a font object is being edited, then do nothing */
1222 // XXX  if( ob == obedit && ob->type == OB_FONT ) return;
1223
1224         if(cu->bevobj) {
1225                 if (cu->bevobj->type!=OB_CURVE) return;
1226
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];
1232
1233                         if (forRender) {
1234                                 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1235                                 dl= bevdisp.first;
1236                         } else {
1237                                 dl= bevcu->disp.first;
1238                                 if(dl==0) {
1239                                         makeDispListCurveTypes(scene, cu->bevobj, 0);
1240                                         dl= bevcu->disp.first;
1241                                 }
1242                         }
1243
1244                         while(dl) {
1245                                 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1246                                         dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1247                                         *dlnew= *dl;
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);
1250
1251                                         if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1252
1253                                         BLI_addtail(disp, dlnew);
1254                                         fp= dlnew->verts;
1255                                         nr= dlnew->parts*dlnew->nr;
1256                                         while(nr--) {
1257                                                 fp[2]= fp[1]*facy;
1258                                                 fp[1]= -fp[0]*facx;
1259                                                 fp[0]= 0.0;
1260                                                 fp+= 3;
1261                                         }
1262                                 }
1263                                 dl= dl->next;
1264                         }
1265
1266                         freedisplist(&bevdisp);
1267                 }
1268         }
1269         else if(cu->ext1==0.0 && cu->ext2==0.0) {
1270                 ;
1271         }
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);
1276                 dl->type= DL_SEGM;
1277                 dl->parts= 1;
1278                 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1279                 dl->nr= 2;
1280                 
1281                 fp= dl->verts;
1282                 fp[0]= fp[1]= 0.0;
1283                 fp[2]= -cu->ext1;
1284                 fp[3]= fp[4]= 0.0;
1285                 fp[5]= cu->ext1;
1286         }
1287         else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f)  { // we make a full round bevel in that case
1288                 
1289                 nr= 4+ 2*cu->bevresol;
1290                    
1291                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1292                 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1293                 BLI_addtail(disp, dl);
1294                 dl->type= DL_POLY;
1295                 dl->parts= 1;
1296                 dl->flag= DL_BACK_CURVE;
1297                 dl->nr= nr;
1298
1299                 /* a circle */
1300                 fp= dl->verts;
1301                 dangle= (2.0f*M_PI/(nr));
1302                 angle= -(nr-1)*dangle;
1303                 
1304                 for(a=0; a<nr; a++) {
1305                         fp[0]= 0.0;
1306                         fp[1]= (float)(cos(angle)*(cu->ext2));
1307                         fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1308                         angle+= dangle;
1309                         fp+= 3;
1310                 }
1311         }
1312         else {
1313                 short dnr;
1314                 
1315                 /* bevel now in three parts, for proper vertex normals */
1316                 /* part 1, back */
1317
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;
1322
1323                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1324                         dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1325                         BLI_addtail(disp, dl);
1326                         dl->type= DL_SEGM;
1327                         dl->parts= 1;
1328                         dl->flag= DL_BACK_CURVE;
1329                         dl->nr= nr;
1330
1331                         /* half a circle */
1332                         fp= dl->verts;
1333                         dangle= (0.5*M_PI/(dnr-1));
1334                         angle= -(nr-1)*dangle;
1335
1336                         for(a=0; a<nr; a++) {
1337                                 fp[0]= 0.0;
1338                                 fp[1]= (float)(cos(angle)*(cu->ext2));
1339                                 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1340                                 angle+= dangle;
1341                                 fp+= 3;
1342                         }
1343                 }
1344                 
1345                 /* part 2, sidefaces */
1346                 if(cu->ext1!=0.0) {
1347                         nr= 2;
1348                         
1349                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1350                         dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1351                         BLI_addtail(disp, dl);
1352                         dl->type= DL_SEGM;
1353                         dl->parts= 1;
1354                         dl->nr= nr;
1355                         
1356                         fp= dl->verts;
1357                         fp[1]= cu->ext2;
1358                         fp[2]= -cu->ext1;
1359                         fp[4]= cu->ext2;
1360                         fp[5]= cu->ext1;
1361                         
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);
1366                                 
1367                                 fp= dl->verts;
1368                                 fp[1]= -fp[1];
1369                                 fp[2]= -fp[2];
1370                                 fp[4]= -fp[4];
1371                                 fp[5]= -fp[5];
1372                         }
1373                 }
1374                 
1375                 /* part 3, front */
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;
1380
1381                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1382                         dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1383                         BLI_addtail(disp, dl);
1384                         dl->type= DL_SEGM;
1385                         dl->flag= DL_FRONT_CURVE;
1386                         dl->parts= 1;
1387                         dl->nr= nr;
1388
1389                         /* half a circle */
1390                         fp= dl->verts;
1391                         angle= 0.0;
1392                         dangle= (0.5*M_PI/(dnr-1));
1393
1394                         for(a=0; a<nr; a++) {
1395                                 fp[0]= 0.0;
1396                                 fp[1]= (float)(cos(angle)*(cu->ext2));
1397                                 fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1398                                 angle+= dangle;
1399                                 fp+= 3;
1400                         }
1401                 }
1402         }
1403 }
1404
1405 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1406 {
1407         /* return:
1408                 -1: colliniar
1409                  0: no intersection of segments
1410                  1: exact intersection of segments
1411                  2: cross-intersection of segments
1412         */
1413         float deler;
1414
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;
1417
1418         *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1419         *labda= -(*labda/deler);
1420
1421         deler= v3[coy]-v4[coy];
1422         if(deler==0) {
1423                 deler=v3[cox]-v4[cox];
1424                 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1425         } else {
1426                 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1427         }
1428         vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1429         vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1430
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;
1433                 return 2;
1434         }
1435         return 0;
1436 }
1437
1438
1439 static short bevelinside(BevList *bl1,BevList *bl2)
1440 {
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;
1446
1447         /* take first vertex of possible hole */
1448
1449         bevp= (BevPoint *)(bl2+1);
1450         hvec1[0]= bevp->vec[0]; 
1451         hvec1[1]= bevp->vec[1]; 
1452         hvec1[2]= 0.0;
1453         VECCOPY(hvec2,hvec1);
1454         hvec2[0]+=1000;
1455
1456         /* test it with all edges of potential surounding poly */
1457         /* count number of transitions left-right  */
1458
1459         bevp= (BevPoint *)(bl1+1);
1460         nr= bl1->nr;
1461         prevbevp= bevp+(nr-1);
1462
1463         while(nr--) {
1464                 min= prevbevp->vec[1];
1465                 max= bevp->vec[1];
1466                 if(max<min) {
1467                         min= max;
1468                         max= prevbevp->vec[1];
1469                 }
1470                 if(min!=max) {
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
1476                                  */
1477                                 if(mode>=0 && lab!=0.0) {
1478                                         if(vec[0]<hvec1[0]) links++;
1479                                         else rechts++;
1480                                 }
1481                         }
1482                 }
1483                 prevbevp= bevp;
1484                 bevp++;
1485         }
1486         
1487         if( (links & 1) && (rechts & 1) ) return 1;
1488         return 0;
1489 }
1490
1491
1492 struct bevelsort {
1493         float left;
1494         BevList *bl;
1495         int dir;
1496 };
1497
1498 static int vergxcobev(const void *a1, const void *a2)
1499 {
1500         const struct bevelsort *x1=a1,*x2=a2;
1501
1502         if( x1->left > x2->left ) return 1;
1503         else if( x1->left < x2->left) return -1;
1504         return 0;
1505 }
1506
1507 /* this function cannot be replaced with atan2, but why? */
1508
1509 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1510 {
1511         float t01, t02, x3, y3;
1512
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;
1517
1518         x1/=t01; 
1519         y1/=t01;
1520         x2/=t02; 
1521         y2/=t02;
1522
1523         t02= x1*x2+y1*y2;
1524         if(fabs(t02)>=1.0) t02= .5*M_PI;
1525         else t02= (saacos(t02))/2.0f;
1526
1527         t02= (float)sin(t02);
1528         if(t02==0.0) t02= 1.0;
1529
1530         x3= x1-x2;
1531         y3= y1-y2;
1532         if(x3==0 && y3==0) {
1533                 x3= y1;
1534                 y3= -x1;
1535         } else {
1536                 t01= (float)sqrt(x3*x3+y3*y3);
1537                 x3/=t01; 
1538                 y3/=t01;
1539         }
1540
1541         *sina= -y3/t02;
1542         *cosa= x3/t02;
1543
1544 }
1545
1546 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1547 {
1548         BezTriple *pprev, *next, *last;
1549         float fac, dfac, t[4];
1550         int a;
1551         
1552         if(tilt_array==NULL && radius_array==NULL)
1553                 return;
1554         
1555         last= nu->bezt+(nu->pntsu-1);
1556         
1557         /* returns a point */
1558         if(prevbezt==nu->bezt) {
1559                 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1560                 else pprev= prevbezt;
1561         }
1562         else pprev= prevbezt-1;
1563         
1564         /* next point */
1565         if(bezt==last) {
1566                 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1567                 else next= bezt;
1568         }
1569         else next= bezt+1;
1570         
1571         fac= 0.0;
1572         dfac= 1.0f/(float)resolu;
1573         
1574         for(a=0; a<resolu; a++, fac+= dfac) {
1575                 if (tilt_array) {
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);
1578                         } else {
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;
1581                         }
1582                         
1583                         tilt_array = (float *)(((char *)tilt_array) + stride); 
1584                 }
1585                 
1586                 if (radius_array) {
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);
1592                         } else {
1593                                 
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);
1597                                 }
1598                                 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1599                         }
1600                         
1601                         radius_array = (float *)(((char *)radius_array) + stride); 
1602                 }
1603
1604                 if(weight_array) {
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);
1607
1608                         weight_array = (float *)(((char *)weight_array) + stride);
1609                 }
1610         }
1611 }
1612
1613 /* make_bevel_list_3D_* funcs, at a minimum these must
1614  * fill in the bezp->quat and bezp->dir values */
1615
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)
1619 {
1620         BevPoint *bevp, *bevp1;
1621
1622         bevp= (BevPoint *)(bl+1);
1623         bevp1= bevp+1;
1624         QUATCOPY(bevp->quat, bevp1->quat);
1625         VECCOPY(bevp->dir, bevp1->dir);
1626         VECCOPY(bevp->tan, bevp1->tan);
1627         bevp= (BevPoint *)(bl+1);
1628         bevp+= (bl->nr-1);
1629         bevp1= bevp-1;
1630         QUATCOPY(bevp->quat, bevp1->quat);
1631         VECCOPY(bevp->dir, bevp1->dir);
1632         VECCOPY(bevp->tan, bevp1->tan);
1633 }
1634 /* utility for make_bevel_list_3D_* funcs */
1635 static void bevel_list_calc_bisect(BevList *bl)
1636 {
1637         BevPoint *bevp2, *bevp1, *bevp0;
1638         int nr;
1639
1640         bevp2= (BevPoint *)(bl+1);
1641         bevp1= bevp2+(bl->nr-1);
1642         bevp0= bevp1-1;
1643
1644         nr= bl->nr;
1645         while(nr--) {
1646                 /* totally simple */
1647                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1648
1649                 bevp0= bevp1;
1650                 bevp1= bevp2;
1651                 bevp2++;
1652         }
1653 }
1654 static void bevel_list_flip_tangents(BevList *bl)
1655 {
1656         BevPoint *bevp2, *bevp1, *bevp0;
1657         int nr;
1658
1659         bevp2= (BevPoint *)(bl+1);
1660         bevp1= bevp2+(bl->nr-1);
1661         bevp0= bevp1-1;
1662
1663         nr= bl->nr;
1664         while(nr--) {
1665                 if(RAD2DEG(angle_v2v2(bevp0->tan, bevp1->tan)) > 90)
1666                         negate_v3(bevp1->tan);
1667
1668                 bevp0= bevp1;
1669                 bevp1= bevp2;
1670                 bevp2++;
1671         }
1672 }
1673 /* apply user tilt */
1674 static void bevel_list_apply_tilt(BevList *bl)
1675 {
1676         BevPoint *bevp2, *bevp1, *bevp0;
1677         int nr;
1678         float q[4];
1679
1680         bevp2= (BevPoint *)(bl+1);
1681         bevp1= bevp2+(bl->nr-1);
1682         bevp0= bevp1-1;
1683
1684         nr= bl->nr;
1685         while(nr--) {
1686                 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1687                 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1688                 normalize_qt(bevp1->quat);
1689
1690                 bevp0= bevp1;
1691                 bevp1= bevp2;
1692                 bevp2++;
1693         }
1694 }
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)
1697 {
1698         BevPoint *bevp2, *bevp1, *bevp0;
1699         int nr;
1700
1701         float q[4];
1702         float bevp0_quat[4];
1703         int a;
1704
1705         for(a=0; a < smooth_iter; a++) {
1706
1707                 bevp2= (BevPoint *)(bl+1);
1708                 bevp1= bevp2+(bl->nr-1);
1709                 bevp0= bevp1-1;
1710
1711                 nr= bl->nr;
1712
1713                 if(bl->poly== -1) { /* check its not cyclic */
1714                         /* skip the first point */
1715                         bevp0= bevp1;
1716                         bevp1= bevp2;
1717                         bevp2++;
1718                         nr--;
1719
1720                         bevp0= bevp1;
1721                         bevp1= bevp2;
1722                         bevp2++;
1723                         nr--;
1724
1725                 }
1726
1727                 QUATCOPY(bevp0_quat, bevp0->quat);
1728
1729                 while(nr--) {
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);
1733                         normalize_qt(q);
1734
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));
1738                         normalize_qt(q2);
1739
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);
1744
1745
1746                         bevp0= bevp1;
1747                         bevp1= bevp2;
1748                         bevp2++;
1749                 }
1750         }
1751 }
1752
1753 static void make_bevel_list_3D_zup(BevList *bl)
1754 {
1755         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1756         int nr;
1757
1758         bevp2= (BevPoint *)(bl+1);
1759         bevp1= bevp2+(bl->nr-1);
1760         bevp0= bevp1-1;
1761
1762         nr= bl->nr;
1763         while(nr--) {
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);
1767
1768                 bevp0= bevp1;
1769                 bevp1= bevp2;
1770                 bevp2++;
1771         }
1772 }
1773
1774 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1775 {
1776         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1777         int nr;
1778         float q[4];
1779
1780         float cross_tmp[3];
1781
1782         bevel_list_calc_bisect(bl);
1783
1784         bevp2= (BevPoint *)(bl+1);
1785         bevp1= bevp2+(bl->nr-1);
1786         bevp0= bevp1-1;
1787
1788         nr= bl->nr;
1789         while(nr--) {
1790
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);
1793                 }
1794                 else {
1795                         float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1796
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);
1801                         }
1802                         else {
1803                                 QUATCOPY(bevp1->quat, bevp0->quat);
1804                         }
1805                 }
1806
1807                 bevp0= bevp1;
1808                 bevp1= bevp2;
1809                 bevp2++;
1810         }
1811
1812         if(bl->poly != -1) { /* check for cyclic */
1813
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
1817                  *
1818                  * note that the split is between last and second last, rather then first/last as youd expect.
1819                  *
1820                  * real order is like this
1821                  * 0,1,2,3,4 --> 1,2,3,4,0
1822                  *
1823                  * this is why we compare last with second last
1824                  * */
1825                 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1826
1827                 BevPoint *bevp_first;
1828                 BevPoint *bevp_last;
1829
1830
1831                 bevp_first= (BevPoint *)(bl+1);
1832                 bevp_first+= bl->nr-1;
1833                 bevp_last = bevp_first;
1834                 bevp_last--;
1835
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);
1841
1842                 /* align the vector, can avoid this and it looks 98% OK but
1843                  * better to align the angle quat roll's before comparing */
1844                 {
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);
1849                 }
1850
1851                 angle= angle_normalized_v3v3(vec_1, vec_2);
1852
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))
1857                         angle = -angle;
1858
1859                 bevp2= (BevPoint *)(bl+1);
1860                 bevp1= bevp2+(bl->nr-1);
1861                 bevp0= bevp1-1;
1862
1863                 nr= bl->nr;
1864                 while(nr--) {
1865                         ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1866
1867                         axis_angle_to_quat(q, bevp1->dir, ang_fac);
1868                         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1869
1870                         bevp0= bevp1;
1871                         bevp1= bevp2;
1872                         bevp2++;
1873                 }
1874         }
1875 }
1876
1877 static void make_bevel_list_3D_tangent(BevList *bl)
1878 {
1879         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1880         int nr;
1881
1882         float bevp0_tan[3], cross_tmp[3];
1883
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);
1888
1889         /* correct the tangents */
1890         bevp2= (BevPoint *)(bl+1);
1891         bevp1= bevp2+(bl->nr-1);
1892         bevp0= bevp1-1;
1893
1894         nr= bl->nr;
1895         while(nr--) {
1896
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);
1900
1901                 bevp0= bevp1;
1902                 bevp1= bevp2;
1903                 bevp2++;
1904         }
1905
1906
1907         /* now for the real twist calc */
1908         bevp2= (BevPoint *)(bl+1);
1909         bevp1= bevp2+(bl->nr-1);
1910         bevp0= bevp1-1;
1911
1912         VECCOPY(bevp0_tan, bevp0->tan);
1913
1914         nr= bl->nr;
1915         while(nr--) {
1916
1917                 /* make perpendicular, modify tan in place, is ok */
1918                 float cross_tmp[3];
1919                 float zero[3] = {0,0,0};
1920
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 */
1924
1925                 bevp0= bevp1;
1926                 bevp1= bevp2;
1927                 bevp2++;
1928         }
1929 }
1930
1931 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1932 {
1933         switch(twist_mode) {
1934         case CU_TWIST_TANGENT:
1935                 make_bevel_list_3D_tangent(bl);
1936                 break;
1937         case CU_TWIST_MINIMUM:
1938                 make_bevel_list_3D_minimum_twist(bl);
1939                 break;
1940         default: /* CU_TWIST_Z_UP default, pre 2.49c */
1941                 make_bevel_list_3D_zup(bl);
1942         }
1943
1944         if(bl->poly== -1) /* check its not cyclic */
1945                 bevel_list_cyclic_fix_3D(bl);
1946
1947         if(smooth_iter)
1948                 bevel_list_smooth(bl, smooth_iter);
1949
1950         bevel_list_apply_tilt(bl);
1951 }
1952
1953
1954
1955 /* only for 2 points */
1956 static void make_bevel_list_segment_3D(BevList *bl)
1957 {
1958         float q[4];
1959
1960         BevPoint *bevp2= (BevPoint *)(bl+1);
1961         BevPoint *bevp1= bevp2+1;
1962
1963         /* simple quat/dir */
1964         sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
1965         normalize_v3(bevp1->dir);
1966
1967         vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1968
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);
1974 }
1975
1976
1977
1978 void makeBevelList(Object *ob)
1979 {
1980         /*
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
1985         */
1986         Curve *cu;
1987         Nurb *nu;
1988         BezTriple *bezt, *prevbezt;
1989         BPoint *bp;
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;
1996         
1997         /* this function needs an object, because of tflag and upflag */
1998         cu= ob->data;
1999
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; */
2002         
2003         /* STEP 1: MAKE POLYS  */
2004
2005         BLI_freelistN(&(cu->bev));
2006         if(cu->editnurb && ob->type!=OB_FONT) {
2007                 ListBase *nurbs= ED_curve_editnurbs(cu);
2008                 nu= nurbs->first;
2009         } else nu= cu->nurb.first;
2010         
2011         while(nu) {
2012                 
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 */
2016                 do_weight = 1;
2017                 
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);
2023                         bl->nr= 0;
2024                 } else {
2025                         if(G.rendering && cu->resolu_ren!=0) 
2026                                 resolu= cu->resolu_ren;
2027                         else
2028                                 resolu= nu->resolu;
2029                         
2030                         if(nu->type == CU_POLY) {
2031                                 len= nu->pntsu;
2032                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2033                                 BLI_addtail(&(cu->bev), bl);
2034         
2035                                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2036                                 else bl->poly= -1;
2037                                 bl->nr= len;
2038                                 bl->dupe_nr= 0;
2039                                 bevp= (BevPoint *)(bl+1);
2040                                 bp= nu->bp;
2041         
2042                                 while(len--) {
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;
2048                                         bevp++;
2049                                         bp++;
2050                                 }
2051                         }
2052                         else if(nu->type == CU_BEZIER) {
2053         
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);
2057         
2058                                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2059                                 else bl->poly= -1;
2060                                 bevp= (BevPoint *)(bl+1);
2061         
2062                                 a= nu->pntsu-1;
2063                                 bezt= nu->bezt;
2064                                 if(nu->flagu & CU_NURB_CYCLIC) {
2065                                         a++;
2066                                         prevbezt= nu->bezt+(nu->pntsu-1);
2067                                 }
2068                                 else {
2069                                         prevbezt= bezt;
2070                                         bezt++;
2071                                 }
2072                                 
2073                                 while(a--) {
2074                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2075
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;
2082                                                 bevp++;
2083                                                 bl->nr++;
2084                                                 bl->dupe_nr= 1;
2085                                         }
2086                                         else {
2087                                                 /* always do all three, to prevent data hanging around */
2088                                                 int j;
2089                                                 
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));
2095                                                 }
2096                                                 
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));
2103
2104                                                 
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));
2110                                                 }
2111
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;
2115                                                 }
2116                                                 else {
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;
2119                                                 }
2120                                                 bl->nr+= resolu;
2121                                                 bevp+= resolu;
2122                                         }
2123                                         prevbezt= bezt;
2124                                         bezt++;
2125                                 }
2126                                 
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;
2132                                         bl->nr++;
2133                                 }
2134                         }
2135                         else if(nu->type == CU_NURBS) {
2136                                 if(nu->pntsv==1) {
2137                                         len= (resolu*SEGMENTSU(nu));
2138                                         
2139                                         bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2140                                         BLI_addtail(&(cu->bev), bl);
2141                                         bl->nr= len;
2142                                         bl->dupe_nr= 0;
2143                                         if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2144                                         else bl->poly= -1;
2145                                         bevp= (BevPoint *)(bl+1);
2146                                         
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));
2152                                 }
2153                         }
2154                 }
2155                 nu= nu->next;
2156         }
2157
2158         /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2159         bl= cu->bev.first;
2160         while(bl) {
2161                 if (bl->nr) { /* null bevel items come from single points */
2162                         nr= bl->nr;
2163                         bevp1= (BevPoint *)(bl+1);
2164                         bevp0= bevp1+(nr-1);
2165                         nr--;
2166                         while(nr--) {
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;
2171                                                         bl->dupe_nr++;
2172                                                 }
2173                                         }
2174                                 }
2175                                 bevp0= bevp1;
2176                                 bevp1++;
2177                         }
2178                 }
2179                 bl= bl->next;
2180         }
2181         bl= cu->bev.first;
2182         while(bl) {
2183                 blnext= bl->next;
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));
2188                         blnew->nr= 0;
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);
2193                         nr= bl->nr;
2194                         while(nr--) {
2195                                 if(bevp0->dupe_tag==0) {
2196                                         memcpy(bevp1, bevp0, sizeof(BevPoint));
2197                                         bevp1++;
2198                                         blnew->nr++;
2199                                 }
2200                                 bevp0++;
2201                         }
2202                         MEM_freeN(bl);
2203                         blnew->dupe_nr= 0;
2204                 }
2205                 bl= blnext;
2206         }
2207
2208         /* STEP 3: POLYS COUNT AND AUTOHOLE */
2209         bl= cu->bev.first;
2210         poly= 0;
2211         while(bl) {
2212                 if(bl->nr && bl->poly>=0) {
2213                         poly++;
2214                         bl->poly= poly;
2215                         bl->hole= 0;
2216                 }
2217                 bl= bl->next;
2218         }
2219         
2220
2221         /* find extreme left points, also test (turning) direction */
2222         if(poly>0) {
2223                 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2224                 bl= cu->bev.first;
2225                 while(bl) {
2226                         if(bl->poly>0) {
2227
2228                                 min= 300000.0;
2229                                 bevp= (BevPoint *)(bl+1);
2230                                 nr= bl->nr;
2231                                 while(nr--) {
2232                                         if(min>bevp->vec[0]) {
2233                                                 min= bevp->vec[0];
2234                                                 bevp1= bevp;
2235                                         }
2236                                         bevp++;
2237                                 }
2238                                 sd->bl= bl;
2239                                 sd->left= min;
2240
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;
2247
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]);
2249
2250                                 if(inp>0.0) sd->dir= 1;
2251                                 else sd->dir= 0;
2252
2253                                 sd++;
2254                         }
2255
2256                         bl= bl->next;
2257                 }
2258                 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2259
2260                 sd= sortdata+1;
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;
2267                                         break;
2268                                 }
2269                         }
2270                 }
2271
2272                 /* turning direction */
2273                 if((cu->flag & CU_3D)==0) {
2274                         sd= sortdata;
2275                         for(a=0; a<poly; a++, sd++) {
2276                                 if(sd->bl->hole==sd->dir) {
2277                                         bl= sd->bl;
2278                                         bevp1= (BevPoint *)(bl+1);
2279                                         bevp2= bevp1+ (bl->nr-1);
2280                                         nr= bl->nr/2;
2281                                         while(nr--) {
2282                                                 SWAP(BevPoint, *bevp1, *bevp2);
2283                                                 bevp1++;
2284                                                 bevp2--;
2285                                         }
2286                                 }
2287                         }
2288                 }
2289                 MEM_freeN(sortdata);
2290         }
2291
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 */
2296                 bl= cu->bev.first;
2297                 while(bl) {
2298
2299                         if(bl->nr < 2) {
2300                                 /* do nothing */
2301                         }
2302                         else if(bl->nr==2) {    /* 2 pnt, treat separate */
2303                                 bevp2= (BevPoint *)(bl+1);
2304                                 bevp1= bevp2+1;
2305
2306                                 x1= bevp1->vec[0]- bevp2->vec[0];
2307                                 y1= bevp1->vec[1]- bevp2->vec[1];
2308
2309                                 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2310                                 bevp2->sina= bevp1->sina;
2311                                 bevp2->cosa= bevp1->cosa;
2312
2313                                 /* fill in dir & quat */
2314                                 make_bevel_list_segment_3D(bl);
2315                         }
2316                         else {
2317                                 bevp2= (BevPoint *)(bl+1);
2318                                 bevp1= bevp2+(bl->nr-1);
2319                                 bevp0= bevp1-1;
2320
2321                                 nr= bl->nr;
2322                                 while(nr--) {
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];
2327
2328                                         calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2329
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 */
2335
2336                                         bevp0= bevp1;
2337                                         bevp1= bevp2;
2338                                         bevp2++;
2339                                 }
2340
2341                                 /* correct non-cyclic cases */
2342                                 if(bl->poly== -1) {
2343                                         bevp= (BevPoint *)(bl+1);
2344                                         bevp1= bevp+1;
2345                                         bevp->sina= bevp1->sina;
2346                                         bevp->cosa= bevp1->cosa;
2347                                         bevp= (BevPoint *)(bl+1);
2348                                         bevp+= (bl->nr-1);
2349                                         bevp1= bevp-1;
2350                                         bevp->sina= bevp1->sina;
2351                                         bevp->cosa= bevp1->cosa;
2352
2353                                         /* correct for the dir/quat, see above why its needed */
2354                                         bevel_list_cyclic_fix_3D(bl);
2355                                 }
2356                         }
2357                         bl= bl->next;
2358                 }
2359         }
2360         else { /* 3D Curves */
2361                 bl= cu->bev.first;
2362                 while(bl) {
2363
2364                         if(bl->nr < 2) {
2365                                 /* do nothing */
2366                         }
2367                         else if(bl->nr==2) {    /* 2 pnt, treat separate */
2368                                 make_bevel_list_segment_3D(bl);
2369                         }
2370                         else {
2371                                 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2372                         }
2373                         bl= bl->next;
2374                 }
2375         }
2376 }
2377
2378 /* ****************** HANDLES ************** */
2379
2380 /*
2381  *   handlecodes:
2382  *              0: nothing,  1:auto,  2:vector,  3:aligned
2383  */
2384
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)
2387 {
2388         float *p1,*p2,*p3, pt[3];
2389         float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2390
2391         if(bezt->h1==0 && bezt->h2==0) return;
2392
2393         p2= bezt->vec[1];
2394
2395         if(prev==0) {
2396                 p3= next->vec[1];
2397                 pt[0]= 2*p2[0]- p3[0];
2398                 pt[1]= 2*p2[1]- p3[1];
2399                 pt[2]= 2*p2[2]- p3[2];
2400                 p1= pt;
2401         }
2402         else p1= prev->vec[1];
2403
2404         if(next==0) {
2405                 pt[0]= 2*p2[0]- p1[0];
2406                 pt[1]= 2*p2[1]- p1[1];
2407                 pt[2]= 2*p2[2]- p1[2];
2408                 p3= pt;
2409         }
2410         else p3= next->vec[1];
2411
2412         dx= p2[0]- p1[0];
2413         dy= p2[1]- p1[1];
2414         dz= p2[2]- p1[2];
2415         
2416         if(mode) len1= dx;
2417         else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2418         
2419         dx1= p3[0]- p2[0];
2420         dy1= p3[1]- p2[1];
2421         dz1= p3[2]- p2[2];
2422         
2423         if(mode) len2= dx1;
2424         else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2425
2426         if(len1==0.0f) len1=1.0f;
2427         if(len2==0.0f) len2=1.0f;
2428
2429
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);
2435                 if(len!=0.0f) {
2436                         int leftviolate=0, rightviolate=0;      /* for mode==2 */
2437                         
2438                         if(len1>5.0f*len2) len1= 5.0f*len2;     
2439                         if(len2>5.0f*len1) len2= 5.0f*len1;
2440                         
2441                         if(bezt->h1==HD_AUTO) {
2442                                 len1/=len;
2443                                 *(p2-3)= *p2-vx*len1;
2444                                 *(p2-2)= *(p2+1)-vy*len1;
2445                                 *(p2-1)= *(p2+2)-vz*len1;
2446                                 
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];
2452                                         }
2453                                         else {                                          // handles should not be beyond y coord of two others
2454                                                 if(ydiff1<=0.0) { 
2455                                                         if(prev->vec[1][1] > bezt->vec[0][1]) {
2456                                                                 bezt->vec[0][1]= prev->vec[1][1]; 
2457                                                                 leftviolate= 1;
2458                                                         }
2459                                                 }
2460                                                 else {
2461                                                         if(prev->vec[1][1] < bezt->vec[0][1]) {
2462                                                                 bezt->vec[0][1]= prev->vec[1][1]; 
2463                                                                 leftviolate= 1;
2464                                                         }
2465                                                 }
2466                                         }
2467                                 }
2468                         }
2469                         if(bezt->h2==HD_AUTO) {
2470                                 len2/=len;
2471                                 *(p2+3)= *p2+vx*len2;
2472                                 *(p2+4)= *(p2+1)+vy*len2;
2473                                 *(p2+5)= *(p2+2)+vz*len2;
2474                                 
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];
2480                                         }
2481                                         else {                                          // handles should not be beyond y coord of two others
2482                                                 if(ydiff1<=0.0) { 
2483                                                         if(next->vec[1][1] < bezt->vec[2][1]) {
2484                                                                 bezt->vec[2][1]= next->vec[1][1]; 
2485                                                                 rightviolate= 1;
2486                                                         }
2487                                                 }
2488                                                 else {
2489                                                         if(next->vec[1][1] > bezt->vec[2][1]) {
2490                                                                 bezt->vec[2][1]= next->vec[1][1]; 
2491                                                                 rightviolate= 1;
2492                                                         }
2493                                                 }
2494                                         }
2495                                 }
2496                         }
2497                         if(leftviolate || rightviolate) {       /* align left handle */
2498                                 float h1[3], h2[3];
2499                                 
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);
2504                                 
2505                                 vz= INPR(h1, h2);
2506                                 
2507                                 if(leftviolate) {
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];
2511                                 }
2512                                 else {
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];
2516                                 }
2517                         }
2518                         
2519                 }
2520         }
2521
2522         if(bezt->h1==HD_VECT) { /* vector */
2523                 dx/=3.0; 
2524                 dy/=3.0; 
2525                 dz/=3.0;
2526                 *(p2-3)= *p2-dx;
2527                 *(p2-2)= *(p2+1)-dy;
2528                 *(p2-1)= *(p2+2)-dz;
2529         }
2530         if(bezt->h2==HD_VECT) {
2531                 dx1/=3.0; 
2532                 dy1/=3.0; 
2533                 dz1/=3.0;
2534                 *(p2+3)= *p2+dx1;
2535                 *(p2+4)= *(p2+1)+dy1;
2536                 *(p2+5)= *(p2+2)+dz1;
2537         }
2538
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;
2543
2544         if(bezt->f1 & SELECT) { /* order of calculation */
2545                 if(bezt->h2==HD_ALIGN) {        /* aligned */
2546                         len= len2/len1;
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]);
2550                 }
2551                 if(bezt->h1==HD_ALIGN) {
2552                         len= len1/len2;
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]);
2556                 }
2557         }
2558         else {
2559                 if(bezt->h1==HD_ALIGN) {
2560                         len= len1/len2;
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]);
2564                 }
2565                 if(bezt->h2==HD_ALIGN) {        /* aligned */
2566                         len= len2/len1;
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]);
2570                 }
2571         }
2572 }
2573
2574 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2575 {
2576         BezTriple *bezt, *prev, *next;
2577         short a;
2578
2579         if(nu->type != CU_BEZIER) return;
2580         if(nu->pntsu<2) return;
2581         
2582         a= nu->pntsu;
2583         bezt= nu->bezt;
2584         if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2585         else prev= 0;
2586         next= bezt+1;
2587
2588         while(a--) {
2589                 calchandleNurb(bezt, prev, next, 0);
2590                 prev= bezt;
2591                 if(a==1) {
2592                         if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2593                         else next= 0;
2594                 }
2595                 else next++;
2596
2597                 bezt++;
2598         }
2599 }
2600
2601
2602 void testhandlesNurb(Nurb *nu)
2603 {
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
2610         */
2611         BezTriple *bezt;
2612         short flag, a;
2613
2614         if(nu->type != CU_BEZIER) return;
2615
2616         bezt= nu->bezt;
2617         a= nu->pntsu;
2618         while(a--) {
2619                 flag= 0;
2620                 if(bezt->f1 & SELECT) flag++;
2621                 if(bezt->f2 & SELECT) flag += 2;
2622                 if(bezt->f3 & SELECT) flag += 4;
2623
2624                 if( !(flag==0 || flag==7) ) {
2625                         if(bezt->h1==HD_AUTO) {   /* auto */
2626                                 bezt->h1= HD_ALIGN;
2627                         }
2628                         if(bezt->h2==HD_AUTO) {   /* auto */
2629                                 bezt->h2= HD_ALIGN;
2630                         }
2631
2632                         if(bezt->h1==HD_VECT) {   /* vector */
2633                                 if(flag < 4) bezt->h1= 0;
2634                         }
2635                         if(bezt->h2==HD_VECT) {   /* vector */
2636                                 if( flag > 3) bezt->h2= 0;
2637                         }
2638                 }
2639                 bezt++;
2640         }
2641
2642         calchandlesNurb(nu);
2643 }
2644
2645 void autocalchandlesNurb(Nurb *nu, int flag)
2646 {
2647         /* checks handle coordinates and calculates type */
2648         
2649         BezTriple *bezt2, *bezt1, *bezt0;
2650         int i, align, leftsmall, rightsmall;
2651
2652         if(nu==0 || nu->bezt==0) return;
2653         
2654         bezt2 = nu->bezt;
2655         bezt1 = bezt2 + (nu->pntsu-1);
2656         bezt0 = bezt1 - 1;
2657         i = nu->pntsu;
2658
2659         while(i--) {
2660                 
2661                 align= leftsmall= rightsmall= 0;
2662                 
2663                 /* left handle: */
2664                 if(flag==0 || (bezt1->f1 & flag) ) {
2665                         bezt1->h1= 0;
2666                         /* distance too short: vectorhandle */
2667                         if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2668                                 bezt1->h1= HD_VECT;
2669                                 leftsmall= 1;
2670                         }
2671                         else {
2672                                 /* aligned handle? */
2673                                 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2674                                         align= 1;
2675                                         bezt1->h1= HD_ALIGN;
2676                                 }
2677                                 /* or vector handle? */
2678                                 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2679                                         bezt1->h1= HD_VECT;
2680                                 
2681                         }
2682                 }
2683                 /* right handle: */
2684                 if(flag==0 || (bezt1->f3 & flag) ) {
2685                         bezt1->h2= 0;
2686                         /* distance too short: vectorhandle */
2687                         if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2688                                 bezt1->h2= HD_VECT;
2689                                 rightsmall= 1;
2690                         }
2691                         else {
2692                                 /* aligned handle? */
2693                                 if(align) bezt1->h2= HD_ALIGN;
2694
2695                                 /* or vector handle? */
2696                                 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2697                                         bezt1->h2= HD_VECT;
2698                                 
2699                         }
2700                 }
2701                 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2702                 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2703                 
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;
2707                 
2708                 bezt0= bezt1;
2709                 bezt1= bezt2;
2710                 bezt2++;
2711         }
2712
2713         calchandlesNurb(nu);
2714 }
2715
2716 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2717 {
2718         Nurb *nu;
2719         
2720         nu= editnurb->first;
2721         while(nu) {
2722                 autocalchandlesNurb(nu, flag);
2723                 nu= nu->next;
2724         }
2725 }
2726
2727 void sethandlesNurb(ListBase *editnurb, short code)
2728 {
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 */
2735         Nurb *nu;
2736         BezTriple *bezt;
2737         short a, ok=0;
2738
2739         if(code==1 || code==2) {
2740                 nu= editnurb->first;
2741                 while(nu) {
2742                         if(nu->type == CU_BEZIER) {
2743                                 bezt= nu->bezt;
2744                                 a= nu->pntsu;
2745                                 while(a--) {
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;
2752                                                 }
2753                                         }
2754                                         bezt++;
2755                                 }
2756                                 calchandlesNurb(nu);
2757                         }
2758                         nu= nu->next;
2759                 }
2760         }
2761         else {
2762                 /* there is 1 handle not FREE: FREE it all, else make ALIGNED  */
2763                 
2764                 nu= editnurb->first;
2765                 if (code == 5) {
2766                         ok = HD_ALIGN;
2767                 } else if (code == 6) {
2768                         ok = HD_FREE;
2769                 } else {
2770                         /* Toggle */
2771                         while(nu) {
2772                                 if(nu->type == CU_BEZIER) {
2773                                         bezt= nu->bezt;
2774                                         a= nu->pntsu;
2775                                         while(a--) {
2776                                                 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2777                                                 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2778                                                 if(ok) break;
2779                                                 bezt++;
2780                                         }
2781                                 }
2782                                 nu= nu->next;
2783                         }
2784                         if(ok) ok= HD_FREE;
2785                         else ok= HD_ALIGN;
2786                 }
2787                 nu= editnurb->first;
2788                 while(nu) {
2789                         if(nu->type == CU_BEZIER) {
2790                                 bezt= nu->bezt;
2791                                 a= nu->pntsu;
2792                                 while(a--) {
2793                                         if(bezt->f1 & SELECT) bezt->h1= ok;
2794                                         if(bezt->f3 & SELECT) bezt->h2= ok;
2795         
2796                                         bezt++;
2797                                 }
2798                                 calchandlesNurb(nu);
2799                         }
2800                         nu= nu->next;
2801                 }
2802         }
2803 }
2804
2805 static void swapdata(void *adr1, void *adr2, int len)
2806 {
2807
2808         if(len<=0) return;
2809
2810         if(len<65) {
2811                 char adr[64];
2812
2813                 memcpy(adr, adr1, len);
2814                 memcpy(adr1, adr2, len);
2815                 memcpy(adr2, adr, len);
2816         }
2817         else {
2818                 char *adr;
2819
2820                 adr= (char *)MEM_mallocN(len, "curve swap");
2821                 memcpy(adr, adr1, len);
2822                 memcpy(adr1, adr2, len);
2823                 memcpy(adr2, adr, len);
2824                 MEM_freeN(adr);
2825         }
2826 }
2827
2828 void switchdirectionNurb(Nurb *nu)
2829 {
2830         BezTriple *bezt1, *bezt2;
2831         BPoint *bp1, *bp2;
2832         float *fp1, *fp2, *tempf;
2833         int a, b;
2834
2835         if(nu->pntsu==1 && nu->pntsv==1) return;
2836
2837         if(nu->type == CU_BEZIER) {
2838                 a= nu->pntsu;
2839                 bezt1= nu->bezt;
2840                 bezt2= bezt1+(a-1);
2841                 if(a & 1) a+= 1;        /* if odd, also swap middle content */
2842                 a/= 2;
2843                 while(a>0) {
2844                         if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2845
2846                         swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2847                         if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2848
2849                         SWAP(char, bezt1->h1, bezt1->h2);
2850                         SWAP(short, bezt1->f1, bezt1->f3);
2851                         
2852                         if(bezt1!=bezt2) {
2853                                 SWAP(char, bezt2->h1, bezt2->h2);
2854                                 SWAP(short, bezt2->f1, bezt2->f3);
2855                                 bezt1->alfa= -bezt1->alfa;
2856                                 bezt2->alfa= -bezt2->alfa;
2857                         }
2858                         a--;
2859                         bezt1++; 
2860                         bezt2--;
2861                 }
2862         }
2863         else if(nu->pntsv==1) {
2864                 a= nu->pntsu;
2865                 bp1= nu->bp;
2866                 bp2= bp1+(a-1);
2867                 a/= 2;
2868                 while(bp1!=bp2 && a>0) {
2869                         SWAP(BPoint, *bp1, *bp2);
2870                         a--;
2871                         bp1->alfa= -bp1->alfa;
2872                         bp2->alfa= -bp2->alfa;
2873                         bp1++; 
2874                         bp2--;
2875                 }
2876                 if(nu->type == CU_NURBS) {
2877                         /* inverse knots */
2878                         a= KNOTSU(nu);
2879                         fp1= nu->knotsu;
2880                         fp2= fp1+(a-1);
2881                         a/= 2;
2882                         while(fp1!=fp2 && a>0) {
2883                                 SWAP(float, *fp1, *fp2);
2884                                 a--;
2885                                 fp1++; 
2886                                 fp2--;
2887                         }
2888                         /* and make in increasing order again */
2889                         a= KNOTSU(nu);
2890                         fp1= nu->knotsu;
2891                         fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2892                         while(a--) {
2893                                 fp2[0]= fabs(fp1[1]-fp1[0]);
2894                                 fp1++;
2895                                 fp2++;
2896                         }
2897         
2898                         a= KNOTSU(nu)-1;
2899                         fp1= nu->knotsu;
2900                         fp2= tempf;
2901                         fp1[0]= 0.0;
2902                         fp1++;
2903                         while(a--) {
2904                                 fp1[0]= fp1[-1]+fp2[0];
2905                                 fp1++;
2906                                 fp2++;
2907                         }
2908                         MEM_freeN(tempf);
2909                 }
2910         }
2911         else {
2912                 
2913                 for(b=0; b<nu->pntsv; b++) {
2914                 
2915                         bp1= nu->bp+b*nu->pntsu;
2916                         a= nu->pntsu;
2917                         bp2= bp1+(a-1);
2918                         a/= 2;
2919                         
2920                         while(bp1!=bp2 && a>0) {
2921                                 SWAP(BPoint, *bp1, *bp2);
2922                                 a--;
2923                                 bp1++; 
2924                                 bp2--;
2925                         }
2926                 }
2927         }
2928 }
2929
2930
2931 float (*curve_getVertexCos(Curve *cu, ListBase *lb, int *numVerts_r))[3]
2932 {
2933         int i, numVerts = *numVerts_r = count_curveverts(lb);
2934         float *co, (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "cu_vcos");
2935         Nurb *nu;
2936
2937         co = cos[0];
2938         for (nu=lb->first; nu; nu=nu->next) {
2939                 if (nu->type == CU_BEZIER) {
2940                         BezTriple *bezt = nu->bezt;
2941
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;
2946                         }
2947                 } else {
2948                         BPoint *bp = nu->bp;
2949
2950