Skip to content

Floating point precision can lead to incorrect triangulation #16

Closed

Description

In rare cases the triangulation fails and appears to originate from the precision of the data sent to the earcut function.

Below is data for a simple ring with a hole which triangulates correctly:

[[143.129527283745121, 61.240160826593642, 12.157953862848911],
  [147.399527283763751, 74.780160826630892, 12.157953862848911],
  [154.049527283757931, 90.260160827077932, 12.157953862848911],
  [174.429527283762581, 81.710160826332872, 12.157953862848911],
  [168.03952728374861, 67.040160826407372, 12.157953862848911],
  [159.099527283746281, 53.590160826221112, 12.157953862848911]],

[[156.85952728375561, 67.430160827003422, 12.157953862848911],
  [157.489527283760251, 67.160160826519132, 12.157953862848911],
  [159.969527283741631, 68.350160826928912, 12.157953862848911],
  [161.339527283766071, 67.640160826966172, 12.157953862848911],
  [159.649527283763751, 63.310160826891662, 12.157953862848911],
  [155.759527283749781, 64.880160826258362, 12.157953862848911]]
]

Modifying the y-coordinate of the first ring point slightly (61.240160826593642 -> 61.240160826593640) produces incorrect triangles (tested with the triangulation test of earcut):

[[143.129527283745121, 61.240160826593640, 12.157953862848911],
  [147.399527283763751, 74.780160826630892, 12.157953862848911],
  [154.049527283757931, 90.260160827077932, 12.157953862848911],
  [174.429527283762581, 81.710160826332872, 12.157953862848911],
  [168.03952728374861, 67.040160826407372, 12.157953862848911],
  [159.099527283746281, 53.590160826221112, 12.157953862848911]],

[[156.85952728375561, 67.430160827003422, 12.157953862848911],
  [157.489527283760251, 67.160160826519132, 12.157953862848911],
  [159.969527283741631, 68.350160826928912, 12.157953862848911],
  [161.339527283766071, 67.640160826966172, 12.157953862848911],
  [159.649527283763751, 63.310160826891662, 12.157953862848911],
  [155.759527283749781, 64.880160826258362, 12.157953862848911]]
]

In the above case, the triangulation appears to deviate in the isEar function, around line 250, where in one case we have a value of t at -1.xxxe-13 and in the other at +1.xxxe-13 leading to different test results on (t >= 0).

Tested with version 1.4.0 of earcut.

Activity

  1. mourner commented on Mar 17, 2015

    @mourner
    Member

    Interesting! I'll take a look. Perhaps we should be less rigid in ear tests and compare things according to some Epsilon error value.

  2. mourner commented on Mar 17, 2015

    @mourner
    Member

    Wow, the original sample doesn't triangulate correctly for me...

  3. mourner commented on Mar 17, 2015

    @mourner
    Member

    Nevermind, it was a regressed viz page not working correctly... So, epsilon in that particular test seems to help with this example but breaks tests for other shapes. So looks like it'll be pretty tricky to solve...

  4. mourner commented on Mar 17, 2015

    @mourner
    Member

    @sctf4 check out how it's done the tests... Basically I'm calculating sum of triangle areas and comparing it to the area of the original polygon. If it doesn't match within a tolerance — it's broken.

  5. mourner commented on Mar 17, 2015

    @mourner
    Member

    @sctf4 actually I just figured out that it fails because of the cureLocalIntersections routine; if we disable that and go straight to splitEarcut, it works!

  6. mourner commented on Mar 17, 2015

    @mourner
    Member

    BTW just wondering, where are you using the library?

  7. mourner commented on Mar 17, 2015

    @mourner
    Member

    OK seems to be fixed, without much of a perf hit. Added your test case so that it doesn't regress in future. Let me know if you find any other failing cases.

  8. mourner commented on Mar 17, 2015

    @mourner
    Member

    Awesome! F4 is such an amazing map. Bummer that it's only 20-30% though, I'd expect a bigger difference. :) Or is it overall processing speed, not just triangulation?

  9. mourner commented on Mar 17, 2015

    @mourner
    Member

    I'll look at it, thanks!

  10. mourner commented on Mar 18, 2015

    @mourner
    Member

    @sctf4 fixed! Committing a patch now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions