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