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