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