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