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