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