我正在使用三角模块生成受限的Delaunay三角剖分(可在http://www.lfd.uci.edu/~gohlke/pythonlibs/上获得)。在某些情况下,函数triangle.triangulate崩溃,windows说'Python.exe已停止响应‘。我尝试过使用try/try结构,如下面的示例所示,该示例不一致地崩溃。我假设三角形过程的一部分是随机的(见下面的“次要问题”),但我不完全确定。
这种不一致是相当令人担忧的。我在窗户上。
from shapely.geometry import Polygon, MultiPolygon, LineString
import numpy as np
import triangle
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import random
def meshXOR(polyRef, shape, otherVerts, otherSegs, otherHole):
verts = []
verts3 = []
segs = []
outerLength = len(polyRef[shape[0]])
for i in range(outerLength-1):#-1 because xorpoly duplicates first point into last spot
verts.append((polyRef[shape[0]][i][0],polyRef[shape[0]][i][1])) #append the point to the verts array which will be fed into the delaunay triangulator
if i == outerLength - 2:
segs.append([i,0])
else:
segs.append([i,i+1])
h = []
for cInd in shape[1]:
shift = len(verts)
innerLength = len(polyRef[cInd])
for i in range(innerLength-1):
verts.append((polyRef[cInd][i][0],polyRef[cInd][i][1]))
if i == innerLength - 2:
segs.append([i+shift,shift])
else:
segs.append([i+shift,i+1+shift])
h += list(Polygon(polyRef[cInd]).representative_point().coords)
print 'verts are: ', verts
#output: verts are: [(0.0, 5.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0), (0.0, 5.0), (7.0, 3.0), (7.0, 7.0)]
print 'segs are: ', segs
#output: segs are: [[0, 1], [1, 2], [2, 3], [3, 4], [4, 0], [5, 6], [6, 7], [7, 5]]
print 'holes are: ', h
#output: holes are: [(5.25, 6.0)]
print 'verts: ', verts == otherVerts
print 'segs: ', segs == otherSegs
print 'hole: ', h == otherHole
return verts, segs, h
pA = Polygon([[0.0,0.0],[10.0,0.0],[10.0,10.0],[0.0,10.0]])
pB = Polygon([[0.0,5.0],[7.0,3.0],[7.0,7.0]])
xorPoly = pA.symmetric_difference(pB)
if xorPoly.geom_type == 'Polygon': xorPoly = MultiPolygon([xorPoly])
otherVerts = [(0.0, 5.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0), (0.0, 5.0), (7.0, 3.0), (7.0, 7.0)]
otherSegs = [[0, 1], [1, 2], [2, 3], [3, 4], [4, 0], [5, 6], [6, 7], [7, 5]]
otherHole = [(5.25,6.0)]
xorPolys = []
shapes = []
for poly in xorPoly:
shapes.append([len(xorPolys), [], len(shapes)])
xorPolys.append(list(poly.exterior.coords))
for ip in poly.interiors:
shapes[-1][1].append(len(xorPolys))
xorPolys.append(list(ip.coords))
try:
verts, segs, holes = meshXOR(xorPolys, shapes[0], otherVerts, otherSegs, otherHole) # i even tried placing it here
except:
print 'failed'
if len(holes)>0:
A = dict(vertices = np.asarray(verts), segments = np.asarray(segs), holes = holes)
else:
A = dict(vertices = np.asarray(verts), segments = np.asarray(segs))
print 'about to tri'
B = triangle.triangulate(A, opts = 'pi') #this is the step that try/except doesn't work on
print 'completed tri'
try:
B_t = B["triangles"].tolist()
except:
print 'no trianlges'
if B_t != []:
cols = []
import random
for tri in B_t:
cols.append(random.random())
plt.figure()
plt.gca().set_aspect('equal')
xy = np.asarray(verts)
plt.tripcolor(xy[:,0], xy[:,1], B_t, facecolors=np.array(cols))
#for tri in B_t:
#print 'tri is: ', [verts[t] for t in tri]
plt.show()
else:
print 'no triangles'我的问题是:是否有一种方法来执行类似“尝试/除”结构之类的操作来捕获此错误?或者,我是不是对三角形模块做错了什么?
编辑:
解决方案:引用Gamrix的响应导致解决方案。如果用太小的差(欧氏距离)分隔点,则三角函数就会崩溃。移除以小于1e-12的数量分隔的点解决了问题。
发布于 2015-08-17 22:18:51
根据您描述的行为,外部代码似乎陷入了死锁或无限循环。您无法在用户端捕获该线程,除非生成一个额外的线程,如果进程花费太长时间,该线程将杀死当前线程。
然而,即使这样也不能真正解决你所面临的问题。我查看了三角库( python版本只是链接到的版本的包装器),我在网站上找到了以下内容。读一读,看看它是否解释了你的问题发生的原因:
三角不终止,也不只是崩溃: 当三角形变得如此小以至于它们之间的顶点之间的距离并不比机器的算术精度大的时候,就会发生糟糕的事情。如果您已经为单精度算法编译了三角形,那么通过将其重新编译为双精度可能会做得更好。再一次,你可能不得不满足在最小角度和最大面积上比你计划的更宽松的限制。 通过确保原点位于顶点集中,甚至在网格最密集的部分,您可以将精度问题降到最低。如果你在对一个x坐标都在6247133到6247134之间的物体进行三角剖分,你就不会为三角形留下太多的浮点精度。 如果输入PSLG包含两个在极角度上相遇(或相交)的段,或者由-c开关引入这样的角度,那么精度问题就会发生。如果你没有意识到一个微小的角度正在形成,你可能永远不会发现为什么三角会崩溃。要检查这种可能性,请使用-S开关(通过反复试验找到的Steiner点数有适当的限制)尽早停止三角形,并使用Show查看输出.poly文件。仔细观察密集的顶点簇形成的区域和片段之间的小角度。近距离放大,因为从远处看,这样的段看起来可能像一个单独的段。 如果某些输入值太大,则在尝试执行方向测试或内圆测试时,三角可能会因溢出而遭受浮动异常。(阅读关于精确算术的一节。)再次,我建议编译三角形的双重(而不是单一)精确算术。 如果您使用质量网格(-q、-a或-u)来处理没有分段限制的输入,即如果您的输入是顶点集,或者您使用的是-c开关,则可能会出现意外的问题。如果输入顶点的凸包在其边界上有共线顶点,你认为位于凸包上的输入顶点实际上可能就在凸包内部。如果是这样的话,一个极薄的三角形是由顶点和它旁边的凸包边形成的。当三角形试图细化网格以加强角度和面积约束时,可能会形成极小的三角形,或者由于浮点精度不足而导致三角形失效。
http://www.cs.cmu.edu/~quake/triangle.trouble.html
发布于 2015-08-17 22:10:58
您所做的似乎已经本地化了错误。如果它确实打印了“即将到tri”,然后在打印“完成tri”之前崩溃,那么它似乎在triangle.triangulate中崩溃了。该函数由于引发异常而崩溃。在这种情况下,我唯一能想到的就是用调试器逐步完成代码,并尝试查看发生了什么。不过,这可能与您的代码无关。看看这篇文章:Random "pythonw.exe has stopped working" crashing有没有办法在非Windows系统上运行这段代码来测试它实际上是否是Windows问题?
发布于 2020-11-14 23:18:25
我在这个库中也遇到了一些问题,我试图得到一个多边形的三角剖分,它的点是完全对齐的。我解决了这个问题,在输入顶点中添加了一些微小的随机偏差,类似于np.random.random(nvert,2)*0.00001。
https://stackoverflow.com/questions/32059556
复制相似问题