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