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