2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
26 * (uit traces) maart 95
29 /** \file blender/blenlib/intern/scanfill.c
38 #include "MEM_guardedalloc.h"
40 #include "BLI_callbacks.h"
41 #include "BLI_listbase.h"
43 #include "BLI_scanfill.h"
44 #include "BLI_utildefines.h"
46 /* callbacks for errors and interrupts and some goo */
47 static void (*BLI_localErrorCallBack)(const char *) = NULL;
48 static int (*BLI_localInterruptCallBack)(void) = NULL;
50 void BLI_setErrorCallBack(void (*f)(const char *))
52 BLI_localErrorCallBack = f;
55 void BLI_setInterruptCallBack(int (*f)(void))
57 BLI_localInterruptCallBack = f;
60 /* just flush the error to /dev/null if the error handler is missing */
61 void callLocalErrorCallBack(const char *msg)
63 if (BLI_localErrorCallBack) {
64 BLI_localErrorCallBack(msg);
69 /* ignore if the interrupt wasn't set */
70 static int callLocalInterruptCallBack(void)
72 if (BLI_localInterruptCallBack) {
73 return BLI_localInterruptCallBack();
82 typedef struct PolyFill {
84 float min_xy[2], max_xy[2];
88 typedef struct ScanFillVertLink {
90 ScanFillEdge *edge_first, *edge_last;
96 #define SF_EPSILON 0.00003f
98 #define SF_VERT_UNKNOWN 1 /* TODO, what is this for exactly? - need to document it! */
99 #define SF_VERT_ZERO_LEN 255
101 /* Optionally set ScanFillEdge f to this to mark original boundary edges.
102 * Only needed if there are internal diagonal edges passed to BLI_scanfill_calc. */
103 #define SF_EDGE_BOUNDARY 1
104 #define SF_EDGE_UNKNOWN 2 /* TODO, what is this for exactly? - need to document it! */
108 /* **** FUNCTIONS FOR QSORT *************************** */
111 static int vergscdata(const void *a1, const void *a2)
113 const ScanFillVertLink *x1 = a1, *x2 = a2;
115 if (x1->vert->xy[1] < x2->vert->xy[1]) return 1;
116 else if (x1->vert->xy[1] > x2->vert->xy[1]) return -1;
117 else if (x1->vert->xy[0] > x2->vert->xy[0]) return 1;
118 else if (x1->vert->xy[0] < x2->vert->xy[0]) return -1;
123 static int vergpoly(const void *a1, const void *a2)
125 const PolyFill *x1 = a1, *x2 = a2;
127 if (x1->min_xy[0] > x2->min_xy[0]) return 1;
128 else if (x1->min_xy[0] < x2->min_xy[0]) return -1;
129 else if (x1->min_xy[1] > x2->min_xy[1]) return 1;
130 else if (x1->min_xy[1] < x2->min_xy[1]) return -1;
135 /* ************* MEMORY MANAGEMENT ************* */
137 /* memory management */
138 struct mem_elements {
139 struct mem_elements *next, *prev;
143 static void *mem_element_new(ScanFillContext *sf_ctx, int size)
145 BLI_assert(!(size > 10000 || size == 0)); /* this is invalid use! */
147 size = (size + 3) & ~3; /* allocate in units of 4 */
149 if (sf_ctx->melem__cur && (size + sf_ctx->melem__offs < MEM_ELEM_BLOCKSIZE)) {
150 void *adr = (void *) (sf_ctx->melem__cur->data + sf_ctx->melem__offs);
151 sf_ctx->melem__offs += size;
155 sf_ctx->melem__cur = MEM_callocN(sizeof(struct mem_elements), "newmem");
156 sf_ctx->melem__cur->data = MEM_callocN(MEM_ELEM_BLOCKSIZE, "newmem");
157 BLI_addtail(&sf_ctx->melem__lb, sf_ctx->melem__cur);
159 sf_ctx->melem__offs = size;
160 return sf_ctx->melem__cur->data;
163 static void mem_element_reset(ScanFillContext *sf_ctx, int keep_first)
165 struct mem_elements *first;
167 if ((first = sf_ctx->melem__lb.first)) { /* can be false if first fill fails */
169 BLI_remlink(&sf_ctx->melem__lb, first);
172 sf_ctx->melem__cur = sf_ctx->melem__lb.first;
173 while (sf_ctx->melem__cur) {
174 MEM_freeN(sf_ctx->melem__cur->data);
175 sf_ctx->melem__cur = sf_ctx->melem__cur->next;
177 BLI_freelistN(&sf_ctx->melem__lb);
179 /*reset the block we're keeping*/
181 BLI_addtail(&sf_ctx->melem__lb, first);
182 memset(first->data, 0, MEM_ELEM_BLOCKSIZE);
190 sf_ctx->melem__cur = first;
191 sf_ctx->melem__offs = 0;
194 void BLI_scanfill_end(ScanFillContext *sf_ctx)
196 mem_element_reset(sf_ctx, FALSE);
198 sf_ctx->fillvertbase.first = sf_ctx->fillvertbase.last = NULL;
199 sf_ctx->filledgebase.first = sf_ctx->filledgebase.last = NULL;
200 sf_ctx->fillfacebase.first = sf_ctx->fillfacebase.last = NULL;
203 /* **** FILL ROUTINES *************************** */
205 ScanFillVert *BLI_scanfill_vert_add(ScanFillContext *sf_ctx, const float vec[3])
209 eve = mem_element_new(sf_ctx, sizeof(ScanFillVert));
210 BLI_addtail(&sf_ctx->fillvertbase, eve);
212 copy_v3_v3(eve->co, vec);
217 ScanFillEdge *BLI_scanfill_edge_add(ScanFillContext *sf_ctx, ScanFillVert *v1, ScanFillVert *v2)
221 newed = mem_element_new(sf_ctx, sizeof(ScanFillEdge));
222 BLI_addtail(&sf_ctx->filledgebase, newed);
230 static void addfillface(ScanFillContext *sf_ctx, ScanFillVert *v1, ScanFillVert *v2, ScanFillVert *v3)
232 /* does not make edges */
233 ScanFillFace *sf_tri;
235 sf_tri = mem_element_new(sf_ctx, sizeof(ScanFillFace));
236 BLI_addtail(&sf_ctx->fillfacebase, sf_tri);
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_xy[0] < pf1->min_xy[0]) return 0;
251 if (pf2->max_xy[1] < pf1->min_xy[1]) return 0;
253 if (pf2->min_xy[0] > pf1->max_xy[0]) return 0;
254 if (pf2->min_xy[1] > pf1->max_xy[1]) return 0;
257 if (pf2->max_xy[0] < pf1->max_xy[0]) pf2->max_xy[0] = pf1->max_xy[0];
258 if (pf2->max_xy[1] < pf1->max_xy[1]) pf2->max_xy[1] = pf1->max_xy[1];
260 if (pf2->min_xy[0] > pf1->min_xy[0]) pf2->min_xy[0] = pf1->min_xy[0];
261 if (pf2->min_xy[1] > pf1->min_xy[1]) pf2->min_xy[1] = pf1->min_xy[1];
267 static void mergepolysSimp(ScanFillContext *sf_ctx, PolyFill *pf1, PolyFill *pf2) /* add pf2 to pf1 */
272 /* replace old poly numbers */
273 eve = sf_ctx->fillvertbase.first;
275 if (eve->poly_nr == pf2->nr) eve->poly_nr = pf1->nr;
278 eed = sf_ctx->filledgebase.first;
280 if (eed->poly_nr == pf2->nr) eed->poly_nr = 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(const float v1[2], const float v2[2], const float v3[2])
291 /* is v3 to the right of v1-v2 ? With exception: v3==v1 || v3==v2 */
295 inp = (v2[0] - v1[0]) * (v1[1] - v3[1]) +
296 (v1[1] - v2[1]) * (v1[0] - v3[0]);
302 if (v1[0] == v3[0] && v1[1] == v3[1]) return 0;
303 if (v2[0] == v3[0] && v2[1] == v3[1]) return 0;
308 static short addedgetoscanvert(ScanFillVertLink *sc, ScanFillEdge *eed)
310 /* find first edge to the right of eed, and insert eed before that */
312 float fac, fac1, x, y;
314 if (sc->edge_first == NULL) {
315 sc->edge_first = sc->edge_last = eed;
316 eed->prev = eed->next = NULL;
323 fac1 = eed->v2->xy[1] - y;
325 fac1 = 1.0e10f * (eed->v2->xy[0] - x);
328 else fac1 = (x - eed->v2->xy[0]) / fac1;
330 for (ed = sc->edge_first; ed; ed = ed->next) {
332 if (ed->v2 == eed->v2) {
336 fac = ed->v2->xy[1] - y;
338 fac = 1.0e10f * (ed->v2->xy[0] - x);
341 fac = (x - ed->v2->xy[0]) / fac;
348 if (ed) BLI_insertlinkbefore((ListBase *)&(sc->edge_first), ed, eed);
349 else BLI_addtail((ListBase *)&(sc->edge_first), eed);
355 static ScanFillVertLink *addedgetoscanlist(ScanFillContext *sf_ctx, ScanFillEdge *eed, int len)
357 /* inserts edge at correct location in ScanFillVertLink list */
358 /* returns sc when edge already exists */
359 ScanFillVertLink *sc, scsearch;
362 /* which vert is left-top? */
363 if (eed->v1->xy[1] == eed->v2->xy[1]) {
364 if (eed->v1->xy[0] > eed->v2->xy[0]) {
370 else if (eed->v1->xy[1] < eed->v2->xy[1]) {
375 /* find location in list */
376 scsearch.vert = eed->v1;
377 sc = (ScanFillVertLink *)bsearch(&scsearch, sf_ctx->_scdata, len,
378 sizeof(ScanFillVertLink), vergscdata);
380 if (sc == 0) printf("Error in search edge: %p\n", (void *)eed);
381 else if (addedgetoscanvert(sc, eed) == 0) return sc;
386 static short boundinsideEV(ScanFillEdge *eed, ScanFillVert *eve)
387 /* is eve inside boundbox eed */
389 float minx, maxx, miny, maxy;
391 if (eed->v1->xy[0] < eed->v2->xy[0]) {
392 minx = eed->v1->xy[0];
393 maxx = eed->v2->xy[0];
396 minx = eed->v2->xy[0];
397 maxx = eed->v1->xy[0];
399 if (eve->xy[0] >= minx && eve->xy[0] <= maxx) {
400 if (eed->v1->xy[1] < eed->v2->xy[1]) {
401 miny = eed->v1->xy[1];
402 maxy = eed->v2->xy[1];
405 miny = eed->v2->xy[1];
406 maxy = eed->v1->xy[1];
408 if (eve->xy[1] >= miny && eve->xy[1] <= maxy) {
416 static void testvertexnearedge(ScanFillContext *sf_ctx)
418 /* only vertices with ->h==1 are being tested for
419 * being close to an edge, if true insert */
422 ScanFillEdge *eed, *ed1;
424 for (eve = sf_ctx->fillvertbase.first; eve; eve = eve->next) {
426 /* find the edge which has vertex eve */
427 ed1 = sf_ctx->filledgebase.first;
429 if (ed1->v1 == eve || ed1->v2 == eve) break;
432 if (ed1->v1 == eve) {
437 for (eed = sf_ctx->filledgebase.first; eed; eed = eed->next) {
438 if (eve != eed->v1 && eve != eed->v2 && eve->poly_nr == eed->poly_nr) {
439 if (compare_v3v3(eve->co, eed->v1->co, SF_EPSILON)) {
445 else if (compare_v3v3(eve->co, eed->v2->co, SF_EPSILON)) {
452 if (boundinsideEV(eed, eve)) {
453 const float dist = dist_to_line_v2(eed->v1->xy, eed->v2->xy, eve->xy);
454 if (dist < SF_EPSILON) {
456 ed1 = BLI_scanfill_edge_add(sf_ctx, eed->v1, eve);
458 /* printf("fill: vertex near edge %x\n",eve); */
460 ed1->poly_nr = eed->poly_nr;
473 static void splitlist(ScanFillContext *sf_ctx, ListBase *tempve, ListBase *temped, short nr)
475 /* everything is in templist, write only poly nr to fillist */
476 ScanFillVert *eve, *nextve;
477 ScanFillEdge *eed, *nexted;
479 BLI_movelisttolist(tempve, &sf_ctx->fillvertbase);
480 BLI_movelisttolist(temped, &sf_ctx->filledgebase);
485 if (eve->poly_nr == nr) {
486 BLI_remlink(tempve, eve);
487 BLI_addtail(&sf_ctx->fillvertbase, eve);
494 if (eed->poly_nr == nr) {
495 BLI_remlink(temped, eed);
496 BLI_addtail(&sf_ctx->filledgebase, eed);
503 static int scanfill(ScanFillContext *sf_ctx, PolyFill *pf)
505 ScanFillVertLink *sc = NULL, *sc1;
506 ScanFillVert *eve, *v1, *v2, *v3;
507 ScanFillEdge *eed, *nexted, *ed1, *ed2, *ed3;
508 int a, b, verts, maxface, totface;
509 short nr, test, twoconnected = 0;
516 eve = sf_ctx->fillvertbase.first;
518 printf("vert: %x co: %f %f\n", eve, eve->xy[0], eve->xy[1]);
521 eed = sf_ctx->filledgebase.first;
523 printf("edge: %x verts: %x %x\n", eed, eed->v1, eed->v2);
528 /* STEP 0: remove zero sized edges */
529 eed = sf_ctx->filledgebase.first;
531 if (equals_v2v2(eed->v1->xy, eed->v2->xy)) {
532 if (eed->v1->f == SF_VERT_ZERO_LEN && eed->v2->f != SF_VERT_ZERO_LEN) {
533 eed->v2->f = SF_VERT_ZERO_LEN;
534 eed->v2->tmp.v = eed->v1->tmp.v;
536 else if (eed->v2->f == SF_VERT_ZERO_LEN && eed->v1->f != SF_VERT_ZERO_LEN) {
537 eed->v1->f = SF_VERT_ZERO_LEN;
538 eed->v1->tmp.v = eed->v2->tmp.v;
540 else if (eed->v2->f == SF_VERT_ZERO_LEN && eed->v1->f == SF_VERT_ZERO_LEN) {
541 eed->v1->tmp.v = eed->v2->tmp.v;
544 eed->v2->f = SF_VERT_ZERO_LEN;
545 eed->v2->tmp.v = eed->v1;
551 /* STEP 1: make using FillVert and FillEdge lists a sorted
552 * ScanFillVertLink list
554 sc = sf_ctx->_scdata = (ScanFillVertLink *)MEM_callocN(pf->verts * sizeof(ScanFillVertLink), "Scanfill1");
555 eve = sf_ctx->fillvertbase.first;
558 if (eve->poly_nr == nr) {
559 if (eve->f != SF_VERT_ZERO_LEN) {
561 eve->f = 0; /* flag for connectedges later on */
569 qsort(sf_ctx->_scdata, verts, sizeof(ScanFillVertLink), vergscdata);
571 eed = sf_ctx->filledgebase.first;
574 BLI_remlink(&sf_ctx->filledgebase, eed);
575 /* This code is for handling zero-length edges that get
576 * collapsed in step 0. It was removed for some time to
577 * fix trunk bug #4544, so if that comes back, this code
578 * may need some work, or there will have to be a better
580 if (eed->v1->f == SF_VERT_ZERO_LEN) {
582 while ((eed->v1->f == SF_VERT_ZERO_LEN) && (eed->v1->tmp.v != v1) && (eed->v1 != eed->v1->tmp.v))
583 eed->v1 = eed->v1->tmp.v;
585 if (eed->v2->f == SF_VERT_ZERO_LEN) {
587 while ((eed->v2->f == SF_VERT_ZERO_LEN) && (eed->v2->tmp.v != v2) && (eed->v2 != eed->v2->tmp.v))
588 eed->v2 = eed->v2->tmp.v;
590 if (eed->v1 != eed->v2) addedgetoscanlist(sf_ctx, eed, verts);
596 for (a = 0; a < verts; a++) {
597 printf("\nscvert: %x\n", sc->v1);
600 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 */
616 sc = sf_ctx->_scdata;
617 for (a = 0; a < verts; a++) {
618 /* printf("VERTEX %d %x\n",a,sc->v1); */
619 ed1 = sc->edge_first;
620 while (ed1) { /* set connectflags */
622 if (ed1->v1->h == 1 || ed1->v2->h == 1) {
623 BLI_remlink((ListBase *)&(sc->edge_first), ed1);
624 BLI_addtail(&sf_ctx->filledgebase, ed1);
625 if (ed1->v1->h > 1) ed1->v1->h--;
626 if (ed1->v2->h > 1) ed1->v2->h--;
628 else ed1->v2->f = SF_VERT_UNKNOWN;
632 while (sc->edge_first) { /* for as long there are edges */
633 ed1 = sc->edge_first;
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->edge_first = sc->edge_last = NULL;
645 /* printf("just 1 edge to vert\n"); */
646 BLI_addtail(&sf_ctx->filledgebase, ed1);
652 /* test rest of vertices */
657 /* this happens with a serial of overlapping edges */
658 if (v1 == v2 || v2 == v3) break;
659 /* printf("test verts %x %x %x\n",v1,v2,v3); */
660 miny = minf(v1->xy[1], v3->xy[1]);
661 /* miny= MIN2(v1->xy[1],v3->xy[1]); */
665 for (b = a + 1; b < verts; b++) {
666 if (sc1->vert->f == 0) {
667 if (sc1->vert->xy[1] <= miny) break;
669 if (testedgeside(v1->xy, v2->xy, sc1->vert->xy))
670 if (testedgeside(v2->xy, v3->xy, sc1->vert->xy))
671 if (testedgeside(v3->xy, v1->xy, sc1->vert->xy)) {
672 /* point in triangle */
681 /* make new edge, and start over */
682 /* printf("add new edge %x %x and start again\n",v2,sc1->vert); */
684 ed3 = BLI_scanfill_edge_add(sf_ctx, v2, sc1->vert);
685 BLI_remlink(&sf_ctx->filledgebase, ed3);
686 BLI_insertlinkbefore((ListBase *)&(sc->edge_first), ed2, ed3);
687 ed3->v2->f = SF_VERT_UNKNOWN;
688 ed3->f = SF_EDGE_UNKNOWN;
694 /* printf("add face %x %x %x\n",v1,v2,v3); */
695 addfillface(sf_ctx, v1, v2, v3);
697 BLI_remlink((ListBase *)&(sc->edge_first), ed1);
698 BLI_addtail(&sf_ctx->filledgebase, ed1);
702 /* ed2 can be removed when it's a boundary edge */
703 if ((ed2->f == 0 && twoconnected) || (ed2->f == SF_EDGE_BOUNDARY)) {
704 BLI_remlink((ListBase *)&(sc->edge_first), ed2);
705 BLI_addtail(&sf_ctx->filledgebase, ed2);
712 ed3 = BLI_scanfill_edge_add(sf_ctx, v1, v3);
713 BLI_remlink(&sf_ctx->filledgebase, ed3);
714 ed3->f = SF_EDGE_UNKNOWN;
718 /* printf("add new edge %x %x\n",v1,v3); */
719 sc1 = addedgetoscanlist(sf_ctx, ed3, verts);
721 if (sc1) { /* ed3 already exists: remove if a boundary */
722 /* printf("Edge exists\n"); */
726 ed3 = sc1->edge_first;
728 if ( (ed3->v1 == v1 && ed3->v2 == v3) || (ed3->v1 == v3 && ed3->v2 == v1) ) {
729 if (twoconnected || ed3->f == SF_EDGE_BOUNDARY) {
730 BLI_remlink((ListBase *)&(sc1->edge_first), ed3);
731 BLI_addtail(&sf_ctx->filledgebase, ed3);
743 /* test for loose edges */
744 ed1 = sc->edge_first;
747 if (ed1->v1->h < 2 || ed1->v2->h < 2) {
748 BLI_remlink((ListBase *)&(sc->edge_first), ed1);
749 BLI_addtail(&sf_ctx->filledgebase, ed1);
750 if (ed1->v1->h > 1) ed1->v1->h--;
751 if (ed1->v2->h > 1) ed1->v2->h--;
760 MEM_freeN(sf_ctx->_scdata);
761 sf_ctx->_scdata = NULL;
767 int BLI_scanfill_begin(ScanFillContext *sf_ctx)
769 memset(sf_ctx, 0, sizeof(*sf_ctx));
774 int BLI_scanfill_calc(ScanFillContext *sf_ctx, const short do_quad_tri_speedup)
776 return BLI_scanfill_calc_ex(sf_ctx, do_quad_tri_speedup, NULL);
779 int BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, const short do_quad_tri_speedup, const float nor_proj[3])
782 * - fill works with its own lists, so create that first (no faces!)
783 * - for vertices, put in ->tmp.v the old pointer
784 * - struct elements xs en ys are not used here: don't hide stuff in it
785 * - edge flag ->f becomes 2 when it's a new edge
786 * - mode: & 1 is check for crossings, then create edges (TO DO )
787 * - returns number of triangle faces added.
789 ListBase tempve, temped;
791 ScanFillEdge *eed, *nexted;
792 PolyFill *pflist, *pf;
793 float *min_xy_p, *max_xy_p;
794 short a, c, poly = 0, ok = 0, toggle = 0;
795 int totfaces = 0; /* total faces added */
798 /* reset variables */
799 eve = sf_ctx->fillvertbase.first;
809 if (do_quad_tri_speedup && (a == 3)) {
810 eve = sf_ctx->fillvertbase.first;
812 addfillface(sf_ctx, eve, eve->next, eve->next->next);
815 else if (do_quad_tri_speedup && (a == 4)) {
816 float vec1[3], vec2[3];
818 eve = sf_ctx->fillvertbase.first;
819 /* no need to check 'eve->next->next->next' is valid, already counted */
820 /* use shortest diagonal for quad */
821 sub_v3_v3v3(vec1, eve->co, eve->next->next->co);
822 sub_v3_v3v3(vec2, eve->next->co, eve->next->next->next->co);
824 if (dot_v3v3(vec1, vec1) < dot_v3v3(vec2, vec2)) {
825 addfillface(sf_ctx, eve, eve->next, eve->next->next);
826 addfillface(sf_ctx, eve->next->next, eve->next->next->next, eve);
829 addfillface(sf_ctx, eve->next, eve->next->next, eve->next->next->next);
830 addfillface(sf_ctx, eve->next->next->next, eve, eve->next);
835 /* first test vertices if they are in edges */
836 /* including resetting of flags */
837 eed = sf_ctx->filledgebase.first;
840 eed->v1->f = SF_VERT_UNKNOWN;
841 eed->v2->f = SF_VERT_UNKNOWN;
846 eve = sf_ctx->fillvertbase.first;
848 if (eve->f & SF_VERT_UNKNOWN) {
862 copy_v3_v3(n, nor_proj);
865 /* define projection: with 'best' normal */
866 /* Newell's Method */
867 /* Similar code used elsewhere, but this checks for double ups
868 * which historically this function supports so better not change */
872 eve = sf_ctx->fillvertbase.last;
875 for (eve = sf_ctx->fillvertbase.first; eve; eve = eve->next) {
876 if (LIKELY(!compare_v3v3(v_prev, eve->co, SF_EPSILON))) {
877 add_newell_cross_v3_v3v3(n, v_prev, eve->co);
883 if (UNLIKELY(normalize_v3(n) == 0.0f)) {
887 axis_dominant_v3(&co_x, &co_y, n);
891 /* STEP 1: COUNT POLYS */
892 eve = sf_ctx->fillvertbase.first;
894 eve->xy[0] = eve->co[co_x];
895 eve->xy[1] = eve->co[co_y];
897 /* get first vertex with no poly number */
898 if (eve->poly_nr == 0) {
900 /* now a sort of select connected */
908 if (toggle & 1) eed = sf_ctx->filledgebase.first;
909 else eed = sf_ctx->filledgebase.last;
912 if (eed->v1->poly_nr == 0 && eed->v2->poly_nr == poly) {
913 eed->v1->poly_nr = poly;
917 else if (eed->v2->poly_nr == 0 && eed->v1->poly_nr == poly) {
918 eed->v2->poly_nr = poly;
922 else if (eed->poly_nr == 0) {
923 if (eed->v1->poly_nr == poly && eed->v2->poly_nr == poly) {
928 if (toggle & 1) eed = eed->next;
929 else eed = eed->prev;
935 /* printf("amount of poly's: %d\n",poly); */
937 /* STEP 2: remove loose edges and strings of edges */
938 eed = sf_ctx->filledgebase.first;
940 if (eed->v1->h++ > 250) break;
941 if (eed->v2->h++ > 250) break;
945 /* otherwise it's impossible to be sure you can clear vertices */
946 callLocalErrorCallBack("No vertices with 250 edges allowed!");
950 /* does it only for vertices with ->h==1 */
951 testvertexnearedge(sf_ctx);
957 if (toggle & 1) eed = sf_ctx->filledgebase.first;
958 else eed = sf_ctx->filledgebase.last;
960 if (toggle & 1) nexted = eed->next;
961 else nexted = eed->prev;
962 if (eed->v1->h == 1) {
964 BLI_remlink(&sf_ctx->fillvertbase, eed->v1);
965 BLI_remlink(&sf_ctx->filledgebase, eed);
968 else if (eed->v2->h == 1) {
970 BLI_remlink(&sf_ctx->fillvertbase, eed->v2);
971 BLI_remlink(&sf_ctx->filledgebase, eed);
977 if (sf_ctx->filledgebase.first == 0) {
978 /* printf("All edges removed\n"); */
984 * - eve->f :1= availalble in edges
985 * - eve->xs :polynumber
986 * - eve->h :amount of edges connected to vertex
987 * - eve->tmp.v :store! original vertex number
989 * - eed->f :1= boundary edge (optionally set by caller)
990 * - eed->poly_nr :poly number
994 /* STEP 3: MAKE POLYFILL STRUCT */
995 pflist = (PolyFill *)MEM_callocN(poly * sizeof(PolyFill), "edgefill");
997 for (a = 1; a <= poly; a++) {
999 pf->min_xy[0] = pf->min_xy[1] = 1.0e20;
1000 pf->max_xy[0] = pf->max_xy[1] = -1.0e20;
1003 eed = sf_ctx->filledgebase.first;
1005 pflist[eed->poly_nr - 1].edges++;
1009 eve = sf_ctx->fillvertbase.first;
1011 pflist[eve->poly_nr - 1].verts++;
1012 min_xy_p = pflist[eve->poly_nr - 1].min_xy;
1013 max_xy_p = pflist[eve->poly_nr - 1].max_xy;
1015 min_xy_p[0] = (min_xy_p[0]) < (eve->xy[0]) ? (min_xy_p[0]) : (eve->xy[0]);
1016 min_xy_p[1] = (min_xy_p[1]) < (eve->xy[1]) ? (min_xy_p[1]) : (eve->xy[1]);
1017 max_xy_p[0] = (max_xy_p[0]) > (eve->xy[0]) ? (max_xy_p[0]) : (eve->xy[0]);
1018 max_xy_p[1] = (max_xy_p[1]) > (eve->xy[1]) ? (max_xy_p[1]) : (eve->xy[1]);
1019 if (eve->h > 2) pflist[eve->poly_nr - 1].f = 1;
1024 /* STEP 4: FIND HOLES OR BOUNDS, JOIN THEM
1025 * ( bounds just to divide it in pieces for optimization,
1026 * the edgefill itself has good auto-hole detection)
1027 * WATCH IT: ONLY WORKS WITH SORTED POLYS!!! */
1030 short *polycache, *pc;
1032 /* so, sort first */
1033 qsort(pflist, poly, sizeof(PolyFill), vergpoly);
1037 for (a = 1; a <= poly; a++) {
1038 printf("poly:%d edges:%d verts:%d flag: %d\n", a, pf->edges, pf->verts, pf->f);
1039 PRINT2(f, f, pf->min[0], pf->min[1]);
1044 polycache = pc = MEM_callocN(sizeof(short) * poly, "polycache");
1046 for (a = 0; a < poly; a++, pf++) {
1047 for (c = a + 1; c < poly; c++) {
1049 /* if 'a' inside 'c': join (bbox too)
1050 * Careful: 'a' can also be inside another poly.
1052 if (boundisect(pf, pflist + c)) {
1056 /* only for optimize! */
1057 /* else if (pf->max_xy[0] < (pflist+c)->min[cox]) break; */
1060 while (pc != polycache) {
1062 mergepolysSimp(sf_ctx, pf, pflist + *pc);
1065 MEM_freeN(polycache);
1069 printf("after merge\n");
1071 for (a = 1; a <= poly; a++) {
1072 printf("poly:%d edges:%d verts:%d flag: %d\n", a, pf->edges, pf->verts, pf->f);
1077 /* STEP 5: MAKE TRIANGLES */
1079 tempve.first = sf_ctx->fillvertbase.first;
1080 tempve.last = sf_ctx->fillvertbase.last;
1081 temped.first = sf_ctx->filledgebase.first;
1082 temped.last = sf_ctx->filledgebase.last;
1083 sf_ctx->fillvertbase.first = sf_ctx->fillvertbase.last = NULL;
1084 sf_ctx->filledgebase.first = sf_ctx->filledgebase.last = NULL;
1087 for (a = 0; a < poly; a++) {
1088 if (pf->edges > 1) {
1089 splitlist(sf_ctx, &tempve, &temped, pf->nr);
1090 totfaces += scanfill(sf_ctx, pf);
1094 BLI_movelisttolist(&sf_ctx->fillvertbase, &tempve);
1095 BLI_movelisttolist(&sf_ctx->filledgebase, &temped);