上次我使用匀称的时候,我真的有 this nice import-and-fly-feeling 。
然而最近,我在这个模块中遇到了一个相当不直观的行为,因为我试图在 3D 空间中找到线段和三角形之间的交点。让我们定义一个线段和一个三角形如下:
l = LineString([[1,0.5,0.5],[3,0.5,0.5]])
p = Polygon([[1.2,0.0,0.],[2.2,1.0,0.],[2.8,0.5,1.]])
为了得到它们的交点,我使用了
l.intersection(p)
,并期望得到一个点,即 POINT Z (POINT Z (2 0.5 0.25))
。它用下面的蓝点表示:相反,我得到的是
LINESTRING Z (1.7 0.5 0.25, 2.8 0.5 1)
- 下面的红线 - 坦率地说,我对它应该代表什么感到非常困惑。奇怪的是,当多边形/三角形在 xz 平面中并与线段正交时,该函数的行为与人们预期的一样。然而,当三角形“倾斜”时,它返回一条线。这暂时让我相信它返回了线和三角形边界框之间的交点。上面的红线证明并非如此。
因此,解决此问题的方法是读取 this very enlightening web-page ,并调整其
C++
代码以处理匀称的对象。 intersection
方法可以很好地检查线是否穿过多边形,下面的函数会找到感兴趣的点。def intersect3D_SegmentPlane(Segment, Plane):
# Points in Segment: Pn Points in Plane: Qn
P0, P1 = np.array(Segment.coords)
Q0, Q1, Q2 = np.array(Plane.exterior)[:-1]
# vectors in Plane
q1 = Q1 - Q0
q2 = Q2 - Q0
# vector normal to Plane
n = np.cross(q1, q2)/np.linalg.norm(np.cross(q1, q2))
u = P1 - P0 # Segment's direction vector
w = P0 - Q0 # vector from plane ref point to segment ref point
## Tests parallelism
if np.dot(n, u) == 0:
print "Segment and plane are parallel"
print "Either Segment is entirely in Plane or they never intersect."
return None
## if intersection is a point
else:
## Si is the scalar where P(Si) = P0 + Si*u lies in Plane
Si = np.dot(-n, w) / np.dot(n, u)
PSi = P0 + Si * u
return PSi
不再是非常进口和飞行了......
所以最后我的问题:
intersection
返回什么?为什么它是一条线? 最佳答案
不幸的是,正如文档所述:
可以通过以下方式验证这一点:
from shapely.geometry import LineString, Polygon
l = LineString([[1,0.5,0.5],[3,0.5,0.5]])
p = Polygon([[1.2,0.0,0.],[2.2,1.0,0.],[2.8,0.5,1.]])
print(l.intersection(p))
#LINESTRING Z (1.7 0.5 0.25, 2.8 0.5 1)
l = LineString([[1,0.5],[3,0.5]])
p = Polygon([[1.2,0.0],[2.2,1.0],[2.8,0.5]])
print(l.intersection(p))
#LINESTRING (1.7 0.5, 2.8 0.5)
甚至:
from shapely.geometry import LineString, Polygon
l = LineString([[1,0.5,0],[3,0.5,0]])
p = Polygon([[1.2,0.0,1],[2.2,1.0,1],[2.8,0.5,1]])
print(l.intersects(p))
#True (even though the objects are in different z-planes)
关于python - Shapely:3D 中线和多边形之间的交点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47359985/