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