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