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