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