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