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