1

我正在为 alpha shape 使用 CGAL Python 绑定

我有 8 个CGAL.Triangulations_2.Vertex对象数组,如下所示:

In [5]: len(alphaShapeVertices)
Out[5]: 8

In [6]: for i, asv in enumerate(alphaShapeVertices):
   ...:     print i
   ...:     print asv
0
[<CGAL.Triangulations_2.Vertex object at 0x03C423E8>, <CGAL.Triangulations_2.Vertex object at 0x03C42420>, <CGAL.Triangulations_2.Vertex object at 0x03C42458>, <CGAL.Triangulations_2.Vertex object at 0x03C42490>, <CGAL.Triangulations_2.Vertex object at 0x03C424C8>, <CGAL.Triangulations_2.Vertex object at 0x03C42500>, <CGAL.Triangulations_2.Vertex object at 0x03C42538>, <CGAL.Triangulations_2.Vertex object at 0x03C42570>, <CGAL.Triangulations_2.Vertex object at 0x03C425A8>, <CGAL.Triangulations_2.Vertex object at 0x03C425E0>, <CGAL.Triangulations_2.Vertex object at 0x03C42618>, <CGAL.Triangulations_2.Vertex object at 0x03C42650>, <CGAL.Triangulations_2.Vertex object at 0x03C42688>, <CGAL.Triangulations_2.Vertex object at 0x03C426C0>, <CGAL.Triangulations_2.Vertex object at 0x03C426F8>, <CGAL.Triangulations_2.Vertex object at 0x03C42730>, <CGAL.Triangulations_2.Vertex object at 0x03C42768>, <CGAL.Triangulations_2.Vertex object at 0x03C427A0>, <CGAL.Triangulations_2.Vertex object at 0x03C427D8>, <CGAL.Triangulations_2.Vertex object at 0x03C42810>, <CGAL.Triangulations_2.Vertex object at 0x03C42848>]
1
[<CGAL.Triangulations_2.Vertex object at 0x03C42880>, <CGAL.Triangulations_2.Vertex object at 0x03C428B8>, <CGAL.Triangulations_2.Vertex object at 0x03C428F0>, <CGAL.Triangulations_2.Vertex object at 0x03C42928>, <CGAL.Triangulations_2.Vertex object at 0x03C42960>, <CGAL.Triangulations_2.Vertex object at 0x03C42998>, <CGAL.Triangulations_2.Vertex object at 0x03C429D0>, <CGAL.Triangulations_2.Vertex object at 0x03C42A08>, <CGAL.Triangulations_2.Vertex object at 0x03C42A40>, <CGAL.Triangulations_2.Vertex object at 0x03C42A78>, <CGAL.Triangulations_2.Vertex object at 0x03C42AB0>, <CGAL.Triangulations_2.Vertex object at 0x03C42AE8>]
2
[<CGAL.Triangulations_2.Vertex object at 0x03C42B20>, <CGAL.Triangulations_2.Vertex object at 0x03C42B58>, <CGAL.Triangulations_2.Vertex object at 0x03C42B90>, <CGAL.Triangulations_2.Vertex object at 0x03C42BC8>, <CGAL.Triangulations_2.Vertex object at 0x03C42C00>, <CGAL.Triangulations_2.Vertex object at 0x03C42C38>, <CGAL.Triangulations_2.Vertex object at 0x03C42C70>, <CGAL.Triangulations_2.Vertex object at 0x03C42CA8>, <CGAL.Triangulations_2.Vertex object at 0x03C42CE0>, <CGAL.Triangulations_2.Vertex object at 0x03C42D18>, <CGAL.Triangulations_2.Vertex object at 0x03C42D50>]
3
[<CGAL.Triangulations_2.Vertex object at 0x03C42D88>, <CGAL.Triangulations_2.Vertex object at 0x03C42DC0>, <CGAL.Triangulations_2.Vertex object at 0x03C42DF8>, <CGAL.Triangulations_2.Vertex object at 0x03C42E30>, <CGAL.Triangulations_2.Vertex object at 0x03C42E68>, <CGAL.Triangulations_2.Vertex object at 0x03C42EA0>, <CGAL.Triangulations_2.Vertex object at 0x03C42ED8>, <CGAL.Triangulations_2.Vertex object at 0x03C42F10>, <CGAL.Triangulations_2.Vertex object at 0x03C42F48>, <CGAL.Triangulations_2.Vertex object at 0x03C42F80>, <CGAL.Triangulations_2.Vertex object at 0x03C42FB8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D030>, <CGAL.Triangulations_2.Vertex object at 0x03C4D068>, <CGAL.Triangulations_2.Vertex object at 0x03C4D0A0>]
4
[<CGAL.Triangulations_2.Vertex object at 0x03C4D0D8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D110>, <CGAL.Triangulations_2.Vertex object at 0x03C4D148>, <CGAL.Triangulations_2.Vertex object at 0x03C4D180>, <CGAL.Triangulations_2.Vertex object at 0x03C4D1B8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D1F0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D228>, <CGAL.Triangulations_2.Vertex object at 0x03C4D260>, <CGAL.Triangulations_2.Vertex object at 0x03C4D298>, <CGAL.Triangulations_2.Vertex object at 0x03C4D2D0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D308>, <CGAL.Triangulations_2.Vertex object at 0x03C4D340>, <CGAL.Triangulations_2.Vertex object at 0x03C4D378>, <CGAL.Triangulations_2.Vertex object at 0x03C4D3B0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D3E8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D420>, <CGAL.Triangulations_2.Vertex object at 0x03C4D458>]
5
[<CGAL.Triangulations_2.Vertex object at 0x03C4D490>, <CGAL.Triangulations_2.Vertex object at 0x03C4D4C8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D500>, <CGAL.Triangulations_2.Vertex object at 0x03C4D538>, <CGAL.Triangulations_2.Vertex object at 0x03C4D570>, <CGAL.Triangulations_2.Vertex object at 0x03C4D5A8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D5E0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D618>, <CGAL.Triangulations_2.Vertex object at 0x03C4D650>]
6
[<CGAL.Triangulations_2.Vertex object at 0x03C4D688>, <CGAL.Triangulations_2.Vertex object at 0x03C4D6C0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D6F8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D730>, <CGAL.Triangulations_2.Vertex object at 0x03C4D768>, <CGAL.Triangulations_2.Vertex object at 0x03C4D7A0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D7D8>]
7
[<CGAL.Triangulations_2.Vertex object at 0x03C4D810>, <CGAL.Triangulations_2.Vertex object at 0x03C4D848>, <CGAL.Triangulations_2.Vertex object at 0x03C4D880>, <CGAL.Triangulations_2.Vertex object at 0x03C4D8B8>, <CGAL.Triangulations_2.Vertex object at 0x03C4D8F0>, <CGAL.Triangulations_2.Vertex object at 0x03C4D928>]

