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