匀称:3D中线和多边形之间的交点

时间:2017-11-17 22:06:30

标签: python vector geometry shapely shapely.geometry

上次我用得很整齐,我真的有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))。下面用蓝点说明:

enter image description here

相反,我得到的是LINESTRING Z (1.7 0.5 0.25, 2.8 0.5 1) - 下面的红线 - 坦率地说,我对它应该代表什么感到非常困惑。 enter image description here

奇怪的是,当多边形/三角形位于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在应用于3D对象时会返回什么?为什么它是一条线?

  • 是否有适合我想要的功能?或任何可选的参数,调整或黑魔术?

  • 在实现我的简单和懒惰的梦想的同时,还有其他图书馆可以做这项工作吗?

1 个答案:

答案 0 :(得分:2)

遗憾的是,正如文件所述:

  

坐标序列是不可变的。在构造实例时可以使用第三个z坐标值,但对几何分析没有影响。所有操作都在x-y平面上执行。

可以通过以下方式验证:

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)
相关问题