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