alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
535 stars 56 forks source link

Cannot figure out a workaround for INFINITE LOOP while unifying polygons #132

Closed Brandon10x15 closed 10 months ago

Brandon10x15 commented 1 year ago

So basically I have a bunch of overlapping circles, which Im transforming to Polygons, checking for any intersecting polygons, and adding all of the polygon coordinates to an array, and then it should unify all of the polygons into one. Here's what I have so far

PolygonsToMerge Here is the area that is being tested to merge.

    let polygon = new Polygon(intersecting[0]); 

    let polygonEdges = [];
    polygon.edges.forEach(edge => { polygonEdges.push([edge.shape.ps.x, edge.shape.ps.y]); });
    console.log(polygonEdges);

    // const m = new Flatten.Matrix().scale(1e6, 1e6);
    // polygon = polygon.transform(m);

    let ind = -1;
    for await (const points of intersecting) {
        ind++;
        if (ind !== 0) {

            let newPolygon = new Polygon(points);

            polygonEdges = [];
            newPolygon.edges.forEach(edge => { polygonEdges.push([edge.shape.ps.x, edge.shape.ps.y]); });
            console.log(polygonEdges);

            //newPolygon = newPolygon.transform(m);
            if ([...polygon.faces][0].orientation() != [...newPolygon.faces][0].orientation()) {
                newPolygon = newPolygon.reverse();
            }

            polygon = unify(polygon, newPolygon); // INFINITE LOOP error here
        } 
    }

    polygonEdges = [];
    polygon.edges.forEach(edge => { polygonEdges.push([edge.shape.ps.x, edge.shape.ps.y]); });
    console.log(polygonEdges);

The code

[
  [ 4590.23, 10480.41 ], [ 4645.07, 10382.11 ],
  [ 4667.33, 10492.44 ], [ 4578.67, 10423.1 ],
  [ 4691.12, 10418.12 ], [ 4608.93, 10495.03 ],
  [ 4621.35, 10383.16 ], [ 4684.66, 10476.23 ],
  [ 4576.04, 10446.69 ], [ 4677.77, 10398.5 ],
  [ 4631.83, 10501.26 ], [ 4599.9, 10393.32 ],
  [ 4694.35, 10454.56 ], [ 4582.78, 10469.45 ],
  [ 4657.85, 10385.58 ], [ 4655.36, 10498.12 ],
  [ 4584.07, 10411.01 ], [ 4694.87, 10430.83 ],
  [ 4597.81, 10487.82 ], [ 4634.48, 10381.4 ],
  [ 4675.82, 10486.1 ],  [ 4576.34, 10433.45 ],
  [ 4686.16, 10408.75 ]
]
[
  [ 4569.61, 10488.97 ], [ 4624.45, 10390.67 ],
  [ 4646.71, 10501 ],    [ 4558.05, 10431.66 ],
  [ 4670.5, 10426.68 ],  [ 4588.31, 10503.59 ],
  [ 4600.73, 10391.72 ], [ 4664.04, 10484.79 ],
  [ 4555.42, 10455.25 ], [ 4657.15, 10407.06 ],
  [ 4611.21, 10509.82 ], [ 4579.28, 10401.88 ],
  [ 4673.73, 10463.12 ], [ 4562.16, 10478.01 ],
  [ 4637.23, 10394.14 ], [ 4634.74, 10506.68 ],
  [ 4563.45, 10419.57 ], [ 4674.25, 10439.39 ],
  [ 4577.19, 10496.38 ], [ 4613.86, 10389.96 ],
  [ 4655.2, 10494.66 ],  [ 4555.72, 10442.01 ],
  [ 4665.54, 10417.31 ]
]

the result coordinates printed to console

Error: Infinite loop
    at Function.get INFINITE_LOOP [as INFINITE_LOOP] (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:191:16)
    at Function.testInfiniteLoop (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:248:38)
    at restoreFaces (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:1381:20)
    at swapLinksAndRestore (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:921:5)
    at booleanOpBinary (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:954:9)
    at unify (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\node_modules\@flatten-js\core\dist\main.cjs.js:772:32)
    at Object.displayZones (C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\functions\zones.js:336:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async C:\Users\Server\Documents\_DayZ\_SpyZ\_BrandonChernarus\functions\izurvive\izurvive.js:214:17

The error (line 336 is the line that calls unify())

I have reviewed all of the old issues for this infinite loop but have not been able to find a workaround

DeveloperUX commented 1 year ago

We're running into a similar issue with the intersect function. Try transforming the polygons before and after you call unify. Scale the polygons up and then rotate them. call unify then unscale and unrotate them.

max-vogler commented 1 year ago

Encountering this as well – would've loved to use the library if not for infinite loops!

alexbol99 commented 1 year ago

Hi, @Brandon10x15 ,

I tried you data and this is what I got:

image

I assume this is not what you planned. Maybe you can give me your "bunch of overlapping circles", I will test on it. In general library supposed to support boolean operations on circles as well.

alexbol99 commented 1 year ago

@DeveloperUX , @max-vogler , Please give me your examples of data, I want to test them too