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