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