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