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