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