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