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