Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intersection() infinite loop for valid MultiPolygons. #62

Closed
erikh2000 opened this issue Feb 5, 2018 · 5 comments
Closed

intersection() infinite loop for valid MultiPolygons. #62

erikh2000 opened this issue Feb 5, 2018 · 5 comments

Comments

@erikh2000
Copy link

erikh2000 commented Feb 5, 2018

I tried to narrow this down as tight as I could. The initial polygons that generated the error had many more points, but I removed as many as possible to still produce the error. This call below will cause an infinite loop and crash the JS stack:

const multiPolygon1 = [
          [
            [
              [-89.7235275385363, 42.0439388757895],
              [-89.7237188969195, 42.0442244474719],
              [-89.7237975, 42.0456047],
              [-89.7235275385363, 42.0439388757895]
            ]
          ],
          [
            [
              [-89.7214720193067, 42.042492561329],
              [-89.7161784, 42.046186],
              [-89.7195447358245, 42.042911552283],
              [-89.7214720193067, 42.042492561329]
            ]
          ]
        ];
const multiPolygon2 = [
          [
            [
              [-89.7232165, 42.0413833],
              [-89.723708, 42.0440331],
              [-89.7237975, 42.0456047],
              [-89.7145748, 42.0462076],
              [-89.7144372, 42.0462097],
              [-89.7239301, 42.0397921],
              [-89.7232165, 42.0413833]
            ]
          ],
          [
            [
              [-89.7240178, 42.0405696],
              [-89.7239695, 42.0422763],
              [-89.7236804, 42.0423162],
              [-89.7240178, 42.0405696]
            ]
          ]
        ];
intersection(multiPolygon1, multiPolygon2);

image

I stepped through it in the debugger. connectEdges() gets caught in an endless loop on the i=6 iteration of the for(i = 0 loop. More specifically in the inner while(pos >= i) loop where nextPos() continuously returns 12 for the next pos value, and the same coordinate is added to resultEvents until the JS stack runs out of memory.

It's a little tough for me to understand how the code is meant to work with my current ignorance, or I would probably just fix the bug and send you the fix. I'll keep messing around with it. Insights, ideas, or of course, your own bug fix would be very welcome.

@erikh2000
Copy link
Author

Confirmed the above bug is still there after a fresh git pull and rebuild just now.

@rowanwins
Copy link
Collaborator

I'll try and take a look this evening if pulls #58 and #60 help resolve this, I suspect #58 might. It's a one-liner fix so should be pretty easy to add to your code locally if you want to look at it straight away.

@rowanwins
Copy link
Collaborator

Yep I can confirm this is resolved by pull requests #58 and #60 thanks to @mfogel

@erikh2000
Copy link
Author

I hand-merged #58 and #60 into my local code and YES!... the test case above passes with the changes. Very nice.

@w8r
Copy link
Owner

w8r commented May 27, 2018

It's resolved in v0.4 https://codepen.io/w8r/pen/bMXPEy

@w8r w8r closed this as completed May 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants