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