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