问题描述
给出两个都具有相同缠绕顺序(逆时针顺序或顺时针顺序)的三维多边形:
poly1 = np.array([[120787.075999871,491779.675000143,-2.0699999332428],[120784.319999829,491781.831000042,5.96999979019165],[120787.075999871,-2.0699999332428]])
poly2 = np.array([[120787.075999871,-2.03999996185303],5.90999984741211],-2.03999996185303]])
如何在保持顺序的同时将这些相邻的多边形合并/合并为一个多边形(在Python中)。
以下是绘制两个相邻多边形的代码:
import matplotlib.pyplot as plt,numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot(poly1[:,0],poly1[:,1],2])
ax.plot(poly2[:,poly2[:,2])
ax.view_init(45,45)
plt.show()
解决方法
poly1
和poly2
中的点的坐标不是精确匹配的(尽管它们看起来像图中的同一点)。
我猜想您希望该算法使用“足够接近”的值(即,如果点之间的距离小于给定的公差,例如0.1
,它们将被视为同一点)。
您可以通过找到一个公共边并将其删除来连接两个多边形。
要找到公共边,我们首先确定多边形的哪些点对两个多边形都是公共的。
我看了一下post并选择了cKDTree方法。
它的工作原理是
- 为一个多边形的每个点查找另一个多边形中最接近的点
- 比较这两个点之间的距离。如果距离小于我们设定的公差,则我们认为它们是相同的点,并且对于两个多边形都是相同的。
一旦确定了哪些点是常见的,就可以验证它们是否相邻。如果是这样,则您已找到要去除的边缘。
最终的多边形将由
组成- 来自
poly1
的所有点 -
poly2
中未形成公共边的那些点
代码如下:
import matplotlib.pyplot as plt,numpy as np
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial import cKDTree
poly1 = np.array([[120787.075999871,491779.675000143,-2.0699999332428],[120784.319999829,491781.831000042,5.96999979019165],[120787.075999871,-2.0699999332428]])
poly2 = np.array([[120787.075999871,-2.03999996185303],5.90999984741211],-2.03999996185303]])
def is_close(a,b,tolerance):
# Get closest distances for each pt in a
dist = cKDTree(b).query(a,k=1)[0] # k=1 selects closest one neighbor
# Check the distances against the given tolerance value
return dist <= tolerance
def find_consecutive_true_values(arr):
i = 0
while i < len(arr) - 1:
if arr[i] and arr[i+1]:
return i
i+=1
raise Exception('No common edge found')
# Find points in poly1,which are closer than given tolerance to any point in poly2
# and vice versa
tolerance = 0.1
points_in_poly1_close_to_poly2 = is_close(poly1,poly2,tolerance)
points_in_poly2_close_to_poly1 = is_close(poly2,poly1,tolerance)
# Scan each array for two adjacent true values (points at those two indices
# form an edge which is common to both polygons and which we want to remove).
# Idx1 (resp. idx2) will contain the index of the first point of that common edge in poly1 (resp. poly2)
idx1 = find_consecutive_true_values(points_in_poly1_close_to_poly2)
idx2 = find_consecutive_true_values(points_in_poly2_close_to_poly1)
#Split poly1 into two parts:
# first part contains points from the start up to the first point of the common edge (inclusive)
# second part contains points from the second point of the common edge to the end
poly1_part1 = poly1[:idx1+1]
poly1_part2 = poly1[idx1+1:]
#Remove common edge from poly2,depending on where it is located,we end up with one or two parts
if idx2 == len(poly2) - 2:
poly2_part1 = poly2[1:len(poly2) - 2]
poly2_part2 = None
elif idx2 == 0:
poly2_part1 = poly2[2:len(poly2) - 1]
poly2_part2 = None
else:
poly2_part1 = poly2[idx2+2:]
poly2_part2 = poly2[1:idx2]
#Create the resulting polygon by concatenating the individual parts (poly2_part2 may be empty)
if(poly2_part2 is None):
poly = np.concatenate((poly1_part1,poly2_part1,poly1_part2))
else:
poly = np.concatenate((poly1_part1,poly2_part2,poly1_part2))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot(poly[:,0],poly[:,1],2])
ax.view_init(45,45)
plt.show()
(代码远不是惯用的,如果您了解Python,请随时对其进行编辑:))
,我刚刚做了一个简单的解决方案,它允许将两个至少具有1个公共点的多边形组合在一起,并可能创建连续线。
现在,没有容忍度(共同点必须相同),但是在最近,我将其添加为函数的参数,因为我完全同意。我将添加合并一些常见线的多边形的可能性,但我需要时间。可能我将其推送到GitHub并在此处粘贴链接。
修改后的多边形(class Customer(models.Model):
first_name = models.CharField(max_length=200,verbose_name='')
last_name = models.CharField(max_length=200,verbose_name='')
def __str__(self):
return self.first_name + ',' + self.last_name
中的第一个和最后一个元素与poly2
中的相同):
poly1
Kinda的平庸解决方案,由于这种情况,代码并不美观,但可以使用:
poly1 = np.array([[120787.075999871,-2.0699999332428]])
此功能经过测试,可以在两个框架中进行所有可能性的测试,但我将为此做更多测试。
如果只想将多边形拼接成一个形状,则只需使用列表即可。
def merge_polygons(p1,p2):
"""
Simple function that allows to combine two polygons (as numpy arrays) with at least 1 common point
and potential created continuous lines.
:return: polygon (merged p1 and p2) as numpy array
"""
poly1_l = list(p1)[1:]
poly2_l = list(p2)[1:]
common_i1 = []
common_i2 = []
# looking for common points
for i,j in ((i,j) for i in range(len(poly1_l)) for j in range(len(poly2_l))):
if np.all(poly1_l[i] == poly2_l[j]):
common_i1.append(i)
common_i2.append(j)
if not common_i1:
raise Exception("Can't merge the polygons - no common point!")
# merging polygons with 1 common point
if len(common_i1) == 1:
poly1_l[common_i1[0]:common_i1[0]] = poly2_l[common_i2[0]:] + poly2_l[:common_i2[0]][::-1]
poly1_l.append(poly1_l[0])
return np.array(poly1_l)
else: # merging polygons with 2+ common points
start = [None,None]
end = [None,None]
# checking,if the common points are creating continuous line
for iterator,common_l in enumerate((common_i1,common_i2)):
for i in common_l:
if not (i - 1) % len(poly1_l) in common_l and not (i + 1) % len(poly1_l) in common_l:
raise Exception("Can't merge the polygons - the common part has to be continuous!")
elif not (i - 1) % len(poly1_l) in common_l: # marking the start and the end of common part
start[iterator] = i
elif not (i + 1) % len(poly1_l) in common_l:
end[iterator] = i
# merging polygons due to location of common part
if isinstance(start[0],int) and isinstance(end[0],int):
poly3_l = []
if start[0] < end[0]: # if the common part in the first polygon is not interrupted by the beginning and the end of list
if start[1] < end[1]: # if the common part in the second polygon is not interrupted by the beginning and the end of list
poly3_l.extend(poly1_l[:start[0]])
if np.all(poly1_l[start[0]] == poly2_l[start[1]]): # if start of chain in first polygon corresponds to start of chain in second polygon
poly3_l.extend(poly2_l[:start[1]+1][::-1])
poly3_l.extend(poly2_l[end[1]:][::-1])
else:
poly3_l.extend(poly2_l[end[1]:])
poly3_l.extend(poly2_l[:start[1]+1])
poly3_l.extend(poly1_l[end[0]+1:])
poly3_l.append(poly3_l[0])
else:
poly3_l.extend(poly1_l[:start[0]])
if np.all(poly1_l[start[0]] == poly2_l[start[1]]):
poly3_l.extend(poly2_l[end[1]:start[1]+1][::-1])
else:
poly3_l.extend(poly2_l[end[1]:start[1]+1])
poly3_l.extend(poly1_l[end[0]+1:])
poly3_l.append(poly3_l[0])
else:
if start[1] < end[1]:
poly3_l.extend(poly2_l[:start[1]+1])
if np.all(poly1_l[start[0]] == poly2_l[start[1]]):
poly3_l.extend(poly1_l[end[0]+1:start[0]][::-1])
else:
poly3_l.extend(poly1_l[end[0]+1:start[0]])
poly3_l.extend(poly2_l[end[1]:])
poly3_l.append(poly3_l[0])
else:
poly3_l.extend(poly1_l[end[0]+1:start[0]])
if np.all(poly1_l[start[0]] == poly2_l[start[1]]):
poly3_l.extend(poly2_l[end[1]:start[1]+1][::-1])
else:
poly3_l.extend(poly2_l[end[1]:start[1]+1])
poly3_l.append(poly3_l[0])
return np.array(poly3_l)
else:
raise Exception("Polygons are the same - there is no need to merge them.")
希望它会对您有所帮助!