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