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