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