some small optimizations
[blender.git] / source / blender / blenkernel / intern / curve.c
1
2 /*  curve.c      MIXED MODEL
3  * 
4  *  maart 95
5  *  
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  */
36
37 #define STRUBI hack
38
39 #include <math.h>  // floor
40 #include <string.h>
41 #include <stdlib.h>  
42
43 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46 #include "MEM_guardedalloc.h"
47 #include "BLI_blenlib.h"  
48 #include "BLI_arithb.h"  
49
50 #include "DNA_object_types.h"  
51 #include "DNA_curve_types.h"  
52 #include "DNA_material_types.h"  
53 #include "DNA_mesh_types.h"
54
55 /* for dereferencing pointers */
56 #include "DNA_ID.h"  
57 #include "DNA_vfont_types.h"  
58 #include "DNA_key_types.h"  
59 #include "DNA_ipo_types.h"  
60
61 #include "BKE_global.h" 
62 #include "BKE_main.h"  
63 #include "BKE_utildefines.h"  // VECCOPY
64 #include "BKE_object.h"  
65 #include "BKE_mesh.h" 
66 #include "BKE_curve.h"  
67 #include "BKE_displist.h"  
68 #include "BKE_ipo.h"  
69 #include "BKE_anim.h"  
70 #include "BKE_library.h"  
71 #include "BKE_key.h"  
72
73
74 /* globals */
75
76 extern ListBase editNurb;  /* editcurve.c */
77
78 /* local */
79 int cu_isectLL(float *v1, float *v2, float *v3, float *v4, 
80                            short cox, short coy, 
81                            float *labda, float *mu, float *vec);
82
83
84 #ifdef STRUBI
85 /* hotfix; copies x*y array into extended (x+dx)*(y+dy) array
86 old[] and new[] can be the same ! */
87 int copyintoExtendedArray(float *old, int oldx, int oldy, float *new, int newx, int newy)
88 {
89         int x, y, ttt, ooo;
90         float *oldp, *newp;
91         
92         if (newx < oldx || newy < oldy) return 0;
93         
94                 
95         for (y = newy - 1; y >= oldy; y--) {    
96                 ttt = y * newx;
97                 for (x = newx - 1; x >= 0; x--) {
98                         newp = new + 3 * (ttt + x);
99                         newp[0] = 0.0; newp[1] = 0.0; newp[2] = 0.0;
100                 }
101         }       
102
103         for (; y >= 0; y--) {
104                 ttt = y * newx;
105                 ooo = y * oldx;
106                 for (x = newx - 1; x >= oldx; x--) {    
107                         newp = new + 3 * (ttt + x);
108                         newp[0] = 0.0; newp[1] = 0.0; newp[2] = 0.0;
109                 }
110                 for (; x  >= 0; x--) {
111                         oldp = old + 3 * (ooo + x);
112                         newp = new + 3 * (ttt + x);
113                         VECCOPY(newp, oldp);
114                 }
115         }
116         return 1;
117 }
118 #endif
119
120 void unlink_curve(Curve *cu)
121 {
122         int a;
123         
124         for(a=0; a<cu->totcol; a++) {
125                 if(cu->mat[a]) cu->mat[a]->id.us--;
126                 cu->mat[a]= 0;
127         }
128         if(cu->vfont) cu->vfont->id.us--; 
129         cu->vfont= 0;
130         if(cu->key) cu->key->id.us--;
131         cu->key= 0;
132         if(cu->ipo) cu->ipo->id.us--;
133         cu->ipo= 0;
134 }
135
136
137 /* niet curve zelf vrijgeven */
138 void free_curve(Curve *cu)
139 {
140
141         freeNurblist(&cu->nurb);
142         BLI_freelistN(&cu->bev);
143         freedisplist(&cu->disp);
144         
145         unlink_curve(cu);
146         
147         if(cu->mat) MEM_freeN(cu->mat);
148         if(cu->str) MEM_freeN(cu->str);
149         if(cu->bb) MEM_freeN(cu->bb);
150         if(cu->path) free_path(cu->path);
151 }
152
153 Curve *add_curve(int type)
154 {
155         Curve *cu;
156         char *str;
157         
158         if(type==OB_CURVE) str= "Curve";
159         else if(type==OB_SURF) str= "Surf";
160         else str= "Text";
161
162         cu= alloc_libblock(&G.main->curve, ID_CU, str);
163         
164         cu->size[0]= cu->size[1]= cu->size[2]= 1.0;
165         cu->flag= CU_FRONT+CU_BACK;
166         cu->pathlen= 100;
167         cu->resolu= cu->resolv= 6;
168         cu->width= 1.0;
169         cu->spacing= cu->linedist= 1.0;
170         cu->fsize= 1.0;
171         cu->texflag= AUTOSPACE;
172         
173         cu->bb= unit_boundbox();
174         
175         return cu;
176 }
177
178 Curve *copy_curve(Curve *cu)
179 {
180         Curve *cun;
181         int a;
182         
183         cun= copy_libblock(cu);
184         cun->nurb.first= cun->nurb.last= 0;
185         duplicateNurblist( &(cun->nurb), &(cu->nurb));
186
187         cun->mat= MEM_dupallocN(cu->mat);
188         for(a=0; a<cun->totcol; a++) {
189                 id_us_plus((ID *)cun->mat[a]);
190         }
191         
192         cun->str= MEM_dupallocN(cu->str);
193         cun->bb= MEM_dupallocN(cu->bb);
194         
195         cun->key= copy_key(cu->key);
196         if(cun->key) cun->key->from= (ID *)cun;
197         
198         cun->disp.first= cun->disp.last= 0;
199         cun->bev.first= cun->bev.last= 0;
200         cun->path= 0;
201
202         /* ook single user ipo */
203         if(cun->ipo) cun->ipo= copy_ipo(cun->ipo);
204
205         id_us_plus((ID *)cun->vfont);
206         
207         return cun;
208 }
209
210 void make_local_curve(Curve *cu)
211 {
212         Object *ob = 0;
213         Curve *cun;
214         int local=0, lib=0;
215         
216         /* - zijn er alleen lib users: niet doen
217          * - zijn er alleen locale users: flag zetten
218          * - mixed: copy
219          */
220         
221         if(cu->id.lib==0) return;
222         
223         if(cu->vfont) cu->vfont->id.lib= 0;
224         
225         if(cu->id.us==1) {
226                 cu->id.lib= 0;
227                 cu->id.flag= LIB_LOCAL;
228                 new_id(0, (ID *)cu, 0);
229                 return;
230         }
231         
232         ob= G.main->object.first;
233         while(ob) {
234                 if(ob->data==cu) {
235                         if(ob->id.lib) lib= 1;
236                         else local= 1;
237                 }
238                 ob= ob->id.next;
239         }
240         
241         if(local && lib==0) {
242                 cu->id.lib= 0;
243                 cu->id.flag= LIB_LOCAL;
244                 new_id(0, (ID *)cu, 0);
245         }
246         else if(local && lib) {
247                 cun= copy_curve(cu);
248                 cun->id.us= 0;
249                 
250                 ob= G.main->object.first;
251                 while(ob) {
252                         if(ob->data==cu) {
253                                 
254                                 if(ob->id.lib==0) {
255                                         ob->data= cun;
256                                         cun->id.us++;
257                                         cu->id.us--;
258                                 }
259                         }
260                         ob= ob->id.next;
261                 }
262         }
263 }
264
265
266 void test_curve_type(Object *ob)
267 {
268         Nurb *nu;
269         Curve *cu;
270         
271         cu= ob->data;
272         if(cu->vfont) {
273                 ob->type= OB_FONT;
274                 return;
275         }
276         else {
277                 nu= cu->nurb.first;
278                 while(nu) {
279                         if(nu->pntsv>1) {
280                                 ob->type= OB_SURF;
281                                 return;
282                         }
283                         nu= nu->next;
284                 }
285         }
286         ob->type= OB_CURVE;
287 }
288
289 void tex_space_curve(Curve *cu)
290 {
291         DispList *dl;
292         BoundBox *bb;
293         float *data, min[3], max[3], loc[3], size[3];
294         int tot, doit= 0;
295         
296         if(cu->bb==0) cu->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
297         bb= cu->bb;
298         
299         INIT_MINMAX(min, max);
300
301         dl= cu->disp.first;
302         while(dl) {
303                 
304                 if(dl->type==DL_INDEX3 || dl->type==DL_INDEX3) tot= dl->nr;
305                 else tot= dl->nr*dl->parts;
306                 
307                 if(tot) doit= 1;
308                 data= dl->verts;
309                 while(tot--) {
310                         DO_MINMAX(data, min, max);
311                         data+= 3;
312                 }
313                 dl= dl->next;
314         }
315
316         if(doit) {
317                 loc[0]= (min[0]+max[0])/2.0f;
318                 loc[1]= (min[1]+max[1])/2.0f;
319                 loc[2]= (min[2]+max[2])/2.0f;
320                 
321                 size[0]= (max[0]-min[0])/2.0f;
322                 size[1]= (max[1]-min[1])/2.0f;
323                 size[2]= (max[2]-min[2])/2.0f;
324         }
325         else {
326                 loc[0]= loc[1]= loc[2]= 0.0f;
327                 size[0]= size[1]= size[2]= 1.0f;
328         }
329         
330         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
331         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
332         
333         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
334         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
335
336         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
337         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
338
339         if(cu->texflag & AUTOSPACE) {
340                 VECCOPY(cu->loc, loc);
341                 VECCOPY(cu->size, size);
342                 cu->rot[0]= cu->rot[1]= cu->rot[2]= 0.0;
343
344                 if(cu->size[0]==0.0) cu->size[0]= 1.0;
345                 else if(cu->size[0]>0.0 && cu->size[0]<0.00001) cu->size[0]= 0.00001;
346                 else if(cu->size[0]<0.0 && cu->size[0]> -0.00001) cu->size[0]= -0.00001;
347         
348                 if(cu->size[1]==0.0) cu->size[1]= 1.0;
349                 else if(cu->size[1]>0.0 && cu->size[1]<0.00001) cu->size[1]= 0.00001;
350                 else if(cu->size[1]<0.0 && cu->size[1]> -0.00001) cu->size[1]= -0.00001;
351         
352                 if(cu->size[2]==0.0) cu->size[2]= 1.0;
353                 else if(cu->size[2]>0.0 && cu->size[2]<0.00001) cu->size[2]= 0.00001;
354                 else if(cu->size[2]<0.0 && cu->size[2]> -0.00001) cu->size[2]= -0.00001;
355
356         }
357 }
358
359
360 int count_curveverts(ListBase *nurb)
361 {
362         Nurb *nu;
363         int tot=0;
364         
365         nu= nurb->first;
366         while(nu) {
367                 if(nu->bezt) tot+= 3*nu->pntsu;
368                 else if(nu->bp) tot+= nu->pntsu*nu->pntsv;
369                 
370                 nu= nu->next;
371         }
372         return tot;
373 }
374
375
376
377 /* **************** NURBS ROUTINES ******************** */
378
379 void freeNurb(Nurb *nu)
380 {
381
382         if(nu==0) return;
383
384         if(nu->bezt) MEM_freeN(nu->bezt);
385         nu->bezt= 0;
386         if(nu->bp) MEM_freeN(nu->bp);
387         nu->bp= 0;
388         if(nu->knotsu) MEM_freeN(nu->knotsu);
389         nu->knotsu= 0;
390         if(nu->knotsv) MEM_freeN(nu->knotsv);
391         nu->knotsv= 0;
392         /* if(nu->trim.first) freeNurblist(&(nu->trim)); */
393
394         MEM_freeN(nu);
395
396 }
397
398
399 void freeNurblist(ListBase *lb)
400 {
401         Nurb *nu, *next;
402
403         if(lb==0) return;
404
405         nu= lb->first;
406         while(nu) {
407                 next= nu->next;
408                 freeNurb(nu);
409                 nu= next;
410         }
411         lb->first= lb->last= 0;
412 }
413
414 Nurb *duplicateNurb(Nurb *nu)
415 {
416         Nurb *newnu;
417         int len;
418
419         newnu= (Nurb*)MEM_mallocN(sizeof(Nurb),"duplicateNurb");
420         if(newnu==0) return 0;
421         memcpy(newnu, nu, sizeof(Nurb));
422
423         if(nu->bezt) {
424                 newnu->bezt=
425                         (BezTriple*)MEM_mallocN((nu->pntsu)* sizeof(BezTriple),"duplicateNurb2");
426                 memcpy(newnu->bezt, nu->bezt, nu->pntsu*sizeof(BezTriple));
427         }
428         else {
429                 len= nu->pntsu*nu->pntsv;
430                 newnu->bp=
431                         (BPoint*)MEM_mallocN((len)* sizeof(BPoint),"duplicateNurb3");
432                 memcpy(newnu->bp, nu->bp, len*sizeof(BPoint));
433                 
434                 newnu->knotsu=newnu->knotsv= 0;
435                 
436                 if(nu->knotsu) {
437                         len= KNOTSU(nu);
438                         if(len) {
439                                 newnu->knotsu= MEM_mallocN(len*sizeof(float), "duplicateNurb4");
440                                 memcpy(newnu->knotsu, nu->knotsu, sizeof(float)*len);
441                         }
442                 }
443                 if(nu->pntsv>1 && nu->knotsv) {
444                         len= KNOTSV(nu);
445                         if(len) {
446                                 newnu->knotsv= MEM_mallocN(len*sizeof(float), "duplicateNurb5");
447                                 memcpy(newnu->knotsv, nu->knotsv, sizeof(float)*len);
448                         }
449                 }
450         }
451         return newnu;
452 }
453
454 void duplicateNurblist(ListBase *lb1, ListBase *lb2)
455 {
456         Nurb *nu, *nun;
457         
458         freeNurblist(lb1);
459         
460         nu= lb2->first;
461         while(nu) {
462                 nun= duplicateNurb(nu);
463                 BLI_addtail(lb1, nun);
464                 
465                 nu= nu->next;
466         }
467 }
468
469 void test2DNurb(Nurb *nu)
470 {
471         BezTriple *bezt;
472         BPoint *bp;
473         int a;
474
475         if( nu->type== CU_BEZIER+CU_2D ) {
476                 a= nu->pntsu;
477                 bezt= nu->bezt;
478                 while(a--) {
479                         bezt->vec[0][2]= 0.0; 
480                         bezt->vec[1][2]= 0.0; 
481                         bezt->vec[2][2]= 0.0;
482                         bezt++;
483                 }
484         }
485         else if(nu->type & CU_2D) {
486                 a= nu->pntsu*nu->pntsv;
487                 bp= nu->bp;
488                 while(a--) {
489                         bp->vec[2]= 0.0;
490                         bp++;
491                 }
492         }
493 }
494
495 void minmaxNurb(Nurb *nu, float *min, float *max)
496 {
497         BezTriple *bezt;
498         BPoint *bp;
499         int a;
500
501         if( (nu->type & 7)==CU_BEZIER ) {
502                 a= nu->pntsu;
503                 bezt= nu->bezt;
504                 while(a--) {
505                         DO_MINMAX(bezt->vec[0], min, max);
506                         DO_MINMAX(bezt->vec[1], min, max);
507                         DO_MINMAX(bezt->vec[2], min, max);
508                         bezt++;
509                 }
510         }
511         else {
512                 a= nu->pntsu*nu->pntsv;
513                 bp= nu->bp;
514                 while(a--) {
515                         DO_MINMAX(bp->vec, min, max);
516                         bp++;
517                 }
518         }
519
520 }
521
522 /* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline berekeningen ~~~~~~~~~~~ */
523
524
525 /* voor de goede orde: eigenlijk horen hier doubles gebruikt te worden */
526
527 void extend_spline(float * pnts, int in, int out)
528 {
529         float *_pnts;
530         double * add;
531         int i, j, k, in2;
532
533         _pnts = pnts;
534         add = (double*)MEM_mallocN((in)* sizeof(double), "extend_spline");
535
536         in2 = in -1;
537
538         for (k = 3; k > 0; k--){
539                 pnts = _pnts;
540
541                 /* punten kopieren naar add */
542                 for (i = 0; i < in; i++){
543                         add[i] = *pnts;
544                         pnts += 3;
545                 }
546
547                 /* inverse forward differencen */
548                 for (i = 0; i < in2; i++){
549                         for (j = in2; j > i; j--){
550                                 add[j] -= add[j - 1];
551                         }
552                 }
553
554                 pnts = _pnts;
555                 for (i = out; i > 0; i--){
556                         *pnts = (float)(add[0]);
557                         pnts += 3;
558                         for (j = 0; j < in2; j++){
559                                 add[j] += add[j+1];
560                         }
561                 }
562
563                 _pnts++;
564         }
565
566         MEM_freeN(add);
567 }
568
569
570 void calcknots(float *knots, short aantal, short order, short type)
571 /* knots: aantal pnts NIET gecorrigeerd voor cyclic */
572 /* aantal, order, type;  0: uniform, 1: endpoints, 2: bezier */
573 {
574         float k;
575         int a, t;
576
577         t = aantal+order;
578         if(type==0) {
579
580                 for(a=0;a<t;a++) {
581                         knots[a]= (float)a;
582                 }
583         }
584         else if(type==1) {
585                 k= 0.0;
586                 for(a=1;a<=t;a++) {
587                         knots[a-1]= k;
588                         if(a>=order && a<=aantal) k+= 1.0;
589                 }
590         }
591         else if(type==2) {
592                 if(order==4) {
593                         k= 0.34;
594                         for(a=0;a<t;a++) {
595                                 knots[a]= (float)floor(k);
596                                 k+= (1.0/3.0);
597                         }
598                 }
599                 else if(order==3) {
600                         k= 0.6;
601                         for(a=0;a<t;a++) {
602                                 if(a>=order && a<=aantal) k+= (0.5);
603                                 knots[a]= (float)floor(k);
604                         }
605                 }
606         }
607 }
608
609 void makecyclicknots(float *knots, short pnts, short order)
610 /* pnts, order: aantal pnts NIET gecorrigeerd voor cyclic */
611 {
612         int a, b, order2, c;
613
614         if(knots==0) return;
615         order2=order-1;
616
617         /* eerst lange rijen (order -1) dezelfde knots aan uiteinde verwijderen */
618         if(order>2) {
619                 b= pnts+order2;
620                 for(a=1; a<order2; a++) {
621                         if(knots[b]!= knots[b-a]) break;
622                 }
623                 if(a==order2) knots[pnts+order-2]+= 1.0;
624         }
625
626         b= order;
627         c=pnts + order + order2;
628         for(a=pnts+order2; a<c; a++) {
629                 knots[a]= knots[a-1]+ (knots[b]-knots[b-1]);
630                 b--;
631         }
632 }
633
634
635 void makeknots(Nurb *nu, short uv, short type)  /* 0: uniform, 1: endpoints, 2: bezier */
636 {
637         if( (nu->type & 7)==CU_NURBS ) {
638                 if(uv & 1) {
639                         if(nu->knotsu) MEM_freeN(nu->knotsu);
640                         if(nu->pntsu>1) {
641                                 nu->knotsu= MEM_callocN(4+sizeof(float)*KNOTSU(nu), "makeknots");
642                                 calcknots(nu->knotsu, nu->pntsu, nu->orderu, type);
643                                 if(nu->flagu & 1) makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
644                         }
645                         else nu->knotsu= 0;
646                 }
647                 if(uv & 2) {
648                         if(nu->knotsv) MEM_freeN(nu->knotsv);
649                         if(nu->pntsv>1) {
650                                 nu->knotsv= MEM_callocN(4+sizeof(float)*KNOTSV(nu), "makeknots");
651                                 calcknots(nu->knotsv, nu->pntsv, nu->orderv, type);
652                                 if(nu->flagv & 1) makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
653                         }
654                         else nu->knotsv= 0;
655                 }
656         }
657 }
658
659 void basisNurb(float t, short order, short pnts, float *knots, float *basis, int *start, int *end)
660 {
661         float d, e;
662         int i, i1 = 0, i2 = 0 ,j, orderpluspnts, opp2, o2;
663
664         orderpluspnts= order+pnts;
665         opp2 = orderpluspnts-1;
666
667         /* this is for float inaccuracy */
668         if(t < knots[0]) t= knots[0];
669         else if(t > knots[opp2]) t= knots[opp2];
670
671         /* dit stuk is order '1' */
672         o2 = order + 1;
673         for(i=0;i<opp2;i++) {
674                 if(knots[i]!=knots[i+1] && t>= knots[i] && t<=knots[i+1]) {
675                         basis[i]= 1.0;
676                         i1= i-o2;
677                         if(i1<0) i1= 0;
678                         i2= i;
679                         i++;
680                         while(i<opp2) {
681                                 basis[i]= 0.0;
682                                 i++;
683                         }
684                         break;
685                 }
686                 else basis[i]= 0.0;
687         }
688         basis[i]= 0.0;
689
690         /* printf("u %f\n", t); for(k=0;k<orderpluspnts;k++) printf(" %2.2f",basis[k]); printf("\n");    */
691         
692         /* dit is order 2,3,... */
693         for(j=2; j<=order; j++) {
694
695                 if(i2+j>= orderpluspnts) i2= opp2-j;
696
697                 for(i= i1; i<=i2; i++) {
698                         if(basis[i]!=0.0)
699                                 d= ((t-knots[i])*basis[i]) / (knots[i+j-1]-knots[i]);
700                         else
701                                 d= 0.0;
702
703                         if(basis[i+1]!=0.0)
704                                 e= ((knots[i+j]-t)*basis[i+1]) / (knots[i+j]-knots[i+1]);
705                         else
706                                 e= 0.0;
707
708                         basis[i]= d+e;
709                 }
710         }
711
712         *start= 1000;
713         *end= 0;
714
715         for(i=i1; i<=i2; i++) {
716                 if(basis[i]>0.0) {
717                         *end= i;
718                         if(*start==1000) *start= i;
719                 }
720         }
721 }
722
723
724 void makeNurbfaces(Nurb *nu, float *data) 
725 /* data  moet 3*4*resolu*resolv lang zijn en op nul staan */
726 {
727         BPoint *bp;
728         float *basisu, *basis, *basisv, *sum, *fp, *in;
729         float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
730         int i, j, iofs, jofs, cycl, len, resolu, resolv;
731         int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
732
733         if(nu->knotsu==0 || nu->knotsv==0) return;
734         if(nu->orderu>nu->pntsu) return;
735         if(nu->orderv>nu->pntsv) return;
736         if(data==0) return;
737
738         /* alloceren en vars goedzetten */
739         len= nu->pntsu*nu->pntsv;
740         if(len==0) return;
741         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbfaces1");
742
743         resolu= nu->resolu;
744         resolv= nu->resolv;
745         len= resolu*resolv;
746         if(len==0) {
747                 MEM_freeN(sum);
748                 return;
749         }
750
751         bp= nu->bp;
752         i= nu->pntsu*nu->pntsv;
753         ratcomp=0;
754         while(i--) {
755                 if(bp->vec[3]!=1.0) {
756                         ratcomp= 1;
757                         break;
758                 }
759                 bp++;
760         }
761
762         fp= nu->knotsu;
763         ustart= fp[nu->orderu-1];
764         if(nu->flagu & 1) uend= fp[nu->pntsu+nu->orderu-1];
765         else uend= fp[nu->pntsu];
766         ustep= (uend-ustart)/(resolu-1+(nu->flagu & 1));
767         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbfaces3");
768
769         fp= nu->knotsv;
770         vstart= fp[nu->orderv-1];
771         
772         if(nu->flagv & 1) vend= fp[nu->pntsv+nu->orderv-1];
773         else vend= fp[nu->pntsv];
774         vstep= (vend-vstart)/(resolv-1+(nu->flagv & 1));
775         len= KNOTSV(nu);
776         basisv= (float *)MEM_mallocN(sizeof(float)*len*resolv, "makeNurbfaces3");
777         jstart= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces4");
778         jend= (int *)MEM_mallocN(sizeof(float)*resolv, "makeNurbfaces5");
779
780         /* voorberekenen basisv en jstart,jend */
781         if(nu->flagv & 1) cycl= nu->orderv-1; 
782         else cycl= 0;
783         v= vstart;
784         basis= basisv;
785         while(resolv--) {
786                 basisNurb(v, nu->orderv, (short)(nu->pntsv+cycl), nu->knotsv, basis, jstart+resolv, jend+resolv);
787                 basis+= KNOTSV(nu);
788                 v+= vstep;
789         }
790
791         if(nu->flagu & 1) cycl= nu->orderu-1; 
792         else cycl= 0;
793         in= data;
794         u= ustart;
795         while(resolu--) {
796
797                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
798
799                 basis= basisv;
800                 resolv= nu->resolv;
801                 while(resolv--) {
802
803                         jsta= jstart[resolv];
804                         jen= jend[resolv];
805
806                         /* bereken sum */
807                         sumdiv= 0.0;
808                         fp= sum;
809
810                         for(j= jsta; j<=jen; j++) {
811
812                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
813                                 else jofs= j;
814                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
815
816                                 for(i= istart; i<=iend; i++, fp++) {
817
818                                         if(i>= nu->pntsu) {
819                                                 iofs= i- nu->pntsu;
820                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
821                                         }
822                                         else bp++;
823
824                                         if(ratcomp) {
825                                                 *fp= basisu[i]*basis[j]*bp->vec[3];
826                                                 sumdiv+= *fp;
827                                         }
828                                         else *fp= basisu[i]*basis[j];
829                                 }
830                         }
831                 
832                         if(ratcomp) {
833                                 fp= sum;
834                                 for(j= jsta; j<=jen; j++) {
835                                         for(i= istart; i<=iend; i++, fp++) {
836                                                 *fp/= sumdiv;
837                                         }
838                                 }
839                         }
840
841                         /* een! (1.0) echt punt nu */
842                         fp= sum;
843                         for(j= jsta; j<=jen; j++) {
844
845                                 if(j>=nu->pntsv) jofs= (j - nu->pntsv);
846                                 else jofs= j;
847                                 bp= nu->bp+ nu->pntsu*jofs+istart-1;
848
849                                 for(i= istart; i<=iend; i++, fp++) {
850
851                                         if(i>= nu->pntsu) {
852                                                 iofs= i- nu->pntsu;
853                                                 bp= nu->bp+ nu->pntsu*jofs+iofs;
854                                         }
855                                         else bp++;
856
857                                         if(*fp!=0.0) {
858                                                 in[0]+= (*fp) * bp->vec[0];
859                                                 in[1]+= (*fp) * bp->vec[1];
860                                                 in[2]+= (*fp) * bp->vec[2];
861                                         }
862                                 }
863                         }
864
865                         in+=3;
866                         basis+= KNOTSV(nu);
867                 }
868                 u+= ustep;
869         }
870
871         /* vrijgeven */
872         MEM_freeN(sum);
873         MEM_freeN(basisu);
874         MEM_freeN(basisv);
875         MEM_freeN(jstart);
876         MEM_freeN(jend);
877 }
878
879
880 void makeNurbcurve_forw(Nurb *nu, float *data)
881 /* *data: moet 3*4*pntsu*resolu lang zijn en op nul staan */
882 {
883         BPoint *bp;
884         float *basisu, *sum, *fp,  *in;
885         float u, ustart, uend, ustep, sumdiv;
886         int i, j, k, len, resolu, istart, iend;
887         int wanted, org;
888
889         if(nu->knotsu==0) return;
890         if(data==0) return;
891
892         /* alloceren en vars goedzetten */
893         len= nu->pntsu;
894         if(len==0) return;
895         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
896
897         resolu= nu->resolu*nu->pntsu;
898         if(resolu==0) {
899                 MEM_freeN(sum);
900                 return;
901         }
902
903         fp= nu->knotsu;
904         ustart= fp[nu->orderu-1];
905         uend= fp[nu->pntsu];
906         ustep= (uend-ustart)/(resolu-1);
907         basisu= (float *)MEM_mallocN(sizeof(float)*(nu->orderu+nu->pntsu), "makeNurbcurve3");
908
909         in= data;
910         u= ustart;
911         for (k = nu->orderu - 1; k < nu->pntsu; k++){
912
913                 wanted = (int)((nu->knotsu[k+1] - nu->knotsu[k]) / ustep);
914                 org = 4;        /* gelijk aan order */
915                 if (org > wanted) org = wanted;
916
917                 for (j = org; j > 0; j--){
918
919                         basisNurb(u, nu->orderu, nu->pntsu, nu->knotsu, basisu, &istart, &iend);
920                         /* bereken sum */
921                         sumdiv= 0.0;
922                         fp= sum;
923                         for(i= istart; i<=iend; i++, fp++) {
924                                 /* hier nog rationele component doen */
925                                 *fp= basisu[i];
926                                 sumdiv+= *fp;
927                         }
928                         if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
929                                 /* is dit normaliseren ook nodig? */
930                                 fp= sum;
931                                 for(i= istart; i<=iend; i++, fp++) {
932                                         *fp/= sumdiv;
933                                 }
934                         }
935
936                         /* een! (1.0) echt punt nu */
937                         fp= sum;
938                         bp= nu->bp+ istart;
939                         for(i= istart; i<=iend; i++, bp++, fp++) {
940
941                                 if(*fp!=0.0) {
942                                         in[0]+= (*fp) * bp->vec[0];
943                                         in[1]+= (*fp) * bp->vec[1];
944                                         in[2]+= (*fp) * bp->vec[2];
945                                 }
946                         }
947
948                         in+=3;
949
950                         u+= ustep;
951                 }
952
953                 if (wanted > org){
954                         extend_spline(in - 3 * org, org, wanted);
955                         in += 3 * (wanted - org);
956                         u += ustep * (wanted - org);
957                 }
958         }
959
960         /* vrijgeven */
961         MEM_freeN(sum);
962         MEM_freeN(basisu);
963 }
964
965
966 void makeNurbcurve(Nurb *nu, float *data, int dim)
967 /* data moet dim*4*pntsu*resolu lang zijn en op nul staan */
968 {
969         BPoint *bp;
970         float u, ustart, uend, ustep, sumdiv;
971         float *basisu, *sum, *fp,  *in;
972         int i, len, resolu, istart, iend, cycl;
973
974         if(nu->knotsu==0) return;
975         if(nu->orderu>nu->pntsu) return;
976         if(data==0) return;
977
978         /* alloceren en vars goedzetten */
979         len= nu->pntsu;
980         if(len==0) return;
981         sum= (float *)MEM_callocN(sizeof(float)*len, "makeNurbcurve1");
982
983         resolu= nu->resolu*nu->pntsu;
984         if(resolu==0) {
985                 MEM_freeN(sum);
986                 return;
987         }
988
989         fp= nu->knotsu;
990         ustart= fp[nu->orderu-1];
991         if(nu->flagu & 1) uend= fp[nu->pntsu+nu->orderu-1];
992         else uend= fp[nu->pntsu];
993         ustep= (uend-ustart)/(resolu-1+(nu->flagu & 1));
994         basisu= (float *)MEM_mallocN(sizeof(float)*KNOTSU(nu), "makeNurbcurve3");
995
996         if(nu->flagu & 1) cycl= nu->orderu-1; 
997         else cycl= 0;
998
999         in= data;
1000         u= ustart;
1001         while(resolu--) {
1002
1003                 basisNurb(u, nu->orderu, (short)(nu->pntsu+cycl), nu->knotsu, basisu, &istart, &iend);
1004                 /* bereken sum */
1005                 sumdiv= 0.0;
1006                 fp= sum;
1007                 bp= nu->bp+ istart-1;
1008                 for(i= istart; i<=iend; i++, fp++) {
1009
1010                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
1011                         else bp++;
1012
1013                         *fp= basisu[i]*bp->vec[3];
1014                         sumdiv+= *fp;
1015                 }
1016                 if(sumdiv!=0.0) if(sumdiv<0.999 || sumdiv>1.001) {
1017                         /* is dit normaliseren ook nodig? */
1018                         fp= sum;
1019                         for(i= istart; i<=iend; i++, fp++) {
1020                                 *fp/= sumdiv;
1021                         }
1022                 }
1023
1024                 /* een! (1.0) echt punt nu */
1025                 fp= sum;
1026                 bp= nu->bp+ istart-1;
1027                 for(i= istart; i<=iend; i++, fp++) {
1028
1029                         if(i>=nu->pntsu) bp= nu->bp+(i - nu->pntsu);
1030                         else bp++;
1031
1032                         if(*fp!=0.0) {
1033                                 
1034                                 in[0]+= (*fp) * bp->vec[0];
1035                                 in[1]+= (*fp) * bp->vec[1];
1036                                 if(dim>=3) {
1037                                         in[2]+= (*fp) * bp->vec[2];
1038                                         if(dim==4) in[3]+= (*fp) * bp->alfa;
1039                                 }
1040                         }
1041                 }
1042
1043                 in+= dim;
1044
1045                 u+= ustep;
1046         }
1047
1048         /* vrijgeven */
1049         MEM_freeN(sum);
1050         MEM_freeN(basisu);
1051 }
1052
1053 void maakbez(float q0, float q1, float q2, float q3, float *p, int it)
1054 {
1055         float rt0,rt1,rt2,rt3,f;
1056         int a;
1057
1058         f= (float)it;
1059         rt0= q0;
1060         rt1= 3.0f*(q1-q0)/f;
1061         f*= f;
1062         rt2= 3.0f*(q0-2.0f*q1+q2)/f;
1063         f*= it;
1064         rt3= (q3-q0+3.0f*(q1-q2))/f;
1065         
1066         q0= rt0;
1067         q1= rt1+rt2+rt3;
1068         q2= 2*rt2+6*rt3;
1069         q3= 6*rt3;
1070   
1071         for(a=0; a<=it; a++) {
1072                 *p= q0;
1073                 p+= 3;
1074                 q0+= q1;
1075                 q1+= q2;
1076                 q2+= q3;
1077         }
1078 }       
1079
1080 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1081
1082 void make_orco_surf(Curve *cu)
1083 {
1084         Nurb *nu;
1085         int a, b, tot=0;
1086         int sizeu, sizev;// ###
1087         float *data;
1088
1089
1090         /* eerst voorspellen hoelang datablok moet worden */
1091         nu= cu->nurb.first;
1092         while(nu) {
1093 #ifdef STRUBI
1094 /* this is a bad hack: as we want to avoid the seam in a cyclic nurbs
1095 texture wrapping, reserve extra orco data space to save these extra needed
1096 vertex based UV coordinates for the meridian vertices.
1097 Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
1098 the renderface/vert construction.
1099
1100 See also blenderWorldManipulation.c: init_render_surf()
1101
1102 */
1103
1104                 sizeu = nu->resolu; sizev = nu->resolv;
1105                 if (nu->flagu & CU_CYCLIC) sizeu++;
1106                 if (nu->flagv & CU_CYCLIC) sizev++;
1107                 if(nu->pntsv>1) tot+= sizeu * sizev;
1108 #else
1109                 if(nu->pntsv>1) tot+= nu->resolu*nu->resolv;
1110 #endif
1111                 nu= nu->next;
1112         }
1113                                 /* makeNurbfaces wil nullen */
1114         data= cu->orco= MEM_callocN(3*sizeof(float)*tot, "make_orco");
1115
1116         nu= cu->nurb.first;
1117         while(nu) {
1118                 if(nu->pntsv>1) {
1119                         sizeu = nu->resolu;
1120                         sizev = nu->resolv;
1121 #ifdef STRUBI
1122                         if (nu->flagu & CU_CYCLIC) sizeu++;
1123                         if (nu->flagv & CU_CYCLIC) sizev++;
1124 #endif
1125                         
1126                         if(cu->flag & CU_UV_ORCO) {
1127                                 for(b=0; b< sizeu; b++) {
1128                                         for(a=0; a< sizev; a++) {
1129                                         
1130                                                 if(sizev <2) data[0]= 0.0f;
1131                                                 else data[0]= -1.0f + 2.0f*((float)a)/(sizev - 1);
1132                                                 
1133                                                 if(sizeu <2) data[1]= 0.0f;
1134                                                 else data[1]= -1.0f + 2.0f*((float)b)/(sizeu - 1);
1135                                                 
1136                                                 data[2]= 0.0;
1137                 
1138                                                 data+= 3;
1139                                         }
1140                                 }
1141                         }
1142                         else {
1143                                 makeNurbfaces(nu, data);
1144 #ifdef STRUBI 
1145                                 for(b=0; b< nu->resolu; b++) {
1146                                         for(a=0; a< nu->resolv; a++) {
1147                                                 data = cu->orco + 3 * (b * nu->resolv + a);
1148                                                 data[0]= (data[0]-cu->loc[0])/cu->size[0];
1149                                                 data[1]= (data[1]-cu->loc[1])/cu->size[1];
1150                                                 data[2]= (data[2]-cu->loc[2])/cu->size[2];
1151                                         }
1152                                 }
1153                                 copyintoExtendedArray(cu->orco, nu->resolv, nu->resolu, cu->orco, sizev, sizeu);
1154                                 /* copy U/V-cyclic orco's */
1155                                 if (nu->flagv & CU_CYCLIC) {
1156                                         b = sizeu - 1;  
1157                                         for(a=0; a< sizev; a++) {
1158                                                 data = cu->orco + 3 * (b * sizev + a);
1159                                                 VECCOPY(data, cu->orco + 3*a);
1160                                         }
1161                                 }       
1162                                 if (nu->flagu & CU_CYCLIC) {
1163                                         a = sizev - 1;  
1164                                         for(b=0; b< sizeu; b++) {
1165                                                 data = cu->orco + 3 * (b * sizev + a);
1166                                                 VECCOPY(data, cu->orco + 3 * b*sizev);
1167                                         }
1168                                 }       
1169
1170 #else
1171                                 tot= sizeu * sizev;
1172                                 while(tot--) {
1173                                         data[0]= (data[0]-cu->loc[0])/cu->size[0];
1174                                         data[1]= (data[1]-cu->loc[1])/cu->size[1];
1175                                         data[2]= (data[2]-cu->loc[2])/cu->size[2];
1176         
1177                                         data+= 3;
1178                                 }
1179 #endif
1180                         }
1181                 }
1182                 nu= nu->next;
1183         }
1184         /* loadkeypostype(22, base, base); */
1185
1186 }
1187
1188
1189
1190 /* ***************** BEVEL ****************** */
1191
1192 void makebevelcurve(Object *ob, ListBase *disp)
1193 {
1194         DispList *dl, *dlnew;
1195         Curve *bevcu, *cu;
1196         float *fp, facx, facy, hoek, dhoek;
1197         int nr, a;
1198
1199         cu= ob->data;
1200
1201         if(cu->bevobj && cu->bevobj!=ob) {
1202                 if(cu->bevobj->type==OB_CURVE) {
1203                         bevcu= cu->bevobj->data;
1204                         if(bevcu->ext1==0.0 && bevcu->ext2==0.0) {
1205                                 facx= cu->bevobj->size[0];
1206                                 facy= cu->bevobj->size[1];
1207
1208                                 dl= bevcu->disp.first;
1209                                 if(dl==0) {
1210                                         makeDispList(cu->bevobj);
1211                                         dl= bevcu->disp.first;
1212                                 }
1213                                 while(dl) {
1214                                         if ELEM(dl->type, DL_POLY, DL_SEGM) {
1215                                                 dlnew= MEM_mallocN(sizeof(DispList), "makebevelcurve1");                                        
1216                                                 *dlnew= *dl;
1217                                                 dlnew->verts= MEM_mallocN(3*sizeof(float)*dl->parts*dl->nr, "makebevelcurve1");
1218                                                 memcpy(dlnew->verts, dl->verts, 3*sizeof(float)*dl->parts*dl->nr);
1219                                                 
1220                                                 BLI_addtail(disp, dlnew);
1221                                                 fp= dlnew->verts;
1222                                                 nr= dlnew->parts*dlnew->nr;
1223                                                 while(nr--) {
1224                                                         fp[2]= fp[1]*facy;
1225                                                         fp[1]= -fp[0]*facx;
1226                                                         fp[0]= 0.0;
1227                                                         fp+= 3;
1228                                                 }
1229                                         }
1230                                         dl= dl->next;
1231                                 }
1232                         }
1233                 }
1234         }
1235         else if(cu->ext2==0.0) {
1236                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve2");
1237                 dl->verts= MEM_mallocN(2*3*sizeof(float), "makebevelcurve2");
1238                 BLI_addtail(disp, dl);
1239                 dl->type= DL_SEGM;
1240                 dl->parts= 1;
1241                 dl->nr= 2;
1242                 fp= dl->verts;
1243                 fp[0]= fp[1]= 0.0;
1244                 fp[2]= -cu->ext1;
1245                 fp[3]= fp[4]= 0.0;
1246                 fp[5]= cu->ext1;
1247         }
1248         else {
1249                 nr= 4+2*cu->bevresol;
1250
1251                 dl= MEM_callocN(sizeof(DispList), "makebevelcurve3");
1252                 dl->verts= MEM_mallocN(nr*3*sizeof(float), "makebevelcurve3");
1253                 BLI_addtail(disp, dl);
1254                 dl->type= DL_SEGM;
1255                 dl->parts= 1;
1256                 dl->nr= nr;
1257
1258                 /* eerst cirkel maken */
1259                 fp= dl->verts;
1260                 hoek= -0.5*M_PI;
1261                 dhoek= (float)(M_PI/(nr-2));
1262                 for(a=0; a<nr; a++) {
1263                         fp[0]= 0.0;
1264                         fp[1]= (float)(cos(hoek)*(cu->ext2));
1265                         fp[2]= (float)(sin(hoek)*(cu->ext2));
1266                         hoek+= dhoek;
1267                         fp+= 3;
1268                         if(cu->ext1!=0.0 && a==((nr/2)-1) ) {
1269                                 VECCOPY(fp, fp-3);
1270                                 fp+=3;
1271                                 a++;
1272                         }
1273                 }
1274                 if(cu->ext1==0.0) dl->nr--;
1275                 else {
1276                         fp= dl->verts;
1277                         for(a=0; a<nr; a++) {
1278                                 if(a<=(nr/2-1)) fp[2]-= (cu->ext1);
1279                                 else fp[2]+= (cu->ext1);
1280                                 fp+= 3;
1281                         }
1282                 }
1283         }
1284
1285 }
1286
1287 int cu_isectLL(float *v1, float *v2, float *v3, float *v4, short cox, short coy, float *labda, float *mu, float *vec)
1288 {
1289         /* return:
1290                 -1: colliniar
1291                  0: no intersection of segments
1292                  1: exact intersection of segments
1293                  2: cross-intersection of segments
1294         */
1295         float deler;
1296
1297         deler= (v1[cox]-v2[cox])*(v3[coy]-v4[coy])-(v3[cox]-v4[cox])*(v1[coy]-v2[coy]);
1298         if(deler==0.0) return -1;
1299
1300         *labda= (v1[coy]-v3[coy])*(v3[cox]-v4[cox])-(v1[cox]-v3[cox])*(v3[coy]-v4[coy]);
1301         *labda= -(*labda/deler);
1302
1303         deler= v3[coy]-v4[coy];
1304         if(deler==0) {
1305                 deler=v3[cox]-v4[cox];
1306                 *mu= -(*labda*(v2[cox]-v1[cox])+v1[cox]-v3[cox])/deler;
1307         } else {
1308                 *mu= -(*labda*(v2[coy]-v1[coy])+v1[coy]-v3[coy])/deler;
1309         }
1310         vec[cox]= *labda*(v2[cox]-v1[cox])+v1[cox];
1311         vec[coy]= *labda*(v2[coy]-v1[coy])+v1[coy];
1312
1313         if(*labda>=0.0 && *labda<=1.0 && *mu>=0.0 && *mu<=1.0) {
1314                 if(*labda==0.0 || *labda==1.0 || *mu==0.0 || *mu==1.0) return 1;
1315                 return 2;
1316         }
1317         return 0;
1318 }
1319
1320
1321 short bevelinside(BevList *bl1,BevList *bl2)
1322 {
1323         /* is bl2 INSIDE bl1 ? met links-rechts methode en "labda's" */
1324         /* geeft als correct gat 1 terug  */
1325         BevPoint *bevp, *prevbevp;
1326         float min,max,vec[3],hvec1[3],hvec2[3],lab,mu;
1327         int nr, links=0,rechts=0,mode;
1328
1329         /* neem eerste vertex van het mogelijke gat */
1330
1331         bevp= (BevPoint *)(bl2+1);
1332         hvec1[0]= bevp->x; 
1333         hvec1[1]= bevp->y; 
1334         hvec1[2]= 0.0;
1335         VECCOPY(hvec2,hvec1);
1336         hvec2[0]+=1000;
1337
1338         /* test deze met alle edges van mogelijk omringende poly */
1339         /* tel aantal overgangen links en rechts */
1340
1341         bevp= (BevPoint *)(bl1+1);
1342         nr= bl1->nr;
1343         prevbevp= bevp+(nr-1);
1344
1345         while(nr--) {
1346                 min= prevbevp->y;
1347                 max= bevp->y;
1348                 if(max<min) {
1349                         min= max;
1350                         max= prevbevp->y;
1351                 }
1352                 if(min!=max) {
1353                         if(min<=hvec1[1] && max>=hvec1[1]) {
1354                                 /* er is een overgang, snijpunt berekenen */
1355                                 mode= cu_isectLL(&(prevbevp->x),&(bevp->x),hvec1,hvec2,0,1,&lab,&mu,vec);
1356                                 /* als lab==0.0 of lab==1.0 dan snijdt de edge exact de overgang
1357                                  * alleen toestaan voor lab= 1.0 (of andersom,  maakt niet uit)
1358                                  */
1359                                 if(mode>=0 && lab!=0.0) {
1360                                         if(vec[0]<hvec1[0]) links++;
1361                                         else rechts++;
1362                                 }
1363                         }
1364                 }
1365                 prevbevp= bevp;
1366                 bevp++;
1367         }
1368         
1369         if( (links & 1) && (rechts & 1) ) return 1;
1370         return 0;
1371 }
1372
1373
1374 struct bevelsort {
1375         float left;
1376         BevList *bl;
1377         int dir;
1378 };
1379
1380 int vergxcobev(const void *a1, const void *a2)
1381 {
1382         const struct bevelsort *x1=a1,*x2=a2;
1383
1384         if( x1->left > x2->left ) return 1;
1385         else if( x1->left < x2->left) return -1;
1386         return 0;
1387 }
1388
1389 /* deze kan niet zomaar door atan2 vervangen worden, maar waarom? */
1390
1391 void calc_bevel_sin_cos(float x1, float y1, float x2, float y2, float *sina, float *cosa)
1392 {
1393         float t01, t02, x3, y3;
1394
1395         t01= (float)sqrt(x1*x1+y1*y1);
1396         t02= (float)sqrt(x2*x2+y2*y2);
1397         if(t01==0.0) t01= 1.0;
1398         if(t02==0.0) t02= 1.0;
1399
1400         x1/=t01; 
1401         y1/=t01;
1402         x2/=t02; 
1403         y2/=t02;
1404
1405         t02= x1*x2+y1*y2;
1406         if(fabs(t02)>=1.0) t02= .5*M_PI;
1407         else t02= (saacos(t02))/2.0f;
1408
1409         t02= (float)sin(t02);
1410         if(t02==0.0) t02= 1.0;
1411
1412         x3= x1-x2;
1413         y3= y1-y2;
1414         if(x3==0 && y3==0) {
1415                 /* printf("x3 en y3  nul \n"); */
1416                 x3= y1;
1417                 y3= -x1;
1418         } else {
1419                 t01= (float)sqrt(x3*x3+y3*y3);
1420                 x3/=t01; 
1421                 y3/=t01;
1422         }
1423
1424         *sina= -y3/t02;
1425         *cosa= x3/t02;
1426
1427 }
1428
1429 void alfa_bezpart(BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *data_a)
1430 {
1431         BezTriple *pprev, *next, *last;
1432         float fac, dfac, t[4];
1433         int a;
1434         
1435         last= nu->bezt+(nu->pntsu-1);
1436         
1437         /* een punt terug */
1438         if(prevbezt==nu->bezt) {
1439                 if(nu->flagu & 1) pprev= last;
1440                 else pprev= prevbezt;
1441         }
1442         else pprev= prevbezt-1;
1443         
1444         /* een punt verder */
1445         if(bezt==last) {
1446                 if(nu->flagu & 1) next= nu->bezt;
1447                 else next= bezt;
1448         }
1449         else next= bezt+1;
1450         
1451         fac= 0.0;
1452         dfac= 1.0f/(float)nu->resolu;
1453         
1454         for(a=0; a<nu->resolu; a++, fac+= dfac) {
1455                 
1456                 set_four_ipo(fac, t, KEY_BSPLINE);
1457                 
1458                 data_a[a]= t[0]*pprev->alfa + t[1]*prevbezt->alfa + t[2]*bezt->alfa + t[3]*next->alfa;
1459         }
1460 }
1461
1462 void makeBevelList(Object *ob)
1463 {
1464         /* - alle curves omzetten in poly's, met aangegeven resol en vlaggen voor dubbele punten
1465        - eventueel intelligent punten verwijderen (geval Nurb) 
1466        - scheiden in verschillende blokken met Boundbox
1467        - Autogat detectie */
1468         Curve *cu;
1469         Nurb *nu;
1470         BezTriple *bezt, *prevbezt;
1471         BPoint *bp;
1472         BevList *bl, *blnew, *blnext;
1473         BevPoint *bevp, *bevp2, *bevp1 = NULL, *bevp0;
1474         float  *data, *data_a, *v1, *v2, min, inp, x1, x2, y1, y2, vec[3];
1475         struct bevelsort *sortdata, *sd, *sd1;
1476         int a, b, len, nr, poly;
1477
1478         /* deze fie moet object hebben in verband met tflag en upflag */
1479         cu= ob->data;
1480
1481         /* STAP 1: POLY'S MAKEN */
1482
1483         BLI_freelistN(&(cu->bev));
1484         if(ob==G.obedit) nu= editNurb.first;
1485         else nu= cu->nurb.first;
1486         
1487         while(nu) {
1488                 if(nu->pntsu>1) {
1489                 
1490                         if((nu->type & 7)==CU_POLY) {
1491         
1492                                 len= nu->pntsu;
1493                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList");
1494                                 BLI_addtail(&(cu->bev), bl);
1495         
1496                                 if(nu->flagu & 1) bl->poly= 0;
1497                                 else bl->poly= -1;
1498                                 bl->nr= len;
1499                                 bl->flag= 0;
1500                                 bevp= (BevPoint *)(bl+1);
1501                                 bp= nu->bp;
1502         
1503                                 while(len--) {
1504                                         bevp->x= bp->vec[0];
1505                                         bevp->y= bp->vec[1];
1506                                         bevp->z= bp->vec[2];
1507                                         bevp->alfa= bp->alfa;
1508                                         bevp->f1= 1;
1509                                         bevp++;
1510                                         bp++;
1511                                 }
1512                         }
1513                         else if((nu->type & 7)==CU_BEZIER) {
1514         
1515                                 len= nu->resolu*(nu->pntsu+ (nu->flagu & 1) -1)+1;      /* voor laatste punt niet cyclic */
1516                                 bl= MEM_callocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList");
1517                                 BLI_addtail(&(cu->bev), bl);
1518         
1519                                 if(nu->flagu & 1) bl->poly= 0;
1520                                 else bl->poly= -1;
1521                                 bevp= (BevPoint *)(bl+1);
1522         
1523                                 a= nu->pntsu-1;
1524                                 bezt= nu->bezt;
1525                                 if(nu->flagu & 1) {
1526                                         a++;
1527                                         prevbezt= nu->bezt+(nu->pntsu-1);
1528                                 }
1529                                 else {
1530                                         prevbezt= bezt;
1531                                         bezt++;
1532                                 }
1533                                 
1534                                 data= MEM_mallocN(3*sizeof(float)*(nu->resolu+1), "makeBevelList2");
1535                                 data_a= MEM_callocN(sizeof(float)*(nu->resolu+1), "data_a");
1536                                 
1537                                 while(a--) {
1538                                         if(prevbezt->h2==HD_VECT && bezt->h1==HD_VECT) {
1539         
1540                                                 bevp->x= prevbezt->vec[1][0];
1541                                                 bevp->y= prevbezt->vec[1][1];
1542                                                 bevp->z= prevbezt->vec[1][2];
1543                                                 bevp->alfa= prevbezt->alfa;
1544                                                 bevp->f1= 1;
1545                                                 bevp->f2= 0;
1546                                                 bevp++;
1547                                                 bl->nr++;
1548                                                 bl->flag= 1;
1549                                         }
1550                                         else {
1551                                                 v1= prevbezt->vec[1];
1552                                                 v2= bezt->vec[0];
1553                                                 
1554                                                 /* altijd alle drie doen: anders blijft data hangen */
1555                                                 maakbez(v1[0], v1[3], v2[0], v2[3], data, nu->resolu);
1556                                                 maakbez(v1[1], v1[4], v2[1], v2[4], data+1, nu->resolu);
1557                                                 maakbez(v1[2], v1[5], v2[2], v2[5], data+2, nu->resolu);
1558                                                 
1559                                                 if((nu->type & CU_2D)==0) {
1560                                                         if(cu->flag & CU_3D) {
1561                                                                 alfa_bezpart(prevbezt, bezt, nu, data_a);
1562                                                         }
1563                                                 }
1564                                                 
1565                                                 
1566                                                 /* met handlecodes dubbele punten aangeven */
1567                                                 if(prevbezt->h1==prevbezt->h2) {
1568                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1;
1569                                                 }
1570                                                 else {
1571                                                         if(prevbezt->h1==0 || prevbezt->h1==HD_VECT) bevp->f1= 1;
1572                                                         else if(prevbezt->h2==0 || prevbezt->h2==HD_VECT) bevp->f1= 1;
1573                                                 }
1574                                                 
1575                                                 v1= data;
1576                                                 v2= data_a;
1577                                                 nr= nu->resolu;
1578                                                 
1579                                                 while(nr--) {
1580                                                         bevp->x= v1[0]; 
1581                                                         bevp->y= v1[1];
1582                                                         bevp->z= v1[2];
1583                                                         bevp->alfa= v2[0];
1584                                                         bevp++;
1585                                                         v1+=3;
1586                                                         v2++;
1587                                                 }
1588                                                 bl->nr+= nu->resolu;
1589         
1590                                         }
1591                                         prevbezt= bezt;
1592                                         bezt++;
1593                                 }
1594                                 
1595                                 MEM_freeN(data);
1596                                 MEM_freeN(data_a);
1597                                 
1598                                 if((nu->flagu & 1)==0) {            /* niet cyclic: endpoint */
1599                                         bevp->x= prevbezt->vec[1][0];
1600                                         bevp->y= prevbezt->vec[1][1];
1601                                         bevp->z= prevbezt->vec[1][2];
1602                                         bl->nr++;
1603                                 }
1604         
1605                         }
1606                         else if((nu->type & 7)==CU_NURBS) {
1607                                 if(nu->pntsv==1) {
1608                                         len= nu->resolu*nu->pntsu;
1609                                         bl= MEM_mallocN(sizeof(BevList)+len*sizeof(BevPoint), "makeBevelList3");
1610                                         BLI_addtail(&(cu->bev), bl);
1611                                         bl->nr= len;
1612                                         bl->flag= 0;
1613                                         if(nu->flagu & 1) bl->poly= 0;
1614                                         else bl->poly= -1;
1615                                         bevp= (BevPoint *)(bl+1);
1616         
1617                                         data= MEM_callocN(4*sizeof(float)*len, "makeBevelList4");    /* moet op nul staan */
1618                                         makeNurbcurve(nu, data, 4);
1619                                         
1620                                         v1= data;
1621                                         while(len--) {
1622                                                 bevp->x= v1[0]; 
1623                                                 bevp->y= v1[1];
1624                                                 bevp->z= v1[2];
1625                                                 bevp->alfa= v1[3];
1626                                                 
1627                                                 bevp->f1= bevp->f2= 0;
1628                                                 bevp++;
1629                                                 v1+=4;
1630                                         }
1631                                         MEM_freeN(data);
1632                                 }
1633                         }
1634                 }
1635                 nu= nu->next;
1636         }
1637
1638         /* STAP 2: DUBBELE PUNTEN EN AUTOMATISCHE RESOLUTIE, DATABLOKKEN VERKLEINEN */
1639         bl= cu->bev.first;
1640         while(bl) {
1641                 nr= bl->nr;
1642                 bevp1= (BevPoint *)(bl+1);
1643                 bevp0= bevp1+(nr-1);
1644                 nr--;
1645                 while(nr--) {
1646                         if( fabs(bevp0->x-bevp1->x)<0.00001 ) {
1647                                 if( fabs(bevp0->y-bevp1->y)<0.00001 ) {
1648                                         if( fabs(bevp0->z-bevp1->z)<0.00001 ) {
1649                                                 bevp0->f2= 1;
1650                                                 bl->flag++;
1651                                         }
1652                                 }
1653                         }
1654                         bevp0= bevp1;
1655                         bevp1++;
1656                 }
1657                 bl= bl->next;
1658         }
1659         bl= cu->bev.first;
1660         while(bl) {
1661                 blnext= bl->next;
1662                 if(bl->flag) {
1663                         nr= bl->nr- bl->flag+1; /* +1 want vectorbezier zet ook flag */
1664                         blnew= MEM_mallocN(sizeof(BevList)+nr*sizeof(BevPoint), "makeBevelList");
1665                         memcpy(blnew, bl, sizeof(BevList));
1666                         blnew->nr= 0;
1667                         BLI_remlink(&(cu->bev), bl);
1668                         BLI_insertlinkbefore(&(cu->bev),blnext,blnew);  /* zodat bevlijst met nurblijst gelijk loopt */
1669                         bevp0= (BevPoint *)(bl+1);
1670                         bevp1= (BevPoint *)(blnew+1);
1671                         nr= bl->nr;
1672                         while(nr--) {
1673                                 if(bevp0->f2==0) {
1674                                         memcpy(bevp1, bevp0, sizeof(BevPoint));
1675                                         bevp1++;
1676                                         blnew->nr++;
1677                                 }
1678                                 bevp0++;
1679                         }
1680                         MEM_freeN(bl);
1681                         blnew->flag= 0;
1682                 }
1683                 bl= blnext;
1684         }
1685
1686         /* STAP 3: POLY'S TELLEN EN AUTOGAT */
1687         bl= cu->bev.first;
1688         poly= 0;
1689         while(bl) {
1690                 if(bl->poly>=0) {
1691                         poly++;
1692                         bl->poly= poly;
1693                         bl->gat= 0;
1694                 }
1695                 bl= bl->next;
1696         }
1697         
1698
1699         /* meest linkse punten vinden, tevens richting testen */
1700         if(poly>0) {
1701                 sd= sortdata= MEM_mallocN(sizeof(struct bevelsort)*poly, "makeBevelList5");
1702                 bl= cu->bev.first;
1703                 while(bl) {
1704                         if(bl->poly>0) {
1705
1706                                 min= 300000.0;
1707                                 bevp= (BevPoint *)(bl+1);
1708                                 nr= bl->nr;
1709                                 while(nr--) {
1710                                         if(min>bevp->x) {
1711                                                 min= bevp->x;
1712                                                 bevp1= bevp;
1713                                         }
1714                                         bevp++;
1715                                 }
1716                                 sd->bl= bl;
1717                                 sd->left= min;
1718
1719                                 bevp= (BevPoint *)(bl+1);
1720                                 if(bevp1== bevp) bevp0= bevp+ (bl->nr-1);
1721                                 else bevp0= bevp1-1;
1722                                 bevp= bevp+ (bl->nr-1);
1723                                 if(bevp1== bevp) bevp2= (BevPoint *)(bl+1);
1724                                 else bevp2= bevp1+1;
1725
1726                                 inp= (bevp1->x- bevp0->x)*(bevp0->y- bevp2->y)
1727                                     +(bevp0->y- bevp1->y)*(bevp0->x- bevp2->x);
1728
1729                                 if(inp>0.0) sd->dir= 1;
1730                                 else sd->dir= 0;
1731
1732                                 sd++;
1733                         }
1734
1735                         bl= bl->next;
1736                 }
1737                 qsort(sortdata,poly,sizeof(struct bevelsort), vergxcobev);
1738
1739                 sd= sortdata+1;
1740                 for(a=1; a<poly; a++, sd++) {
1741                         bl= sd->bl;         /* is bl een gat? */
1742                         sd1= sortdata+ (a-1);
1743                         for(b=a-1; b>=0; b--, sd1--) {  /* alle polys links ervan */
1744                                 if(bevelinside(sd1->bl, bl)) {
1745                                         bl->gat= 1- sd1->bl->gat;
1746                                         break;
1747                                 }
1748                         }
1749                 }
1750
1751                 /* draairichting */
1752                 if((cu->flag & CU_3D)==0) {
1753                         sd= sortdata;
1754                         for(a=0; a<poly; a++, sd++) {
1755                                 if(sd->bl->gat==sd->dir) {
1756                                         bl= sd->bl;
1757                                         bevp1= (BevPoint *)(bl+1);
1758                                         bevp2= bevp1+ (bl->nr-1);
1759                                         nr= bl->nr/2;
1760                                         while(nr--) {
1761                                                 SWAP(BevPoint, *bevp1, *bevp2);
1762                                                 bevp1++;
1763                                                 bevp2--;
1764                                         }
1765                                 }
1766                         }
1767                 }
1768                 MEM_freeN(sortdata);
1769         }
1770
1771         /* STAP 4: COSINUSSEN */
1772         bl= cu->bev.first;
1773         while(bl) {
1774         
1775                 if(bl->nr==2) { /* 2 pnt, apart afhandelen: KAN DAT NIET AFGESCHAFT? */
1776                         bevp2= (BevPoint *)(bl+1);
1777                         bevp1= bevp2+1;
1778
1779                         x1= bevp1->x- bevp2->x;
1780                         y1= bevp1->y- bevp2->y;
1781
1782                         calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
1783                         bevp2->sina= bevp1->sina;
1784                         bevp2->cosa= bevp1->cosa;
1785
1786                         if(cu->flag & CU_3D) {  /* 3D */
1787                                 float *quat, q[4];
1788                         
1789                                 vec[0]= bevp1->x - bevp2->x;
1790                                 vec[1]= bevp1->y - bevp2->y;
1791                                 vec[2]= bevp1->z - bevp2->z;
1792                                 
1793                                 quat= vectoquat(vec, 5, 1);
1794                                 
1795                                 Normalise(vec);
1796                                 q[0]= (float)cos(0.5*bevp1->alfa);
1797                                 x1= (float)sin(0.5*bevp1->alfa);
1798                                 q[1]= x1*vec[0];
1799                                 q[2]= x1*vec[1];
1800                                 q[3]= x1*vec[2];
1801                                 QuatMul(quat, q, quat);
1802                                 
1803                                 QuatToMat3(quat, bevp1->mat);
1804                                 Mat3CpyMat3(bevp2->mat, bevp1->mat);
1805                         }
1806
1807                 }
1808                 else if(bl->nr>2) {
1809                         bevp2= (BevPoint *)(bl+1);
1810                         bevp1= bevp2+(bl->nr-1);
1811                         bevp0= bevp1-1;
1812
1813                 
1814                         nr= bl->nr;
1815         
1816                         while(nr--) {
1817         
1818                                 if(cu->flag & CU_3D) {  /* 3D */
1819                                         float *quat, q[4];
1820                                 
1821                                         vec[0]= bevp2->x - bevp0->x;
1822                                         vec[1]= bevp2->y - bevp0->y;
1823                                         vec[2]= bevp2->z - bevp0->z;
1824                                         
1825                                         Normalise(vec);
1826
1827                                         quat= vectoquat(vec, 5, 1);
1828                                         
1829                                         q[0]= (float)cos(0.5*bevp1->alfa);
1830                                         x1= (float)sin(0.5*bevp1->alfa);
1831                                         q[1]= x1*vec[0];
1832                                         q[2]= x1*vec[1];
1833                                         q[3]= x1*vec[2];
1834                                         QuatMul(quat, q, quat);
1835                                         
1836                                         QuatToMat3(quat, bevp1->mat);
1837                                 }
1838                                 
1839                                 x1= bevp1->x- bevp0->x;
1840                                 x2= bevp1->x- bevp2->x;
1841                                 y1= bevp1->y- bevp0->y;
1842                                 y2= bevp1->y- bevp2->y;
1843                         
1844                                 calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
1845                                 
1846                                 
1847                                 bevp0= bevp1;
1848                                 bevp1= bevp2;
1849                                 bevp2++;
1850                         }
1851                         /* niet cyclic gevallen corrigeren */
1852                         if(bl->poly== -1) {
1853                                 if(bl->nr>2) {
1854                                         bevp= (BevPoint *)(bl+1);
1855                                         bevp1= bevp+1;
1856                                         bevp->sina= bevp1->sina;
1857                                         bevp->cosa= bevp1->cosa;
1858                                         Mat3CpyMat3(bevp->mat, bevp1->mat);
1859                                         bevp= (BevPoint *)(bl+1);
1860                                         bevp+= (bl->nr-1);
1861                                         bevp1= bevp-1;
1862                                         bevp->sina= bevp1->sina;
1863                                         bevp->cosa= bevp1->cosa;
1864                                         Mat3CpyMat3(bevp->mat, bevp1->mat);
1865                                 }
1866                         }
1867                 }
1868                 bl= bl->next;
1869         }
1870 }
1871
1872 /* ****************** HANDLES ************** */
1873
1874 /*
1875  *   handlecodes:
1876  *              1: niets,  1:auto,  2:vector,  3:aligned
1877  */
1878
1879
1880 void calchandleNurb(BezTriple *bezt,BezTriple *prev, BezTriple *next, int mode)
1881 {
1882         float *p1,*p2,*p3,pt[3];
1883         float dx1,dy1,dz1,dx,dy,dz,vx,vy,vz,len,len1,len2;
1884
1885         if(bezt->h1==0 && bezt->h2==0) return;
1886
1887         p2= bezt->vec[1];
1888
1889         if(prev==0) {
1890                 p3= next->vec[1];
1891                 pt[0]= 2*p2[0]- p3[0];
1892                 pt[1]= 2*p2[1]- p3[1];
1893                 pt[2]= 2*p2[2]- p3[2];
1894                 p1= pt;
1895         }
1896         else p1= prev->vec[1];
1897
1898         if(next==0) {
1899                 pt[0]= 2*p2[0]- p1[0];
1900                 pt[1]= 2*p2[1]- p1[1];
1901                 pt[2]= 2*p2[2]- p1[2];
1902                 p3= pt;
1903         }
1904         else p3= next->vec[1];
1905
1906         if(mode && bezt->h1==HD_AUTO && prev) {
1907                 dx= p2[0] - (p1[0]+p1[3])/2.0f;
1908                 dy= p2[1] - (p1[1]+p1[4])/2.0f;
1909                 dz= p2[2] - (p1[2]+p1[5])/2.0f;
1910         }
1911         else {
1912                 dx= p2[0]- p1[0];
1913                 dy= p2[1]- p1[1];
1914                 dz= p2[2]- p1[2];
1915         }
1916         len1= (float)sqrt(dx*dx+dy*dy+dz*dz);
1917         
1918         if(mode && bezt->h2==HD_AUTO && next) {
1919                 dx1= (p3[0]+p3[-3])/2.0f - p2[0];
1920                 dy1= (p3[1]+p3[-2])/2.0f - p2[1];
1921                 dz1= (p3[2]+p3[-1])/2.0f - p2[2];
1922         }
1923         else {
1924                 dx1= p3[0]- p2[0];
1925                 dy1= p3[1]- p2[1];
1926                 dz1= p3[2]- p2[2];
1927         }
1928         len2= (float)sqrt(dx1*dx1+dy1*dy1+dz1*dz1);
1929
1930         if(len1==0.0f) len1=1.0f;
1931         if(len2==0.0f) len2=1.0f;
1932
1933
1934         if(bezt->h1==HD_AUTO || bezt->h2==HD_AUTO) {    /* auto */
1935                 vx= dx1/len2 + dx/len1;
1936                 vy= dy1/len2 + dy/len1;
1937                 vz= dz1/len2 + dz/len1;
1938                 len= 2.71f*(float)sqrt(vx*vx + vy*vy + vz*vz);
1939                 if(len!=0.0f) {
1940                 
1941                         if(len1>5.0f*len2) len1= 5.0f*len2;     
1942                         if(len2>5.0f*len1) len2= 5.0f*len1;
1943                         
1944                         if(bezt->h1==HD_AUTO) {
1945                                 len1/=len;
1946                                 *(p2-3)= *p2-vx*len1;
1947                                 *(p2-2)= *(p2+1)-vy*len1;
1948                                 *(p2-1)= *(p2+2)-vz*len1;
1949                         }
1950                         if(bezt->h2==HD_AUTO) {
1951                                 len2/=len;
1952                                 *(p2+3)= *p2+vx*len2;
1953                                 *(p2+4)= *(p2+1)+vy*len2;
1954                                 *(p2+5)= *(p2+2)+vz*len2;
1955                         }
1956                 }
1957         }
1958
1959         if(bezt->h1==HD_VECT) { /* vector */
1960                 dx/=3.0; 
1961                 dy/=3.0; 
1962                 dz/=3.0;
1963                 *(p2-3)= *p2-dx;
1964                 *(p2-2)= *(p2+1)-dy;
1965                 *(p2-1)= *(p2+2)-dz;
1966         }
1967         if(bezt->h2==HD_VECT) {
1968                 dx1/=3.0; 
1969                 dy1/=3.0; 
1970                 dz1/=3.0;
1971                 *(p2+3)= *p2+dx1;
1972                 *(p2+4)= *(p2+1)+dy1;
1973                 *(p2+5)= *(p2+2)+dz1;
1974         }
1975
1976         len2= VecLenf(p2, p2+3);
1977         len1= VecLenf(p2, p2-3);
1978         if(len1==0.0) len1=1.0;
1979         if(len2==0.0) len2=1.0;
1980         if(bezt->f1 & 1) { /* volgorde van berekenen */
1981                 if(bezt->h2==HD_ALIGN) {        /* aligned */
1982                         len= len2/len1;
1983                         p2[3]= p2[0]+len*(p2[0]-p2[-3]);
1984                         p2[4]= p2[1]+len*(p2[1]-p2[-2]);
1985                         p2[5]= p2[2]+len*(p2[2]-p2[-1]);
1986                 }
1987                 if(bezt->h1==HD_ALIGN) {
1988                         len= len1/len2;
1989                         p2[-3]= p2[0]+len*(p2[0]-p2[3]);
1990                         p2[-2]= p2[1]+len*(p2[1]-p2[4]);
1991                         p2[-1]= p2[2]+len*(p2[2]-p2[5]);
1992                 }
1993         }
1994         else {
1995                 if(bezt->h1==HD_ALIGN) {
1996                         len= len1/len2;
1997                         p2[-3]= p2[0]+len*(p2[0]-p2[3]);
1998                         p2[-2]= p2[1]+len*(p2[1]-p2[4]);
1999                         p2[-1]= p2[2]+len*(p2[2]-p2[5]);
2000                 }
2001                 if(bezt->h2==HD_ALIGN) {        /* aligned */
2002                         len= len2/len1;
2003                         p2[3]= p2[0]+len*(p2[0]-p2[-3]);
2004                         p2[4]= p2[1]+len*(p2[1]-p2[-2]);
2005                         p2[5]= p2[2]+len*(p2[2]-p2[-1]);
2006                 }
2007         }
2008 }
2009
2010 void calchandlesNurb(Nurb *nu) /* wel eerst (zonodig) de handlevlaggen zetten */
2011 {
2012         BezTriple *bezt, *prev, *next;
2013         short a;
2014
2015         if((nu->type & 7)!=1) return;
2016         if(nu->pntsu<2) return;
2017         
2018         a= nu->pntsu;
2019         bezt= nu->bezt;
2020         if(nu->flagu & 1) prev= bezt+(a-1);
2021         else prev= 0;
2022         next= bezt+1;
2023
2024         while(a--) {
2025                 calchandleNurb(bezt, prev, next, 0);
2026                 prev= bezt;
2027                 if(a==1) {
2028                         if(nu->flagu & 1) next= nu->bezt;
2029                         else next= 0;
2030                 }
2031                 else next++;
2032
2033                 bezt++;
2034         }
2035 }
2036
2037
2038 void testhandlesNurb(Nurb *nu)
2039 {
2040         /* Te gebruiken als er iets an de handles is veranderd.
2041          * Loopt alle BezTriples af met de volgende regels:
2042      * FASE 1: types veranderen?
2043      *  Autocalchandles: worden ligned als NOT(000 || 111)
2044      *  Vectorhandles worden 'niets' als (selected en andere niet) 
2045      * FASE 2: handles herbereken
2046      */
2047         BezTriple *bezt;
2048         short flag, a;
2049
2050         if((nu->type & 7)!=CU_BEZIER) return;
2051
2052         bezt= nu->bezt;
2053         a= nu->pntsu;
2054         while(a--) {
2055                 flag= 0;
2056                 if(bezt->f1 & 1) flag++;
2057                 if(bezt->f2 & 1) flag += 2;
2058                 if(bezt->f3 & 1) flag += 4;
2059
2060                 if( !(flag==0 || flag==7) ) {
2061                         if(bezt->h1==HD_AUTO) {   /* auto */
2062                                 bezt->h1= HD_ALIGN;
2063                         }
2064                         if(bezt->h2==HD_AUTO) {   /* auto */
2065                                 bezt->h2= HD_ALIGN;
2066                         }
2067
2068                         if(bezt->h1==HD_VECT) {   /* vector */
2069                                 if(flag < 4) bezt->h1= 0;
2070                         }
2071                         if(bezt->h2==HD_VECT) {   /* vector */
2072                                 if( flag > 3) bezt->h2= 0;
2073                         }
2074                 }
2075                 bezt++;
2076         }
2077
2078         calchandlesNurb(nu);
2079 }
2080
2081 void autocalchandlesNurb(Nurb *nu, int flag)
2082 {
2083         /* Kijkt naar de coordinaten van de handles en berekent de soort */
2084         
2085         BezTriple *bezt2, *bezt1, *bezt0;
2086         int i, align, leftsmall, rightsmall;
2087
2088         if(nu==0 || nu->bezt==0) return;
2089         
2090         bezt2 = nu->bezt;
2091         bezt1 = bezt2 + (nu->pntsu-1);
2092         bezt0 = bezt1 - 1;
2093         i = nu->pntsu;
2094
2095         while(i--) {
2096                 
2097                 align= leftsmall= rightsmall= 0;
2098                 
2099                 /* linker handle: */
2100                 if(flag==0 || (bezt1->f1 & flag) ) {
2101                         bezt1->h1= 0;
2102                         /* afstand te klein: vectorhandle */
2103                         if( VecLenf( bezt1->vec[1], bezt0->vec[1] ) < 0.0001) {
2104                                 bezt1->h1= HD_VECT;
2105                                 leftsmall= 1;
2106                         }
2107                         else {
2108                                 /* aligned handle? */
2109                                 if(DistVL2Dfl(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < 0.0001) {
2110                                         align= 1;
2111                                         bezt1->h1= HD_ALIGN;
2112                                 }
2113                                 /* of toch vector handle? */
2114                                 if(DistVL2Dfl(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < 0.0001)
2115                                         bezt1->h1= HD_VECT;
2116                                 
2117                         }
2118                 }
2119                 /* rechter handle: */
2120                 if(flag==0 || (bezt1->f3 & flag) ) {
2121                         bezt1->h2= 0;
2122                         /* afstand te klein: vectorhandle */
2123                         if( VecLenf( bezt1->vec[1], bezt2->vec[1] ) < 0.0001) {
2124                                 bezt1->h2= HD_VECT;
2125                                 rightsmall= 1;
2126                         }
2127                         else {
2128                                 /* aligned handle? */
2129                                 if(align) bezt1->h2= HD_ALIGN;
2130
2131                                 /* of toch vector handle? */
2132                                 if(DistVL2Dfl(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < 0.0001)
2133                                         bezt1->h2= HD_VECT;
2134                                 
2135                         }
2136                 }
2137                 if(leftsmall && bezt1->h2==HD_ALIGN) bezt1->h2= 0;
2138                 if(rightsmall && bezt1->h1==HD_ALIGN) bezt1->h1= 0;
2139                 
2140                 /* onzalige combinatie: */
2141                 if(bezt1->h1==HD_ALIGN && bezt1->h2==HD_VECT) bezt1->h1= 0;
2142                 if(bezt1->h2==HD_ALIGN && bezt1->h1==HD_VECT) bezt1->h2= 0;
2143                 
2144                 bezt0= bezt1;
2145                 bezt1= bezt2;
2146                 bezt2++;
2147         }
2148
2149         calchandlesNurb(nu);
2150 }
2151
2152 void autocalchandlesNurb_all(int flag)
2153 {
2154         Nurb *nu;
2155         
2156         nu= editNurb.first;
2157         while(nu) {
2158                 autocalchandlesNurb(nu, flag);
2159                 nu= nu->next;
2160         }
2161 }
2162
2163 void sethandlesNurb(short code)
2164 {
2165         /* code==1: set autohandle */
2166         /* code==2: set vectorhandle */
2167         /* als code==3 (HD_ALIGN) toggelt het, vectorhandles worden HD_FREE */
2168         Nurb *nu;
2169         BezTriple *bezt;
2170         short a, ok=0;
2171
2172         if(code==1 || code==2) {
2173                 nu= editNurb.first;
2174                 while(nu) {
2175                         if( (nu->type & 7)==1) {
2176                                 bezt= nu->bezt;
2177                                 a= nu->pntsu;
2178                                 while(a--) {
2179                                         if(bezt->f1 || bezt->f3) {
2180                                                 if(bezt->f1) bezt->h1= code;
2181                                                 if(bezt->f3) bezt->h2= code;
2182                                                 if(bezt->h1!=bezt->h2) {
2183                                                         if ELEM(bezt->h1, HD_ALIGN, HD_AUTO) bezt->h1= HD_FREE;
2184                                                         if ELEM(bezt->h2, HD_ALIGN, HD_AUTO) bezt->h2= HD_FREE;
2185                                                 }
2186                                         }
2187                                         bezt++;
2188                                 }
2189                                 calchandlesNurb(nu);
2190                         }
2191                         nu= nu->next;
2192                 }
2193         }
2194         else {
2195                 /* is er 1 handle NIET vrij: alles vrijmaken, else ALIGNED maken */
2196                 
2197                 nu= editNurb.first;
2198                 while(nu) {
2199                         if( (nu->type & 7)==1) {
2200                                 bezt= nu->bezt;
2201                                 a= nu->pntsu;
2202                                 while(a--) {
2203                                         if(bezt->f1 && bezt->h1) ok= 1;
2204                                         if(bezt->f3 && bezt->h2) ok= 1;
2205                                         if(ok) break;
2206                                         bezt++;
2207                                 }
2208                         }
2209                         nu= nu->next;
2210                 }
2211                 if(ok) ok= HD_FREE;
2212                 else ok= HD_ALIGN;
2213                 
2214                 nu= editNurb.first;
2215                 while(nu) {
2216                         if( (nu->type & 7)==1) {
2217                                 bezt= nu->bezt;
2218                                 a= nu->pntsu;
2219                                 while(a--) {
2220                                         if(bezt->f1) bezt->h1= ok;
2221                                         if(bezt->f3 ) bezt->h2= ok;
2222         
2223                                         bezt++;
2224                                 }
2225                                 calchandlesNurb(nu);
2226                         }
2227                         nu= nu->next;
2228                 }
2229         }
2230 }
2231
2232 void swapdata(void *adr1, void *adr2, int len)
2233 {
2234
2235         if(len<=0) return;
2236
2237         if(len<65) {
2238                 char adr[64];
2239
2240                 memcpy(adr, adr1, len);
2241                 memcpy(adr1, adr2, len);
2242                 memcpy(adr2, adr, len);
2243         }
2244         else {
2245                 char *adr;
2246
2247                 adr= (char *)malloc(len);
2248                 memcpy(adr, adr1, len);
2249                 memcpy(adr1, adr2, len);
2250                 memcpy(adr2, adr, len);
2251                 free(adr);
2252         }
2253 }
2254
2255 void switchdirectionNurb(Nurb *nu)
2256 {
2257         BezTriple *bezt1, *bezt2;
2258         BPoint *bp1, *bp2;
2259         float *fp1, *fp2, *tempf;
2260         int a, b;
2261
2262         if(nu->pntsu==1 && nu->pntsv==1) return;
2263
2264         if((nu->type & 7)==CU_BEZIER) {
2265                 a= nu->pntsu;
2266                 bezt1= nu->bezt;
2267                 bezt2= bezt1+(a-1);
2268                 if(a & 1) a+= 1;        /* bij oneven ook van middelste inhoud swappen */
2269                 a/= 2;
2270                 while(a>0) {
2271                         if(bezt1!=bezt2) SWAP(BezTriple, *bezt1, *bezt2);
2272
2273                         swapdata(bezt1->vec[0], bezt1->vec[2], 12);
2274                         if(bezt1!=bezt2) swapdata(bezt2->vec[0], bezt2->vec[2], 12);
2275
2276                         SWAP(char, bezt1->h1, bezt1->h2);
2277                         SWAP(short, bezt1->f1, bezt1->f3);
2278                         
2279                         if(bezt1!=bezt2) {
2280                                 SWAP(char, bezt2->h1, bezt2->h2);
2281                                 SWAP(short, bezt2->f1, bezt2->f3);
2282                                 bezt1->alfa= -bezt1->alfa;
2283                                 bezt2->alfa= -bezt2->alfa;
2284                         }
2285                         a--;
2286                         bezt1++; 
2287                         bezt2--;
2288                 }
2289         }
2290         else if(nu->pntsv==1) {
2291                 a= nu->pntsu;
2292                 bp1= nu->bp;
2293                 bp2= bp1+(a-1);
2294                 a/= 2;
2295                 while(bp1!=bp2 && a>0) {
2296                         SWAP(BPoint, *bp1, *bp2);
2297                         a--;
2298                         bp1->alfa= -bp1->alfa;
2299                         bp2->alfa= -bp2->alfa;
2300                         bp1++; 
2301                         bp2--;
2302                 }
2303                 if((nu->type & 7)==CU_NURBS) {
2304                         /* de knots omkeren */
2305                         a= KNOTSU(nu);
2306                         fp1= nu->knotsu;
2307                         fp2= fp1+(a-1);
2308                         a/= 2;
2309                         while(fp1!=fp2 && a>0) {
2310                                 SWAP(float, *fp1, *fp2);
2311                                 a--;
2312                                 fp1++; 
2313                                 fp2--;
2314                         }
2315                         /* en weer in stijgende lijn maken */
2316                         a= KNOTSU(nu);
2317                         fp1= nu->knotsu;
2318                         fp2=tempf= MEM_mallocN(sizeof(float)*a, "switchdirect");
2319                         while(a--) {
2320                                 fp2[0]= fabs(fp1[1]-fp1[0]);
2321                                 fp1++;
2322                                 fp2++;
2323                         }
2324         
2325                         a= KNOTSU(nu)-1;
2326                         fp1= nu->knotsu;
2327                         fp2= tempf;
2328                         fp1[0]= 0.0;
2329                         fp1++;
2330                         while(a--) {
2331                                 fp1[0]= fp1[-1]+fp2[0];
2332                                 fp1++;
2333                                 fp2++;
2334                         }
2335                         MEM_freeN(tempf);
2336                 }
2337         }
2338         else {
2339                 
2340                 for(b=0; b<nu->pntsv; b++) {
2341                 
2342                         bp1= nu->bp+b*nu->pntsu;
2343                         a= nu->pntsu;
2344                         bp2= bp1+(a-1);
2345                         a/= 2;
2346                         
2347                         while(bp1!=bp2 && a>0) {
2348                                 SWAP(BPoint, *bp1, *bp2);
2349                                 a--;
2350                                 bp1++; 
2351                                 bp2--;
2352                         }
2353                 }
2354         }
2355 }