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