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