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