xaviergonz / js-angusj-clipper

Polygon and line clipping and offsetting library (Javascript) - a port of Angus Johnson's clipper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clipping open paths raises uncaught exception

AlexJWayne opened this issue · comments

I'm trying to get the open paths that are inside a closed polygon. And clipper seems to support this, according to the docs.

But if I use an input that is an open path, I get a cryptic exception.

  clipper.clipToPaths({
    clipType: clipper.lib.ClipType.Intersection,
    subjectFillType: clipper.lib.PolyFillType.Positive,
    subjectInputs: [
      {
        data: [
          { x: 10, y: 10 },
          { x: 90, y: 10 },
          { x: 90, y: 90 },
        ],
        closed: false,
      },
    ],
    clipInputs: [
      {
        data: [
          { x: 0, y: 0 },
          { x: 0, y: 50 },
          { x: 50, y: 50 },
          { x: 50, y: 0 },
        ],
      },
    ],
  })

Which throws:

Uncaught 5257664 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

If I changed the above so that subjectInputs[0].closed = true then it returns [] which isn't right either.

Hi! thanks for your bug report.

about closed: true not giving a proper result it is because the winding (clockwise/counter-clockwise direction) of the clip input is counter-clockwise, which means a "hole", so the intersection between a triangle and a "hole" (nothing) is nothing.

If you change the clip polygon to [{ x: 0, y: 0 }, { x: 50, y: 0 }, { x: 50, y: 50 }, { x: 0, y: 50 }] then it gives a triangle as expected.

See https://github.com/xaviergonz/js-angusj-clipper/blob/master/docs/apiReference/shared/PolyFillType.md for more info on the winding rules.

It seems like closed: false is crashing indeed, I'm taking a look into it
The expected output should be (once winding is fixed): [ [ { X: 10, Y: 10 }, { X: 50, Y: 10 } ] ]

OK, I found why closed: false didn't work

Also, only the PolyTree structure can differentiate
   * between open and closed paths since each PolyNode has an IsOpen property. (The Path structure has no member indicating whether it's open or closed.)
   * For this reason, when open paths are passed to a Clipper object, the user must use a PolyTree object as the solution parameter, otherwise an exception
   * will be raised.

Basically it only works with clipToPolyTree

The next version will throw an exception like clip to a PolyTree (not to a Path) when using open paths to make this more clear

Ahhh, ok. Thank you for the help! That will be a very welcome exception.

v1.0.0 published, it will show that exception as long as process.env.NODE_ENV is not set to "production"
cheers!