我正在尝试访问point()每个对象的方法。

但是,我只能做到第二个数组。从 3 日起,我遇到了一个异常。

In [7]: alphaShapeVertices[0][0].point()
Out[7]: Point_2(1.00000001401,2.90491385964e-290)

In [8]: alphaShapeVertices[1][0].point()
Out[8]: Point_2(3.96551377978e-164,3.05578113171e-151)

In [9]: alphaShapeVertices[2][0].point()

# HERE: python.exe has stopped working

发生异常时的截图:

在此处输入图像描述

如您所见,异常可能并不总是发生在第三个数组。在屏幕截图中,它发生在 7 日。我认为它与内存分配有关。

调试器告诉我

python.exe 中 0x00000002 处的第一次机会异常:0xC0000005:访问冲突读取位置 0x00000002。

如果有这个异常的处理程序,程序可以安全地继续。

我已经挣扎了好几天。任何人都可以帮助我为什么以及如何解决它?



MWE

如果没有,您可能需要安装CGAL-Python(计算几何算法库的绑定)CTRL+FCGAL-Python)。

clusters 数据可以在 Pastie 上找到。

准备好数据和库后,运行以下 Python 脚本:

from sys import *
path.append("../../cgal_package")

from CGAL.Alpha_shapes_2 import *
from CGAL.Triangulations_2 import Delaunay_triangulation_2
from CGAL.Kernel import *

from random import *

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

import constants

def Point_2_str(self):
    return 'Point_2'+str((self.x(), self.y()))
# now we turn it into a member function
Point_2.__str__ = Point_2_str


def getAlphaShape(cluster):
    list_of_points = [Point_2(e[0], e[1]) for e in cluster]
    L = []
    verbose = True

    a = Alpha_shape_2()
    a.make_alpha_shape(list_of_points)
    a.set_mode(Alpha_shape_2.Mode.REGULARIZED)
    a.set_alpha(1000)
    alpha_shape_edges = []
    alpha_shape_vertices = []
    for it in a.alpha_shape_edges:
        alpha_shape_edges.append(a.segment(it))
    for it in a.alpha_shape_vertices:
        alpha_shape_vertices.append(it)
    return alpha_shape_vertices

# get alpha-shape of each cluster
alphaShapeVertices = []
for cluster in clusters:
    alphaShapeVertices.append(alphashape.getAlphaShape(cluster))
# start testing
print alphaShapeVertices[0][0].point()
print alphaShapeVertices[1][0].point()
print alphaShapeVertices[2][0].point()
print alphaShapeVertices[3][0].point()
# somewhere around here the exception occurs
print alphaShapeVertices[4][0].point()
print alphaShapeVertices[5][0].point()
print alphaShapeVertices[6][0].point()
4

0 回答 0