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