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