4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * (uit traces) maart 95
35 #include "MEM_guardedalloc.h"
37 #include "DNA_listBase.h"
38 #include "DNA_mesh_types.h"
40 #include "BLI_editVert.h"
41 #include "BLI_listbase.h"
43 #include "BLI_scanfill.h"
44 #include "BLI_callbacks.h"
50 /* callbacks for errors and interrupts and some goo */
51 static void (*BLI_localErrorCallBack)(char*) = NULL;
52 static int (*BLI_localInterruptCallBack)(void) = NULL;
54 void BLI_setErrorCallBack(void (*f)(char*))
56 BLI_localErrorCallBack = f;
59 void BLI_setInterruptCallBack(int (*f)(void))
61 BLI_localInterruptCallBack = f;
64 /* just flush the error to /dev/null if the error handler is missing */
65 void callLocalErrorCallBack(char* msg)
67 if (BLI_localErrorCallBack) {
68 BLI_localErrorCallBack(msg);
73 /* ignore if the interrupt wasn't set */
74 static int callLocalInterruptCallBack(void)
76 if (BLI_localInterruptCallBack) {
77 return BLI_localInterruptCallBack();
85 typedef struct PolyFill {
91 typedef struct ScFillVert {
93 EditEdge *first,*last;
100 #define COMPLIMIT 0.00003
102 static ScFillVert *scdata;
104 ListBase fillvertbase = {0,0};
105 ListBase filledgebase = {0,0};
106 ListBase fillfacebase = {0,0};
108 static short cox, coy;
110 /* **** FUBCTIONS FOR QSORT *************************** */
113 static int vergscdata(const void *a1, const void *a2)
115 const ScFillVert *x1=a1,*x2=a2;
117 if( x1->v1->co[coy] < x2->v1->co[coy] ) return 1;
118 else if( x1->v1->co[coy] > x2->v1->co[coy]) return -1;
119 else if( x1->v1->co[cox] > x2->v1->co[cox] ) return 1;
120 else if( x1->v1->co[cox] < x2->v1->co[cox]) return -1;
125 static int vergpoly(const void *a1, const void *a2)
127 const PolyFill *x1=a1, *x2=a2;
129 if( x1->min[cox] > x2->min[cox] ) return 1;
130 else if( x1->min[cox] < x2->min[cox] ) return -1;
131 else if( x1->min[coy] > x2->min[coy] ) return 1;
132 else if( x1->min[coy] < x2->min[coy] ) return -1;
137 /* ************* MEMORY MANAGEMENT ************* */
139 struct mem_elements {
140 struct mem_elements *next, *prev;
145 /* simple optimization for allocating thousands of small memory blocks
146 only to be used within loops, and not by one function at a time
147 free in the end, with argument '-1'
150 static void *new_mem_element(int size)
152 int blocksize= 16384;
153 static int offs= 0; /* the current free adress */
154 static struct mem_elements *cur= 0;
155 static ListBase lb= {0, 0};
158 if(size>10000 || size==0) {
159 printf("incorrect use of new_mem_element\n");
164 MEM_freeN(cur->data);
172 size= 4*( (size+3)/4 );
175 if(size+offs < blocksize) {
176 adr= (void *) (cur->data+offs);
182 cur= MEM_callocN( sizeof(struct mem_elements), "newmem");
183 cur->data= MEM_callocN(blocksize, "newmem");
184 BLI_addtail(&lb, cur);
190 void BLI_end_edgefill(void)
194 fillvertbase.first= fillvertbase.last= 0;
195 filledgebase.first= filledgebase.last= 0;
196 fillfacebase.first= fillfacebase.last= 0;
199 /* **** FILL ROUTINES *************************** */
201 EditVert *BLI_addfillvert(float *vec)
205 eve= new_mem_element(sizeof(EditVert));
206 BLI_addtail(&fillvertbase, eve);
215 EditEdge *BLI_addfilledge(EditVert *v1, EditVert *v2)
219 newed= new_mem_element(sizeof(EditEdge));
220 BLI_addtail(&filledgebase, newed);
228 static void addfillface(EditVert *v1, EditVert *v2, EditVert *v3, int mat_nr)
230 /* does not make edges */
233 evl= new_mem_element(sizeof(EditFace));
234 BLI_addtail(&fillfacebase, evl);
243 static int boundisect(PolyFill *pf2, PolyFill *pf1)
245 /* has pf2 been touched (intersected) by pf1 ? with bounding box */
246 /* test first if polys exist */
248 if(pf1->edges==0 || pf2->edges==0) return 0;
250 if(pf2->max[cox] < pf1->min[cox] ) return 0;
251 if(pf2->max[coy] < pf1->min[coy] ) return 0;
253 if(pf2->min[cox] > pf1->max[cox] ) return 0;
254 if(pf2->min[coy] > pf1->max[coy] ) return 0;
257 if(pf2->max[cox]<pf1->max[cox]) pf2->max[cox]= pf1->max[cox];
258 if(pf2->max[coy]<pf1->max[coy]) pf2->max[coy]= pf1->max[coy];
260 if(pf2->min[cox]>pf1->min[cox]) pf2->min[cox]= pf1->min[cox];
261 if(pf2->min[coy]>pf1->min[coy]) pf2->min[coy]= pf1->min[coy];
267 static void mergepolysSimp(PolyFill *pf1, PolyFill *pf2) /* add pf2 to pf1 */
272 /* replace old poly numbers */
273 eve= fillvertbase.first;
275 if(eve->xs== pf2->nr) eve->xs= pf1->nr;
278 eed= filledgebase.first;
280 if(eed->f1== pf2->nr) eed->f1= pf1->nr;
284 pf1->verts+= pf2->verts;
285 pf1->edges+= pf2->edges;
286 pf2->verts= pf2->edges= 0;
287 pf1->f= (pf1->f | pf2->f);
290 static short testedgeside(float *v1, float *v2, float *v3)
291 /* is v3 to the right of v1-v2 ? With exception: v3==v1 || v3==v2 */
295 inp= (v2[cox]-v1[cox])*(v1[coy]-v3[coy])
296 +(v1[coy]-v2[coy])*(v1[cox]-v3[cox]);
298 if(inp<0.0) return 0;
300 if(v1[cox]==v3[cox] && v1[coy]==v3[coy]) return 0;
301 if(v2[cox]==v3[cox] && v2[coy]==v3[coy]) return 0;
306 static short addedgetoscanvert(ScFillVert *sc, EditEdge *eed)
308 /* find first edge to the right of eed, and insert eed before that */
313 sc->first= sc->last= eed;
314 eed->prev= eed->next=0;
321 fac1= eed->v2->co[coy]-y;
323 fac1= 1.0e10*(eed->v2->co[cox]-x);
326 else fac1= (x-eed->v2->co[cox])/fac1;
331 if(ed->v2==eed->v2) return 0;
333 fac= ed->v2->co[coy]-y;
335 fac= 1.0e10*(ed->v2->co[cox]-x);
338 else fac= (x-ed->v2->co[cox])/fac;
343 if(ed) BLI_insertlinkbefore((ListBase *)&(sc->first), ed, eed);
344 else BLI_addtail((ListBase *)&(sc->first),eed);
350 static ScFillVert *addedgetoscanlist(EditEdge *eed, int len)
352 /* inserts edge at correct location in ScFillVert list */
353 /* returns sc when edge already exists */
354 ScFillVert *sc,scsearch;
357 /* which vert is left-top? */
358 if(eed->v1->co[coy] == eed->v2->co[coy]) {
359 if(eed->v1->co[cox] > eed->v2->co[cox]) {
365 else if(eed->v1->co[coy] < eed->v2->co[coy]) {
370 /* find location in list */
371 scsearch.v1= eed->v1;
372 sc= (ScFillVert *)bsearch(&scsearch,scdata,len,
373 sizeof(ScFillVert), vergscdata);
375 if(sc==0) printf("Error in search edge: %p\n",eed);
376 else if(addedgetoscanvert(sc,eed)==0) return sc;
381 static short boundinsideEV(EditEdge *eed, EditVert *eve)
382 /* is eve inside boundbox eed */
384 float minx,maxx,miny,maxy;
386 if(eed->v1->co[cox]<eed->v2->co[cox]) {
387 minx= eed->v1->co[cox];
388 maxx= eed->v2->co[cox];
390 minx= eed->v2->co[cox];
391 maxx= eed->v1->co[cox];
393 if(eve->co[cox]>=minx && eve->co[cox]<=maxx) {
394 if(eed->v1->co[coy]<eed->v2->co[coy]) {
395 miny= eed->v1->co[coy];
396 maxy= eed->v2->co[coy];
398 miny= eed->v2->co[coy];
399 maxy= eed->v1->co[coy];
401 if(eve->co[coy]>=miny && eve->co[coy]<=maxy) return 1;
407 static void testvertexnearedge(void)
409 /* only vertices with ->h==1 are being tested for
410 being close to an edge, if true insert */
414 float dist,vec1[2],vec2[2],vec3[2];
416 eve= fillvertbase.first;
419 vec3[0]= eve->co[cox];
420 vec3[1]= eve->co[coy];
421 /* find the edge which has vertex eve */
422 ed1= filledgebase.first;
424 if(ed1->v1==eve || ed1->v2==eve) break;
431 eed= filledgebase.first;
433 if(eve!=eed->v1 && eve!=eed->v2 && eve->xs==eed->f1) {
434 if(compare_v3v3(eve->co,eed->v1->co, COMPLIMIT)) {
440 else if(compare_v3v3(eve->co,eed->v2->co, COMPLIMIT)) {
447 vec1[0]= eed->v1->co[cox];
448 vec1[1]= eed->v1->co[coy];
449 vec2[0]= eed->v2->co[cox];
450 vec2[1]= eed->v2->co[coy];
451 if(boundinsideEV(eed,eve)) {
452 dist= dist_to_line_v2(vec1,vec2,vec3);
455 ed1= BLI_addfilledge(eed->v1, eve);
457 /* printf("fill: vertex near edge %x\n",eve); */
474 static void splitlist(ListBase *tempve, ListBase *temped, short nr)
476 /* everything is in templist, write only poly nr to fillist */
477 EditVert *eve,*nextve;
478 EditEdge *eed,*nexted;
480 addlisttolist(tempve,&fillvertbase);
481 addlisttolist(temped,&filledgebase);
487 BLI_remlink(tempve,eve);
488 BLI_addtail(&fillvertbase,eve);
496 BLI_remlink(temped,eed);
497 BLI_addtail(&filledgebase,eed);
504 static void scanfill(PolyFill *pf, int mat_nr)
506 ScFillVert *sc = NULL, *sc1;
507 EditVert *eve,*v1,*v2,*v3;
508 EditEdge *eed,*nexted,*ed1,*ed2,*ed3;
510 int a,b,verts, maxface, totface;
511 short nr, test, twoconnected=0;
517 eve= fillvertbase.first;
519 printf("vert: %x co: %f %f\n",eve,eve->co[cox],eve->co[coy]);
522 eed= filledgebase.first;
524 printf("edge: %x verts: %x %x\n",eed,eed->v1,eed->v2);
528 /* STEP 0: remove zero sized edges */
529 eed= filledgebase.first;
531 if(eed->v1->co[cox]==eed->v2->co[cox]) {
532 if(eed->v1->co[coy]==eed->v2->co[coy]) {
533 if(eed->v1->f==255 && eed->v2->f!=255) {
535 eed->v2->tmp.v= eed->v1->tmp.v;
537 else if(eed->v2->f==255 && eed->v1->f!=255) {
539 eed->v1->tmp.v= eed->v2->tmp.v;
541 else if(eed->v2->f==255 && eed->v1->f==255) {
542 eed->v1->tmp.v= eed->v2->tmp.v;
546 eed->v2->tmp.v = eed->v1->tmp.v;
553 /* STEP 1: make using FillVert and FillEdge lists a sorted
556 sc= scdata= (ScFillVert *)MEM_callocN(pf->verts*sizeof(ScFillVert),"Scanfill1");
557 eve= fillvertbase.first;
563 eve->f= 0; /* flag for connectedges later on */
571 qsort(scdata, verts, sizeof(ScFillVert), vergscdata);
574 eed= filledgebase.first;
578 BLI_remlink(&filledgebase,eed);
579 /* commented all of this out, this I have no idea for what it is for, probably from ancient past */
580 /* it does crash blender, since it uses mixed original and new vertices (ton) */
581 // if(eed->v1->f==255) {
583 // while((eed->v1->f == 255) && (eed->v1->tmp.v != v1))
584 // eed->v1 = eed->v1->tmp.v;
586 // if(eed->v2->f==255) {
588 // while((eed->v2->f == 255) && (eed->v2->tmp.v != v2))
589 // eed->v2 = eed->v2->tmp.v;
591 if(eed->v1!=eed->v2) addedgetoscanlist(eed,verts);
597 for(a=0;a<verts;a++) {
598 printf("\nscvert: %x\n",sc->v1);
601 printf(" ed %x %x %x\n",eed,eed->v1,eed->v2);
608 /* STEP 2: FILL LOOP */
610 if(pf->f==0) twoconnected= 1;
612 /* (temporal) security: never much more faces than vertices */
614 maxface= 2*verts; /* 2*verts: based at a filled circle within a triangle */
617 for(a=0;a<verts;a++) {
618 /* printf("VERTEX %d %x\n",a,sc->v1); */
620 while(ed1) { /* set connectflags */
622 if(ed1->v1->h==1 || ed1->v2->h==1) {
623 BLI_remlink((ListBase *)&(sc->first),ed1);
624 BLI_addtail(&filledgebase,ed1);
625 if(ed1->v1->h>1) ed1->v1->h--;
626 if(ed1->v2->h>1) ed1->v2->h--;
632 while(sc->first) { /* for as long there are edges */
636 /* commented out... the ESC here delivers corrupted memory (and doesnt work during grab) */
637 /* if(callLocalInterruptCallBack()) break; */
638 if(totface>maxface) {
639 /* printf("Fill error: endless loop. Escaped at vert %d, tot: %d.\n", a, verts); */
644 sc->first=sc->last= 0;
645 /* printf("just 1 edge to vert\n"); */
646 BLI_addtail(&filledgebase,ed1);
651 /* test rest of vertices */
655 /* this happens with a serial of overlapping edges */
656 if(v1==v2 || v2==v3) break;
657 /* printf("test verts %x %x %x\n",v1,v2,v3); */
658 miny = ( (v1->co[coy])<(v3->co[coy]) ? (v1->co[coy]) : (v3->co[coy]) );
659 /* miny= MIN2(v1->co[coy],v3->co[coy]); */
663 for(b=a+1;b<verts;b++) {
665 if(sc1->v1->co[coy] <= miny) break;
667 if(testedgeside(v1->co,v2->co,sc1->v1->co))
668 if(testedgeside(v2->co,v3->co,sc1->v1->co))
669 if(testedgeside(v3->co,v1->co,sc1->v1->co)) {
670 /* point in triangle */
679 /* make new edge, and start over */
680 /* printf("add new edge %x %x and start again\n",v2,sc1->v1); */
682 ed3= BLI_addfilledge(v2, sc1->v1);
683 BLI_remlink(&filledgebase, ed3);
684 BLI_insertlinkbefore((ListBase *)&(sc->first), ed2, ed3);
692 /* printf("add face %x %x %x\n",v1,v2,v3); */
693 addfillface(v1, v2, v3, mat_nr);
695 BLI_remlink((ListBase *)&(sc->first),ed1);
696 BLI_addtail(&filledgebase,ed1);
700 /* ed2 can be removed when it's an old one */
701 if(ed2->f==0 && twoconnected) {
702 BLI_remlink((ListBase *)&(sc->first),ed2);
703 BLI_addtail(&filledgebase,ed2);
710 ed3= BLI_addfilledge(v1, v3);
711 BLI_remlink(&filledgebase, ed3);
716 /* printf("add new edge %x %x\n",v1,v3); */
717 sc1= addedgetoscanlist(ed3, verts);
719 if(sc1) { /* ed3 already exists: remove */
720 /* printf("Edge exists\n"); */
724 if(twoconnected) ed3= sc1->first;
727 if( (ed3->v1==v1 && ed3->v2==v3) || (ed3->v1==v3 && ed3->v2==v1) ) {
728 BLI_remlink((ListBase *)&(sc1->first),ed3);
729 BLI_addtail(&filledgebase,ed3);
740 /* test for loose edges */
744 if(ed1->v1->h<2 || ed1->v2->h<2) {
745 BLI_remlink((ListBase *)&(sc->first),ed1);
746 BLI_addtail(&filledgebase,ed1);
747 if(ed1->v1->h>1) ed1->v1->h--;
748 if(ed1->v2->h>1) ed1->v2->h--;
762 int BLI_edgefill(int mode, int mat_nr)
765 - fill works with its own lists, so create that first (no faces!)
766 - for vertices, put in ->tmp.v the old pointer
767 - struct elements xs en ys are not used here: don't hide stuff in it
768 - edge flag ->f becomes 2 when it's a new edge
769 - mode: & 1 is check for crossings, then create edges (TO DO )
771 ListBase tempve, temped;
773 EditEdge *eed,*nexted;
774 PolyFill *pflist,*pf;
775 float *minp, *maxp, *v1, *v2, norm[3], len;
776 short a,c,poly=0,ok=0,toggle=0;
778 /* reset variables */
779 eve= fillvertbase.first;
787 /* first test vertices if they are in edges */
788 /* including resetting of flags */
789 eed= filledgebase.first;
791 eed->f= eed->f1= eed->h= 0;
798 eve= fillvertbase.first;
809 /* NEW NEW! define projection: with 'best' normal */
810 /* just use the first three different vertices */
812 /* THIS PART STILL IS PRETTY WEAK! (ton) */
814 eve= fillvertbase.last;
818 eve= fillvertbase.first;
821 if( compare_v3v3(v2, eve->co, COMPLIMIT)==0) {
822 len= normal_tri_v3( norm,v1, v2, eve->co);
823 if(len != 0.0) break;
826 else if(compare_v3v3(v1, eve->co, COMPLIMIT)==0) {
832 if(len==0.0) return 0; /* no fill possible */
834 norm[0]= fabs(norm[0]);
835 norm[1]= fabs(norm[1]);
836 norm[2]= fabs(norm[2]);
838 if(norm[2]>=norm[0] && norm[2]>=norm[1]) {
841 else if(norm[1]>=norm[0] && norm[1]>=norm[2]) {
848 /* STEP 1: COUNT POLYS */
849 eve= fillvertbase.first;
851 /* get first vertex with no poly number */
854 /* now a sortof select connected */
862 if(toggle & 1) eed= filledgebase.first;
863 else eed= filledgebase.last;
866 if(eed->v1->xs==0 && eed->v2->xs==poly) {
871 else if(eed->v2->xs==0 && eed->v1->xs==poly) {
876 else if(eed->f1==0) {
877 if(eed->v1->xs==poly && eed->v2->xs==poly) {
882 if(toggle & 1) eed= eed->next;
889 /* printf("amount of poly's: %d\n",poly); */
891 /* STEP 2: remove loose edges and strings of edges */
892 eed= filledgebase.first;
894 if(eed->v1->h++ >250) break;
895 if(eed->v2->h++ >250) break;
899 /* otherwise it's impossible to be sure you can clear vertices */
900 callLocalErrorCallBack("No vertices with 250 edges allowed!");
904 /* does it only for vertices with ->h==1 */
905 testvertexnearedge();
911 if(toggle & 1) eed= filledgebase.first;
912 else eed= filledgebase.last;
914 if(toggle & 1) nexted= eed->next;
915 else nexted= eed->prev;
918 BLI_remlink(&fillvertbase,eed->v1);
919 BLI_remlink(&filledgebase,eed);
922 else if(eed->v2->h==1) {
924 BLI_remlink(&fillvertbase,eed->v2);
925 BLI_remlink(&filledgebase,eed);
931 if(filledgebase.first==0) {
932 /* printf("All edges removed\n"); */
938 - eve->f :1= availalble in edges
939 - eve->xs :polynumber
940 - eve->h :amount of edges connected to vertex
941 - eve->tmp.v :store! original vertex number
944 - eed->f1 :poly number
948 /* STEP 3: MAKE POLYFILL STRUCT */
949 pflist= (PolyFill *)MEM_callocN(poly*sizeof(PolyFill),"edgefill");
951 for(a=1;a<=poly;a++) {
953 pf->min[0]=pf->min[1]=pf->min[2]= 1.0e20;
954 pf->max[0]=pf->max[1]=pf->max[2]= -1.0e20;
957 eed= filledgebase.first;
959 pflist[eed->f1-1].edges++;
963 eve= fillvertbase.first;
965 pflist[eve->xs-1].verts++;
966 minp= pflist[eve->xs-1].min;
967 maxp= pflist[eve->xs-1].max;
969 minp[cox]= (minp[cox])<(eve->co[cox]) ? (minp[cox]) : (eve->co[cox]);
970 minp[coy]= (minp[coy])<(eve->co[coy]) ? (minp[coy]) : (eve->co[coy]);
971 maxp[cox]= (maxp[cox])>(eve->co[cox]) ? (maxp[cox]) : (eve->co[cox]);
972 maxp[coy]= (maxp[coy])>(eve->co[coy]) ? (maxp[coy]) : (eve->co[coy]);
973 if(eve->h>2) pflist[eve->xs-1].f= 1;
978 /* STEP 4: FIND HOLES OR BOUNDS, JOIN THEM
979 * ( bounds just to divide it in pieces for optimization,
980 * the edgefill itself has good auto-hole detection)
981 * WATCH IT: ONLY WORKS WITH SORTED POLYS!!! */
984 short *polycache, *pc;
987 qsort(pflist, poly, sizeof(PolyFill), vergpoly);
990 for(a=1;a<=poly;a++) {
991 printf("poly:%d edges:%d verts:%d flag: %d\n",a,pf->edges,pf->verts,pf->f);
992 PRINT2(f, f, pf->min[0], pf->min[1]);
996 polycache= pc= MEM_callocN(sizeof(short)*poly, "polycache");
998 for(a=0; a<poly; a++, pf++) {
999 for(c=a+1;c<poly;c++) {
1001 /* if 'a' inside 'c': join (bbox too)
1002 * Careful: 'a' can also be inside another poly.
1004 if(boundisect(pf, pflist+c)) {
1008 /* only for optimize! */
1009 /* else if(pf->max[cox] < (pflist+c)->min[cox]) break; */
1012 while(pc!=polycache) {
1014 mergepolysSimp(pf, pflist+ *pc);
1017 MEM_freeN(polycache);
1021 /* printf("after merge\n");
1022 for(a=1;a<=poly;a++) {
1023 printf("poly:%d edges:%d verts:%d flag: %d\n",a,pf->edges,pf->verts,pf->f);
1027 /* STEP 5: MAKE TRIANGLES */
1029 tempve.first= fillvertbase.first;
1030 tempve.last= fillvertbase.last;
1031 temped.first= filledgebase.first;
1032 temped.last= filledgebase.last;
1033 fillvertbase.first=fillvertbase.last= 0;
1034 filledgebase.first=filledgebase.last= 0;
1037 for(a=0;a<poly;a++) {
1039 splitlist(&tempve,&temped,pf->nr);
1040 scanfill(pf, mat_nr);
1044 addlisttolist(&fillvertbase,&tempve);
1045 addlisttolist(&filledgebase,&temped);