fix for own mistake when changing curve flags - Reported as [#7888] Extrude broken...
[blender.git] / source / blender / blenkernel / intern / curve.c
1
2 /*  curve.c 
3  * 
4  *  
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL 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. The Blender
13  * Foundation also sells licenses for use in proprietary software under
14  * the Blender License.  See http://www.blender.org/BL/ for information
15  * about this.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #include <math.h>  // floor
37 #include <string.h>
38 #include <stdlib.h>  
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "MEM_guardedalloc.h"
45 #include "BLI_blenlib.h"  
46 #include "BLI_arithb.h"  
47
48 #include "DNA_object_types.h"  
49 #include "DNA_curve_types.h"  
50 #include "DNA_material_types.h"  
51
52 /* for dereferencing pointers */
53 #include "DNA_ID.h"  
54 #include "DNA_vfont_types.h"  
55 #include "DNA_key_types.h"  
56 #include "DNA_ipo_types.h"  
57
58 #include "BKE_global.h" 
59 #include "BKE_main.h"  
60 #include "BKE_utildefines.h"  // VECCOPY
61 #include "BKE_object.h"  
62 #include "BKE_mesh.h" 
63 #include "BKE_curve.h"  
64 #include "BKE_displist.h"  
65 #include "BKE_ipo.h"  
66 #include "BKE_anim.h"  
67 #include "BKE_library.h"  
68 #include "BKE_key.h"  
69
70
71 /* globals */
72
73 extern ListBase editNurb;  /* editcurve.c */
74
75 /* local */
76 int cu_isectLL(float *v1, float *v2, float *v3, float *v4, 
77                            short cox, short coy, 
78                            float *labda, float *mu, float *vec);
79
80 void unlink_curve(Curve *cu)
81 {
82         int a;
83         
84         for(a=0; a<cu->totcol; a++) {
85                 if(cu->mat[a]) cu->mat[a]->id.us--;
86                 cu->mat[a]= 0;
87         }
88         if(cu->vfont) cu->vfont->id.us--; 
89         cu->vfont= 0;
90         if(cu->key) cu->key->id.us--;
91         cu->key= 0;
92         if(cu->ipo) cu->ipo->id.us--;
93         cu->ipo= 0;
94 }
95
96
97 /* niet curve zelf vrijgeven */
98 void free_curve(Curve *cu)
99 {
100
101         freeNurblist(&cu->nurb);
102         BLI_freelistN(&cu->bev);
103         freedisplist(&cu->disp);
104         
105         unlink_curve(cu);
106         
107         if(cu->mat) MEM_freeN(cu->mat);
108         if(cu->str) MEM_freeN(cu->str);
109         if(cu->strinfo) MEM_freeN(cu->strinfo);
110         if(cu->bb) MEM_freeN(cu->bb);
111         if(cu->path) free_path(cu->path);
112         if(cu->tb) MEM_freeN(cu->tb);
113 }
114
115 Curve *add_curve(char *name, int type)
116 {
117         Curve *cu;
118
119         cu= alloc_libblock(&G.main->curve, ID_CU, name);
120         
121         cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
122         cu->flag= CU_FRONT+CU_BACK;
123         cu->pathlen= 100;
124         cu->resolu= cu->resolv= 12;
125         cu->width= 1.0;
126         cu->wordspace = 1.0;
127         cu->spacing= cu->linedist= 1.0;
128         cu->fsize= 1.0;
129         cu->ulheight = 0.05;    
130         cu->texflag= CU_AUTOSPACE;
131         
132         cu->bb= unit_boundbox();
133         
134         return cu;
135 }
136
137 Curve *copy_curve(Curve *cu)
138 {
139         Curve *cun;
140         int a;
141         
142         cun= copy_libblock(cu);
143         cun->nurb.first= cun->nurb.last= 0;
144         duplicateNurblist( &(cun->nurb), &(cu->nurb));
145
146         cun->mat= MEM_dupallocN(cu->mat);
147         for(a=0; a<cun->totcol; a++) {
148                 id_us_plus((ID *)cun->mat[a]);
149         }
150         
151         cun->str= MEM_dupallocN(cu->str);
152         cun->strinfo= MEM_dupallocN(cu->strinfo);       
153         cun->tb= MEM_dupallocN(cu->tb);
154         cun->bb= MEM_dupallocN(cu->bb);
155         
156         cun->key= copy_key(cu->key);
157         if(cun->key) cun->key->from= (ID *)cun;
158         
159         cun->disp.first= cun->disp.last= 0;
160         cun->bev.first= cun->bev.last= 0;
161         cun->path= 0;
162
163         /* single user ipo too */
164         if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
165
166         id_us_plus((ID *)cun->vfont);
167         id_us_plus((ID *)cun->vfontb);  
168         id_us_plus((ID *)cun->vfonti);
169         id_us_plus((ID *)cun->vfontbi);
170         
171         return cun;
172 }
173
174 void make_local_curve(Curve *cu)
175 {
176         Object *ob = 0;
177         Curve *cun;
178         int local=0, lib=0;
179         
180         /* - when there are only lib users: don't do
181          * - when there are only local users: set flag
182          * - mixed: do a copy
183          */
184         
185         if(cu->id.lib==0) return;
186         
187         if(cu->vfont) cu->vfont->id.lib= 0;
188         
189         if(cu->id.us==1) {
190                 cu->id.lib= 0;
191                 cu->id.flag= LIB_LOCAL;
192                 new_id(0, (ID *)cu, 0);
193                 return;
194         }
195         
196         ob= G.main->object.first;
197         while(ob) {
198                 if(ob->data==cu) {
199                         if(ob->id.lib) lib= 1;
200                         else local= 1;
201                 }
202                 ob= ob->id.next;
203         }
204         
205         if(local && lib==0) {
206                 cu->id.lib= 0;
207                 cu->id.flag= LIB_LOCAL;
208                 new_id(0, (ID *)cu, 0);
209         }
210         else if(local && lib) {
211                 cun= copy_curve(cu);
212                 cun->id.us= 0;
213                 
214                 ob= G.main->object.first;
215                 while(ob) {
216                         if(ob->data==cu) {
217                                 
218                                 if(ob->id.lib==0) {
219                                         ob->data= cun;
220                                         cun->id.us++;
221                                         cu->id.us--;
222                                 }
223                         }
224                         ob= ob->id.next;
225                 }
226         }
227 }
228
229 short curve_type(Curve *cu)
230 {
231         Nurb *nu;
232         if(cu->vfont) {
233                 return OB_FONT;
234         }
235         for (nu= cu->nurb.first; nu; nu= nu->next) {
236                 if(nu->pntsv>1) {
237                         return OB_SURF;
238                 }
239         }
240         
241         return OB_CURVE;
242 }
243
244 void test_curve_type(Object *ob)
245 {       
246         ob->type = curve_type(ob->data);
247 }
248
249 void tex_space_curve(Curve *cu)
250 {
251         DispList *dl;
252         BoundBox *bb;
253         float *data, min[3], max[3], loc[3], size[3];
254         int tot, doit= 0;
255         
256         if(cu->bb==NULL) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
257         bb= cu->bb;
258         
259         INIT_MINMAX(min, max);
260
261         dl= cu->disp.first;
262         while(dl) {
263                 
264                 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
265                 else tot= dl->nr*dl->parts;
266                 
267                 if(tot) doit= 1;
268                 data= dl->verts;
269                 while(tot--) {
270                         DO_MINMAX(data, min, max);
271                         data+= 3;
272                 }
273                 dl= dl->next;
274         }
275
276         if(!doit) {
277                 min[0] = min[1] = min[2] = -1.0f;
278                 max[0] = max[1] = max[2] = 1.0f;
279         }
280         
281         loc[0]= (min[0]+max[0])/2.0f;
282         loc[1]= (min[1]+max[1])/2.0f;
283         loc[2]= (min[2]+max[2])/2.0f;
284         
285         size[0]= (max[0]-min[0])/2.0f;
286         size[1]= (max[1]-min[1])/2.0f;
287         size[2]= (max[2]-min[2])/2.0f;
288
289         boundbox_set_from_min_max(bb, min, max);
290
291         if(cu->texflag & CU_AUTOSPACE) {
292                 VECCOPY(cu->loc, loc);
293                 VECCOPY(cu->size, size);
294                 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
295
296                 if(cu->size[0]==0.0) cu->size[0]= 1.0;
297                 else if(cu->size[0]>0.0 && cu->size[0]<0.00001) cu->size[0]= 0.00001;
298                 else if(cu->size[0]<0.0 && cu->size[0]> -0.00001) cu->size[0]= -0.00001;
299         
300                 if(cu->size[1]==0.0) cu->size[1]= 1.0;
301                 else if(cu->size[1]>0.0 && cu->size[1]<0.00001) cu->size[1]= 0.00001;
302                 else if(cu->size[1]<0.0 && cu->size[1]> -0.00001) cu->size[1]= -0.00001;
303         
304                 if(cu->size[2]==0.0) cu->size[2]= 1.0;
305                 else if(cu->size[2]>0.0 && cu->size[2]<0.00001) cu->size[2]= 0.00001;
306                 else if(cu->size[2]<0.0 && cu->size[2]> -0.00001) cu->size[2]= -0.00001;
307
308         }
309 }
310
311
312 int count_curveverts(ListBase *nurb)
313 {
314         Nurb *nu;
315         int tot=0;
316         
317         nu= nurb->first;
318         while(nu) {
319                 if(nu->bezt) tot+= 3*nu->pntsu;
320                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
321                 
322                 nu= nu->next;
323         }
324         return tot;
325 }
326
327 int count_curveverts_without_handles(ListBase *nurb)
328 {
329         Nurb *nu;
330         int tot=0;
331         
332         nu= nurb->first;
333         while(nu) {
334                 if(nu->bezt) tot+= nu->pntsu;
335                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
336                 
337                 nu= nu->next;
338         }
339         return tot;
340 }
341
342 /* **************** NURBS ROUTINES ******************** */
343
344 void freeNurb(Nurb *nu)
345 {
346
347         if(nu==0) return;
348
349         if(nu->bezt) MEM_freeN(nu->bezt);
350         nu->bezt= 0;
351         if(nu->bp) MEM_freeN(nu->bp);
352         nu->bp= 0;
353         if(nu->knotsu) MEM_freeN(nu->knotsu);
354         nu->knotsu= 0;
355         if(nu->knotsv) MEM_freeN(nu->knotsv);
356         nu->knotsv= 0;
357         /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
358
359         MEM_freeN(nu);
360
361 }
362
363
364 void freeNurblist(ListBase *lb)
365 {
366         Nurb *nu, *next;
367
368         if(lb==0) return;
369
370         nu= lb->first;
371         while(nu) {
372                 next= nu->next;
373                 freeNurb(nu);
374                 nu= next;
375         }
376         lb->first= lb->last= 0;
377 }
378
379 Nurb *duplicateNurb(Nurb *nu)
380 {
381         Nurb *newnu;
382         int len;
383
384         newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
385         if(newnu==0) return 0;
386         memcpy(newnu, nu, sizeof(Nurb));
387
388         if(nu->bezt) {
389                 newnu->bezt=
390                         (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
391                 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
392         }
393         else {
394                 len= nu->pntsu*nu->pntsv;
395                 newnu->bp=
396                         (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
397                 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
398                 
399                 newnu->knotsu=newnu->knotsv= 0;
400                 
401                 if(nu->knotsu) {
402                         len= KNOTSU(nu);
403                         if(len) {
404                                 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
405                                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
406                         }
407                 }
408                 if(nu->pntsv>1 && nu->knotsv) {
409                         len= KNOTSV(nu);
410                         if(len) {
411                                 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
412                                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
413                         }
414                 }
415         }
416         return newnu;
417 }
418
419 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
420 {
421         Nurb *nu, *nun;
422         
423         freeNurblist(lb1);
424         
425         nu= lb2->first;
426         while(nu) {
427                 nun= duplicateNurb(nu);
428                 BLI_addtail(lb1, nun);
429                 
430                 nu= nu->next;
431         }
432 }
433
434 void test2DNurb(Nurb *nu)
435 {
436         BezTriple *bezt;
437         BPoint *bp;
438         int a;
439
440         if( nu->type== CU_BEZIER+CU_2D ) {
441                 a= nu->pntsu;
442                 bezt= nu->bezt;
443                 while(a--) {
444                         bezt->vec[0][2]= 0.0; 
445                         bezt->vec[1][2]= 0.0; 
446                         bezt->vec[2][2]= 0.0;
447                         bezt++;
448                 }
449         }
450         else if(nu->type & CU_2D) {
451                 a= nu->pntsu*nu->pntsv;
452                 bp= nu->bp;
453                 while(a--) {
454                         bp->vec[2]= 0.0;
455                         bp++;
456                 }
457         }
458 }
459
460 void minmaxNurb(Nurb *nu, float *min, float *max)
461 {
462         BezTriple *bezt;
463         BPoint *bp;
464         int a;
465
466         if( (nu->type & 7)==CU_BEZIER ) {
467                 a= nu->pntsu;
468                 bezt= nu->bezt;
469                 while(a--) {
470                         DO_MINMAX(bezt->vec[0], min, max);
471                         DO_MINMAX(bezt->vec[1], min, max);
472                         DO_MINMAX(bezt->vec[2], min, max);
473                         bezt++;
474                 }
475         }
476         else {
477                 a= nu->pntsu*nu->pntsv;
478                 bp= nu->bp;
479                 while(a--) {
480                         DO_MINMAX(bp->vec, min, max);
481                         bp++;
482                 }
483         }
484
485 }
486
487 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
488
489
490 static void calcknots(float *knots, short aantal, short order, short type)
491 /* knots: number of pnts NOT corrected for cyclic */
492 /* type;         0: uniform, 1: endpoints, 2: bezier */
493 {
494         float k;
495         int a, t;
496
497         t = aantal+order;
498         if(type==0) {
499
500                 for(a=0;a<t;a++) {
501                         knots[a]= (float)a;
502                 }
503         }
504         else if(type==1) {
505                 k= 0.0;
506                 for(a=1;a<=t;a++) {
507                         knots[a-1]= k;
508                         if(a>=order && a<=aantal) k+= 1.0;
509                 }
510         }
511         else if(type==2) {
512                 if(order==4) {
513                         k= 0.34;
514                         for(a=0;a<t;a++) {
515                                 knots[a]= (float)floor(k);
516                                 k+= (1.0/3.0);
517                         }
518                 }
519                 else if(order==3) {
520                         k= 0.6;
521                         for(a=0;a<t;a++) {
522                                 if(a>=order && a<=aantal) k+= (0.5);
523                                 knots[a]= (float)floor(k);
524                         }
525                 }
526         }
527 }
528
529 static void makecyclicknots(float *knots, short pnts, short order)
530 /* pnts, order: number of pnts NOT corrected for cyclic */
531 {
532         int a, b, order2, c;
533
534         if(knots==0) return;
535         order2=order-1;
536
537         /* do first long rows (order -1), remove identical knots at endpoints */
538         if(order>2) {
539                 b= pnts+order2;
540                 for(a=1; a<order2; a++) {
541                         if(knots[b]!= knots[b-a]) break;
542                 }
543                 if(a==order2) knots[pnts+order-2]+= 1.0;
544         }
545
546         b= order;
547         c=pnts + order + order2;
548         for(a=pnts+order2; a<c; a++) {
549                 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
550                 b--;
551         }
552 }
553
554
555 void makeknots(Nurb *nu, short uv, short type)  /* 0: uniform, 1: endpoints, 2: bezier */
556 {
557         if( (nu->type & 7)==CU_NURBS ) {
558                 if(uv & 1) {
559                         if(nu->knotsu) MEM_freeN(nu->knotsu);
560                         if(nu->pntsu>1) {
561                                 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
562                                 calcknots(nu->knotsu, nu->pntsu, nu->orderu, type);
563                                 if(nu->flagu & 1) makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
564                         }
565                         else nu->knotsu= 0;
566                 }
567                 if(uv & 2) {
568                         if(nu->knotsv) MEM_freeN(nu->knotsv);
569                         if(nu->pntsv>1) {
570                                 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
571                                 calcknots(nu->knotsv, nu->pntsv, nu->orderv, type);
572                                 if(nu->flagv & 1) makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
573                         }
574                         else nu->knotsv= 0;
575                 }
576         }
577 }
578
579 static void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
580 {
581         float d, e;
582         int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
583
584         orderpluspnts= order+pnts;
585         opp2 = orderpluspnts-1;
586
587         /* this is for float inaccuracy */
588         if(t < knots[0]) t= knots[0];
589         else if(t > knots[opp2]) t= knots[opp2];
590
591         /* this part is order '1' */
592         o2 = order + 1;
593         for(i=0;i<opp2;i++) {
594                 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
595                         basis[i]= 1.0;
596                         i1= i-o2;
597                         if(i1<0) i1= 0;
598                         i2= i;
599                         i++;
600                         while(i<opp2) {
601                                 basis[i]= 0.0;
602                                 i++;
603                         }
604                         break;
605                 }
606                 else basis[i]= 0.0;
607         }
608         basis[i]= 0.0;
609         
610         /* this is order 2,3,... */
611         for(j=2; j<=order; j++) {
612
613                 if(i2+j>= orderpluspnts) i2= opp2-j;
614
615                 for(i= i1; i<=i2; i++) {
616                         if(basis[i]!=0.0)
617                                 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
618                         else
619                                 d= 0.0;
620
621                         if(basis[i+1]!=0.0)
622                                 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
623                         else
624                                 e= 0.0;
625
626                         basis[i]= d+e;
627                 }
628         }
629
630         *start= 1000;
631         *end= 0;
632
633         for(i=i1; i<=i2; i++) {
634                 if(basis[i]>0.0) {
635                         *end= i;
636                         if(*start==1000) *start= i;
637                 }
638         }
639 }
640
641
642 void makeNurbfaces(Nurb *nu, float *data, int rowstride) 
643 /* data  has to be 3*4*resolu*resolv in size, and zero-ed */
644 {
645         BPoint *bp;
646         float *basisu, *basis, *basisv, *sum, *fp, *in;
647         float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
648         int i, j, iofs, jofs, cycl, len, resolu, resolv;
649         int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
650
651         if(nu->knotsu==0 || nu->knotsv==0) return;
652         if(nu->orderu>nu->pntsu) return;
653         if(nu->orderv>nu->pntsv) return;
654         if(data==0) return;
655
656         /* allocate and initialize */
657         len= nu->pntsu*nu->pntsv;
658         if(len==0) return;
659         
660
661         
662         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
663
664         resolu= nu->resolu;
665         resolv= nu->resolv;
666         len= resolu*resolv;
667         if(len==0) {
668                 MEM_freeN(sum);
669                 return;
670         }
671
672         bp= nu->bp;
673         i= nu->pntsu*nu->pntsv;
674         ratcomp=0;
675         while(i--) {
676                 if(bp->vec[3]!=1.0) {
677                         ratcomp= 1;
678                         break;
679                 }
680                 bp++;
681         }
682
683         fp= nu->knotsu;
684         ustart= fp[nu->orderu-1];
685         if(nu->flagu & 1) uend= fp[nu->pntsu+nu->orderu-1];
686         else uend= fp[nu->pntsu];
687         ustep= (uend-ustart)/(resolu-1+(nu->flagu & 1));
688         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
689
690         fp= nu->knotsv;
691         vstart= fp[nu->orderv-1];
692         
693         if(nu->flagv & 1) vend= fp[nu->pntsv+nu->orderv-1];
694         else vend= fp[nu->pntsv];
695         vstep= (vend-vstart)/(resolv-1+(nu->flagv & 1));
696         len= KNOTSV(nu);
697         basisv= (float *)MEM_mallocN(sizeof(float)*len*resolv, "makeNurbfaces3");
698         jstart= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces4");
699         jend= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces5");
700
701         /* precalculation of basisv and jstart,jend */
702         if(nu->flagv & 1) cycl= nu->orderv-1; 
703         else cycl= 0;
704         v= vstart;
705         basis= basisv;
706         while(resolv--) {
707                 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+resolv, jend+resolv);
708                 basis+= KNOTSV(nu);
709                 v+= vstep;
710         }
711
712         if(nu->flagu & 1) cycl= nu->orderu-1; 
713         else cycl= 0;
714         in= data;
715         u= ustart;
716         while(resolu--) {
717
718                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
719
720                 basis= basisv;
721                 resolv= nu->resolv;
722                 while(resolv--) {
723
724                         jsta= jstart[resolv];
725                         jen= jend[resolv];
726
727                         /* calculate sum */
728                         sumdiv= 0.0;
729                         fp= sum;
730
731                         for(j= jsta; j<=jen; j++) {
732
733                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
734                                 else jofs= j;
735                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
736
737                                 for(i= istart; i<=iend; i++, fp++) {
738
739                                         if(i>= nu->pntsu) {
740                                                 iofs= i- nu->pntsu;
741                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
742                                         }
743                                         else bp++;
744
745                                         if(ratcomp) {
746                                                 *fp= basisu[i]*basis[j]*bp->vec[3];
747                                                 sumdiv+= *fp;
748                                         }
749                                         else *fp= basisu[i]*basis[j];
750                                 }
751                         }
752                 
753                         if(ratcomp) {
754                                 fp= sum;
755                                 for(j= jsta; j<=jen; j++) {
756                                         for(i= istart; i<=iend; i++, fp++) {
757                                                 *fp/= sumdiv;
758                                         }
759                                 }
760                         }
761
762                         /* one! (1.0) real point now */
763                         fp= sum;
764                         for(j= jsta; j<=jen; j++) {
765
766                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
767                                 else jofs= j;
768                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
769
770                                 for(i= istart; i<=iend; i++, fp++) {
771
772                                         if(i>= nu->pntsu) {
773                                                 iofs= i- nu->pntsu;
774                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
775                                         }
776                                         else bp++;
777
778                                         if(*fp!=0.0) {
779                                                 in[0]+= (*fp) * bp->vec[0];
780                                                 in[1]+= (*fp) * bp->vec[1];
781                                                 in[2]+= (*fp) * bp->vec[2];
782                                         }
783                                 }
784                         }
785
786                         in+=3;
787                         basis+= KNOTSV(nu);
788                 }
789                 u+= ustep;
790                 if (rowstride!=0) in = (float*) (((unsigned char*) in) + (rowstride - 3*nu->resolv*sizeof(*in)));
791         }
792
793         /* free */
794         MEM_freeN(sum);
795         MEM_freeN(basisu);
796         MEM_freeN(basisv);
797         MEM_freeN(jstart);
798         MEM_freeN(jend);
799 }
800
801 void makeNurbcurve(Nurb *nu, float *data, int resolu, int dim)
802 /* data has to be dim*4*pntsu*resolu in size and zero-ed */
803 {
804         BPoint *bp;
805         float u, ustart, uend, ustep, sumdiv;
806         float *basisu, *sum, *fp,  *in;
807         int i, len, istart, iend, cycl;
808
809         if(nu->knotsu==0) return;
810         if(nu->orderu>nu->pntsu) return;
811         if(data==0) return;
812
813         /* allocate and initialize */
814         len= nu->pntsu;
815         if(len==0) return;
816         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
817
818         resolu*= nu->pntsu;
819         if(resolu==0) {
820                 MEM_freeN(sum);
821                 return;
822         }
823
824         fp= nu->knotsu;
825         ustart= fp[nu->orderu-1];
826         if(nu->flagu & 1) uend= fp[nu->pntsu+nu->orderu-1];
827         else uend= fp[nu->pntsu];
828         ustep= (uend-ustart)/(resolu-1+(nu->flagu & 1));
829         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
830
831         if(nu->flagu & 1) cycl= nu->orderu-1; 
832         else cycl= 0;
833
834         in= data;
835         u= ustart;
836         while(resolu--) {
837
838                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
839                 /* calc sum */
840                 sumdiv= 0.0;
841                 fp= sum;
842                 bp= nu->bp+ istart-1;
843                 for(i= istart; i<=iend; i++, fp++) {
844
845                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
846                         else bp++;
847
848                         *fp= basisu[i]*bp->vec[3];
849                         sumdiv+= *fp;
850                 }
851                 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
852                         /* is normalizing needed? */
853                         fp= sum;
854                         for(i= istart; i<=iend; i++, fp++) {
855                                 *fp/= sumdiv;
856                         }
857                 }
858
859                 /* one! (1.0) real point */
860                 fp= sum;
861                 bp= nu->bp+ istart-1;
862                 for(i= istart; i<=iend; i++, fp++) {
863
864                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
865                         else bp++;
866
867                         if(*fp!=0.0) {
868                                 
869                                 in[0]+= (*fp) * bp->vec[0];
870                                 in[1]+= (*fp) * bp->vec[1];
871                                 if(dim>=3) {
872                                         in[2]+= (*fp) * bp->vec[2];
873                                         if(dim==4) in[3]+= (*fp) * bp->alfa;
874                                 }
875                         }
876                 }
877
878                 in+= dim;
879
880                 u+= ustep;
881         }
882
883         /* free */
884         MEM_freeN(sum);
885         MEM_freeN(basisu);
886 }
887
888 /* forward differencing method for bezier curve */
889 void forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
890 {
891         float rt0,rt1,rt2,rt3,f;
892         int a;
893
894         f= (float)it;
895         rt0= q0;
896         rt1= 3.0f*(q1-q0)/f;
897         f*= f;
898         rt2= 3.0f*(q0-2.0f*q1+q2)/f;
899         f*= it;
900         rt3= (q3-q0+3.0f*(q1-q2))/f;
901         
902         q0= rt0;
903         q1= rt1+rt2+rt3;
904         q2= 2*rt2+6*rt3;
905         q3= 6*rt3;
906   
907         for(a=0; a<=it; a++) {
908                 *p= q0;
909                 p+= stride;
910                 q0+= q1;
911                 q1+= q2;
912                 q2+= q3;
913         }
914 }       
915
916 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
917
918 float *make_orco_surf(Object *ob)
919 {
920         Curve *cu= ob->data;
921         Nurb *nu;
922         int a, b, tot=0;
923         int sizeu, sizev;
924         float *data, *orco;
925         
926         /* first calculate the size of the datablock */
927         nu= cu->nurb.first;
928         while(nu) {
929                 /* as we want to avoid the seam in a cyclic nurbs
930                 texture wrapping, reserve extra orco data space to save these extra needed
931                 vertex based UV coordinates for the meridian vertices.
932                 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
933                 the renderface/vert construction.
934                 
935                 See also convertblender.c: init_render_surf()
936                 */
937                 
938                 sizeu = nu->resolu; 
939                 sizev = nu->resolv;
940                 if (nu->flagu & CU_CYCLIC) sizeu++;
941                 if (nu->flagv & CU_CYCLIC) sizev++;
942                 if(nu->pntsv>1) tot+= sizeu * sizev;
943                 
944                 nu= nu->next;
945         }
946         /* makeNurbfaces wants zeros */
947         data= orco= MEM_callocN(3*sizeof(float)*tot, "make_orco");
948         
949         nu= cu->nurb.first;
950         while(nu) {
951                 if(nu->pntsv>1) {
952                         sizeu = nu->resolu;
953                         sizev = nu->resolv;
954                         if (nu->flagu & CU_CYCLIC) sizeu++;
955                         if (nu->flagv & CU_CYCLIC) sizev++;
956                         
957                         if(cu->flag & CU_UV_ORCO) {
958                                 for(b=0; b< sizeu; b++) {
959                                         for(a=0; a< sizev; a++) {
960                                                 
961                                                 if(sizev <2) data[0]= 0.0f;
962                                                 else data[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
963                                                 
964                                                 if(sizeu <2) data[1]= 0.0f;
965                                                 else data[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
966                                                 
967                                                 data[2]= 0.0;
968                                                 
969                                                 data+= 3;
970                                         }
971                                 }
972                         }
973                         else {
974                                 float *_tdata= MEM_callocN(nu->resolu*nu->resolv*3*sizeof(float), "temp data");
975                                 float *tdata= _tdata;
976                                 
977                                 makeNurbfaces(nu, tdata, 0);
978                                 
979                                 for(b=0; b<sizeu; b++) {
980                                         int use_b= b;
981                                         if (b==sizeu-1 && (nu->flagu & CU_CYCLIC))
982                                                 use_b= 0;
983                                         
984                                         for(a=0; a<sizev; a++) {
985                                                 int use_a= a;
986                                                 if (a==sizev-1 && (nu->flagv & CU_CYCLIC))
987                                                         use_a= 0;
988                                                 
989                                                 tdata = _tdata + 3 * (use_b * nu->resolv + use_a);
990                                                 
991                                                 data[0]= (tdata[0]-cu->loc[0])/cu->size[0];
992                                                 data[1]= (tdata[1]-cu->loc[1])/cu->size[1];
993                                                 data[2]= (tdata[2]-cu->loc[2])/cu->size[2];
994                                                 data+= 3;
995                                         }
996                                 }
997                                 
998                                 MEM_freeN(_tdata);
999                         }
1000                 }
1001                 nu= nu->next;
1002         }
1003         
1004         return orco;
1005 }
1006
1007
1008         /* NOTE: This routine is tied to the order of vertex
1009          * built by displist and as passed to the renderer.
1010          */
1011 float *make_orco_curve(Object *ob)
1012 {
1013         Curve *cu = ob->data;
1014         DispList *dl;
1015         int u, v, numVerts;
1016         float *fp, *orco;
1017         int remakeDisp = 0;
1018
1019         if (!(cu->flag&CU_UV_ORCO) && cu->key && cu->key->refkey) {
1020                 cp_cu_key(cu, cu->key->refkey, 0, count_curveverts(&cu->nurb));
1021                 makeDispListCurveTypes(ob, 1);
1022                 remakeDisp = 1;
1023         }
1024
1025         /* Assumes displist has been built */
1026
1027         numVerts = 0;
1028         for (dl=cu->disp.first; dl; dl=dl->next) {
1029                 if (dl->type==DL_INDEX3) {
1030                         numVerts += dl->nr;
1031                 } else if (dl->type==DL_SURF) {
1032                         /* convertblender.c uses the Surface code for creating renderfaces when cyclic U only (closed circle beveling) */
1033                         if (dl->flag & DL_CYCL_U) {
1034                                 if (dl->flag & DL_CYCL_V)
1035                                         numVerts += (dl->parts+1)*(dl->nr+1);
1036                                 else
1037                                         numVerts += dl->parts*(dl->nr+1);
1038                         }
1039                         else
1040                                 numVerts += dl->parts*dl->nr;
1041                 }
1042         }
1043
1044         fp= orco= MEM_mallocN(3*sizeof(float)*numVerts, "cu_orco");
1045         for (dl=cu->disp.first; dl; dl=dl->next) {
1046                 if (dl->type==DL_INDEX3) {
1047                         for (u=0; u<dl->nr; u++, fp+=3) {
1048                                 if (cu->flag & CU_UV_ORCO) {
1049                                         fp[0]= 2.0f*u/(dl->nr-1) - 1.0f;
1050                                         fp[1]= 0.0;
1051                                         fp[2]= 0.0;
1052                                 } else {
1053                                         VECCOPY(fp, &dl->verts[u*3]);
1054
1055                                         fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1056                                         fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1057                                         fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1058                                 }
1059                         }
1060                 } else if (dl->type==DL_SURF) {
1061                         int sizeu= dl->nr, sizev= dl->parts;
1062                         
1063                         /* exception as handled in convertblender.c too */
1064                         if (dl->flag & DL_CYCL_U) {
1065                                 sizeu++;
1066                                 if (dl->flag & DL_CYCL_V)
1067                                         sizev++;
1068                         }
1069                         
1070                         for (u=0; u<sizev; u++) {
1071                                 for (v=0; v<sizeu; v++,fp+=3) {
1072                                         if (cu->flag & CU_UV_ORCO) {
1073                                                 fp[0]= 2.0f*u/(dl->parts-1) - 1.0f;
1074                                                 fp[1]= 2.0f*v/(dl->nr-1) - 1.0f;
1075                                                 fp[2]= 0.0;
1076                                         } else {
1077                                                 int realv= v % dl->nr;
1078
1079                                                 VECCOPY(fp, &dl->verts[(dl->nr*u + realv)*3]);
1080
1081                                                 fp[0]= (fp[0]-cu->loc[0])/cu->size[0];
1082                                                 fp[1]= (fp[1]-cu->loc[1])/cu->size[1];
1083                                                 fp[2]= (fp[2]-cu->loc[2])/cu->size[2];
1084                                         }
1085                                 }
1086                         }
1087                 }
1088         }
1089
1090         if (remakeDisp) {
1091                 makeDispListCurveTypes(ob, 0);
1092         }
1093
1094         return orco;
1095 }
1096
1097
1098 /* ***************** BEVEL ****************** */
1099
1100 void makebevelcurve(Object *ob, ListBase *disp)
1101 {
1102         DispList *dl, *dlnew;
1103         Curve *bevcu, *cu;
1104         float *fp, facx, facy, angle, dangle;
1105         int nr, a;
1106
1107         cu= ob->data;
1108         disp->first = disp->last = NULL;
1109
1110         /* if a font object is being edited, then do nothing */
1111         if( ob == G.obedit && ob->type == OB_FONT ) return;
1112
1113         if(cu->bevobj && cu->bevobj!=ob) {
1114                 if(cu->bevobj->type==OB_CURVE) {
1115                         bevcu= cu->bevobj->data;
1116                         if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1117                                 facx= cu->bevobj->size[0];
1118                                 facy= cu->bevobj->size[1];
1119
1120                                 dl= bevcu->disp.first;
1121                                 if(dl==0) {
1122                                         makeDispListCurveTypes(cu->bevobj, 0);
1123                                         dl= bevcu->disp.first;
1124                                 }
1125                                 while(dl) {
1126                                         if ELEM(dl->type, DL_POLY, DL_SEGM) {
1127                                                 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");                                        
1128                                                 *dlnew= *dl;
1129                                                 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1130                                                 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1131                                                 
1132                                                 if(dlnew->type==DL_SEGM) dlnew->flag |= (DL_FRONT_CURVE|DL_BACK_CURVE);
1133                                                 
1134                                                 BLI_addtail(disp, dlnew);
1135                                                 fp= dlnew->verts;
1136                                                 nr= dlnew->parts*dlnew->nr;
1137                                                 while(nr--) {
1138                                                         fp[2]= fp[1]*facy;
1139                                                         fp[1]= -fp[0]*facx;
1140                                                         fp[0]= 0.0;
1141                                                         fp+= 3;
1142                                                 }
1143                                         }
1144                                         dl= dl->next;
1145                                 }
1146                         }
1147                 }
1148         }
1149         else if(cu->ext1==0.0 && cu->ext2==0.0) {
1150                 ;
1151         }
1152         else if(cu->ext2==0.0) {
1153                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1154                 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1155                 BLI_addtail(disp, dl);
1156                 dl->type= DL_SEGM;
1157                 dl->parts= 1;
1158                 dl->flag= DL_FRONT_CURVE|DL_BACK_CURVE;
1159                 dl->nr= 2;
1160                 
1161                 fp= dl->verts;
1162                 fp[0]= fp[1]= 0.0;
1163                 fp[2]= -cu->ext1;
1164                 fp[3]= fp[4]= 0.0;
1165                 fp[5]= cu->ext1;
1166         }
1167         else if( (cu->flag & (CU_FRONT|CU_BACK))==0 && cu->ext1==0.0f)  { // we make a full round bevel in that case
1168                 
1169                 nr= 4+ 2*cu->bevresol;
1170                    
1171                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1172                 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1173                 BLI_addtail(disp, dl);
1174                 dl->type= DL_POLY;
1175                 dl->parts= 1;
1176                 dl->flag= DL_BACK_CURVE;
1177                 dl->nr= nr;
1178
1179                 /* a circle */
1180                 fp= dl->verts;
1181                 dangle= (2.0f*M_PI/(nr));
1182                 angle= -(nr-1)*dangle;
1183                 
1184                 for(a=0; a<nr; a++) {
1185                         fp[0]= 0.0;
1186                         fp[1]= (float)(cos(angle)*(cu->ext2));
1187                         fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1188                         angle+= dangle;
1189                         fp+= 3;
1190                 }
1191         }
1192         else {
1193                 short dnr;
1194                 
1195                 /* bevel now in three parts, for proper vertex normals */
1196                 /* part 1 */
1197                 dnr= nr= 2+ cu->bevresol;
1198                 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1199                         nr= 3+ 2*cu->bevresol;
1200                    
1201                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p1");
1202                 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p1");
1203                 BLI_addtail(disp, dl);
1204                 dl->type= DL_SEGM;
1205                 dl->parts= 1;
1206                 dl->flag= DL_BACK_CURVE;
1207                 dl->nr= nr;
1208
1209                 /* half a circle */
1210                 fp= dl->verts;
1211                 dangle= (0.5*M_PI/(dnr-1));
1212                 angle= -(nr-1)*dangle;
1213                 
1214                 for(a=0; a<nr; a++) {
1215                         fp[0]= 0.0;
1216                         fp[1]= (float)(cos(angle)*(cu->ext2));
1217                         fp[2]= (float)(sin(angle)*(cu->ext2)) - cu->ext1;
1218                         angle+= dangle;
1219                         fp+= 3;
1220                 }
1221                 
1222                 /* part 2, sidefaces */
1223                 if(cu->ext1!=0.0) {
1224                         nr= 2;
1225                         
1226                         dl= MEM_callocN(sizeof(DispList), "makebevelcurve p2");
1227                         dl->verts= MEM_callocN(nr*3*sizeof(float), "makebevelcurve p2");
1228                         BLI_addtail(disp, dl);
1229                         dl->type= DL_SEGM;
1230                         dl->parts= 1;
1231                         dl->nr= nr;
1232                         
1233                         fp= dl->verts;
1234                         fp[1]= cu->ext2;
1235                         fp[2]= -cu->ext1;
1236                         fp[4]= cu->ext2;
1237                         fp[5]= cu->ext1;
1238                         
1239                         if( (cu->flag & (CU_FRONT|CU_BACK))==0) {
1240                                 dl= MEM_dupallocN(dl);
1241                                 dl->verts= MEM_dupallocN(dl->verts);
1242                                 BLI_addtail(disp, dl);
1243                                 
1244                                 fp= dl->verts;
1245                                 fp[1]= -fp[1];
1246                                 fp[2]= -fp[2];
1247                                 fp[4]= -fp[4];
1248                                 fp[5]= -fp[5];
1249                         }
1250                 }
1251                 
1252                 /* part 3 */
1253                 dnr= nr= 2+ cu->bevresol;
1254                 if( (cu->flag & (CU_FRONT|CU_BACK))==0)
1255                         nr= 3+ 2*cu->bevresol;
1256                 
1257                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve p3");
1258                 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve p3");
1259                 BLI_addtail(disp, dl);
1260                 dl->type= DL_SEGM;
1261                 dl->flag= DL_FRONT_CURVE;
1262                 dl->parts= 1;
1263                 dl->nr= nr;
1264                 
1265                 /* half a circle */
1266                 fp= dl->verts;
1267                 angle= 0.0;
1268                 dangle= (0.5*M_PI/(dnr-1));
1269                 
1270                 for(a=0; a<nr; a++) {
1271                         fp[0]= 0.0;
1272                         fp[1]= (float)(cos(angle)*(cu->ext2));
1273                         fp[2]= (float)(sin(angle)*(cu->ext2)) + cu->ext1;
1274                         angle+= dangle;
1275                         fp+= 3;
1276                 }
1277         }
1278 }
1279
1280 int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1281 {
1282         /* return:
1283                 -1: colliniar
1284                  0: no intersection of segments
1285                  1: exact intersection of segments
1286                  2: cross-intersection of segments
1287         */
1288         float deler;
1289
1290         deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1291         if(deler==0.0) return -1;
1292
1293         *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1294         *labda= -(*labda/deler);
1295
1296         deler= v3[coy]-v4[coy];
1297         if(deler==0) {
1298                 deler=v3[cox]-v4[cox];
1299                 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1300         } else {
1301                 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1302         }
1303         vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1304         vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1305
1306         if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1307                 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1308                 return 2;
1309         }
1310         return 0;
1311 }
1312
1313
1314 static short bevelinside(BevList *bl1,BevList *bl2)
1315 {
1316         /* is bl2 INSIDE bl1 ? with left-right method and "labda's" */
1317         /* returns '1' if correct hole  */
1318         BevPoint *bevp, *prevbevp;
1319         float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1320         int nr, links=0,rechts=0,mode;
1321
1322         /* take first vertex of possible hole */
1323
1324         bevp= (BevPoint *)(bl2+1);
1325         hvec1[0]= bevp->x; 
1326         hvec1[1]= bevp->y; 
1327         hvec1[2]= 0.0;
1328         VECCOPY(hvec2,hvec1);
1329         hvec2[0]+=1000;
1330
1331         /* test it with all edges of potential surounding poly */
1332         /* count number of transitions left-right  */
1333
1334         bevp= (BevPoint *)(bl1+1);
1335         nr= bl1->nr;
1336         prevbevp= bevp+(nr-1);
1337
1338         while(nr--) {
1339                 min= prevbevp->y;
1340                 max= bevp->y;
1341                 if(max<min) {
1342                         min= max;
1343                         max= prevbevp->y;
1344                 }
1345                 if(min!=max) {
1346                         if(min<=hvec1[1] && max>=hvec1[1]) {
1347                                 /* there's a transition, calc intersection point */
1348                                 mode= cu_isectLL(&(prevbevp->x),&(bevp->x),hvec1,hvec2,0,1,&lab,&mu,vec);
1349                                 /* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
1350                                    only allow for one situation: we choose lab= 1.0
1351                                  */
1352                                 if(mode>=0 && lab!=0.0) {
1353                                         if(vec[0]<hvec1[0]) links++;
1354                                         else rechts++;
1355                                 }
1356                         }
1357                 }
1358                 prevbevp= bevp;
1359                 bevp++;
1360         }
1361         
1362         if( (links & 1) && (rechts & 1) ) return 1;
1363         return 0;
1364 }
1365
1366
1367 struct bevelsort {
1368         float left;
1369         BevList *bl;
1370         int dir;
1371 };
1372
1373 static int vergxcobev(const void *a1, const void *a2)
1374 {
1375         const struct bevelsort *x1=a1,*x2=a2;
1376
1377         if( x1->left > x2->left ) return 1;
1378         else if( x1->left < x2->left) return -1;
1379         return 0;
1380 }
1381
1382 /* this function cannot be replaced with atan2, but why? */
1383
1384 static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1385 {
1386         float t01, t02, x3, y3;
1387
1388         t01= (float)sqrt(x1*x1+y1*y1);
1389         t02= (float)sqrt(x2*x2+y2*y2);
1390         if(t01==0.0) t01= 1.0;
1391         if(t02==0.0) t02= 1.0;
1392
1393         x1/=t01; 
1394         y1/=t01;
1395         x2/=t02; 
1396         y2/=t02;
1397
1398         t02= x1*x2+y1*y2;
1399         if(fabs(t02)>=1.0) t02= .5*M_PI;
1400         else t02= (saacos(t02))/2.0f;
1401
1402         t02= (float)sin(t02);
1403         if(t02==0.0) t02= 1.0;
1404
1405         x3= x1-x2;
1406         y3= y1-y2;
1407         if(x3==0 && y3==0) {
1408                 x3= y1;
1409                 y3= -x1;
1410         } else {
1411                 t01= (float)sqrt(x3*x3+y3*y3);
1412                 x3/=t01; 
1413                 y3/=t01;
1414         }
1415
1416         *sina= -y3/t02;
1417         *cosa= x3/t02;
1418
1419 }
1420
1421 static void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *data_a, int resolu)
1422 {
1423         BezTriple *pprev, *next, *last;
1424         float fac, dfac, t[4];
1425         int a;
1426         
1427         last= nu->bezt+(nu->pntsu-1);
1428         
1429         /* returns a point */
1430         if(prevbezt==nu->bezt) {
1431                 if(nu->flagu & 1) pprev= last;
1432                 else pprev= prevbezt;
1433         }
1434         else pprev= prevbezt-1;
1435         
1436         /* next point */
1437         if(bezt==last) {
1438                 if(nu->flagu & 1) next= nu->bezt;
1439                 else next= bezt;
1440         }
1441         else next= bezt+1;
1442         
1443         fac= 0.0;
1444         dfac= 1.0f/(float)resolu;
1445         
1446         for(a=0; a<resolu; a++, fac+= dfac) {
1447                 
1448                 set_four_ipo(fac, t, nu->tilt_interp);
1449                 
1450                 data_a[a]= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1451         }
1452 }
1453
1454 void makeBevelList(Object *ob)
1455 {
1456         /*
1457          - convert all curves to polys, with indication of resol and flags for double-vertices
1458          - possibly; do a smart vertice removal (in case Nurb)
1459          - separate in individual blicks with BoundBox
1460          - AutoHole detection
1461         */
1462         Curve *cu;
1463         Nurb *nu;
1464         BezTriple *bezt, *prevbezt;
1465         BPoint *bp;
1466         BevList *bl, *blnew, *blnext;
1467         BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
1468         float  *data, *data_a, *v1, *v2, min, inp, x1, x2, y1, y2, vec[3];
1469         struct bevelsort *sortdata, *sd, *sd1;
1470         int a, b, len, nr, poly, resolu;
1471
1472         /* this function needs an object, because of tflag and upflag */
1473         cu= ob->data;
1474
1475         /* STEP 1: MAKE POLYS  */
1476
1477         BLI_freelistN(&(cu->bev));
1478         if(ob==G.obedit && ob->type!=OB_FONT) nu= editNurb.first;
1479         else nu= cu->nurb.first;
1480         
1481         while(nu) {
1482                 if(nu->pntsu<=1) {
1483                         bl= MEM_callocN(sizeof(BevList)+1*sizeof(BevPoint), "makeBevelList");
1484                         BLI_addtail(&(cu->bev), bl);
1485                         bl->nr= 0;
1486                 } else {
1487                         if(G.rendering && cu->resolu_ren!=0) 
1488                                 resolu= cu->resolu_ren;
1489                         else
1490                                 resolu= nu->resolu;
1491                         
1492                         if((nu->type & 7)==CU_POLY) {
1493                                 
1494                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList");
1495                                 BLI_addtail(&(cu->bev), bl);
1496         
1497                                 if(nu->flagu & 1) bl->poly= 0;
1498                                 else bl->poly= -1;
1499                                 bl->nr= len;
1500                                 bl->flag= 0;
1501                                 bevp= (BevPoint *)(bl+1);
1502                                 bp= nu->bp;
1503         
1504                                 while(len--) {
1505                                         bevp->x= bp->vec[0];
1506                                         bevp->y= bp->vec[1];
1507                                         bevp->z= bp->vec[2];
1508                                         bevp->alfa= bp->alfa;
1509                                         bevp->f1= 1;
1510                                         bevp++;
1511                                         bp++;
1512                                 }
1513                         }
1514                         else if((nu->type & 7)==CU_BEZIER) {
1515         
1516                                 len= resolu*(nu->pntsu+ (nu->flagu & 1) -1)+1;  /* in case last point is not cyclic */
1517                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList");
1518                                 BLI_addtail(&(cu->bev), bl);
1519         
1520                                 if(nu->flagu & 1) bl->poly= 0;
1521                                 else bl->poly= -1;
1522                                 bevp= (BevPoint *)(bl+1);
1523         
1524                                 a= nu->pntsu-1;
1525                                 bezt= nu->bezt;
1526                                 if(nu->flagu & 1) {
1527                                         a++;
1528                                         prevbezt= nu->bezt+(nu->pntsu-1);
1529                                 }
1530                                 else {
1531                                         prevbezt= bezt;
1532                                         bezt++;
1533                                 }
1534                                 
1535                                 data= MEM_mallocN(3*sizeof(float)*(resolu+1), "makeBevelList2");
1536                                 data_a= MEM_callocN(sizeof(float)*(resolu+1), "data_a");
1537                                 
1538                                 while(a--) {
1539                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
1540         
1541                                                 bevp->x= prevbezt->vec[1][0];
1542                                                 bevp->y= prevbezt->vec[1][1];
1543                                                 bevp->z= prevbezt->vec[1][2];
1544                                                 bevp->alfa= prevbezt->alfa;
1545                                                 bevp->f1= SELECT;
1546                                                 bevp->f2= 0;
1547                                                 bevp++;
1548                                                 bl->nr++;
1549                                                 bl->flag= 1;
1550                                         }
1551                                         else {
1552                                                 v1= prevbezt->vec[1];
1553                                                 v2= bezt->vec[0];
1554                                                 
1555                                                 /* always do all three, to prevent data hanging around */
1556                                                 forward_diff_bezier(v1[0], v1[3], v2[0], v2[3], data, resolu, 3);
1557                                                 forward_diff_bezier(v1[1], v1[4], v2[1], v2[4], data+1, resolu, 3);
1558                                                 forward_diff_bezier(v1[2], v1[5], v2[2], v2[5], data+2, resolu, 3);
1559                                                 
1560                                                 if((nu->type & CU_2D)==0) {
1561                                                         if(cu->flag & CU_3D) {
1562                                                                 alfa_bezpart(prevbezt, bezt, nu, data_a, resolu);
1563                                                         }
1564                                                 }
1565                                                 
1566                                                 
1567                                                 /* indicate with handlecodes double points */
1568                                                 if(prevbezt->h1==prevbezt->h2) {
1569                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1;
1570                                                 }
1571                                                 else {
1572                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1;
1573                                                         else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->f1= 1;
1574                                                 }
1575                                                 
1576                                                 v1= data;
1577                                                 v2= data_a;
1578                                                 nr= resolu;
1579                                                 
1580                                                 while(nr--) {
1581                                                         bevp->x= v1[0]; 
1582                                                         bevp->y= v1[1];
1583                                                         bevp->z= v1[2];
1584                                                         bevp->alfa= v2[0];
1585                                                         bevp++;
1586                                                         v1+=3;
1587                                                         v2++;
1588                                                 }
1589                                                 bl->nr+= resolu;
1590         
1591                                         }
1592                                         prevbezt= bezt;
1593                                         bezt++;
1594                                 }
1595                                 
1596                                 MEM_freeN(data);
1597                                 MEM_freeN(data_a);
1598                                 
1599                                 if((nu->flagu & 1)==0) {            /* not cyclic: endpoint */
1600                                         bevp->x= prevbezt->vec[1][0];
1601                                         bevp->y= prevbezt->vec[1][1];
1602                                         bevp->z= prevbezt->vec[1][2];
1603                                         bevp->alfa= prevbezt->alfa;
1604                                         bl->nr++;
1605                                 }
1606         
1607                         }
1608                         else if((nu->type & 7)==CU_NURBS) {
1609                                 if(nu->pntsv==1) {
1610                                         len= resolu*nu->pntsu;
1611                                         bl= MEM_mallocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
1612                                         BLI_addtail(&(cu->bev), bl);
1613                                         bl->nr= len;
1614                                         bl->flag= 0;
1615                                         if(nu->flagu & 1) bl->poly= 0;
1616                                         else bl->poly= -1;
1617                                         bevp= (BevPoint *)(bl+1);
1618         
1619                                         data= MEM_callocN(4*sizeof(float)*len, "makeBevelList4");    /* has to be zero-ed */
1620                                         makeNurbcurve(nu, data, resolu, 4);
1621                                         
1622                                         v1= data;
1623                                         while(len--) {
1624                                                 bevp->x= v1[0]; 
1625                                                 bevp->y= v1[1];
1626                                                 bevp->z= v1[2];
1627                                                 bevp->alfa= v1[3];
1628                                                 
1629                                                 bevp->f1= bevp->f2= 0;
1630                                                 bevp++;
1631                                                 v1+=4;
1632                                         }
1633                                         MEM_freeN(data);
1634                                 }
1635                         }
1636                 }
1637                 nu= nu->next;
1638         }
1639
1640         /* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
1641         bl= cu->bev.first;
1642         while(bl) {
1643                 if (bl->nr) { /* null bevel items come from single points */
1644                         nr= bl->nr;
1645                         bevp1= (BevPoint *)(bl+1);
1646                         bevp0= bevp1+(nr-1);
1647                         nr--;
1648                         while(nr--) {
1649                                 if( fabs(bevp0->x-bevp1->x)<0.00001 ) {
1650                                         if( fabs(bevp0->y-bevp1->y)<0.00001 ) {
1651                                                 if( fabs(bevp0->z-bevp1->z)<0.00001 ) {
1652                                                         bevp0->f2= SELECT;
1653                                                         bl->flag++;
1654                                                 }
1655                                         }
1656                                 }
1657                                 bevp0= bevp1;
1658                                 bevp1++;
1659                         }
1660                 }
1661                 bl= bl->next;
1662         }
1663         bl= cu->bev.first;
1664         while(bl) {
1665                 blnext= bl->next;
1666                 if(bl->nr && bl->flag) {
1667                         nr= bl->nr- bl->flag+1; /* +1 because vectorbezier sets flag too */
1668                         blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList");
1669                         memcpy(blnew, bl, sizeof(BevList));
1670                         blnew->nr= 0;
1671                         BLI_remlink(&(cu->bev), bl);
1672                         BLI_insertlinkbefore(&(cu->bev),blnext,blnew);  /* to make sure bevlijst is tuned with nurblist */
1673                         bevp0= (BevPoint *)(bl+1);
1674                         bevp1= (BevPoint *)(blnew+1);
1675                         nr= bl->nr;
1676                         while(nr--) {
1677                                 if(bevp0->f2==0) {
1678                                         memcpy(bevp1, bevp0, sizeof(BevPoint));
1679                                         bevp1++;
1680                                         blnew->nr++;
1681                                 }
1682                                 bevp0++;
1683                         }
1684                         MEM_freeN(bl);
1685                         blnew->flag= 0;
1686                 }
1687                 bl= blnext;
1688         }
1689
1690         /* STEP 3: COUNT POLYS TELLEN AND AUTOHOLE */
1691         bl= cu->bev.first;
1692         poly= 0;
1693         while(bl) {
1694                 if(bl->nr && bl->poly>=0) {
1695                         poly++;
1696                         bl->poly= poly;
1697                         bl->gat= 0;     /* 'gat' is dutch for hole */
1698                 }
1699                 bl= bl->next;
1700         }
1701         
1702
1703         /* find extreme left points, also test (turning) direction */
1704         if(poly>0) {
1705                 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
1706                 bl= cu->bev.first;
1707                 while(bl) {
1708                         if(bl->poly>0) {
1709
1710                                 min= 300000.0;
1711                                 bevp= (BevPoint *)(bl+1);
1712                                 nr= bl->nr;
1713                                 while(nr--) {
1714                                         if(min>bevp->x) {
1715                                                 min= bevp->x;
1716                                                 bevp1= bevp;
1717                                         }
1718                                         bevp++;
1719                                 }
1720                                 sd->bl= bl;
1721                                 sd->left= min;
1722
1723                                 bevp= (BevPoint *)(bl+1);
1724                                 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
1725                                 else bevp0= bevp1-1;
1726                                 bevp= bevp+ (bl->nr-1);
1727                                 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
1728                                 else bevp2= bevp1+1;
1729
1730                                 inp= (bevp1->x- bevp0->x)*(bevp0->y- bevp2->y)
1731                                     +(bevp0->y- bevp1->y)*(bevp0->x- bevp2->x);
1732
1733                                 if(inp>0.0) sd->dir= 1;
1734                                 else sd->dir= 0;
1735
1736                                 sd++;
1737                         }
1738
1739                         bl= bl->next;
1740                 }
1741                 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
1742
1743                 sd= sortdata+1;
1744                 for(a=1; a<poly; a++, sd++) {
1745                         bl= sd->bl;         /* is bl a hole? */
1746                         sd1= sortdata+ (a-1);
1747                         for(b=a-1; b>=0; b--, sd1--) {  /* all polys to the left */
1748                                 if(bevelinside(sd1->bl, bl)) {
1749                                         bl->gat= 1- sd1->bl->gat;
1750                                         break;
1751                                 }
1752                         }
1753                 }
1754
1755                 /* turning direction */
1756                 if((cu->flag & CU_3D)==0) {
1757                         sd= sortdata;
1758                         for(a=0; a<poly; a++, sd++) {
1759                                 if(sd->bl->gat==sd->dir) {
1760                                         bl= sd->bl;
1761                                         bevp1= (BevPoint *)(bl+1);
1762                                         bevp2= bevp1+ (bl->nr-1);
1763                                         nr= bl->nr/2;
1764                                         while(nr--) {
1765                                                 SWAP(BevPoint, *bevp1, *bevp2);
1766                                                 bevp1++;
1767                                                 bevp2--;
1768                                         }
1769                                 }
1770                         }
1771                 }
1772                 MEM_freeN(sortdata);
1773         }
1774
1775         /* STEP 4: COSINES */
1776         bl= cu->bev.first;
1777         while(bl) {
1778         
1779                 if(bl->nr==2) { /* 2 pnt, treat separate */
1780                         bevp2= (BevPoint *)(bl+1);
1781                         bevp1= bevp2+1;
1782
1783                         x1= bevp1->x- bevp2->x;
1784                         y1= bevp1->y- bevp2->y;
1785
1786                         calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
1787                         bevp2->sina= bevp1->sina;
1788                         bevp2->cosa= bevp1->cosa;
1789
1790                         if(cu->flag & CU_3D) {  /* 3D */
1791                                 float *quat, q[4];
1792                         
1793                                 vec[0]= bevp1->x - bevp2->x;
1794                                 vec[1]= bevp1->y - bevp2->y;
1795                                 vec[2]= bevp1->z - bevp2->z;
1796                                 
1797                                 quat= vectoquat(vec, 5, 1);
1798                                 
1799                                 Normalize(vec);
1800                                 q[0]= (float)cos(0.5*bevp1->alfa);
1801                                 x1= (float)sin(0.5*bevp1->alfa);
1802                                 q[1]= x1*vec[0];
1803                                 q[2]= x1*vec[1];
1804                                 q[3]= x1*vec[2];
1805                                 QuatMul(quat, q, quat);
1806                                 
1807                                 QuatToMat3(quat, bevp1->mat);
1808                                 Mat3CpyMat3(bevp2->mat, bevp1->mat);
1809                         }
1810
1811                 }
1812                 else if(bl->nr>2) {
1813                         bevp2= (BevPoint *)(bl+1);
1814                         bevp1= bevp2+(bl->nr-1);
1815                         bevp0= bevp1-1;
1816
1817                 
1818                         nr= bl->nr;
1819         
1820                         while(nr--) {
1821         
1822                                 if(cu->flag & CU_3D) {  /* 3D */
1823                                         float *quat, q[4];
1824                                 
1825                                         vec[0]= bevp2->x - bevp0->x;
1826                                         vec[1]= bevp2->y - bevp0->y;
1827                                         vec[2]= bevp2->z - bevp0->z;
1828                                         
1829                                         Normalize(vec);
1830
1831                                         quat= vectoquat(vec, 5, 1);
1832                                         
1833                                         q[0]= (float)cos(0.5*bevp1->alfa);
1834                                         x1= (float)sin(0.5*bevp1->alfa);
1835                                         q[1]= x1*vec[0];
1836                                         q[2]= x1*vec[1];
1837                                         q[3]= x1*vec[2];
1838                                         QuatMul(quat, q, quat);
1839                                         
1840                                         QuatToMat3(quat, bevp1->mat);
1841                                 }
1842                                 
1843                                 x1= bevp1->x- bevp0->x;
1844                                 x2= bevp1->x- bevp2->x;
1845                                 y1= bevp1->y- bevp0->y;
1846                                 y2= bevp1->y- bevp2->y;
1847                         
1848                                 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
1849                                 
1850                                 
1851                                 bevp0= bevp1;
1852                                 bevp1= bevp2;
1853                                 bevp2++;
1854                         }
1855                         /* correct non-cyclic cases */
1856                         if(bl->poly== -1) {
1857                                 if(bl->nr>2) {
1858                                         bevp= (BevPoint *)(bl+1);
1859                                         bevp1= bevp+1;
1860                                         bevp->sina= bevp1->sina;
1861                                         bevp->cosa= bevp1->cosa;
1862                                         Mat3CpyMat3(bevp->mat, bevp1->mat);
1863                                         bevp= (BevPoint *)(bl+1);
1864                                         bevp+= (bl->nr-1);
1865                                         bevp1= bevp-1;
1866                                         bevp->sina= bevp1->sina;
1867                                         bevp->cosa= bevp1->cosa;
1868                                         Mat3CpyMat3(bevp->mat, bevp1->mat);
1869                                 }
1870                         }
1871                 }
1872                 bl= bl->next;
1873         }
1874 }
1875
1876 /* calculates a bevel width (radius) for a particular subdivided curve part,
1877  * based on the radius value of the surrounding CVs */
1878 float calc_curve_subdiv_radius(Curve *cu, Nurb *nu, int cursubdiv)
1879 {
1880         BezTriple *bezt, *beztfirst, *beztlast, *beztnext, *beztprev;
1881         BPoint *bp, *bpfirst, *bplast;
1882         int resolu;
1883         float prevrad=0.0, nextrad=0.0, rad=0.0, ratio=0.0;
1884         int vectseg=0, subdivs=0;
1885         
1886         if((nu==NULL) || (nu->pntsu<=1)) return 1.0; 
1887         bezt= nu->bezt;
1888         bp = nu->bp;
1889         
1890         if(G.rendering && cu->resolu_ren!=0) resolu= cu->resolu_ren;
1891         else resolu= nu->resolu;
1892         
1893         if(((nu->type & 7)==CU_BEZIER) && (bezt != NULL)) {
1894                 beztfirst = nu->bezt;
1895                 beztlast = nu->bezt + (nu->pntsu - 1);
1896                 
1897                 /* loop through the CVs to end up with a pointer to the CV before the subdiv in question, and a ratio
1898                  * of how far that subdiv is between this CV and the next */
1899                 while(bezt<=beztlast) {
1900                         beztnext = bezt+1;
1901                         beztprev = bezt-1;
1902                         vectseg=0;
1903                         
1904                         if (subdivs==cursubdiv) {
1905                                 ratio= 0.0;
1906                                 break;
1907                         }
1908
1909                         /* check to see if we're looking at a vector segment (no subdivisions) */
1910                         if (nu->flagu & CU_CYCLIC) {
1911                                 if (bezt == beztfirst) {
1912                                         if ((beztlast->h2==HD_VECT) && (bezt->h1==HD_VECT)) vectseg = 1;
1913                                 } else {
1914                                         if ((beztprev->h2==HD_VECT) && (bezt->h1==HD_VECT)) vectseg = 1;
1915                                 }
1916                         } else if ((bezt->h2==HD_VECT) && (beztnext->h1==HD_VECT)) vectseg = 1;
1917                         
1918                         
1919                         if (vectseg==0) {
1920                                 /* if it's NOT a vector segment, check to see if the subdiv falls within the segment */
1921                                 subdivs += resolu;
1922                                 
1923                                 if (cursubdiv < subdivs) {
1924                                         ratio = 1.0 - ((subdivs - cursubdiv)/(float)resolu);
1925                                         break;
1926                                 }
1927                         } else {
1928                                 /* must be a vector segment.. loop again! */
1929                                 subdivs += 1;
1930                         } 
1931                         
1932                         bezt++;
1933                 }
1934                 
1935                 /* Now we have a nice bezt pointer to the CV that we want. But cyclic messes it up, so must correct for that..
1936                  * (cyclic goes last-> first -> first+1 -> first+2 -> ...) */
1937                 if (nu->flagu & CU_CYCLIC) {
1938                         if (bezt == beztfirst) bezt = beztlast;
1939                         else bezt--;
1940                 }
1941                  
1942                 /* find the radii at the bounding CVs and interpolate between them based on ratio */
1943                 rad = prevrad = bezt->radius;
1944                 
1945                 if ((bezt == beztlast) && (nu->flagu & CU_CYCLIC)) { /* loop around */
1946                         bezt= beztfirst;
1947                 } else if (bezt != beztlast) {
1948                         bezt++;
1949                 }
1950                 nextrad = bezt->radius;
1951                 
1952         }
1953         else if( ( ((nu->type & 7)==CU_NURBS) || ((nu->type & 7)==CU_POLY)) && (bp != NULL)) {
1954                 /* follows similar algo as for bezt above */
1955                 bpfirst = nu->bp;
1956                 bplast = nu->bp + (nu->pntsu - 1);
1957                 
1958                 if ((nu->type & 7)==CU_POLY) resolu=1;
1959                 
1960                 while(bp<=bplast) {
1961                         if (subdivs==cursubdiv) {
1962                                 ratio= 0.0;
1963                                 break;
1964                         }
1965                         
1966                         subdivs += resolu;
1967
1968                         if (cursubdiv < subdivs) {
1969                                 ratio = 1.0 - ((subdivs - cursubdiv)/(float)resolu);
1970                                 break;
1971                         }
1972
1973                         bp++;
1974                 }
1975                 
1976                 if ( ((nu->type & 7)==CU_NURBS) && (nu->flagu & CU_CYCLIC)) {
1977                         if (bp == bplast) bp = bpfirst;
1978                         else bp++;
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 }