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