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