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