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