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