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