Todo item: linked curve objects behaves incorrect with modifiers
[blender.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]= NULL;
78         }
79         if(cu->vfont) cu->vfont->id.us--; 
80         cu->vfont= NULL;
81
82         if(cu->vfontb) cu->vfontb->id.us--; 
83         cu->vfontb= NULL;
84
85         if(cu->vfonti) cu->vfonti->id.us--; 
86         cu->vfonti= NULL;
87
88         if(cu->vfontbi) cu->vfontbi->id.us--; 
89         cu->vfontbi= NULL;
90         
91         if(cu->key) cu->key->id.us--;
92         cu->key= NULL;
93 }
94
95 /* frees editcurve entirely */
96 void BKE_free_editfont(Curve *cu)
97 {
98         if(cu->editfont) {
99                 EditFont *ef= cu->editfont;
100                 
101                 if(ef->oldstr) MEM_freeN(ef->oldstr);
102                 if(ef->oldstrinfo) MEM_freeN(ef->oldstrinfo);
103                 if(ef->textbuf) MEM_freeN(ef->textbuf);
104                 if(ef->textbufinfo) MEM_freeN(ef->textbufinfo);
105                 if(ef->copybuf) MEM_freeN(ef->copybuf);
106                 if(ef->copybufinfo) MEM_freeN(ef->copybufinfo);
107                 
108                 MEM_freeN(ef);
109                 cu->editfont= NULL;
110         }
111 }
112
113 /* don't free curve itself */
114 void free_curve(Curve *cu)
115 {
116         freeNurblist(&cu->nurb);
117         BLI_freelistN(&cu->bev);
118         freedisplist(&cu->disp);
119         BKE_free_editfont(cu);
120
121         free_curve_editNurb(cu);
122         unlink_curve(cu);
123         BKE_free_animdata((ID *)cu);
124         
125         if(cu->mat) MEM_freeN(cu->mat);
126         if(cu->str) MEM_freeN(cu->str);
127         if(cu->strinfo) MEM_freeN(cu->strinfo);
128         if(cu->bb) MEM_freeN(cu->bb);
129         if(cu->path) free_path(cu->path);
130         if(cu->tb) MEM_freeN(cu->tb);
131 }
132
133 Curve *add_curve(const char *name, int type)
134 {
135         Curve *cu;
136
137         cu= alloc_libblock(&G.main->curve, ID_CU, name);
138         
139         cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
140         cu->flag= CU_FRONT|CU_BACK|CU_DEFORM_BOUNDS_OFF|CU_PATH_RADIUS;
141         cu->pathlen= 100;
142         cu->resolu= cu->resolv= (type == OB_SURF) ? 4 : 12;
143         cu->width= 1.0;
144         cu->wordspace = 1.0;
145         cu->spacing= cu->linedist= 1.0;
146         cu->fsize= 1.0;
147         cu->ulheight = 0.05;    
148         cu->texflag= CU_AUTOSPACE;
149         cu->smallcaps_scale= 0.75f;
150         cu->twist_mode= CU_TWIST_MINIMUM;       // XXX: this one seems to be the best one in most cases, at least for curve deform...
151         
152         cu->bb= unit_boundbox();
153         
154         if(type==OB_FONT) {
155                 cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
156                 cu->vfont->id.us+=4;
157                 cu->str= MEM_mallocN(12, "str");
158                 BLI_strncpy(cu->str, "Text", 12);
159                 cu->len= cu->pos= 4;
160                 cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
161                 cu->totbox= cu->actbox= 1;
162                 cu->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
163                 cu->tb[0].w = cu->tb[0].h = 0.0;
164         }
165         
166         return cu;
167 }
168
169 Curve *copy_curve(Curve *cu)
170 {
171         Curve *cun;
172         int a;
173         
174         cun= copy_libblock(cu);
175         cun->nurb.first= cun->nurb.last= 0;
176         duplicateNurblist( &(cun->nurb), &(cu->nurb));
177
178         cun->mat= MEM_dupallocN(cu->mat);
179         for(a=0; a<cun->totcol; a++) {
180                 id_us_plus((ID *)cun->mat[a]);
181         }
182         
183         cun->str= MEM_dupallocN(cu->str);
184         cun->strinfo= MEM_dupallocN(cu->strinfo);       
185         cun->tb= MEM_dupallocN(cu->tb);
186         cun->bb= MEM_dupallocN(cu->bb);
187         
188         cun->key= copy_key(cu->key);
189         if(cun->key) cun->key->from= (ID *)cun;
190         
191         cun->disp.first= cun->disp.last= 0;
192         cun->bev.first= cun->bev.last= 0;
193         cun->path= 0;
194
195         cun->editnurb= NULL;
196         cun->editfont= NULL;
197
198 #if 0   // XXX old animation system
199         /* single user ipo too */
200         if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
201 #endif // XXX old animation system
202
203         id_us_plus((ID *)cun->vfont);
204         id_us_plus((ID *)cun->vfontb);  
205         id_us_plus((ID *)cun->vfonti);
206         id_us_plus((ID *)cun->vfontbi);
207         
208         return cun;
209 }
210
211 void make_local_curve(Curve *cu)
212 {
213         Object *ob = 0;
214         Curve *cun;
215         int local=0, lib=0;
216         
217         /* - when there are only lib users: don't do
218          * - when there are only local users: set flag
219          * - mixed: do a copy
220          */
221         
222         if(cu->id.lib==0) return;
223
224         if(cu->vfont) cu->vfont->id.lib= NULL;
225         if(cu->vfontb) cu->vfontb->id.lib= NULL;
226         if(cu->vfonti) cu->vfonti->id.lib= NULL;
227         if(cu->vfontbi) cu->vfontbi->id.lib= NULL;
228
229         if(cu->id.us==1) {
230                 cu->id.lib= 0;
231                 cu->id.flag= LIB_LOCAL;
232                 new_id(0, (ID *)cu, 0);
233                 return;
234         }
235         
236         ob= G.main->object.first;
237         while(ob) {
238                 if(ob->data==cu) {
239                         if(ob->id.lib) lib= 1;
240                         else local= 1;
241                 }
242                 ob= ob->id.next;
243         }
244         
245         if(local && lib==0) {
246                 cu->id.lib= 0;
247                 cu->id.flag= LIB_LOCAL;
248                 new_id(0, (ID *)cu, 0);
249         }
250         else if(local && lib) {
251                 cun= copy_curve(cu);
252                 cun->id.us= 0;
253                 
254                 ob= G.main->object.first;
255                 while(ob) {
256                         if(ob->data==cu) {
257                                 
258                                 if(ob->id.lib==0) {
259                                         ob->data= cun;
260                                         cun->id.us++;
261                                         cu->id.us--;
262                                 }
263                         }
264                         ob= ob->id.next;
265                 }
266         }
267 }
268
269 short curve_type(Curve *cu)
270 {
271         Nurb *nu;
272         if(cu->vfont) {
273                 return OB_FONT;
274         }
275         for (nu= cu->nurb.first; nu; nu= nu->next) {
276                 if(nu->pntsv>1) {
277                         return OB_SURF;
278                 }
279         }
280         
281         return OB_CURVE;
282 }
283
284 void test_curve_type(Object *ob)
285 {       
286         ob->type = curve_type(ob->data);
287 }
288
289 void tex_space_curve(Curve *cu)
290 {
291         DispList *dl;
292         BoundBox *bb;
293         float *fp, min[3], max[3];
294         int tot, doit= 0;
295         
296         if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
297         bb= cu->bb;
298         
299         INIT_MINMAX(min, max);
300
301         dl= cu->disp.first;
302         while(dl) {
303                 
304                 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
305                 else tot= dl->nr*dl->parts;
306                 
307                 if(tot) doit= 1;
308                 fp= dl->verts;
309                 while(tot--) {
310                         DO_MINMAX(fp, min, max);
311                         fp += 3;
312                 }
313                 dl= dl->next;
314         }
315
316         if(!doit) {
317                 min[0] = min[1] = min[2] = -1.0f;
318                 max[0] = max[1] = max[2] = 1.0f;
319         }
320
321         boundbox_set_from_min_max(bb, min, max);
322
323         if(cu->texflag & CU_AUTOSPACE) {
324                 mid_v3_v3v3(cu->loc, min, max);
325                 cu->size[0]= (max[0]-min[0])/2.0f;
326                 cu->size[1]= (max[1]-min[1])/2.0f;
327                 cu->size[2]= (max[2]-min[2])/2.0f;
328
329                 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
330
331                 if(cu->size[0]==0.0) cu->size[0]= 1.0;
332                 else if(cu->size[0]>0.0 && cu->size[0]<0.00001) cu->size[0]= 0.00001;
333                 else if(cu->size[0]<0.0 && cu->size[0]> -0.00001) cu->size[0]= -0.00001;
334         
335                 if(cu->size[1]==0.0) cu->size[1]= 1.0;
336                 else if(cu->size[1]>0.0 && cu->size[1]<0.00001) cu->size[1]= 0.00001;
337                 else if(cu->size[1]<0.0 && cu->size[1]> -0.00001) cu->size[1]= -0.00001;
338         
339                 if(cu->size[2]==0.0) cu->size[2]= 1.0;
340                 else if(cu->size[2]>0.0 && cu->size[2]<0.00001) cu->size[2]= 0.00001;
341                 else if(cu->size[2]<0.0 && cu->size[2]> -0.00001) cu->size[2]= -0.00001;
342
343         }
344 }
345
346
347 int count_curveverts(ListBase *nurb)
348 {
349         Nurb *nu;
350         int tot=0;
351         
352         nu= nurb->first;
353         while(nu) {
354                 if(nu->bezt) tot+= 3*nu->pntsu;
355                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
356                 
357                 nu= nu->next;
358         }
359         return tot;
360 }
361
362 int count_curveverts_without_handles(ListBase *nurb)
363 {
364         Nurb *nu;
365         int tot=0;
366         
367         nu= nurb->first;
368         while(nu) {
369                 if(nu->bezt) tot+= nu->pntsu;
370                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
371                 
372                 nu= nu->next;
373         }
374         return tot;
375 }
376
377 /* **************** NURBS ROUTINES ******************** */
378
379 void freeNurb(Nurb *nu)
380 {
381
382         if(nu==0) return;
383
384         if(nu->bezt) MEM_freeN(nu->bezt);
385         nu->bezt= 0;
386         if(nu->bp) MEM_freeN(nu->bp);
387         nu->bp= 0;
388         if(nu->knotsu) MEM_freeN(nu->knotsu);
389         nu->knotsu= NULL;
390         if(nu->knotsv) MEM_freeN(nu->knotsv);
391         nu->knotsv= NULL;
392         /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
393
394         MEM_freeN(nu);
395
396 }
397
398
399 void freeNurblist(ListBase *lb)
400 {
401         Nurb *nu, *next;
402
403         if(lb==0) return;
404
405         nu= lb->first;
406         while(nu) {
407                 next= nu->next;
408                 freeNurb(nu);
409                 nu= next;
410         }
411         lb->first= lb->last= 0;
412 }
413
414 Nurb *duplicateNurb(Nurb *nu)
415 {
416         Nurb *newnu;
417         int len;
418
419         newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
420         if(newnu==0) return 0;
421         memcpy(newnu, nu, sizeof(Nurb));
422
423         if(nu->bezt) {
424                 newnu->bezt=
425                         (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
426                 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
427         }
428         else {
429                 len= nu->pntsu*nu->pntsv;
430                 newnu->bp=
431                         (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
432                 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
433                 
434                 newnu->knotsu= newnu->knotsv= NULL;
435                 
436                 if(nu->knotsu) {
437                         len= KNOTSU(nu);
438                         if(len) {
439                                 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
440                                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
441                         }
442                 }
443                 if(nu->pntsv>1 && nu->knotsv) {
444                         len= KNOTSV(nu);
445                         if(len) {
446                                 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
447                                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
448                         }
449                 }
450         }
451         return newnu;
452 }
453
454 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
455 {
456         Nurb *nu, *nun;
457         
458         freeNurblist(lb1);
459         
460         nu= lb2->first;
461         while(nu) {
462                 nun= duplicateNurb(nu);
463                 BLI_addtail(lb1, nun);
464                 
465                 nu= nu->next;
466         }
467 }
468
469 void test2DNurb(Nurb *nu)
470 {
471         BezTriple *bezt;
472         BPoint *bp;
473         int a;
474         
475         if((nu->flag & CU_2D)==0)
476                 return;
477
478         if(nu->type == CU_BEZIER) {
479                 a= nu->pntsu;
480                 bezt= nu->bezt;
481                 while(a--) {
482                         bezt->vec[0][2]= 0.0; 
483                         bezt->vec[1][2]= 0.0; 
484                         bezt->vec[2][2]= 0.0;
485                         bezt++;
486                 }
487         }
488         else {
489                 a= nu->pntsu*nu->pntsv;
490                 bp= nu->bp;
491                 while(a--) {
492                         bp->vec[2]= 0.0;
493                         bp++;
494                 }
495         }
496 }
497
498 void minmaxNurb(Nurb *nu, float *min, float *max)
499 {
500         BezTriple *bezt;
501         BPoint *bp;
502         int a;
503
504         if(nu->type == CU_BEZIER) {
505                 a= nu->pntsu;
506                 bezt= nu->bezt;
507                 while(a--) {
508                         DO_MINMAX(bezt->vec[0], min, max);
509                         DO_MINMAX(bezt->vec[1], min, max);
510                         DO_MINMAX(bezt->vec[2], min, max);
511                         bezt++;
512                 }
513         }
514         else {
515                 a= nu->pntsu*nu->pntsv;
516                 bp= nu->bp;
517                 while(a--) {
518                         DO_MINMAX(bp->vec, min, max);
519                         bp++;
520                 }
521         }
522 }
523
524 /* be sure to call makeknots after this */
525 void addNurbPoints(Nurb *nu, int number)
526 {
527         BPoint *tmp= nu->bp;
528         int i;
529         nu->bp= (BPoint *)MEM_mallocN((nu->pntsu + number) * sizeof(BPoint), "rna_Curve_spline_points_add");
530
531         if(tmp) {
532                 memmove(nu->bp, tmp, nu->pntsu * sizeof(BPoint));
533                 MEM_freeN(tmp);
534         }
535
536         memset(nu->bp + nu->pntsu, 0, number * sizeof(BPoint));
537
538         for(i=0, tmp= nu->bp + nu->pntsu; i < number; i++, tmp++) {
539                 tmp->radius= 1.0f;
540         }
541
542         nu->pntsu += number;
543 }
544
545 void addNurbPointsBezier(Nurb *nu, int number)
546 {
547         BezTriple *tmp= nu->bezt;
548         int i;
549         nu->bezt= (BezTriple *)MEM_mallocN((nu->pntsu + number) * sizeof(BezTriple), "rna_Curve_spline_points_add");
550
551         if(tmp) {
552                 memmove(nu->bezt, tmp, nu->pntsu * sizeof(BezTriple));
553                 MEM_freeN(tmp);
554         }
555
556         memset(nu->bezt + nu->pntsu, 0, number * sizeof(BezTriple));
557
558         for(i=0, tmp= nu->bezt + nu->pntsu; i < number; i++, tmp++) {
559                 tmp->radius= 1.0f;
560         }
561
562         nu->pntsu += number;
563 }
564
565 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
566
567
568 static void calcknots(float *knots, short aantal, short order, short type)
569 /* knots: number of pnts NOT corrected for cyclic */
570 /* type;         0: uniform, 1: endpoints, 2: bezier */
571 {
572         float k;
573         int a, t;
574
575                 t = aantal+order;
576         if(type==0) {
577
578                 for(a=0;a<t;a++) {
579                         knots[a]= (float)a;
580                 }
581         }
582         else if(type==1) {
583                 k= 0.0;
584                 for(a=1;a<=t;a++) {
585                         knots[a-1]= k;
586                         if(a>=order && a<=aantal) k+= 1.0;
587                 }
588         }
589         else if(type==2) {
590                 /* Warning, the order MUST be 2 or 4, if this is not enforced, the displist will be corrupt */
591                 if(order==4) {
592                         k= 0.34;
593                         for(a=0;a<t;a++) {
594                                 knots[a]= (float)floor(k);
595                                 k+= (1.0/3.0);
596                         }
597                 }
598                 else if(order==3) {
599                         k= 0.6;
600                         for(a=0;a<t;a++) {
601                                 if(a>=order && a<=aantal) k+= (0.5);
602                                 knots[a]= (float)floor(k);
603                         }
604                 }
605                 else {
606                         printf("bez nurb curve order is not 3 or 4, should never happen\n");
607                 }
608         }
609 }
610
611 static void makecyclicknots(float *knots, short pnts, short order)
612 /* pnts, order: number of pnts NOT corrected for cyclic */
613 {
614         int a, b, order2, c;
615
616         if(knots==0) return;
617
618         order2=order-1;
619
620         /* do first long rows (order -1), remove identical knots at endpoints */
621         if(order>2) {
622                 b= pnts+order2;
623                 for(a=1; a<order2; a++) {
624                         if(knots[b]!= knots[b-a]) break;
625                 }
626                 if(a==order2) knots[pnts+order-2]+= 1.0;
627         }
628
629         b= order;
630                 c=pnts + order + order2;
631         for(a=pnts+order2; a<c; a++) {
632                 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
633                 b--;
634         }
635 }
636
637
638
639 static void makeknots(Nurb *nu, short uv)
640 {
641         if(nu->type == CU_NURBS) {
642                 if(uv == 1) {
643                         if(nu->knotsu) MEM_freeN(nu->knotsu);
644                         if(check_valid_nurb_u(nu)) {
645                                 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
646                                 if(nu->flagu & CU_NURB_CYCLIC) {
647                                         calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0);  /* cyclic should be uniform */
648                                         makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
649                                 } else {
650                                         calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu>>1);
651                                 }
652                         }
653                         else nu->knotsu= NULL;
654                 
655                 } else if(uv == 2) {
656                         if(nu->knotsv) MEM_freeN(nu->knotsv);
657                         if(check_valid_nurb_v(nu)) {
658                                 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
659                                 if(nu->flagv & CU_NURB_CYCLIC) {
660                                         calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0);  /* cyclic should be uniform */
661                                         makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
662                                 } else {
663                                         calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv>>1);
664                                 }
665                         }
666                         else nu->knotsv= NULL;
667                 }
668         }
669 }
670
671 void nurbs_knot_calc_u(Nurb *nu)
672 {
673         makeknots(nu, 1);
674 }
675
676 void nurbs_knot_calc_v(Nurb *nu)
677 {
678         makeknots(nu, 2);
679 }
680
681 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
682 {
683         float d, e;
684         int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
685
686         orderpluspnts= order+pnts;
687                 opp2 = orderpluspnts-1;
688
689         /* this is for float inaccuracy */
690         if(t < knots[0]) t= knots[0];
691         else if(t > knots[opp2]) t= knots[opp2];
692
693         /* this part is order '1' */
694                 o2 = order + 1;
695         for(i=0;i<opp2;i++) {
696                 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
697                         basis[i]= 1.0;
698                         i1= i-o2;
699                         if(i1<0) i1= 0;
700                         i2= i;
701                         i++;
702                         while(i<opp2) {
703                                 basis[i]= 0.0;
704                                 i++;
705                         }
706                         break;
707                 }
708                 else basis[i]= 0.0;
709         }
710         basis[i]= 0.0;
711         
712         /* this is order 2,3,... */
713         for(j=2; j<=order; j++) {
714
715                 if(i2+j>= orderpluspnts) i2= opp2-j;
716
717                 for(i= i1; i<=i2; i++) {
718                         if(basis[i]!=0.0)
719                                 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
720                         else
721                                 d= 0.0;
722
723                         if(basis[i+1]!=0.0)
724                                 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
725                         else
726                                 e= 0.0;
727
728                         basis[i]= d+e;
729                 }
730         }
731
732         *start= 1000;
733         *end= 0;
734
735         for(i=i1; i<=i2; i++) {
736                 if(basis[i]>0.0) {
737                         *end= i;
738                         if(*start==1000) *start= i;
739                 }
740         }
741 }
742
743
744 void makeNurbfaces(Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
745 /* coord_array  has to be 3*4*resolu*resolv in size, and zero-ed */
746 {
747         BPoint *bp;
748         float *basisu, *basis, *basisv, *sum, *fp, *in;
749         float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
750         int i, j, iofs, jofs, cycl, len, curu, curv;
751         int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
752         
753         int totu = nu->pntsu*resolu, totv = nu->pntsv*resolv;
754         
755         if(nu->knotsu==NULL || nu->knotsv==NULL) return;
756         if(nu->orderu>nu->pntsu) return;
757         if(nu->orderv>nu->pntsv) return;
758         if(coord_array==NULL) return;
759         
760         /* allocate and initialize */
761         len = totu * totv;
762         if(len==0) return;
763         
764
765         
766         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
767         
768         len= totu*totv;
769         if(len==0) {
770                 MEM_freeN(sum);
771                 return;
772         }
773
774         bp= nu->bp;
775         i= nu->pntsu*nu->pntsv;
776         ratcomp=0;
777         while(i--) {
778                 if(bp->vec[3]!=1.0) {
779                         ratcomp= 1;
780                         break;
781                 }
782                 bp++;
783         }
784         
785         fp= nu->knotsu;
786         ustart= fp[nu->orderu-1];
787         if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
788         else uend= fp[nu->pntsu];
789         ustep= (uend-ustart)/((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
790         
791         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
792
793         fp= nu->knotsv;
794         vstart= fp[nu->orderv-1];
795         
796         if(nu->flagv & CU_NURB_CYCLIC) vend= fp[nu->pntsv+nu->orderv-1];
797         else vend= fp[nu->pntsv];
798         vstep= (vend-vstart)/((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
799         
800         len= KNOTSV(nu);
801         basisv= (float *)MEM_mallocN(sizeof(float)*len*totv, "makeNurbfaces3");
802         jstart= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces4");
803         jend= (int *)MEM_mallocN(sizeof(float)*totv, "makeNurbfaces5");
804
805         /* precalculation of basisv and jstart,jend */
806         if(nu->flagv & CU_NURB_CYCLIC) cycl= nu->orderv-1; 
807         else cycl= 0;
808         v= vstart;
809         basis= basisv;
810         curv= totv;
811         while(curv--) {
812                 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+curv, jend+curv);
813                 basis+= KNOTSV(nu);
814                 v+= vstep;
815         }
816
817         if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
818         else cycl= 0;
819         in= coord_array;
820         u= ustart;
821         curu= totu;
822         while(curu--) {
823
824                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
825
826                 basis= basisv;
827                 curv= totv;
828                 while(curv--) {
829
830                         jsta= jstart[curv];
831                         jen= jend[curv];
832
833                         /* calculate sum */
834                         sumdiv= 0.0;
835                         fp= sum;
836
837                         for(j= jsta; j<=jen; j++) {
838
839                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
840                                 else jofs= j;
841                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
842
843                                 for(i= istart; i<=iend; i++, fp++) {
844
845                                         if(i>= nu->pntsu) {
846                                                 iofs= i- nu->pntsu;
847                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
848                                         }
849                                         else bp++;
850
851                                         if(ratcomp) {
852                                                 *fp= basisu[i]*basis[j]*bp->vec[3];
853                                                 sumdiv+= *fp;
854                                         }
855                                         else *fp= basisu[i]*basis[j];
856                                 }
857                         }
858                 
859                         if(ratcomp) {
860                                 fp= sum;
861                                 for(j= jsta; j<=jen; j++) {
862                                         for(i= istart; i<=iend; i++, fp++) {
863                                                 *fp/= sumdiv;
864                                         }
865                                 }
866                         }
867
868                         /* one! (1.0) real point now */
869                         fp= sum;
870                         for(j= jsta; j<=jen; j++) {
871
872                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
873                                 else jofs= j;
874                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
875
876                                 for(i= istart; i<=iend; i++, fp++) {
877
878                                         if(i>= nu->pntsu) {
879                                                 iofs= i- nu->pntsu;
880                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
881                                         }
882                                         else bp++;
883
884                                         if(*fp!=0.0) {
885                                                 in[0]+= (*fp) * bp->vec[0];
886                                                 in[1]+= (*fp) * bp->vec[1];
887                                                 in[2]+= (*fp) * bp->vec[2];
888                                         }
889                                 }
890                         }
891
892                         in+=3;
893                         basis+= KNOTSV(nu);
894                 }
895                 u+= ustep;
896                 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*totv*sizeof(*in)));
897         }
898
899         /* free */
900         MEM_freeN(sum);
901         MEM_freeN(basisu);
902         MEM_freeN(basisv);
903         MEM_freeN(jstart);
904         MEM_freeN(jend);
905 }
906
907 void makeNurbcurve(Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
908 /* coord_array has to be 3*4*pntsu*resolu in size and zero-ed
909  * tilt_array and radius_array will be written to if valid */
910 {
911         BPoint *bp;
912         float u, ustart, uend, ustep, sumdiv;
913         float *basisu, *sum, *fp;
914         float *coord_fp= coord_array, *tilt_fp= tilt_array, *radius_fp= radius_array, *weight_fp= weight_array;
915         int i, len, istart, iend, cycl;
916
917         if(nu->knotsu==NULL) return;
918         if(nu->orderu>nu->pntsu) return;
919         if(coord_array==0) return;
920
921         /* allocate and initialize */
922         len= nu->pntsu;
923         if(len==0) return;
924         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
925         
926         resolu= (resolu*SEGMENTSU(nu));
927         
928         if(resolu==0) {
929                 MEM_freeN(sum);
930                 return;
931         }
932
933         fp= nu->knotsu;
934         ustart= fp[nu->orderu-1];
935         if(nu->flagu & CU_NURB_CYCLIC) uend= fp[nu->pntsu+nu->orderu-1];
936         else uend= fp[nu->pntsu];
937         ustep= (uend-ustart)/(resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
938         
939         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
940
941         if(nu->flagu & CU_NURB_CYCLIC) cycl= nu->orderu-1; 
942         else cycl= 0;
943
944         u= ustart;
945         while(resolu--) {
946
947                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
948                 /* calc sum */
949                 sumdiv= 0.0;
950                 fp= sum;
951                 bp= nu->bp+ istart-1;
952                 for(i= istart; i<=iend; i++, fp++) {
953
954                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
955                         else bp++;
956
957                         *fp= basisu[i]*bp->vec[3];
958                         sumdiv+= *fp;
959                 }
960                 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
961                         /* is normalizing needed? */
962                         fp= sum;
963                         for(i= istart; i<=iend; i++, fp++) {
964                                 *fp/= sumdiv;
965                         }
966                 }
967
968                 /* one! (1.0) real point */
969                 fp= sum;
970                 bp= nu->bp+ istart-1;
971                 for(i= istart; i<=iend; i++, fp++) {
972
973                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
974                         else bp++;
975
976                         if(*fp!=0.0) {
977                                 
978                                 coord_fp[0]+= (*fp) * bp->vec[0];
979                                 coord_fp[1]+= (*fp) * bp->vec[1];
980                                 coord_fp[2]+= (*fp) * bp->vec[2];
981                                 
982                                 if (tilt_fp)
983                                         (*tilt_fp) += (*fp) * bp->alfa;
984                                 
985                                 if (radius_fp)
986                                         (*radius_fp) += (*fp) * bp->radius;
987
988                                 if (weight_fp)
989                                         (*weight_fp) += (*fp) * bp->weight;
990                                 
991                         }
992                 }
993
994                 coord_fp = (float *)(((char *)coord_fp) + stride);
995                 
996                 if (tilt_fp)    tilt_fp = (float *)(((char *)tilt_fp) + stride);
997                 if (radius_fp)  radius_fp = (float *)(((char *)radius_fp) + stride);
998                 if (weight_fp)  weight_fp = (float *)(((char *)weight_fp) + stride);
999                 
1000                 u+= ustep;
1001         }
1002
1003         /* free */
1004         MEM_freeN(sum);
1005         MEM_freeN(basisu);
1006 }
1007
1008 /* forward differencing method for bezier curve */
1009 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
1010 {
1011         float rt0,rt1,rt2,rt3,f;
1012         int a;
1013
1014         f= (float)it;
1015         rt0= q0;
1016         rt1= 3.0f*(q1-q0)/f;
1017         f*= f;
1018         rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1019         f*= it;
1020         rt3= (q3-q0+3.0f*(q1-q2))/f;
1021         
1022           q0= rt0;
1023         q1= rt1+rt2+rt3;
1024         q2= 2*rt2+6*rt3;
1025         q3= 6*rt3;
1026   
1027           for(a=0; a<=it; a++) {
1028                 *p= q0;
1029                 p = (float *)(((char *)p)+stride);
1030                 q0+= q1;
1031                  q1+= q2;
1032                  q2+= q3;
1033          }
1034 }
1035
1036 static void forward_diff_bezier_cotangent(float *p0, float *p1, float *p2, float *p3, float *p, int it, int stride)
1037 {
1038         /* note that these are not purpendicular to the curve
1039          * they need to be rotated for this,
1040          *
1041          * This could also be optimized like forward_diff_bezier */
1042         int a;
1043           for(a=0; a<=it; a++) {
1044                 float t = (float)a / (float)it;
1045
1046                 int i;
1047                 for(i=0; i<3; i++) {
1048                         p[i]= (-6*t + 6)*p0[i] + (18*t - 12)*p1[i] + (-18*t + 6)*p2[i] + (6*t)*p3[i];
1049                 }
1050                 normalize_v3(p);
1051                 p = (float *)(((char *)p)+stride);
1052          }
1053 }
1054
1055 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1056
1057 float *make_orco_surf(Object *ob)
1058 {
1059         /* Note: this function is used in convertblender only atm, so
1060          * suppose nonzero curve's render resolution should always be used */
1061         Curve *cu= ob->data;
1062         Nurb *nu;
1063         int a, b, tot=0;
1064         int sizeu, sizev;
1065         int resolu, resolv;
1066         float *fp, *coord_array;
1067         
1068         /* first calculate the size of the datablock */
1069         nu= cu->nurb.first;
1070         while(nu) {
1071                 /* as we want to avoid the seam in a cyclic nurbs
1072                 texture wrapping, reserve extra orco data space to save these extra needed
1073                 vertex based UV coordinates for the meridian vertices.
1074                 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1075                 the renderface/vert construction.
1076                 
1077                 See also convertblender.c: init_render_surf()
1078                 */
1079
1080                 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1081                 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1082                 
1083                 sizeu = nu->pntsu*resolu;
1084                 sizev = nu->pntsv*resolv;
1085                 if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1086                 if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1087                  if(nu->pntsv>1) tot+= sizeu * sizev;
1088                 
1089                 nu= nu->next;
1090         }
1091         /* makeNurbfaces wants zeros */
1092         fp= coord_array= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1093         
1094         nu= cu->nurb.first;
1095         while(nu) {
1096                 resolu= cu->resolu_ren ? cu->resolu_ren : nu->resolu;
1097                 resolv= cu->resolv_ren ? cu->resolv_ren : nu->resolv;
1098
1099                 if(nu->pntsv>1) {
1100                         sizeu = nu->pntsu*resolu;
1101                         sizev = nu->pntsv*resolv;
1102                         if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
1103                         if (nu->flagv & CU_NURB_CYCLIC) sizev++;
1104                         
1105                         if(cu->flag & CU_UV_ORCO) {
1106                                 for(b=0; b< sizeu; b++) {
1107                                         for(a=0; a< sizev; a++) {
1108                                                 
1109                                                 if(sizev <2) fp[0]= 0.0f;
1110                                                 else fp[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1111                                                 
1112                                                 if(sizeu <2) fp[1]= 0.0f;
1113                                                 else fp[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1114                                                 
1115                                                 fp[2]= 0.0;
1116                                                 
1117                                                 fp+= 3;
1118                                         }
1119                                 }
1120                         }
1121                         else {
1122                                 float *_tdata= MEM_callocN((nu->pntsu*resolu) * (nu->pntsv*resolv) *3*sizeof(float), "temp data");
1123                                 float *tdata= _tdata;
1124                                 
1125                                 makeNurbfaces(nu, tdata, 0, resolu, resolv);
1126                                 
1127                                 for(b=0; b<sizeu; b++) {
1128                                         int use_b= b;
1129                                         if (b==sizeu-1 && (nu->flagu & CU_NURB_CYCLIC))
1130                                                 use_b= 0;
1131                                         
1132                                         for(a=0; a<sizev; a++) {
1133                                                 int use_a= a;
1134                                                 if (a==sizev-1 && (nu->flagv & CU_NURB_CYCLIC))
1135                                                         use_a= 0;
1136                                                 
1137                                                 tdata = _tdata + 3 * (use_b * (nu->pntsv*resolv) + use_a);
1138                                                 
1139                                                 fp[0]= (tdata[0]-cu->loc[0])/cu->size[0];
1140                                                 fp[1]= (tdata[1]-cu->loc[1])/cu->size[1];
1141                                                 fp[2]= (tdata[2]-cu->loc[2])/cu->size[2];
1142                                                 fp+= 3;
1143                                         }
1144                                 }
1145                                 
1146                                 MEM_freeN(_tdata);
1147                         }
1148                 }
1149                 nu= nu->next;
1150         }
1151         
1152         return coord_array;
1153 }
1154
1155
1156         /* NOTE: This routine is tied to the order of vertex
1157          * built by displist and as passed to the renderer.
1158          */
1159 float *make_orco_curve(Scene *scene, Object *ob)
1160 {
1161         Curve *cu = ob->data;
1162         DispList *dl;
1163         int u, v, numVerts;
1164         float *fp, *coord_array;
1165         ListBase disp = {NULL, NULL};
1166
1167         makeDispListCurveTypes_forOrco(scene, ob, &disp);
1168
1169         numVerts = 0;
1170         for (dl=disp.first; dl; dl=dl->next) {
1171                 if (dl->type==DL_INDEX3) {
1172                         numVerts += dl->nr;
1173                 } else if (dl->type==DL_SURF) {
1174                         /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1175                         if (dl->flag & DL_CYCL_U) {
1176                                 if (dl->flag & DL_CYCL_V)
1177                                         numVerts += (dl->parts+1)*(dl->nr+1);
1178                                 else
1179                                         numVerts += dl->parts*(dl->nr+1);
1180                         }
1181                         else
1182                                 numVerts += dl->parts*dl->nr;
1183                 }
1184         }
1185
1186         fp= coord_array= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1187         for (dl=disp.first; dl; dl=dl->next) {
1188                 if (dl->type==DL_INDEX3) {
1189                         for (u=0; u<dl->nr; u++, fp+=3) {
1190                                 if (cu->flag & CU_UV_ORCO) {
1191                                         fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1192                                         fp[1]= 0.0;
1193                                         fp[2]= 0.0;
1194                                 } else {
1195                                         VECCOPY(fp, &dl->verts[u*3]);
1196
1197                                         fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1198                                         fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1199                                         fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1200                                 }
1201                         }
1202                 } else if (dl->type==DL_SURF) {
1203                         int sizeu= dl->nr, sizev= dl->parts;
1204                         
1205                         /* exception as handled in convertblender.c too */
1206                         if (dl->flag & DL_CYCL_U) {
1207                                 sizeu++;
1208                                 if (dl->flag & DL_CYCL_V)
1209                                         sizev++;
1210                         }
1211                         
1212                         for (u=0; u<sizev; u++) {
1213                                 for (v=0; v<sizeu; v++,fp+=3) {
1214                                         if (cu->flag & CU_UV_ORCO) {
1215                                                 fp[0]= 2.0f*u/(sizev - 1) - 1.0f;
1216                                                 fp[1]= 2.0f*v/(sizeu - 1) - 1.0f;
1217                                                 fp[2]= 0.0;
1218                                         } else {
1219                                                 float *vert;
1220                                                 int realv= v % dl->nr;
1221                                                 int realu= u % dl->parts;
1222                                                 
1223                                                 vert= dl->verts + 3*(dl->nr*realu + realv);
1224                                                 VECCOPY(fp, vert);
1225
1226                                                 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1227                                                 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1228                                                 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1229                                         }
1230                                 }
1231                         }
1232                 }
1233         }
1234
1235         freedisplist(&disp);
1236
1237         return coord_array;
1238 }
1239
1240
1241 /* ***************** BEVEL ****************** */
1242
1243 void makebevelcurve(Scene *scene, Object *ob, ListBase *disp, int forRender)
1244 {
1245         DispList *dl, *dlnew;
1246         Curve *bevcu, *cu;
1247         float *fp, facx, facy, angle, dangle;
1248         int nr, a;
1249
1250         cu= ob->data;
1251         disp->first = disp->last = NULL;
1252
1253         /* if a font object is being edited, then do nothing */
1254 // XXX  if( ob == obedit && ob->type == OB_FONT ) return;
1255
1256         if(cu->bevobj) {
1257                 if (cu->bevobj->type!=OB_CURVE) return;
1258
1259                 bevcu= cu->bevobj->data;
1260                 if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1261                         ListBase bevdisp= {NULL, NULL};
1262                         facx= cu->bevobj->size[0];
1263                         facy= cu->bevobj->size[1];
1264
1265                         if (forRender) {
1266                                 makeDispListCurveTypes_forRender(scene, cu->bevobj, &bevdisp, NULL, 0);
1267                                 dl= bevdisp.first;
1268                         } else {
1269                                 dl= cu->bevobj->disp.first;
1270                                 if(dl==0) {
1271                                         makeDispListCurveTypes(scene, cu->bevobj, 0);
1272                                         dl= cu->bevobj->disp.first;
1273                                 }
1274                         }
1275
1276                         while(dl) {
1277                                 if ELEM(dl->type, DL_POLY, DL_SEGM) {
1278                                         dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");
1279                                         *dlnew= *dl;
1280                                         dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1281                                         memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1282
1283                                         if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1284
1285                                         BLI_addtail(disp, dlnew);
1286                                         fp= dlnew->verts;
1287                                         nr= dlnew->parts*dlnew->nr;
1288                                         while(nr--) {
1289                                                 fp[2]= fp[1]*facy;
1290                                                 fp[1]= -fp[0]*facx;
1291                                                 fp[0]= 0.0;
1292                                                 fp+= 3;
1293                                         }
1294                                 }
1295                                 dl= dl->next;
1296                         }
1297
1298                         freedisplist(&bevdisp);
1299                 }
1300         }
1301         else if(cu->ext1==0.0 && cu->ext2==0.0) {
1302                 ;
1303         }
1304         else if(cu->ext2==0.0) {
1305                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1306                 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1307                 BLI_addtail(disp, dl);
1308                 dl->type= DL_SEGM;
1309                 dl->parts= 1;
1310                 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1311                 dl->nr= 2;
1312                 
1313                 fp= dl->verts;
1314                 fp[0]= fp[1]= 0.0;
1315                 fp[2]= -cu->ext1;
1316                 fp[3]= fp[4]= 0.0;
1317                 fp[5]= cu->ext1;
1318         }
1319         else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f)  { // we make a full round bevel in that case
1320                 
1321                 nr= 4+ 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_POLY;
1327                 dl->parts= 1;
1328                 dl->flag= DL_BACK_CURVE;
1329                 dl->nr= nr;
1330
1331                 /* a circle */
1332                 fp= dl->verts;
1333                 dangle= (2.0f*M_PI/(nr));
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         else {
1345                 short dnr;
1346                 
1347                 /* bevel now in three parts, for proper vertex normals */
1348                 /* part 1, back */
1349
1350                 if((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
1351                         dnr= nr= 2+ cu->bevresol;
1352                         if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1353                                 nr= 3+ 2*cu->bevresol;
1354
1355                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1356                         dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1357                         BLI_addtail(disp, dl);
1358                         dl->type= DL_SEGM;
1359                         dl->parts= 1;
1360                         dl->flag= DL_BACK_CURVE;
1361                         dl->nr= nr;
1362
1363                         /* half a circle */
1364                         fp= dl->verts;
1365                         dangle= (0.5*M_PI/(dnr-1));
1366                         angle= -(nr-1)*dangle;
1367
1368                         for(a=0; a<nr; a++) {
1369                                 fp[0]= 0.0;
1370                                 fp[1]= (float)(cos(angle)*(cu->ext2));
1371                                 fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1372                                 angle+= dangle;
1373                                 fp+= 3;
1374                         }
1375                 }
1376                 
1377                 /* part 2, sidefaces */
1378                 if(cu->ext1!=0.0) {
1379                         nr= 2;
1380                         
1381                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1382                         dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1383                         BLI_addtail(disp, dl);
1384                         dl->type= DL_SEGM;
1385                         dl->parts= 1;
1386                         dl->nr= nr;
1387                         
1388                         fp= dl->verts;
1389                         fp[1]= cu->ext2;
1390                         fp[2]= -cu->ext1;
1391                         fp[4]= cu->ext2;
1392                         fp[5]= cu->ext1;
1393                         
1394                         if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1395                                 dl= MEM_dupallocN(dl);
1396                                 dl->verts= MEM_dupallocN(dl->verts);
1397                                 BLI_addtail(disp, dl);
1398                                 
1399                                 fp= dl->verts;
1400                                 fp[1]= -fp[1];
1401                                 fp[2]= -fp[2];
1402                                 fp[4]= -fp[4];
1403                                 fp[5]= -fp[5];
1404                         }
1405                 }
1406                 
1407                 /* part 3, front */
1408                 if((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
1409                         dnr= nr= 2+ cu->bevresol;
1410                         if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1411                                 nr= 3+ 2*cu->bevresol;
1412
1413                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1414                         dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1415                         BLI_addtail(disp, dl);
1416                         dl->type= DL_SEGM;
1417                         dl->flag= DL_FRONT_CURVE;
1418                         dl->parts= 1;
1419                         dl->nr= nr;
1420
1421                         /* half a circle */
1422                         fp= dl->verts;
1423                         angle= 0.0;
1424                         dangle= (0.5*M_PI/(dnr-1));
1425
1426                         for(a=0; a<nr; a++) {
1427                                 fp[0]= 0.0;
1428                                 fp[1]= (float)(cos(angle)*(cu->ext2));
1429                                 fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1430                                 angle+= dangle;
1431                                 fp+= 3;
1432                         }
1433                 }
1434         }
1435 }
1436
1437 static int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1438 {
1439         /* return:
1440                 -1: colliniar
1441                  0: no intersection of segments
1442                  1: exact intersection of segments
1443                  2: cross-intersection of segments
1444         */
1445         float deler;
1446
1447         deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1448         if(deler==0.0) return -1;
1449
1450         *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1451         *labda= -(*labda/deler);
1452
1453         deler= v3[coy]-v4[coy];
1454         if(deler==0) {
1455                 deler=v3[cox]-v4[cox];
1456                 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1457         } else {
1458                 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1459         }
1460         vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1461         vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1462
1463         if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1464                 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1465                 return 2;
1466         }
1467         return 0;
1468 }
1469
1470
1471 static short bevelinside(BevList *bl1,BevList *bl2)
1472 {
1473         /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1474         /* returns '1' if correct hole  */
1475         BevPoint *bevp, *prevbevp;
1476         float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1477         int nr, links=0,rechts=0,mode;
1478
1479         /* take first vertex of possible hole */
1480
1481         bevp= (BevPoint *)(bl2+1);
1482         hvec1[0]= bevp->vec[0]; 
1483         hvec1[1]= bevp->vec[1]; 
1484         hvec1[2]= 0.0;
1485         VECCOPY(hvec2,hvec1);
1486         hvec2[0]+=1000;
1487
1488         /* test it with all edges of potential surounding poly */
1489         /* count number of transitions left-right  */
1490
1491         bevp= (BevPoint *)(bl1+1);
1492         nr= bl1->nr;
1493         prevbevp= bevp+(nr-1);
1494
1495         while(nr--) {
1496                 min= prevbevp->vec[1];
1497                 max= bevp->vec[1];
1498                 if(max<min) {
1499                         min= max;
1500                         max= prevbevp->vec[1];
1501                 }
1502                 if(min!=max) {
1503                         if(min<=hvec1[1] && max>=hvec1[1]) {
1504                                 /* there's a transition, calc intersection point */
1505                                 mode= cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
1506                                 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1507                                            only allow for one situation: we choose lab= 1.0
1508                                  */
1509                                 if(mode>=0 && lab!=0.0) {
1510                                         if(vec[0]<hvec1[0]) links++;
1511                                         else rechts++;
1512                                 }
1513                         }
1514                 }
1515                 prevbevp= bevp;
1516                 bevp++;
1517         }
1518         
1519         if( (links & 1) && (rechts & 1) ) return 1;
1520         return 0;
1521 }
1522
1523
1524 struct bevelsort {
1525         float left;
1526         BevList *bl;
1527         int dir;
1528 };
1529
1530 static int vergxcobev(const void *a1, const void *a2)
1531 {
1532         const struct bevelsort *x1=a1,*x2=a2;
1533
1534         if( x1->left > x2->left ) return 1;
1535         else if( x1->left < x2->left) return -1;
1536         return 0;
1537 }
1538
1539 /* this function cannot be replaced with atan2, but why? */
1540
1541 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1542 {
1543         float t01, t02, x3, y3;
1544
1545         t01= (float)sqrt(x1*x1+y1*y1);
1546         t02= (float)sqrt(x2*x2+y2*y2);
1547         if(t01==0.0) t01= 1.0;
1548         if(t02==0.0) t02= 1.0;
1549
1550         x1/=t01; 
1551         y1/=t01;
1552         x2/=t02; 
1553         y2/=t02;
1554
1555         t02= x1*x2+y1*y2;
1556         if(fabs(t02)>=1.0) t02= .5*M_PI;
1557         else t02= (saacos(t02))/2.0f;
1558
1559         t02= (float)sin(t02);
1560         if(t02==0.0) t02= 1.0;
1561
1562         x3= x1-x2;
1563         y3= y1-y2;
1564         if(x3==0 && y3==0) {
1565                 x3= y1;
1566                 y3= -x1;
1567         } else {
1568                 t01= (float)sqrt(x3*x3+y3*y3);
1569                 x3/=t01; 
1570                 y3/=t01;
1571         }
1572
1573         *sina= -y3/t02;
1574         *cosa= x3/t02;
1575
1576 }
1577
1578 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride)
1579 {
1580         BezTriple *pprev, *next, *last;
1581         float fac, dfac, t[4];
1582         int a;
1583         
1584         if(tilt_array==NULL && radius_array==NULL)
1585                 return;
1586         
1587         last= nu->bezt+(nu->pntsu-1);
1588         
1589         /* returns a point */
1590         if(prevbezt==nu->bezt) {
1591                 if(nu->flagu & CU_NURB_CYCLIC) pprev= last;
1592                 else pprev= prevbezt;
1593         }
1594         else pprev= prevbezt-1;
1595         
1596         /* next point */
1597         if(bezt==last) {
1598                 if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
1599                 else next= bezt;
1600         }
1601         else next= bezt+1;
1602         
1603         fac= 0.0;
1604         dfac= 1.0f/(float)resolu;
1605         
1606         for(a=0; a<resolu; a++, fac+= dfac) {
1607                 if (tilt_array) {
1608                         if (nu->tilt_interp==KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
1609                                 *tilt_array = prevbezt->alfa + (bezt->alfa - prevbezt->alfa)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1610                         } else {
1611                                 key_curve_position_weights(fac, t, nu->tilt_interp);
1612                                 *tilt_array= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1613                         }
1614                         
1615                         tilt_array = (float *)(((char *)tilt_array) + stride); 
1616                 }
1617                 
1618                 if (radius_array) {
1619                         if (nu->radius_interp==KEY_CU_EASE) {
1620                                 /* Support 2.47 ease interp
1621                                  * Note! - this only takes the 2 points into account,
1622                                  * giving much more localized results to changes in radius, sometimes you want that */
1623                                 *radius_array = prevbezt->radius + (bezt->radius - prevbezt->radius)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1624                         } else {
1625                                 
1626                                 /* reuse interpolation from tilt if we can */
1627                                 if (tilt_array==NULL || nu->tilt_interp != nu->radius_interp) {
1628                                         key_curve_position_weights(fac, t, nu->radius_interp);
1629                                 }
1630                                 *radius_array= t[0]*pprev->radius + t[1]*prevbezt->radius + t[2]*bezt->radius + t[3]*next->radius;
1631                         }
1632                         
1633                         radius_array = (float *)(((char *)radius_array) + stride); 
1634                 }
1635
1636                 if(weight_array) {
1637                         /* basic interpolation for now, could copy tilt interp too  */
1638                         *weight_array = prevbezt->weight + (bezt->weight - prevbezt->weight)*(3.0f*fac*fac - 2.0f*fac*fac*fac);
1639
1640                         weight_array = (float *)(((char *)weight_array) + stride);
1641                 }
1642         }
1643 }
1644
1645 /* make_bevel_list_3D_* funcs, at a minimum these must
1646  * fill in the bezp->quat and bezp->dir values */
1647
1648 /* correct non-cyclic cases by copying direction and rotation
1649  * values onto the first & last end-points */
1650 static void bevel_list_cyclic_fix_3D(BevList *bl)
1651 {
1652         BevPoint *bevp, *bevp1;
1653
1654         bevp= (BevPoint *)(bl+1);
1655         bevp1= bevp+1;
1656         QUATCOPY(bevp->quat, bevp1->quat);
1657         VECCOPY(bevp->dir, bevp1->dir);
1658         VECCOPY(bevp->tan, bevp1->tan);
1659         bevp= (BevPoint *)(bl+1);
1660         bevp+= (bl->nr-1);
1661         bevp1= bevp-1;
1662         QUATCOPY(bevp->quat, bevp1->quat);
1663         VECCOPY(bevp->dir, bevp1->dir);
1664         VECCOPY(bevp->tan, bevp1->tan);
1665 }
1666 /* utility for make_bevel_list_3D_* funcs */
1667 static void bevel_list_calc_bisect(BevList *bl)
1668 {
1669         BevPoint *bevp2, *bevp1, *bevp0;
1670         int nr;
1671
1672         bevp2= (BevPoint *)(bl+1);
1673         bevp1= bevp2+(bl->nr-1);
1674         bevp0= bevp1-1;
1675
1676         nr= bl->nr;
1677         while(nr--) {
1678                 /* totally simple */
1679                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1680
1681                 bevp0= bevp1;
1682                 bevp1= bevp2;
1683                 bevp2++;
1684         }
1685 }
1686 static void bevel_list_flip_tangents(BevList *bl)
1687 {
1688         BevPoint *bevp2, *bevp1, *bevp0;
1689         int nr;
1690
1691         bevp2= (BevPoint *)(bl+1);
1692         bevp1= bevp2+(bl->nr-1);
1693         bevp0= bevp1-1;
1694
1695         nr= bl->nr;
1696         while(nr--) {
1697                 if(RAD2DEG(angle_v2v2(bevp0->tan, bevp1->tan)) > 90)
1698                         negate_v3(bevp1->tan);
1699
1700                 bevp0= bevp1;
1701                 bevp1= bevp2;
1702                 bevp2++;
1703         }
1704 }
1705 /* apply user tilt */
1706 static void bevel_list_apply_tilt(BevList *bl)
1707 {
1708         BevPoint *bevp2, *bevp1, *bevp0;
1709         int nr;
1710         float q[4];
1711
1712         bevp2= (BevPoint *)(bl+1);
1713         bevp1= bevp2+(bl->nr-1);
1714         bevp0= bevp1-1;
1715
1716         nr= bl->nr;
1717         while(nr--) {
1718                 axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
1719                 mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1720                 normalize_qt(bevp1->quat);
1721
1722                 bevp0= bevp1;
1723                 bevp1= bevp2;
1724                 bevp2++;
1725         }
1726 }
1727 /* smooth quats, this function should be optimized, it can get slow with many iterations. */
1728 static void bevel_list_smooth(BevList *bl, int smooth_iter)
1729 {
1730         BevPoint *bevp2, *bevp1, *bevp0;
1731         int nr;
1732
1733         float q[4];
1734         float bevp0_quat[4];
1735         int a;
1736
1737         for(a=0; a < smooth_iter; a++) {
1738
1739                 bevp2= (BevPoint *)(bl+1);
1740                 bevp1= bevp2+(bl->nr-1);
1741                 bevp0= bevp1-1;
1742
1743                 nr= bl->nr;
1744
1745                 if(bl->poly== -1) { /* check its not cyclic */
1746                         /* skip the first point */
1747                         bevp0= bevp1;
1748                         bevp1= bevp2;
1749                         bevp2++;
1750                         nr--;
1751
1752                         bevp0= bevp1;
1753                         bevp1= bevp2;
1754                         bevp2++;
1755                         nr--;
1756
1757                 }
1758
1759                 QUATCOPY(bevp0_quat, bevp0->quat);
1760
1761                 while(nr--) {
1762                         /* interpolate quats */
1763                         float zaxis[3] = {0,0,1}, cross[3], q2[4];
1764                         interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
1765                         normalize_qt(q);
1766
1767                         mul_qt_v3(q, zaxis);
1768                         cross_v3_v3v3(cross, zaxis, bevp1->dir);
1769                         axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
1770                         normalize_qt(q2);
1771
1772                         QUATCOPY(bevp0_quat, bevp1->quat);
1773                         mul_qt_qtqt(q, q2, q);
1774                         interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
1775                         normalize_qt(bevp1->quat);
1776
1777
1778                         bevp0= bevp1;
1779                         bevp1= bevp2;
1780                         bevp2++;
1781                 }
1782         }
1783 }
1784
1785 static void make_bevel_list_3D_zup(BevList *bl)
1786 {
1787         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1788         int nr;
1789
1790         bevp2= (BevPoint *)(bl+1);
1791         bevp1= bevp2+(bl->nr-1);
1792         bevp0= bevp1-1;
1793
1794         nr= bl->nr;
1795         while(nr--) {
1796                 /* totally simple */
1797                 bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
1798                 vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1799
1800                 bevp0= bevp1;
1801                 bevp1= bevp2;
1802                 bevp2++;
1803         }
1804 }
1805
1806 static void make_bevel_list_3D_minimum_twist(BevList *bl)
1807 {
1808         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1809         int nr;
1810         float q[4];
1811
1812         float cross_tmp[3];
1813
1814         bevel_list_calc_bisect(bl);
1815
1816         bevp2= (BevPoint *)(bl+1);
1817         bevp1= bevp2+(bl->nr-1);
1818         bevp0= bevp1-1;
1819
1820         nr= bl->nr;
1821         while(nr--) {
1822
1823                 if(nr+4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
1824                         vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
1825                 }
1826                 else {
1827                         float angle= angle_normalized_v3v3(bevp0->dir, bevp1->dir);
1828
1829                         if(angle > 0.0f) { /* otherwise we can keep as is */
1830                                 cross_v3_v3v3(cross_tmp, bevp0->dir, bevp1->dir);
1831                                 axis_angle_to_quat(q, cross_tmp, angle);
1832                                 mul_qt_qtqt(bevp1->quat, q, bevp0->quat);
1833                         }
1834                         else {
1835                                 QUATCOPY(bevp1->quat, bevp0->quat);
1836                         }
1837                 }
1838
1839                 bevp0= bevp1;
1840                 bevp1= bevp2;
1841                 bevp2++;
1842         }
1843
1844         if(bl->poly != -1) { /* check for cyclic */
1845
1846                 /* Need to correct for the start/end points not matching
1847                  * do this by calculating the tilt angle difference, then apply
1848                  * the rotation gradually over the entire curve
1849                  *
1850                  * note that the split is between last and second last, rather then first/last as youd expect.
1851                  *
1852                  * real order is like this
1853                  * 0,1,2,3,4 --> 1,2,3,4,0
1854                  *
1855                  * this is why we compare last with second last
1856                  * */
1857                 float vec_1[3]= {0,1,0}, vec_2[3]= {0,1,0}, angle, ang_fac, cross_tmp[3];
1858
1859                 BevPoint *bevp_first;
1860                 BevPoint *bevp_last;
1861
1862
1863                 bevp_first= (BevPoint *)(bl+1);
1864                 bevp_first+= bl->nr-1;
1865                 bevp_last = bevp_first;
1866                 bevp_last--;
1867
1868                 /* quats and vec's are normalized, should not need to re-normalize */
1869                 mul_qt_v3(bevp_first->quat, vec_1);
1870                 mul_qt_v3(bevp_last->quat, vec_2);
1871                 normalize_v3(vec_1);
1872                 normalize_v3(vec_2);
1873
1874                 /* align the vector, can avoid this and it looks 98% OK but
1875                  * better to align the angle quat roll's before comparing */
1876                 {
1877                         cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
1878                         angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
1879                         axis_angle_to_quat(q, cross_tmp, angle);
1880                         mul_qt_v3(q, vec_2);
1881                 }
1882
1883                 angle= angle_normalized_v3v3(vec_1, vec_2);
1884
1885                 /* flip rotation if needs be */
1886                 cross_v3_v3v3(cross_tmp, vec_1, vec_2);
1887                 normalize_v3(cross_tmp);
1888                 if(angle_normalized_v3v3(bevp_first->dir, cross_tmp) < 90/(180.0/M_PI))
1889                         angle = -angle;
1890
1891                 bevp2= (BevPoint *)(bl+1);
1892                 bevp1= bevp2+(bl->nr-1);
1893                 bevp0= bevp1-1;
1894
1895                 nr= bl->nr;
1896                 while(nr--) {
1897                         ang_fac= angle * (1.0f-((float)nr/bl->nr)); /* also works */
1898
1899                         axis_angle_to_quat(q, bevp1->dir, ang_fac);
1900                         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
1901
1902                         bevp0= bevp1;
1903                         bevp1= bevp2;
1904                         bevp2++;
1905                 }
1906         }
1907 }
1908
1909 static void make_bevel_list_3D_tangent(BevList *bl)
1910 {
1911         BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
1912         int nr;
1913
1914         float bevp0_tan[3], cross_tmp[3];
1915
1916         bevel_list_calc_bisect(bl);
1917         if(bl->poly== -1) /* check its not cyclic */
1918                 bevel_list_cyclic_fix_3D(bl); // XXX - run this now so tangents will be right before doing the flipping
1919         bevel_list_flip_tangents(bl);
1920
1921         /* correct the tangents */
1922         bevp2= (BevPoint *)(bl+1);
1923         bevp1= bevp2+(bl->nr-1);
1924         bevp0= bevp1-1;
1925
1926         nr= bl->nr;
1927         while(nr--) {
1928
1929                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1930                 cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
1931                 normalize_v3(bevp1->tan);
1932
1933                 bevp0= bevp1;
1934                 bevp1= bevp2;
1935                 bevp2++;
1936         }
1937
1938
1939         /* now for the real twist calc */
1940         bevp2= (BevPoint *)(bl+1);
1941         bevp1= bevp2+(bl->nr-1);
1942         bevp0= bevp1-1;
1943
1944         VECCOPY(bevp0_tan, bevp0->tan);
1945
1946         nr= bl->nr;
1947         while(nr--) {
1948
1949                 /* make perpendicular, modify tan in place, is ok */
1950                 float cross_tmp[3];
1951                 float zero[3] = {0,0,0};
1952
1953                 cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
1954                 normalize_v3(cross_tmp);
1955                 tri_to_quat( bevp1->quat,zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
1956
1957                 bevp0= bevp1;
1958                 bevp1= bevp2;
1959                 bevp2++;
1960         }
1961 }
1962
1963 static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
1964 {
1965         switch(twist_mode) {
1966         case CU_TWIST_TANGENT:
1967                 make_bevel_list_3D_tangent(bl);
1968                 break;
1969         case CU_TWIST_MINIMUM:
1970                 make_bevel_list_3D_minimum_twist(bl);
1971                 break;
1972         default: /* CU_TWIST_Z_UP default, pre 2.49c */
1973                 make_bevel_list_3D_zup(bl);
1974         }
1975
1976         if(bl->poly== -1) /* check its not cyclic */
1977                 bevel_list_cyclic_fix_3D(bl);
1978
1979         if(smooth_iter)
1980                 bevel_list_smooth(bl, smooth_iter);
1981
1982         bevel_list_apply_tilt(bl);
1983 }
1984
1985
1986
1987 /* only for 2 points */
1988 static void make_bevel_list_segment_3D(BevList *bl)
1989 {
1990         float q[4];
1991
1992         BevPoint *bevp2= (BevPoint *)(bl+1);
1993         BevPoint *bevp1= bevp2+1;
1994
1995         /* simple quat/dir */
1996         sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
1997         normalize_v3(bevp1->dir);
1998
1999         vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2000
2001         axis_angle_to_quat(q, bevp1->dir, bevp1->alfa);
2002         mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
2003         normalize_qt(bevp1->quat);
2004         VECCOPY(bevp2->dir, bevp1->dir);
2005         QUATCOPY(bevp2->quat, bevp1->quat);
2006 }
2007
2008
2009
2010 void makeBevelList(Object *ob)
2011 {
2012         /*
2013          - convert all curves to polys, with indication of resol and flags for double-vertices
2014          - possibly; do a smart vertice removal (in case Nurb)
2015          - separate in individual blicks with BoundBox
2016          - AutoHole detection
2017         */
2018         Curve *cu;
2019         Nurb *nu;
2020         BezTriple *bezt, *prevbezt;
2021         BPoint *bp;
2022         BevList *bl, *blnew, *blnext;
2023         BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
2024         float min, inp, x1, x2, y1, y2;
2025         struct bevelsort *sortdata, *sd, *sd1;
2026         int a, b, nr, poly, resolu = 0, len = 0;
2027         int do_tilt, do_radius, do_weight;
2028         
2029         /* this function needs an object, because of tflag and upflag */
2030         cu= ob->data;
2031
2032         /* do we need to calculate the radius for each point? */
2033         /* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
2034         
2035         /* STEP 1: MAKE POLYS  */
2036
2037         BLI_freelistN(&(cu->bev));
2038         if(cu->editnurb && ob->type!=OB_FONT) {
2039                 ListBase *nurbs= ED_curve_editnurbs(cu);
2040                 nu= nurbs->first;
2041         } else nu= cu->nurb.first;
2042         
2043         while(nu) {
2044                 
2045                 /* check if we will calculate tilt data */
2046                 do_tilt = CU_DO_TILT(cu, nu);
2047                 do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
2048                 do_weight = 1;
2049                 
2050                 /* check we are a single point? also check we are not a surface and that the orderu is sane,
2051                  * enforced in the UI but can go wrong possibly */
2052                 if(!check_valid_nurb_u(nu)) {
2053                         bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList1");
2054                         BLI_addtail(&(cu->bev), bl);
2055                         bl->nr= 0;
2056                 } else {
2057                         if(G.rendering && cu->resolu_ren!=0) 
2058                                 resolu= cu->resolu_ren;
2059                         else
2060                                 resolu= nu->resolu;
2061                         
2062                         if(nu->type == CU_POLY) {
2063                                 len= nu->pntsu;
2064                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList2");
2065                                 BLI_addtail(&(cu->bev), bl);
2066         
2067                                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2068                                 else bl->poly= -1;
2069                                 bl->nr= len;
2070                                 bl->dupe_nr= 0;
2071                                 bevp= (BevPoint *)(bl+1);
2072                                 bp= nu->bp;
2073         
2074                                 while(len--) {
2075                                         VECCOPY(bevp->vec, bp->vec);
2076                                         bevp->alfa= bp->alfa;
2077                                         bevp->radius= bp->radius;
2078                                         bevp->weight= bp->weight;
2079                                         bevp->split_tag= TRUE;
2080                                         bevp++;
2081                                         bp++;
2082                                 }
2083                         }
2084                         else if(nu->type == CU_BEZIER) {
2085         
2086                                 len= resolu*(nu->pntsu+ (nu->flagu & CU_NURB_CYCLIC) -1)+1;     /* in case last point is not cyclic */
2087                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelBPoints");
2088                                 BLI_addtail(&(cu->bev), bl);
2089         
2090                                 if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2091                                 else bl->poly= -1;
2092                                 bevp= (BevPoint *)(bl+1);
2093         
2094                                 a= nu->pntsu-1;
2095                                 bezt= nu->bezt;
2096                                 if(nu->flagu & CU_NURB_CYCLIC) {
2097                                         a++;
2098                                         prevbezt= nu->bezt+(nu->pntsu-1);
2099                                 }
2100                                 else {
2101                                         prevbezt= bezt;
2102                                         bezt++;
2103                                 }
2104                                 
2105                                 while(a--) {
2106                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
2107
2108                                                 VECCOPY(bevp->vec, prevbezt->vec[1]);
2109                                                 bevp->alfa= prevbezt->alfa;
2110                                                 bevp->radius= prevbezt->radius;
2111                                                 bevp->weight= prevbezt->weight;
2112                                                 bevp->split_tag= TRUE;
2113                                                 bevp->dupe_tag= FALSE;
2114                                                 bevp++;
2115                                                 bl->nr++;
2116                                                 bl->dupe_nr= 1;
2117                                         }
2118                                         else {
2119                                                 /* always do all three, to prevent data hanging around */
2120                                                 int j;
2121                                                 
2122                                                 /* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
2123                                                 for(j=0; j<3; j++) {
2124                                                         forward_diff_bezier(    prevbezt->vec[1][j],    prevbezt->vec[2][j],
2125                                                                                                         bezt->vec[0][j],                bezt->vec[1][j],
2126                                                                                                         &(bevp->vec[j]), resolu, sizeof(BevPoint));
2127                                                 }
2128                                                 
2129                                                 /* if both arrays are NULL do nothiong */
2130                                                 alfa_bezpart(   prevbezt, bezt, nu,
2131                                                                                  do_tilt        ? &bevp->alfa : NULL,
2132                                                                                  do_radius      ? &bevp->radius : NULL,
2133                                                                                  do_weight      ? &bevp->weight : NULL,
2134                                                                                  resolu, sizeof(BevPoint));
2135
2136                                                 
2137                                                 if(cu->twist_mode==CU_TWIST_TANGENT) {
2138                                                         forward_diff_bezier_cotangent(
2139                                                                                                         prevbezt->vec[1],       prevbezt->vec[2],
2140                                                                                                         bezt->vec[0],           bezt->vec[1],
2141                                                                                                         bevp->tan, resolu, sizeof(BevPoint));
2142                                                 }
2143
2144                                                 /* indicate with handlecodes double points */
2145                                                 if(prevbezt->h1==prevbezt->h2) {
2146                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2147                                                 }
2148                                                 else {
2149                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->split_tag= TRUE;
2150                                                         else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->split_tag= TRUE;
2151                                                 }
2152                                                 bl->nr+= resolu;
2153                                                 bevp+= resolu;
2154                                         }
2155                                         prevbezt= bezt;
2156                                         bezt++;
2157                                 }
2158                                 
2159                                 if((nu->flagu & CU_NURB_CYCLIC)==0) {       /* not cyclic: endpoint */
2160                                         VECCOPY(bevp->vec, prevbezt->vec[1]);
2161                                         bevp->alfa= prevbezt->alfa;
2162                                         bevp->radius= prevbezt->radius;
2163                                         bevp->weight= prevbezt->weight;
2164                                         bl->nr++;
2165                                 }
2166                         }
2167                         else if(nu->type == CU_NURBS) {
2168                                 if(nu->pntsv==1) {
2169                                         len= (resolu*SEGMENTSU(nu));
2170                                         
2171                                         bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
2172                                         BLI_addtail(&(cu->bev), bl);
2173                                         bl->nr= len;
2174                                         bl->dupe_nr= 0;
2175                                         if(nu->flagu & CU_NURB_CYCLIC) bl->poly= 0;
2176                                         else bl->poly= -1;
2177                                         bevp= (BevPoint *)(bl+1);
2178                                         
2179                                         makeNurbcurve(  nu, &bevp->vec[0],
2180                                                                         do_tilt         ? &bevp->alfa : NULL,
2181                                                                         do_radius       ? &bevp->radius : NULL,
2182                                                                         do_weight       ? &bevp->weight : NULL,
2183                                                                         resolu, sizeof(BevPoint));
2184                                 }
2185                         }
2186                 }
2187                 nu= nu->next;
2188         }
2189
2190         /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
2191         bl= cu->bev.first;
2192         while(bl) {
2193                 if (bl->nr) { /* null bevel items come from single points */
2194                         nr= bl->nr;
2195                         bevp1= (BevPoint *)(bl+1);
2196                         bevp0= bevp1+(nr-1);
2197                         nr--;
2198                         while(nr--) {
2199                                 if( fabs(bevp0->vec[0]-bevp1->vec[0])<0.00001 ) {
2200                                         if( fabs(bevp0->vec[1]-bevp1->vec[1])<0.00001 ) {
2201                                                 if( fabs(bevp0->vec[2]-bevp1->vec[2])<0.00001 ) {
2202                                                         bevp0->dupe_tag= TRUE;
2203                                                         bl->dupe_nr++;
2204                                                 }
2205                                         }
2206                                 }
2207                                 bevp0= bevp1;
2208                                 bevp1++;
2209                         }
2210                 }
2211                 bl= bl->next;
2212         }
2213         bl= cu->bev.first;
2214         while(bl) {
2215                 blnext= bl->next;
2216                 if(bl->nr && bl->dupe_nr) {
2217                         nr= bl->nr- bl->dupe_nr+1;      /* +1 because vectorbezier sets flag too */
2218                         blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList4");
2219                         memcpy(blnew, bl, sizeof(BevList));
2220                         blnew->nr= 0;
2221                         BLI_remlink(&(cu->bev), bl);
2222                         BLI_insertlinkbefore(&(cu->bev),blnext,blnew);  /* to make sure bevlijst is tuned with nurblist */
2223                         bevp0= (BevPoint *)(bl+1);
2224                         bevp1= (BevPoint *)(blnew+1);
2225                         nr= bl->nr;
2226                         while(nr--) {
2227                                 if(bevp0->dupe_tag==0) {
2228                                         memcpy(bevp1, bevp0, sizeof(BevPoint));
2229                                         bevp1++;
2230                                         blnew->nr++;
2231                                 }
2232                                 bevp0++;
2233                         }
2234                         MEM_freeN(bl);
2235                         blnew->dupe_nr= 0;
2236                 }
2237                 bl= blnext;
2238         }
2239
2240         /* STEP 3: POLYS COUNT AND AUTOHOLE */
2241         bl= cu->bev.first;
2242         poly= 0;
2243         while(bl) {
2244                 if(bl->nr && bl->poly>=0) {
2245                         poly++;
2246                         bl->poly= poly;
2247                         bl->hole= 0;
2248                 }
2249                 bl= bl->next;
2250         }
2251         
2252
2253         /* find extreme left points, also test (turning) direction */
2254         if(poly>0) {
2255                 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
2256                 bl= cu->bev.first;
2257                 while(bl) {
2258                         if(bl->poly>0) {
2259
2260                                 min= 300000.0;
2261                                 bevp= (BevPoint *)(bl+1);
2262                                 nr= bl->nr;
2263                                 while(nr--) {
2264                                         if(min>bevp->vec[0]) {
2265                                                 min= bevp->vec[0];
2266                                                 bevp1= bevp;
2267                                         }
2268                                         bevp++;
2269                                 }
2270                                 sd->bl= bl;
2271                                 sd->left= min;
2272
2273                                 bevp= (BevPoint *)(bl+1);
2274                                 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
2275                                 else bevp0= bevp1-1;
2276                                 bevp= bevp+ (bl->nr-1);
2277                                 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
2278                                 else bevp2= bevp1+1;
2279
2280                                 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]);
2281
2282                                 if(inp>0.0) sd->dir= 1;
2283                                 else sd->dir= 0;
2284
2285                                 sd++;
2286                         }
2287
2288                         bl= bl->next;
2289                 }
2290                 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
2291
2292                 sd= sortdata+1;
2293                 for(a=1; a<poly; a++, sd++) {
2294                         bl= sd->bl;         /* is bl a hole? */
2295                         sd1= sortdata+ (a-1);
2296                         for(b=a-1; b>=0; b--, sd1--) {  /* all polys to the left */
2297                                 if(bevelinside(sd1->bl, bl)) {
2298                                         bl->hole= 1- sd1->bl->hole;
2299                                         break;
2300                                 }
2301                         }
2302                 }
2303
2304                 /* turning direction */
2305                 if((cu->flag & CU_3D)==0) {
2306                         sd= sortdata;
2307                         for(a=0; a<poly; a++, sd++) {
2308                                 if(sd->bl->hole==sd->dir) {
2309                                         bl= sd->bl;
2310                                         bevp1= (BevPoint *)(bl+1);
2311                                         bevp2= bevp1+ (bl->nr-1);
2312                                         nr= bl->nr/2;
2313                                         while(nr--) {
2314                                                 SWAP(BevPoint, *bevp1, *bevp2);
2315                                                 bevp1++;
2316                                                 bevp2--;
2317                                         }
2318                                 }
2319                         }
2320                 }
2321                 MEM_freeN(sortdata);
2322         }
2323
2324         /* STEP 4: 2D-COSINES or 3D ORIENTATION */
2325         if((cu->flag & CU_3D)==0) {
2326                 /* note: bevp->dir and bevp->quat are not needed for beveling but are
2327                  * used when making a path from a 2D curve, therefor they need to be set - Campbell */
2328                 bl= cu->bev.first;
2329                 while(bl) {
2330
2331                         if(bl->nr < 2) {
2332                                 /* do nothing */
2333                         }
2334                         else if(bl->nr==2) {    /* 2 pnt, treat separate */
2335                                 bevp2= (BevPoint *)(bl+1);
2336                                 bevp1= bevp2+1;
2337
2338                                 x1= bevp1->vec[0]- bevp2->vec[0];
2339                                 y1= bevp1->vec[1]- bevp2->vec[1];
2340
2341                                 calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
2342                                 bevp2->sina= bevp1->sina;
2343                                 bevp2->cosa= bevp1->cosa;
2344
2345                                 /* fill in dir & quat */
2346                                 make_bevel_list_segment_3D(bl);
2347                         }
2348                         else {
2349                                 bevp2= (BevPoint *)(bl+1);
2350                                 bevp1= bevp2+(bl->nr-1);
2351                                 bevp0= bevp1-1;
2352
2353                                 nr= bl->nr;
2354                                 while(nr--) {
2355                                         x1= bevp1->vec[0]- bevp0->vec[0];
2356                                         x2= bevp1->vec[0]- bevp2->vec[0];
2357                                         y1= bevp1->vec[1]- bevp0->vec[1];
2358                                         y2= bevp1->vec[1]- bevp2->vec[1];
2359
2360                                         calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
2361
2362                                         /* from: make_bevel_list_3D_zup, could call but avoid a second loop.
2363                                          * no need for tricky tilt calculation as with 3D curves */
2364                                         bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
2365                                         vec_to_quat( bevp1->quat,bevp1->dir, 5, 1);
2366                                         /* done with inline make_bevel_list_3D_zup */
2367
2368                                         bevp0= bevp1;
2369                                         bevp1= bevp2;
2370                                         bevp2++;
2371                                 }
2372
2373                                 /* correct non-cyclic cases */
2374                                 if(bl->poly== -1) {
2375                                         bevp= (BevPoint *)(bl+1);
2376                                         bevp1= bevp+1;
2377                                         bevp->sina= bevp1->sina;
2378                                         bevp->cosa= bevp1->cosa;
2379                                         bevp= (BevPoint *)(bl+1);
2380                                         bevp+= (bl->nr-1);
2381                                         bevp1= bevp-1;
2382                                         bevp->sina= bevp1->sina;
2383                                         bevp->cosa= bevp1->cosa;
2384
2385                                         /* correct for the dir/quat, see above why its needed */
2386                                         bevel_list_cyclic_fix_3D(bl);
2387                                 }
2388                         }
2389                         bl= bl->next;
2390                 }
2391         }
2392         else { /* 3D Curves */
2393                 bl= cu->bev.first;
2394                 while(bl) {
2395
2396                         if(bl->nr < 2) {
2397                                 /* do nothing */
2398                         }
2399                         else if(bl->nr==2) {    /* 2 pnt, treat separate */
2400                                 make_bevel_list_segment_3D(bl);
2401                         }
2402                         else {
2403                                 make_bevel_list_3D(bl, (int)(resolu*cu->twist_smooth), cu->twist_mode);
2404                         }
2405                         bl= bl->next;
2406                 }
2407         }
2408 }
2409
2410 /* ****************** HANDLES ************** */
2411
2412 /*
2413  *   handlecodes:
2414  *              0: nothing,  1:auto,  2:vector,  3:aligned
2415  */
2416
2417 /* mode: is not zero when FCurve, is 2 when forced horizontal for autohandles */
2418 void calchandleNurb(BezTriple *bezt, BezTriple *prev, BezTriple *next, int mode)
2419 {
2420         float *p1,*p2,*p3, pt[3];
2421         float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
2422
2423         if(bezt->h1==0 && bezt->h2==0) return;
2424
2425         p2= bezt->vec[1];
2426
2427         if(prev==0) {
2428                 p3= next->vec[1];
2429                 pt[0]= 2*p2[0]- p3[0];
2430                 pt[1]= 2*p2[1]- p3[1];
2431                 pt[2]= 2*p2[2]- p3[2];
2432                 p1= pt;
2433         }
2434         else p1= prev->vec[1];
2435
2436         if(next==0) {
2437                 pt[0]= 2*p2[0]- p1[0];
2438                 pt[1]= 2*p2[1]- p1[1];
2439                 pt[2]= 2*p2[2]- p1[2];
2440                 p3= pt;
2441         }
2442         else p3= next->vec[1];
2443
2444         dx= p2[0]- p1[0];
2445         dy= p2[1]- p1[1];
2446         dz= p2[2]- p1[2];
2447         
2448         if(mode) len1= dx;
2449         else len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
2450         
2451         dx1= p3[0]- p2[0];
2452         dy1= p3[1]- p2[1];
2453         dz1= p3[2]- p2[2];
2454         
2455         if(mode) len2= dx1;
2456         else len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
2457
2458         if(len1==0.0f) len1=1.0f;
2459         if(len2==0.0f) len2=1.0f;
2460
2461
2462         if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) {    /* auto */
2463                 vx= dx1/len2 + dx/len1;
2464                 vy= dy1/len2 + dy/len1;
2465                 vz= dz1/len2 + dz/len1;
2466                 len= 2.5614f*(float)sqrt(vx*vx + vy*vy + vz*vz);
2467                 if(len!=0.0f) {
2468                         int leftviolate=0, rightviolate=0;      /* for mode==2 */
2469                         
2470                         if(len1>5.0f*len2) len1= 5.0f*len2;     
2471                         if(len2>5.0f*len1) len2= 5.0f*len1;
2472                         
2473                         if(bezt->h1==HD_AUTO) {
2474                                 len1/=len;
2475                                 *(p2-3)= *p2-vx*len1;
2476                                 *(p2-2)= *(p2+1)-vy*len1;
2477                                 *(p2-1)= *(p2+2)-vz*len1;
2478                                 
2479                                 if(mode==2 && next && prev) {   // keep horizontal if extrema
2480                                         float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2481                                         float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2482                                         if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2483                                                 bezt->vec[0][1]= bezt->vec[1][1];
2484                                         }
2485                                         else {                                          // handles should not be beyond y coord of two others
2486                                                 if(ydiff1<=0.0) { 
2487                                                         if(prev->vec[1][1] > bezt->vec[0][1]) {
2488                                                                 bezt->vec[0][1]= prev->vec[1][1]; 
2489                                                                 leftviolate= 1;
2490                                                         }
2491                                                 }
2492                                                 else {
2493                                                         if(prev->vec[1][1] < bezt->vec[0][1]) {
2494                                                                 bezt->vec[0][1]= prev->vec[1][1]; 
2495                                                                 leftviolate= 1;
2496                                                         }
2497                                                 }
2498                                         }
2499                                 }
2500                         }
2501                         if(bezt->h2==HD_AUTO) {
2502                                 len2/=len;
2503                                 *(p2+3)= *p2+vx*len2;
2504                                 *(p2+4)= *(p2+1)+vy*len2;
2505                                 *(p2+5)= *(p2+2)+vz*len2;
2506                                 
2507                                 if(mode==2 && next && prev) {   // keep horizontal if extrema
2508                                         float ydiff1= prev->vec[1][1] - bezt->vec[1][1];
2509                                         float ydiff2= next->vec[1][1] - bezt->vec[1][1];
2510                                         if( (ydiff1<=0.0 && ydiff2<=0.0) || (ydiff1>=0.0 && ydiff2>=0.0) ) {
2511                                                 bezt->vec[2][1]= bezt->vec[1][1];
2512                                         }
2513                                         else {                                          // handles should not be beyond y coord of two others
2514                                                 if(ydiff1<=0.0) { 
2515                                                         if(next->vec[1][1] < bezt->vec[2][1]) {
2516                                                                 bezt->vec[2][1]= next->vec[1][1]; 
2517                                                                 rightviolate= 1;
2518                                                         }
2519                                                 }
2520                                                 else {
2521                                                         if(next->vec[1][1] > bezt->vec[2][1]) {
2522                                                                 bezt->vec[2][1]= next->vec[1][1]; 
2523                                                                 rightviolate= 1;
2524                                                         }
2525                                                 }
2526                                         }
2527                                 }
2528                         }
2529                         if(leftviolate || rightviolate) {       /* align left handle */
2530                                 float h1[3], h2[3];
2531                                 
2532                                 sub_v3_v3v3(h1, p2-3, p2);
2533                                 sub_v3_v3v3(h2, p2, p2+3);
2534                                 len1= normalize_v3(h1);
2535                                 len2= normalize_v3(h2);
2536                                 
2537                                 vz= INPR(h1, h2);
2538                                 
2539                                 if(leftviolate) {
2540                                         *(p2+3)= *(p2)   - vz*len2*h1[0];
2541                                         *(p2+4)= *(p2+1) - vz*len2*h1[1];
2542                                         *(p2+5)= *(p2+2) - vz*len2*h1[2];
2543                                 }
2544                                 else {
2545                                         *(p2-3)= *(p2)   + vz*len1*h2[0];
2546                                         *(p2-2)= *(p2+1) + vz*len1*h2[1];
2547                                         *(p2-1)= *(p2+2) + vz*len1*h2[2];
2548                                 }
2549                         }
2550                         
2551                 }
2552         }
2553
2554         if(bezt->h1==HD_VECT) { /* vector */
2555                 dx/=3.0; 
2556                 dy/=3.0; 
2557                 dz/=3.0;
2558                 *(p2-3)= *p2-dx;
2559                 *(p2-2)= *(p2+1)-dy;
2560                 *(p2-1)= *(p2+2)-dz;
2561         }
2562         if(bezt->h2==HD_VECT) {
2563                 dx1/=3.0; 
2564                 dy1/=3.0; 
2565                 dz1/=3.0;
2566                 *(p2+3)= *p2+dx1;
2567                 *(p2+4)= *(p2+1)+dy1;
2568                 *(p2+5)= *(p2+2)+dz1;
2569         }
2570
2571         len2= len_v3v3(p2, p2+3);
2572         len1= len_v3v3(p2, p2-3);
2573         if(len1==0.0) len1=1.0;
2574         if(len2==0.0) len2=1.0;
2575
2576         if(bezt->f1 & SELECT) { /* order of calculation */
2577                 if(bezt->h2==HD_ALIGN) {        /* aligned */
2578                         len= len2/len1;
2579                         p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2580                         p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2581                         p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2582                 }
2583                 if(bezt->h1==HD_ALIGN) {
2584                         len= len1/len2;
2585                         p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2586                         p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2587                         p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2588                 }
2589         }
2590         else {
2591                 if(bezt->h1==HD_ALIGN) {
2592                         len= len1/len2;
2593                         p2[-3]= p2[0]+len*(p2[0]-p2[3]);
2594                         p2[-2]= p2[1]+len*(p2[1]-p2[4]);
2595                         p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2596                 }
2597                 if(bezt->h2==HD_ALIGN) {        /* aligned */
2598                         len= len2/len1;
2599                         p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2600                         p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2601                         p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2602                 }
2603         }
2604 }
2605
2606 void calchandlesNurb(Nurb *nu) /* first, if needed, set handle flags */
2607 {
2608         BezTriple *bezt, *prev, *next;
2609         short a;
2610
2611         if(nu->type != CU_BEZIER) return;
2612         if(nu->pntsu<2) return;
2613         
2614         a= nu->pntsu;
2615         bezt= nu->bezt;
2616         if(nu->flagu & CU_NURB_CYCLIC) prev= bezt+(a-1);
2617         else prev= 0;
2618         next= bezt+1;
2619
2620         while(a--) {
2621                 calchandleNurb(bezt, prev, next, 0);
2622                 prev= bezt;
2623                 if(a==1) {
2624                         if(nu->flagu & CU_NURB_CYCLIC) next= nu->bezt;
2625                         else next= 0;
2626                 }
2627                 else next++;
2628
2629                 bezt++;
2630         }
2631 }
2632
2633
2634 void testhandlesNurb(Nurb *nu)
2635 {
2636         /* use when something has changed with handles.
2637         it treats all BezTriples with the following rules:
2638         PHASE 1: do types have to be altered?
2639            Auto handles: become aligned when selection status is NOT(000 || 111)
2640            Vector handles: become 'nothing' when (one half selected AND other not)
2641         PHASE 2: recalculate handles
2642         */
2643         BezTriple *bezt;
2644         short flag, a;
2645
2646         if(nu->type != CU_BEZIER) return;
2647
2648         bezt= nu->bezt;
2649         a= nu->pntsu;
2650         while(a--) {
2651                 flag= 0;
2652                 if(bezt->f1 & SELECT) flag++;
2653                 if(bezt->f2 & SELECT) flag += 2;
2654                 if(bezt->f3 & SELECT) flag += 4;
2655
2656                 if( !(flag==0 || flag==7) ) {
2657                         if(bezt->h1==HD_AUTO) {   /* auto */
2658                                 bezt->h1= HD_ALIGN;
2659                         }
2660                         if(bezt->h2==HD_AUTO) {   /* auto */
2661                                 bezt->h2= HD_ALIGN;
2662                         }
2663
2664                         if(bezt->h1==HD_VECT) {   /* vector */
2665                                 if(flag < 4) bezt->h1= 0;
2666                         }
2667                         if(bezt->h2==HD_VECT) {   /* vector */
2668                                 if( flag > 3) bezt->h2= 0;
2669                         }
2670                 }
2671                 bezt++;
2672         }
2673
2674         calchandlesNurb(nu);
2675 }
2676
2677 void autocalchandlesNurb(Nurb *nu, int flag)
2678 {
2679         /* checks handle coordinates and calculates type */
2680         
2681         BezTriple *bezt2, *bezt1, *bezt0;
2682         int i, align, leftsmall, rightsmall;
2683
2684         if(nu==0 || nu->bezt==0) return;
2685         
2686         bezt2 = nu->bezt;
2687         bezt1 = bezt2 + (nu->pntsu-1);
2688         bezt0 = bezt1 - 1;
2689         i = nu->pntsu;
2690
2691         while(i--) {
2692                 
2693                 align= leftsmall= rightsmall= 0;
2694                 
2695                 /* left handle: */
2696                 if(flag==0 || (bezt1->f1 & flag) ) {
2697                         bezt1->h1= 0;
2698                         /* distance too short: vectorhandle */
2699                         if( len_v3v3( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2700                                 bezt1->h1= HD_VECT;
2701                                 leftsmall= 1;
2702                         }
2703                         else {
2704                                 /* aligned handle? */
2705                                 if(dist_to_line_v2(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2706                                         align= 1;
2707                                         bezt1->h1= HD_ALIGN;
2708                                 }
2709                                 /* or vector handle? */
2710                                 if(dist_to_line_v2(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2711                                         bezt1->h1= HD_VECT;
2712                                 
2713                         }
2714                 }
2715                 /* right handle: */
2716                 if(flag==0 || (bezt1->f3 & flag) ) {
2717                         bezt1->h2= 0;
2718                         /* distance too short: vectorhandle */
2719                         if( len_v3v3( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2720                                 bezt1->h2= HD_VECT;
2721                                 rightsmall= 1;
2722                         }
2723                         else {
2724                                 /* aligned handle? */
2725                                 if(align) bezt1->h2= HD_ALIGN;
2726
2727                                 /* or vector handle? */
2728                                 if(dist_to_line_v2(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2729                                         bezt1->h2= HD_VECT;
2730                                 
2731                         }
2732                 }
2733                 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2734                 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2735                 
2736                 /* undesired combination: */
2737                 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2738                 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2739                 
2740                 bezt0= bezt1;
2741                 bezt1= bezt2;
2742                 bezt2++;
2743         }
2744
2745         calchandlesNurb(nu);
2746 }
2747
2748 void autocalchandlesNurb_all(ListBase *editnurb, int flag)
2749 {
2750         Nurb *nu;
2751         
2752         nu= editnurb->first;
2753         while(nu) {
2754                 autocalchandlesNurb(nu, flag);
2755                 nu= nu->next;
2756         }
2757 }
2758
2759 void sethandlesNurb(ListBase *editnurb, short code)
2760 {
2761         /* code==1: set autohandle */
2762         /* code==2: set vectorhandle */
2763         /* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
2764         /* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
2765         /* code==5: Set align, like 3 but no toggle */
2766         /* code==6: Clear align, like 3 but no toggle */
2767         Nurb *nu;
2768         BezTriple *bezt;
2769         short a, ok=0;
2770
2771         if(code==1 || code==2) {
2772                 nu= editnurb->first;
2773                 while(nu) {
2774                         if(nu->type == CU_BEZIER) {
2775                                 bezt= nu->bezt;
2776                                 a= nu->pntsu;
2777                                 while(a--) {
2778                                         if((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
2779                                                 if(bezt->f1 & SELECT) bezt->h1= code;
2780                                                 if(bezt->f3 & SELECT) bezt->h2= code;
2781                                                 if(bezt->h1!=bezt->h2) {
2782                                                         if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2783                                                         if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2784                                                 }
2785                                         }
2786                                         bezt++;
2787                                 }
2788                                 calchandlesNurb(nu);
2789                         }
2790                         nu= nu->next;
2791                 }
2792         }
2793         else {
2794                 /* there is 1 handle not FREE: FREE it all, else make ALIGNED  */
2795                 
2796                 nu= editnurb->first;
2797                 if (code == 5) {
2798                         ok = HD_ALIGN;
2799                 } else if (code == 6) {
2800                         ok = HD_FREE;
2801                 } else {
2802                         /* Toggle */
2803                         while(nu) {
2804                                 if(nu->type == CU_BEZIER) {
2805                                         bezt= nu->bezt;
2806                                         a= nu->pntsu;
2807                                         while(a--) {
2808                                                 if((bezt->f1 & SELECT) && bezt->h1) ok= 1;
2809                                                 if((bezt->f3 & SELECT) && bezt->h2) ok= 1;
2810                                                 if(ok) break;
2811                                                 bezt++;
2812                                         }
2813                                 }
2814                                 nu= nu->next;
2815                         }
2816                         if(ok) ok= HD_FREE;
2817                         else ok= HD_ALIGN;
2818                 }
2819                 nu= editnurb->first;
2820                 while(nu) {
2821                         if(nu->type == CU_BEZIER) {
2822                                 bezt= nu->bezt;
2823                                 a= nu->pntsu;
2824                                 while(a--) {
2825                                         if(bezt->f1 & SELECT) bezt->h1= ok;
2826                                         if(bezt->f3 & SELECT) bezt->h2= ok;
2827         
2828                                         bezt++;
2829                                 }
2830                                 calchandlesNurb(nu);
2831                         }
2832                         nu= nu->next;
2833                 }
2834         }
2835 }
2836
2837 static void swapdata(void *adr1, void *adr2, int len)
2838 {
2839
2840         if(len<=0) return;
2841
2842         if(len<65) {
2843                 char adr[64];
2844
2845                 memcpy(adr, adr1, len);
2846                 memcpy(adr1, adr2, len);
2847                 memcpy(adr2, adr, len);
2848         }
2849         else {
2850                 char *adr;
2851
2852                 adr= (char *)MEM_mallocN(len, "curve swap");
2853                 memcpy(adr, adr1, len);
2854                 memcpy(adr1, adr2, len);
2855                 memcpy(adr2, adr, len);
2856                 MEM_freeN(adr);
2857         }
2858 }
2859
2860 void switchdirectionNurb(Nurb *nu)
2861 {
2862         BezTriple *bezt1, *bezt2;
2863         BPoint *bp1, *bp2;
2864         float *fp1, *fp2, *tempf;
2865         int a, b;
2866
2867         if(nu->pntsu==1 && nu->pntsv==1) return;
2868
2869         if(nu->type == CU_BEZIER) {
2870                 a= nu->pntsu;
2871                 bezt1= nu->bezt;
2872                 bezt2= bezt1+(a-1);
2873                 if(a & 1) a+= 1;        /* if odd, also swap middle content */
2874                 a/= 2;
2875                 while(a>0) {
2876                         if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2877
2878                         swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2879                         if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2880
2881                         SWAP(char, bezt1->h1, bezt1->h2);
2882                         SWAP(short, bezt1->f1, bezt1->f3);
2883                         
2884                         if(bezt1!=bezt2) {
2885                                 SWAP(char, bezt2->h1, bezt2->h2);
2886                                 SWAP(short, bezt2->f1, bezt2->f3);
2887                                 bezt1->alfa= -bezt1->alfa;
2888                                 bezt2->alfa= -bezt2->alfa;
2889                         }
2890                         a--;
2891                         bezt1++; 
2892                         bezt2--;
2893                 }
2894         }
2895         else if(nu->pntsv==1) {
2896                 a= nu->pntsu;
2897                 bp1= nu->bp;
2898                 bp2= bp1+(a-1);
2899                 a/= 2;
2900                 while(bp1!=bp2 && a>0) {
2901                         SWAP(BPoint, *bp1, *bp2);
2902                         a--;
2903                         bp1->alfa= -bp1->alfa;
2904                         bp2->alfa= -bp2->alfa;
2905                         bp1++; 
2906                         bp2--;
2907                 }
2908                 if(nu->type == CU_NURBS) {
2909                         /* no knots for too short paths */
2910                         if(nu->knotsu) {
2911                                 /* inverse knots */
2912                                 a= KNOTSU(nu);
2913                                 fp1= nu->knotsu;
2914                                 fp2= fp1+(a-1);
2915                                 a/= 2;
2916                                 while(fp1!=fp2 && a>0) {
2917                                         SWAP(float, *fp1, *fp2);
2918                                         a--;
2919                                         fp1++; 
2920                                         fp2--;
2921                                 }
2922                                 /* and make in increasing order again */
2923                                 a= KNOTSU(nu);
2924                                 fp1= nu->knotsu;
2925                                 fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2926                                 while(a--) {
2927                                         fp2[0]= fabs(fp1[1]-fp1[0]);
2928                                         fp1++;
2929                                         fp2++;
2930                                 }
2931                 
2932                                 a= KNOTSU(nu)-1;
2933                                 fp1= nu->knotsu;
2934                                 fp2= tempf;
2935                                 fp1[0]= 0.0;
2936                                 fp1++;
2937                                 while(a--) {
2938                                         fp1[0]= fp1[-1]+fp2[0];
2939                                         fp1++;
2940                                         fp2++;
2941                                 }
2942                                 MEM_freeN(tempf);
2943                         }
2944                 }
2945         }
2946         else {
2947                 
2948                 for(b=0; b<nu->pntsv; b++) {
2949                 
2950                         bp1= nu->bp+b*nu->pntsu;
2951                         a= nu->pntsu;
2952                         bp2= bp1+(a-1);
2953                         a/= 2;
2954                         
2955                         while(bp1!=bp2 && a>0) {
2956                                 SWAP(BPoint, *bp1, *bp2);
2957                                 a--;
2958                                 bp1++; 
2959                                 bp2--;
2960                         }
2961                 }
2962         }
2963 }
2964
2965
2966 float (*curve_getVertexCos(Curve *UNUSED(cu), ListBase *lb, int *numVerts_r))[3]
2967 {
2968         int i, numVerts = *numVerts_r = count_curveverts(lb);
2969         float *co, (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "cu_vcos");
2970         Nurb *nu;
2971
2972         co = cos[0];
2973         for (nu=lb->first; nu; nu=nu->next) {
2974                 if (nu->type == CU_BEZIER) {
2975                         BezTriple *bezt = nu->bezt;
2976
2977                         for (i=0; i<nu->pntsu; i++,bezt++) {
2978                                 VECCOPY(co, bezt->vec[0]); co+=3;
2979                                 VECCOPY(co, bezt->vec[1]); co+=3;
2980                                 VECCOPY(co, bezt->vec[2]); co+=3;
2981                         }
2982                 } else {
2983                         BPoint *bp = nu->bp;
2984
2985                         for (i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
2986                                 VECCOPY(co, bp->vec); co+=3;
2987                         }
2988                 }
2989         }
2990
2991         return cos;
2992 }
2993
2994 void curve_applyVertexCos(Curve *UNUSED(cu), ListBase *lb, float (*vertexCos)[3])
2995 {
2996         float *co = vertexCos[0];
2997         Nurb *nu;
2998         int i;
2999
3000         for (nu=lb->first; nu; nu=nu->next) {
3001                 if (nu->type == CU_BEZIER) {
3002                         BezTriple *bezt = nu->bezt;
3003
3004                         for (i=0; i<nu->pntsu; i++,bezt++) {
3005                                 VECCOPY(bezt->vec[0], co); co+=3;
3006                                 VECCOPY(bezt->vec[1], co); co+=3;
3007                                 VECCOPY(bezt->vec[2], co); co+=3;
3008                         }
3009                 } else {
3010                         BPoint *bp = nu->bp;
3011
3012                         for (i=0; i<nu->pntsu*nu->pntsv; i++,bp++) {
3013                                 VECCOPY(bp->vec, co); co+=3;
3014                         }
3015                 }
3016         }
3017 }
3018
3019 float (*curve_getKeyVertexCos(Curve *UNUSED(cu), ListBase *lb, float *key))[3]
3020 {
3021         int i, numVerts = count_curveverts(lb);
3022         float *co, (*cos)[3] = MEM_mal