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