- added make_orco_curf, even does keys!
[blender.git] / source / blender / blenlib / intern / scanfill.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * (uit traces) maart 95
32  */
33
34 #include <stdio.h>
35 #include <math.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41
42 #include "BLI_util.h"
43 #include "DNA_listBase.h"
44 #include "DNA_mesh_types.h"
45 #include "BLI_editVert.h"
46 #include "BLI_arithb.h"
47 #include "BLI_scanfill.h"
48 #include "BLI_callbacks.h"
49
50 #ifdef HAVE_CONFIG_H
51 #include <config.h>
52 #endif
53
54 /* callbacks for errors and interrupts and some goo */
55 static void (*BLI_localErrorCallBack)(char*) = NULL;
56 static int (*BLI_localInterruptCallBack)(void) = NULL;
57
58 void BLI_setErrorCallBack(void (*f)(char*))
59 {
60         BLI_localErrorCallBack = f;
61 }
62
63 void BLI_setInterruptCallBack(int (*f)(void))
64 {
65         BLI_localInterruptCallBack = f;
66 }
67
68 /* just flush the error to /dev/null if the error handler is missing */
69 void callLocalErrorCallBack(char* msg)
70 {
71         if (BLI_localErrorCallBack) {
72                 BLI_localErrorCallBack(msg);
73         }
74 }
75
76 /* ignore if the interrupt wasn't set */
77 static int callLocalInterruptCallBack(void)
78 {
79         if (BLI_localInterruptCallBack) {
80                 return BLI_localInterruptCallBack();
81         } else {
82                 return 0;
83         }
84 }
85
86
87 /* local types */
88 typedef struct PolyFill {
89         int edges,verts;
90         float min[3],max[3];
91         short f,nr;
92 } PolyFill;
93
94 typedef struct ScFillVert {
95         EditVert *v1;
96         EditEdge *first,*last;
97         short f,f1;
98 } ScFillVert;
99
100
101 /* local funcs */
102
103 #define COMPLIMIT       0.0003
104
105 static ScFillVert *scdata;
106
107 ListBase fillvertbase = {0,0};
108 ListBase filledgebase = {0,0};
109 ListBase fillfacebase = {0,0};
110
111 static short cox, coy;
112
113 /* ****  FUBCTIONS FOR QSORT *************************** */
114
115
116 static int vergscdata(const void *a1, const void *a2)
117 {
118         const ScFillVert *x1=a1,*x2=a2;
119         
120         if( x1->v1->co[coy] < x2->v1->co[coy] ) return 1;
121         else if( x1->v1->co[coy] > x2->v1->co[coy]) return -1;
122         else if( x1->v1->co[cox] > x2->v1->co[cox] ) return 1;
123         else if( x1->v1->co[cox] < x2->v1->co[cox]) return -1;
124
125         return 0;
126 }
127
128 static int vergpoly(const void *a1, const void *a2)
129 {
130         const PolyFill *x1=a1, *x2=a2;
131
132         if( x1->min[cox] > x2->min[cox] ) return 1;
133         else if( x1->min[cox] < x2->min[cox] ) return -1;
134         else if( x1->min[coy] > x2->min[coy] ) return 1;
135         else if( x1->min[coy] < x2->min[coy] ) return -1;
136         
137         return 0;
138 }
139
140 /* ************* MEMORY MANAGEMENT ************* */
141
142 struct mem_elements {
143         struct mem_elements *next, *prev;
144         char *data;
145 };
146
147
148 /* simple optimization for allocating thousands of small memory blocks
149    only to be used within loops, and not by one function at a time
150    free in the end, with argument '-1'
151 */
152
153 static void *new_mem_element(int size)
154 {
155         int blocksize= 16384;
156         static int offs= 0;             /* the current free adress */
157         static struct mem_elements *cur= 0;
158         static ListBase lb= {0, 0};
159         void *adr;
160         
161         if(size>10000 || size==0) {
162                 printf("incorrect use of new_mem_element\n");
163         }
164         else if(size== -1) {
165                 cur= lb.first;
166                 while(cur) {
167                         MEM_freeN(cur->data);
168                         cur= cur->next;
169                 }
170                 BLI_freelistN(&lb);
171                 
172                 return NULL;    
173         }
174         
175         size= 4*( (size+3)/4 );
176         
177         if(cur) {
178                 if(size+offs < blocksize) {
179                         adr= (void *) (cur->data+offs);
180                         offs+= size;
181                         return adr;
182                 }
183         }
184         
185         cur= MEM_callocN( sizeof(struct mem_elements), "newmem");
186         cur->data= MEM_callocN(blocksize, "newmem");
187         BLI_addtail(&lb, cur);
188         
189         offs= size;
190         return cur->data;
191 }
192
193 void BLI_end_edgefill(void)
194 {
195         new_mem_element(-1);
196         
197         fillvertbase.first= fillvertbase.last= 0;
198         filledgebase.first= filledgebase.last= 0;
199         fillfacebase.first= fillfacebase.last= 0;
200 }
201
202 /* ****  FILL ROUTINES *************************** */
203
204 EditVert *BLI_addfillvert(float *vec)
205 {
206         EditVert *eve;
207         
208         eve= new_mem_element(sizeof(EditVert));
209         BLI_addtail(&fillvertbase, eve);
210         
211         eve->co[0] = vec[0];
212         eve->co[1] = vec[1];
213         eve->co[2] = vec[2];
214
215         return eve;     
216 }
217
218 EditEdge *BLI_addfilledge(EditVert *v1, EditVert *v2)
219 {
220         EditEdge *newed;
221
222         newed= new_mem_element(sizeof(EditEdge));
223         BLI_addtail(&filledgebase, newed);
224         
225         newed->v1= v1;
226         newed->v2= v2;
227
228         return newed;
229 }
230
231 static void addfillface(EditVert *v1, EditVert *v2, EditVert *v3, int mat_nr)
232 {
233         /* does not make edges */
234         EditFace *evl;
235
236         evl= new_mem_element(sizeof(EditFace));
237         BLI_addtail(&fillfacebase, evl);
238         
239         evl->v1= v1;
240         evl->v2= v2;
241         evl->v3= v3;
242         evl->f= 2;
243         evl->mat_nr= mat_nr;
244 }
245
246
247 static int boundinside(PolyFill *pf1, PolyFill *pf2)
248 {
249         /* is pf2 INSIDE pf1 ? using bounding box */
250         /* test first if polys exist */
251
252         if(pf1->edges==0 || pf2->edges==0) return 0;
253
254         if(pf2->max[cox]<pf1->max[cox])
255                 if(pf2->max[coy]<pf1->max[coy])
256                         if(pf2->min[cox]>pf1->min[cox])
257                                 if(pf2->min[coy]>pf1->min[coy]) return 1;
258         return 0;
259 }
260
261 static int boundisect(PolyFill *pf2, PolyFill *pf1)
262 {
263         /* has pf2 been touched (intersected) by pf1 ? with bounding box */
264         /* test first if polys exist */
265
266         if(pf1->edges==0 || pf2->edges==0) return 0;
267
268         if(pf2->max[cox] < pf1->min[cox] ) return 0;
269         if(pf2->max[coy] < pf1->min[coy] ) return 0;
270
271         if(pf2->min[cox] > pf1->max[cox] ) return 0;
272         if(pf2->min[coy] > pf1->max[coy] ) return 0;
273
274         /* join */
275         if(pf2->max[cox]<pf1->max[cox]) pf2->max[cox]= pf1->max[cox];
276         if(pf2->max[coy]<pf1->max[coy]) pf2->max[coy]= pf1->max[coy];
277
278         if(pf2->min[cox]>pf1->min[cox]) pf2->min[cox]= pf1->min[cox];
279         if(pf2->min[coy]>pf1->min[coy]) pf2->min[coy]= pf1->min[coy];
280
281         return 1;
282 }
283
284
285
286
287
288 static void mergepolysSimp(PolyFill *pf1, PolyFill *pf2)        /* add pf2 to pf1 */
289 {
290         EditVert *eve;
291         EditEdge *eed;
292
293         /* replace old poly numbers */
294         eve= fillvertbase.first;
295         while(eve) {
296                 if(eve->xs== pf2->nr) eve->xs= pf1->nr;
297                 eve= eve->next;
298         }
299         eed= filledgebase.first;
300         while(eed) {
301                 if(eed->f1== pf2->nr) eed->f1= pf1->nr;
302                 eed= eed->next;
303         }
304
305         pf1->verts+= pf2->verts;
306         pf1->edges+= pf2->edges;
307         pf2->verts= pf2->edges= 0;
308         pf1->f= (pf1->f | pf2->f);
309 }
310
311
312
313 static EditEdge *existfilledge(EditVert *v1, EditVert *v2)
314 {
315         EditEdge *eed;
316
317         eed= filledgebase.first;
318         while(eed) {
319                 if(eed->v1==v1 && eed->v2==v2) return eed;
320                 if(eed->v2==v1 && eed->v1==v2) return eed;
321                 eed= eed->next;
322         }
323         return 0;
324 }
325
326
327 static short testedgeside(float *v1, float *v2, float *v3)
328 /* is v3 to the right of v1-v2 ? With exception: v3==v1 || v3==v2 */
329 {
330         float inp;
331
332         inp= (v2[cox]-v1[cox])*(v1[coy]-v3[coy])
333             +(v1[coy]-v2[coy])*(v1[cox]-v3[cox]);
334
335         if(inp<0.0) return 0;
336         else if(inp==0) {
337                 if(v1[cox]==v3[cox] && v1[coy]==v3[coy]) return 0;
338                 if(v2[cox]==v3[cox] && v2[coy]==v3[coy]) return 0;
339         }
340         return 1;
341 }
342
343 static short testedgeside2(float *v1, float *v2, float *v3)
344 /* is v3 to the right of v1-v2 ? no intersection allowed! */
345 {
346         float inp;
347
348         inp= (v2[cox]-v1[cox])*(v1[coy]-v3[coy])
349             +(v1[coy]-v2[coy])*(v1[cox]-v3[cox]);
350
351         if(inp<=0.0) return 0;
352         return 1;
353 }
354
355 static short addedgetoscanvert(ScFillVert *sc, EditEdge *eed)
356 {
357         /* find first edge to the right of eed, and insert eed before that */
358         EditEdge *ed;
359         float fac,fac1,x,y;
360
361         if(sc->first==0) {
362                 sc->first= sc->last= eed;
363                 eed->prev= eed->next=0;
364                 return 1;
365         }
366
367         x= eed->v1->co[cox];
368         y= eed->v1->co[coy];
369
370         fac1= eed->v2->co[coy]-y;
371         if(fac1==0.0) {
372                 fac1= 1.0e10*(eed->v2->co[cox]-x);
373
374         }
375         else fac1= (x-eed->v2->co[cox])/fac1;
376
377         ed= sc->first;
378         while(ed) {
379
380                 if(ed->v2==eed->v2) return 0;
381
382                 fac= ed->v2->co[coy]-y;
383                 if(fac==0.0) {
384                         fac= 1.0e10*(ed->v2->co[cox]-x);
385
386                 }
387                 else fac= (x-ed->v2->co[cox])/fac;
388                 if(fac>fac1) break;
389
390                 ed= ed->next;
391         }
392         if(ed) BLI_insertlinkbefore((ListBase *)&(sc->first), ed, eed);
393         else BLI_addtail((ListBase *)&(sc->first),eed);
394
395         return 1;
396 }
397
398
399 static ScFillVert *addedgetoscanlist(EditEdge *eed, int len)
400 {
401         /* inserts edge at correct location in ScFillVert list */
402         /* returns sc when edge already exists */
403         ScFillVert *sc,scsearch;
404         EditVert *eve;
405
406         /* which vert is left-top? */
407         if(eed->v1->co[coy] == eed->v2->co[coy]) {
408                 if(eed->v1->co[cox] > eed->v2->co[cox]) {
409                         eve= eed->v1;
410                         eed->v1= eed->v2;
411                         eed->v2= eve;
412                 }
413         }
414         else if(eed->v1->co[coy] < eed->v2->co[coy]) {
415                 eve= eed->v1;
416                 eed->v1= eed->v2;
417                 eed->v2= eve;
418         }
419         /* find location in list */
420         scsearch.v1= eed->v1;
421         sc= (ScFillVert *)bsearch(&scsearch,scdata,len,
422             sizeof(ScFillVert), vergscdata);
423
424         if(sc==0) printf("Error in search edge: %lx\n",eed);
425         else if(addedgetoscanvert(sc,eed)==0) return sc;
426
427         return 0;
428 }
429
430 static short boundinsideEV(EditEdge *eed, EditVert *eve)
431 /* is eve inside boundbox eed */
432 {
433         float minx,maxx,miny,maxy;
434
435         if(eed->v1->co[cox]<eed->v2->co[cox]) {
436                 minx= eed->v1->co[cox];
437                 maxx= eed->v2->co[cox];
438         } else {
439                 minx= eed->v2->co[cox];
440                 maxx= eed->v1->co[cox];
441         }
442         if(eve->co[cox]>=minx && eve->co[cox]<=maxx) {
443                 if(eed->v1->co[coy]<eed->v2->co[coy]) {
444                         miny= eed->v1->co[coy];
445                         maxy= eed->v2->co[coy];
446                 } else {
447                         miny= eed->v2->co[coy];
448                         maxy= eed->v1->co[coy];
449                 }
450                 if(eve->co[coy]>=miny && eve->co[coy]<=maxy) return 1;
451         }
452         return 0;
453 }
454
455
456 static void testvertexnearedge(void)
457 {
458         /* only vertices with ->h==1 are being tested for
459                 being close to an edge, if true insert */
460
461         EditVert *eve;
462         EditEdge *eed,*ed1;
463         float dist,vec1[2],vec2[2],vec3[2];
464
465         eve= fillvertbase.first;
466         while(eve) {
467                 if(eve->h==1) {
468                         vec3[0]= eve->co[cox];
469                         vec3[1]= eve->co[coy];
470                         /* find the edge which has vertex eve */
471                         ed1= filledgebase.first;
472                         while(ed1) {
473                                 if(ed1->v1==eve || ed1->v2==eve) break;
474                                 ed1= ed1->next;
475                         }
476                         if(ed1->v1==eve) {
477                                 ed1->v1= ed1->v2;
478                                 ed1->v2= eve;
479                         }
480                         eed= filledgebase.first;
481                         while(eed) {
482                                 if(eve!=eed->v1 && eve!=eed->v2 && eve->xs==eed->f1) {
483                                         if(FloatCompare(eve->co,eed->v1->co, COMPLIMIT)) {
484                                                 ed1->v2= eed->v1;
485                                                 eed->v1->h++;
486                                                 eve->h= 0;
487                                                 break;
488                                         }
489                                         else if(FloatCompare(eve->co,eed->v2->co, COMPLIMIT)) {
490                                                 ed1->v2= eed->v2;
491                                                 eed->v2->h++;
492                                                 eve->h= 0;
493                                                 break;
494                                         }
495                                         else {
496                                                 vec1[0]= eed->v1->co[cox];
497                                                 vec1[1]= eed->v1->co[coy];
498                                                 vec2[0]= eed->v2->co[cox];
499                                                 vec2[1]= eed->v2->co[coy];
500                                                 if(boundinsideEV(eed,eve)) {
501                                                         dist= DistVL2Dfl(vec1,vec2,vec3);
502                                                         if(dist<COMPLIMIT) {
503                                                                 /* new edge */
504                                                                 ed1= BLI_addfilledge(eed->v1, eve);
505                                                                 
506                                                                 /* printf("fill: vertex near edge %x\n",eve); */
507                                                                 ed1->f= ed1->h= 0;
508                                                                 ed1->f1= eed->f1;
509                                                                 eed->v1= eve;
510                                                                 eve->h= 3;
511                                                                 break;
512                                                         }
513                                                 }
514                                         }
515                                 }
516                                 eed= eed->next;
517                         }
518                 }
519                 eve= eve->next;
520         }
521 }
522
523 static void splitlist(ListBase *tempve, ListBase *temped, short nr)
524 {
525         /* everything is in templist, write only poly nr to fillist */
526         EditVert *eve,*nextve;
527         EditEdge *eed,*nexted;
528
529         addlisttolist(tempve,&fillvertbase);
530         addlisttolist(temped,&filledgebase);
531
532         eve= tempve->first;
533         while(eve) {
534                 nextve= eve->next;
535                 if(eve->xs==nr) {
536                         BLI_remlink(tempve,eve);
537                         BLI_addtail(&fillvertbase,eve);
538                 }
539                 eve= nextve;
540         }
541         eed= temped->first;
542         while(eed) {
543                 nexted= eed->next;
544                 if(eed->f1==nr) {
545                         BLI_remlink(temped,eed);
546                         BLI_addtail(&filledgebase,eed);
547                 }
548                 eed= nexted;
549         }
550 }
551
552
553 static void scanfill(PolyFill *pf, int mat_nr)
554 {
555         ScFillVert *sc = NULL, *sc1;
556         EditVert *eve,*v1,*v2,*v3;
557         EditEdge *eed,*nexted,*ed1,*ed2,*ed3;
558         float miny = 0.0;
559         int a,b,verts, maxface, totface;        
560         short nr, test, twoconnected=0;
561
562         nr= pf->nr;
563         verts= pf->verts;
564
565         /* PRINTS
566         eve= fillvertbase.first;
567         while(eve) {
568                 printf("vert: %x co: %f %f\n",eve,eve->co[cox],eve->co[coy]);
569                 eve= eve->next;
570         }       
571         eed= filledgebase.first;
572         while(eed) {
573                 printf("edge: %x  verts: %x %x\n",eed,eed->v1,eed->v2);
574                 eed= eed->next;
575         } */
576
577         /* STEP 0: remove zero sized edges */
578         eed= filledgebase.first;
579         while(eed) {
580                 if(eed->v1->co[cox]==eed->v2->co[cox]) {
581                         if(eed->v1->co[coy]==eed->v2->co[coy]) {
582                                 if(eed->v1->f==255 && eed->v2->f!=255) {
583                                         eed->v2->f= 255;
584                                         eed->v2->vn= eed->v1->vn;
585                                 }
586                                 else if(eed->v2->f==255 && eed->v1->f!=255) {
587                                         eed->v1->f= 255;
588                                         eed->v1->vn= eed->v2->vn;
589                                 }
590                                 else if(eed->v2->f==255 && eed->v1->f==255) {
591                                         eed->v1->vn= eed->v2->vn;
592                                 }
593                                 else {
594                                         eed->v2->f= 255;
595                                         eed->v2->vn= eed->v1;
596                                 }
597                         }
598                 }
599                 eed= eed->next;
600         }
601
602         /* STEP 1: make using FillVert and FillEdge lists a sorted
603                 ScFillVert list
604         */
605         sc= scdata= (ScFillVert *)MEM_callocN(pf->verts*sizeof(ScFillVert),"Scanfill1");
606         eve= fillvertbase.first;
607         verts= 0;
608         while(eve) {
609                 if(eve->xs==nr) {
610                         if(eve->f!= 255) {
611                                 verts++;
612                                 eve->f= 0;      /* flag for connectedges later on */
613                                 sc->v1= eve;
614                                 sc++;
615                         }
616                 }
617                 eve= eve->next;
618         }
619
620         qsort(scdata, verts, sizeof(ScFillVert), vergscdata);
621
622         sc= scdata;
623         eed= filledgebase.first;
624         while(eed) {
625                 nexted= eed->next;
626                 eed->f= 0;
627                 BLI_remlink(&filledgebase,eed);
628                 if(eed->v1->f==255) {
629                         v1= eed->v1;
630                         while(eed->v1->f==255 && eed->v1->vn!=v1) eed->v1= eed->v1->vn;
631                 }
632                 if(eed->v2->f==255) {
633                         v2= eed->v2;
634                         while(eed->v2->f==255 && eed->v2->vn!=v2) eed->v2= eed->v2->vn;
635                 }
636                 if(eed->v1!=eed->v2) addedgetoscanlist(eed,verts);
637
638                 eed= nexted;
639         }
640         /*
641         sc= scdata;
642         for(a=0;a<verts;a++) {
643                 printf("\nscvert: %x\n",sc->v1);
644                 eed= sc->first;
645                 while(eed) {
646                         printf(" ed %x %x %x\n",eed,eed->v1,eed->v2);
647                         eed= eed->next;
648                 }
649                 sc++;
650         }*/
651
652
653         /* STEP 2: FILL LOOP */
654
655         if(pf->f==0) twoconnected= 1;
656
657         /* (temporal) security: never much more faces than vertices */
658         totface= 0;
659         maxface= 2*verts;               /* 2*verts: based at a filled circle within a triangle */
660
661         sc= scdata;
662         for(a=0;a<verts;a++) {
663                 /* printf("VERTEX %d %x\n",a,sc->v1); */
664                 ed1= sc->first;
665                 while(ed1) {    /* set connectflags  */
666                         nexted= ed1->next;
667                         if(ed1->v1->h==1 || ed1->v2->h==1) {
668                                 BLI_remlink((ListBase *)&(sc->first),ed1);
669                                 BLI_addtail(&filledgebase,ed1);
670                                 if(ed1->v1->h>1) ed1->v1->h--;
671                                 if(ed1->v2->h>1) ed1->v2->h--;
672                         }
673                         else ed1->v2->f= 1;
674
675                         ed1= nexted;
676                 }
677                 while(sc->first) {      /* for as long there are edges */
678                         ed1= sc->first;
679                         ed2= ed1->next;
680                         
681                         /* commented out... the ESC here delivers corrupted memory (and doesnt work during grab) */
682                         /* if(callLocalInterruptCallBack()) break; */
683                         if(totface>maxface) {
684                                 /* printf("Fill error: endless loop. Escaped at vert %d,  tot: %d.\n", a, verts); */
685                                 a= verts;
686                                 break;
687                         }
688                         if(ed2==0) {
689                                 sc->first=sc->last= 0;
690                                 /* printf("just 1 edge to vert\n"); */
691                                 BLI_addtail(&filledgebase,ed1);
692                                 ed1->v2->f= 0;
693                                 ed1->v1->h--; 
694                                 ed1->v2->h--;
695                         } else {
696                                 /* test rest of vertices */
697                                 v1= ed1->v2;
698                                 v2= ed1->v1;
699                                 v3= ed2->v2;
700                                 /* this happens with a serial of overlapping edges */
701                                 if(v1==v2 || v2==v3) break;
702                                 /* printf("test verts %x %x %x\n",v1,v2,v3); */
703                                 miny = ( (v1->co[coy])<(v3->co[coy]) ? (v1->co[coy]) : (v3->co[coy]) );
704                                 /*  miny= MIN2(v1->co[coy],v3->co[coy]); */
705                                 sc1= sc+1;
706                                 test= 0;
707
708                                 for(b=a+1;b<verts;b++) {
709                                         if(sc1->v1->f==0) {
710                                                 if(sc1->v1->co[coy] <= miny) break;
711
712                                                 if(testedgeside(v1->co,v2->co,sc1->v1->co))
713                                                         if(testedgeside(v2->co,v3->co,sc1->v1->co))
714                                                                 if(testedgeside(v3->co,v1->co,sc1->v1->co)) {
715                                                                         /* point in triangle */
716                                                                 
717                                                                         test= 1;
718                                                                         break;
719                                                                 }
720                                         }
721                                         sc1++;
722                                 }
723                                 if(test) {
724                                         /* make new edge, and start over */
725                                         /* printf("add new edge %x %x and start again\n",v2,sc1->v1); */
726
727                                         ed3= BLI_addfilledge(v2, sc1->v1);
728                                         BLI_remlink(&filledgebase, ed3);
729                                         BLI_insertlinkbefore((ListBase *)&(sc->first), ed2, ed3);
730                                         ed3->v2->f= 1;
731                                         ed3->f= 2;
732                                         ed3->v1->h++; 
733                                         ed3->v2->h++;
734                                 }
735                                 else {
736                                         /* new triangle */
737                                         /* printf("add face %x %x %x\n",v1,v2,v3); */
738                                         addfillface(v1, v2, v3, mat_nr);
739                                         totface++;
740                                         BLI_remlink((ListBase *)&(sc->first),ed1);
741                                         BLI_addtail(&filledgebase,ed1);
742                                         ed1->v2->f= 0;
743                                         ed1->v1->h--; 
744                                         ed1->v2->h--;
745                                         /* ed2 can be removed when it's an old one */
746                                         if(ed2->f==0 && twoconnected) {
747                                                 BLI_remlink((ListBase *)&(sc->first),ed2);
748                                                 BLI_addtail(&filledgebase,ed2);
749                                                 ed2->v2->f= 0;
750                                                 ed2->v1->h--; 
751                                                 ed2->v2->h--;
752                                         }
753
754                                         /* new edge */
755                                         ed3= BLI_addfilledge(v1, v3);
756                                         BLI_remlink(&filledgebase, ed3);
757                                         ed3->f= 2;
758                                         ed3->v1->h++; 
759                                         ed3->v2->h++;
760                                         
761                                         /* printf("add new edge %x %x\n",v1,v3); */
762                                         sc1= addedgetoscanlist(ed3, verts);
763                                         
764                                         if(sc1) {       /* ed3 already exists: remove */
765                                                 /* printf("Edge exists\n"); */
766                                                 ed3->v1->h--; 
767                                                 ed3->v2->h--;
768
769                                                 if(twoconnected) ed3= sc1->first;
770                                                 else ed3= 0;
771                                                 while(ed3) {
772                                                         if( (ed3->v1==v1 && ed3->v2==v3) || (ed3->v1==v3 && ed3->v2==v1) ) {
773                                                                 BLI_remlink((ListBase *)&(sc1->first),ed3);
774                                                                 BLI_addtail(&filledgebase,ed3);
775                                                                 ed3->v1->h--; 
776                                                                 ed3->v2->h--;
777                                                                 break;
778                                                         }
779                                                         ed3= ed3->next;
780                                                 }
781                                         }
782
783                                 }
784                         }
785                         /* test for loose edges */
786                         ed1= sc->first;
787                         while(ed1) {
788                                 nexted= ed1->next;
789                                 if(ed1->v1->h<2 || ed1->v2->h<2) {
790                                         BLI_remlink((ListBase *)&(sc->first),ed1);
791                                         BLI_addtail(&filledgebase,ed1);
792                                         if(ed1->v1->h>1) ed1->v1->h--;
793                                         if(ed1->v2->h>1) ed1->v2->h--;
794                                 }
795
796                                 ed1= nexted;
797                         }
798                 }
799                 sc++;
800         }
801
802         MEM_freeN(scdata);
803 }
804
805
806
807 int BLI_edgefill(int mode, int mat_nr)
808 {
809         /*
810           - fill works with its own lists, so create that first (no faces!)
811           - for vertices, put in ->vn the old pointer
812           - struct elements xs en ys are not used here: don't hide stuff in it
813           - edge flag ->f becomes 2 when it's a new edge
814           - mode: & 1 is check for crossings, then create edges (TO DO )
815         */
816         ListBase tempve, temped;
817         EditVert *eve;
818         EditEdge *eed,*nexted;
819         PolyFill *pflist,*pf;
820         float *minp, *maxp, *v1, *v2, norm[3], len;
821         short a,c,poly=0,ok=0,toggle=0;
822
823         /* reset variables */
824         eve= fillvertbase.first;
825         while(eve) {
826                 eve->f= 0;
827                 eve->xs= 0;
828                 eve->h= 0;
829                 eve= eve->next;
830         }
831
832         /* first test vertices if they are in edges */
833         /* including resetting of flags */
834         eed= filledgebase.first;
835         while(eed) {
836                 eed->f= eed->f1= eed->h= 0;
837                 eed->v1->f= 1;
838                 eed->v2->f= 1;
839
840                 eed= eed->next;
841         }
842
843         eve= fillvertbase.first;
844         while(eve) {
845                 if(eve->f & 1) {
846                         ok=1; 
847                         break;
848                 }
849                 eve= eve->next;
850         }
851
852         if(ok==0) return 0;
853
854         /* NEW NEW! define projection: with 'best' normal */
855         /* just use the first three different vertices */
856         
857         /* THIS PART STILL IS PRETTY WEAK! (ton) */
858         
859         eve= fillvertbase.last;
860         len= 0.0;
861         v1= eve->co;
862         v2= 0;
863         eve= fillvertbase.first;
864         while(eve) {
865                 if(v2) {
866                         if( FloatCompare(v2, eve->co,  0.0003)==0) {
867                                 len= CalcNormFloat(v1, v2, eve->co, norm);
868                                 if(len != 0.0) break;
869                         }
870                 }
871                 else if(FloatCompare(v1, eve->co,  0.0003)==0) {
872                         v2= eve->co;
873                 }
874                 eve= eve->next;
875         }
876
877         if(len==0.0) return 0;  /* no fill possible */
878
879         norm[0]= fabs(norm[0]);
880         norm[1]= fabs(norm[1]);
881         norm[2]= fabs(norm[2]);
882         
883         if(norm[2]>=norm[0] && norm[2]>=norm[1]) {
884                 cox= 0; coy= 1;
885         }
886         else if(norm[1]>=norm[0] && norm[1]>=norm[2]) {
887                 cox= 0; coy= 2;
888         }
889         else {
890                 cox= 1; coy= 2;
891         }
892
893         /* STEP 1: COUNT POLYS */
894         eve= fillvertbase.first;
895         while(eve) {
896                 /* get first vertex with no poly number */
897                 if(eve->xs==0) {
898                         poly++;
899                         /* now a sortof select connected */
900                         ok= 1;
901                         eve->xs= poly;
902                         
903                         while(ok) {
904                                 
905                                 ok= 0;
906                                 toggle++;
907                                 if(toggle & 1) eed= filledgebase.first;
908                                 else eed= filledgebase.last;
909
910                                 while(eed) {
911                                         if(eed->v1->xs==0 && eed->v2->xs==poly) {
912                                                 eed->v1->xs= poly;
913                                                 eed->f1= poly;
914                                                 ok= 1;
915                                         }
916                                         else if(eed->v2->xs==0 && eed->v1->xs==poly) {
917                                                 eed->v2->xs= poly;
918                                                 eed->f1= poly;
919                                                 ok= 1;
920                                         }
921                                         else if(eed->f1==0) {
922                                                 if(eed->v1->xs==poly && eed->v2->xs==poly) {
923                                                         eed->f1= poly;
924                                                         ok= 1;
925                                                 }
926                                         }
927                                         if(toggle & 1) eed= eed->next;
928                                         else eed= eed->prev;
929                                 }
930                         }
931                 }
932                 eve= eve->next;
933         }
934         /* printf("amount of poly's: %d\n",poly); */
935
936         /* STEP 2: remove loose edges and strings of edges */
937         eed= filledgebase.first;
938         while(eed) {
939                 if(eed->v1->h++ >250) break;
940                 if(eed->v2->h++ >250) break;
941                 eed= eed->next;
942         }
943         if(eed) {
944                 /* otherwise it's impossible to be sure you can clear vertices */
945                 callLocalErrorCallBack("No vertices with 250 edges allowed!");
946                 return 0;
947         }
948         
949         /* does it only for vertices with ->h==1 */
950         testvertexnearedge();
951
952         ok= 1;
953         while(ok) {
954                 ok= 0;
955                 toggle++;
956                 if(toggle & 1) eed= filledgebase.first;
957                 else eed= filledgebase.last;
958                 while(eed) {
959                         if(toggle & 1) nexted= eed->next;
960                         else nexted= eed->prev;
961                         if(eed->v1->h==1) {
962                                 eed->v2->h--;
963                                 BLI_remlink(&fillvertbase,eed->v1); 
964                                 BLI_remlink(&filledgebase,eed); 
965                                 ok= 1;
966                         }
967                         else if(eed->v2->h==1) {
968                                 eed->v1->h--;
969                                 BLI_remlink(&fillvertbase,eed->v2); 
970                                 BLI_remlink(&filledgebase,eed); 
971                                 ok= 1;
972                         }
973                         eed= nexted;
974                 }
975         }
976         if(filledgebase.first==0) {
977                 /* printf("All edges removed\n"); */
978                 return 0;
979         }
980
981
982         /* CURRENT STATUS:
983         - eve->f  :1= availalble in edges
984         - eve->xs :polynumber
985         - eve->h  :amount of edges connected to vertex
986         - eve->vn :store! original vertex number
987
988         - eed->f  :
989         - eed->f1 :poly number
990         */
991
992
993         /* STEP 3: MAKE POLYFILL STRUCT */
994         pflist= (PolyFill *)MEM_callocN(poly*sizeof(PolyFill),"edgefill");
995         pf= pflist;
996         for(a=1;a<=poly;a++) {
997                 pf->nr= a;
998                 pf->min[0]=pf->min[1]=pf->min[2]= 1.0e20;
999                 pf->max[0]=pf->max[1]=pf->max[2]= -1.0e20;
1000                 pf++;
1001         }
1002         eed= filledgebase.first;
1003         while(eed) {
1004                 pflist[eed->f1-1].edges++;
1005                 eed= eed->next;
1006         }
1007
1008         eve= fillvertbase.first;
1009         while(eve) {
1010                 pflist[eve->xs-1].verts++;
1011                 minp= pflist[eve->xs-1].min;
1012                 maxp= pflist[eve->xs-1].max;
1013
1014                 minp[cox]= (minp[cox])<(eve->co[cox]) ? (minp[cox]) : (eve->co[cox]);
1015                 minp[coy]= (minp[coy])<(eve->co[coy]) ? (minp[coy]) : (eve->co[coy]);
1016                 maxp[cox]= (maxp[cox])>(eve->co[cox]) ? (maxp[cox]) : (eve->co[cox]);
1017                 maxp[coy]= (maxp[coy])>(eve->co[coy]) ? (maxp[coy]) : (eve->co[coy]);
1018                 if(eve->h>2) pflist[eve->xs-1].f= 1;
1019
1020                 eve= eve->next;
1021         }
1022
1023         /* STEP 4: FIND HOLES OR BOUNDS, JOIN THEM
1024          *  ( bounds just to divide it in pieces for optimization, 
1025          *    the edgefill itself has good auto-hole detection)
1026          * WATCH IT: ONLY WORKS WITH SORTED POLYS!!! */
1027         
1028         if(poly>1) {
1029                 short *polycache, *pc;
1030
1031                 /* so, sort first */
1032                 qsort(pflist, poly, sizeof(PolyFill), vergpoly);
1033                 
1034                 /*pf= pflist;
1035                 for(a=1;a<=poly;a++) {
1036                         printf("poly:%d edges:%d verts:%d flag: %d\n",a,pf->edges,pf->verts,pf->f);
1037                         PRINT2(f, f, pf->min[0], pf->min[1]);
1038                         pf++;
1039                 }*/
1040         
1041                 polycache= pc= MEM_callocN(sizeof(short)*poly, "polycache");
1042                 pf= pflist;
1043                 for(a=0; a<poly; a++, pf++) {
1044                         for(c=a+1;c<poly;c++) {
1045                                 
1046                                 /* if 'a' inside 'c': join (bbox too)
1047                                  * Careful: 'a' can also be inside another poly.
1048                                  */
1049                                 if(boundisect(pf, pflist+c)) {
1050                                         *pc= c;
1051                                         pc++;
1052                                 }
1053                                 /* only for optimize! */
1054                                 /* else if(pf->max[cox] < (pflist+c)->min[cox]) break; */
1055                                 
1056                         }
1057                         while(pc!=polycache) {
1058                                 pc--;
1059                                 mergepolysSimp(pf, pflist+ *pc);
1060                         }
1061                 }
1062                 MEM_freeN(polycache);
1063         }
1064         
1065         pf= pflist;
1066         /* printf("after merge\n");
1067         for(a=1;a<=poly;a++) {
1068                 printf("poly:%d edges:%d verts:%d flag: %d\n",a,pf->edges,pf->verts,pf->f);
1069                 pf++;
1070         } */
1071
1072         /* STEP 5: MAKE TRIANGLES */
1073
1074         tempve.first= fillvertbase.first;
1075         tempve.last= fillvertbase.last;
1076         temped.first= filledgebase.first;
1077         temped.last= filledgebase.last;
1078         fillvertbase.first=fillvertbase.last= 0;
1079         filledgebase.first=filledgebase.last= 0;
1080
1081         pf= pflist;
1082         for(a=0;a<poly;a++) {
1083                 if(pf->edges>1) {
1084                         splitlist(&tempve,&temped,pf->nr);
1085                         scanfill(pf, mat_nr);
1086                 }
1087                 pf++;
1088         }
1089         addlisttolist(&fillvertbase,&tempve);
1090         addlisttolist(&filledgebase,&temped);
1091
1092         /* FREE */
1093
1094         MEM_freeN(pflist);
1095         return 1;
1096
1097 }