【问题标题】:pyclipper: Crash on trivial case ("terminate called throwing an exception")pyclipper:在琐碎的情况下崩溃(“终止称为抛出异常”)
【发布时间】:2015-11-17 21:23:02
【问题描述】:

我正在尝试使用Clipper Python bindings 使用多边形来剪裁一条线。但是进程在绑定或裁剪器库中崩溃:

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)

当我运行上述代码时,进程在调用pc.Execute() 期间终止,并将以下消息写入标准错误:

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

我在 OS X 10.8.5 上使用 Python 3.4.3 以及 PyPI 上可用的最新版本 pyclipper (0.9.3b0),它使用 Clipper 6.2.1。

我做错了什么还是这是 Clipper 或 pyclipper 中的错误?

【问题讨论】:

    标签: python clipperlib


    【解决方案1】:

    我在 Ubuntu 15.04 上使用 Python 3.4.3 尝试了您的示例,但出现以下错误:

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

    正如错误消息所说,在剪切打开的路径时应使用PolyTree struct。

    Clipper 库在 Clipper 类中有两个名为 Execute 的函数。一种接受Paths 作为解决方案参数类型,另一种接受PolyTree 作为解决方案参数类型。正如错误消息所说,在您的情况下,您应该使用第二个。第二个函数在Pyclipper::Execute2 函数中调用。因此,将第 12 行替换为以下行,以便使用正确的类型:

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

    如果这能解决您的问题,请报告。

    【讨论】:

    • 看来确实是这个原因。我已经用您的代码替换了对Execute 的调用,并将对print() 的调用替换为print([i.Contour for i in solution.Childs])。现在我得到了[[[0.0, 0.0], [1.0, 0.0]]] 的正确解决方案。谢谢!
    • 太棒了!请注意,您从Execute2 得到的结果是一棵树,因此这种打印方式仅在树的深度为 1 时有效。您可以使用pyclipper.PolyTreeToPaths 函数将其转换为路径列表。然后你就可以像以前一样打印了。
    • 哦,谢谢你的指点。 print(pyclipper.PolyTreeToPaths(solution)) 是。
    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多