pythonclipperlib

pyclipper: Crash on trivial case ("terminate called throwing an exception")


I'm trying to use the Clipper Python bindings to clip a line using a polygon. But the process crashes inside the bindings or the clipper library:

import pyclipper

pc = pyclipper.Pyclipper()

# Add a single line as the subject.
pc.AddPath([(-1, -1), (2, 1)], pyclipper.PT_SUBJECT, False)

# Add a square as the clipping region.
pc.AddPath([(0, 0), (1, 0), (1, 1), (0, 1)], pyclipper.PT_CLIP, True)

# Clip the line using the rectangle.
solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)

print(solution)

When I run the above code, the process terminates during the call to pc.Execute() with the following message written to standard error:

libc++abi.dylib: terminate called throwing an exception

I'm using Python 3.4.3 on OS X 10.8.5 with the newest version of pyclipper (0.9.3b0) available on PyPI, which uses Clipper 6.2.1.

Am I doing something wrong or is this a bug in either Clipper or pyclipper?


Solution

  • I tried your example with Python 3.4.3 on Ubuntu 15.04 and I get the following error:

    terminate called after throwing an instance of 'ClipperLib::clipperException'
      what():  Error: PolyTree struct is need for open path clipping.
    

    As the error message says, PolyTree struct should be used when clipping paths that are open.

    Clipper library has 2 functions named Execute in the Clipper class. One accepts Paths as the solution parameter type, the other one accepts PolyTree as the solution parameter type. As the error message says, in your case you should use the second one. The second function is called in the Pyclipper::Execute2 function. So replace the line 12 with the following one so that correct types are used:

    solution = pc.Execute2(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
    

    Please report if this solves your problem.