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