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