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