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