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