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