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