三维线平面交点

时间:2011-04-14 16:11:45

标签: 3d line intersection plane

如果给定一条线(由线上的矢量或两个点表示),如何找到线与平面相交的点?我已经找到了大量的资源,但我无法理解那里的方程(它们似乎不是标准的代数)。我想要一个标准编程语言可以解释的等式(无论多长时间)(我正在使用Java)。

8 个答案:

答案 0 :(得分:29)

这是一个Python示例,它可以找到直线和平面的交点。

如果平面可以是点和法线,也可以是4d矢量(普通形式), 在下面的示例中,(提供了两者的代码)

另请注意,此函数计算一个值,表示该点在该行的位置(在下面的代码中称为fac)。您可能也希望返回此值,因为从0到1的值与线段相交 - 这可能对调用者有用。

代码注释中注明的其他详细信息。


注意:此示例使用纯函数,没有任何依赖性 - 使其易于移动到其他语言。使用Vector数据类型和运算符重载,它可以更简洁(包含在下面的示例中)。

# intersection function
def isect_line_plane_v3(p0, p1, p_co, p_no, epsilon=1e-6):
    """
    p0, p1: define the line
    p_co, p_no: define the plane:
        p_co is a point on the plane (plane coordinate).
        p_no is a normal vector defining the plane direction;
             (does not need to be normalized).

    return a Vector or None (when the intersection can't be found).
    """

    u = sub_v3v3(p1, p0)
    dot = dot_v3v3(p_no, u)

    if abs(dot) > epsilon:
        # the factor of the point between p0 -> p1 (0 - 1)
        # if 'fac' is between (0 - 1) the point intersects with the segment.
        # otherwise:
        #  < 0.0: behind p0.
        #  > 1.0: infront of p1.
        w = sub_v3v3(p0, p_co)
        fac = -dot_v3v3(p_no, w) / dot
        u = mul_v3_fl(u, fac)
        return add_v3v3(p0, u)
    else:
        # The segment is parallel to plane
        return None

# ----------------------
# generic math functions

def add_v3v3(v0, v1):
    return (
        v0[0] + v1[0],
        v0[1] + v1[1],
        v0[2] + v1[2],
        )


def sub_v3v3(v0, v1):
    return (
        v0[0] - v1[0],
        v0[1] - v1[1],
        v0[2] - v1[2],
        )


def dot_v3v3(v0, v1):
    return (
        (v0[0] * v1[0]) +
        (v0[1] * v1[1]) +
        (v0[2] * v1[2])
        )


def len_squared_v3(v0):
    return dot_v3v3(v0, v0)


def mul_v3_fl(v0, f):
    return (
        v0[0] * f,
        v0[1] * f,
        v0[2] * f,
        )

如果将平面定义为4d向量(法线形式),我们需要在平面上找到一个点,然后像以前一样计算交点(参见p_co赋值)。

def isect_line_plane_v3_4d(p0, p1, plane, epsilon=1e-6):    
    u = sub_v3v3(p1, p0)
    dot = dot_v3v3(plane, u)

    if abs(dot) > epsilon:
        # calculate a point on the plane
        # (divide can be omitted for unit hessian-normal form).
        p_co = mul_v3_fl(plane, -plane[3] / len_squared_v3(plane))

        w = sub_v3v3(p0, p_co)
        fac = -dot_v3v3(plane, w) / dot
        u = mul_v3_fl(u, fac)
        return add_v3v3(p0, u)
    else:
        return None

为了进一步参考,这是从Blender中获取并适用于Python。 math_geom.c

中的isect_line_plane_v3()

为清楚起见,这里是使用mathutils API 的版本(可以修改其他带有运算符重载的数学库)

# point-normal plane
def isect_line_plane_v3(p0, p1, p_co, p_no, epsilon=1e-6):
    u = p1 - p0
    dot = p_no * u
    if abs(dot) > epsilon:
        w = p0 - p_co
        fac = -(plane * w) / dot
        return p0 + (u * fac)
    else:
        return None


# normal-form plane
def isect_line_plane_v3_4d(p0, p1, plane, epsilon=1e-6):    
    u = p1 - p0
    dot = plane.xyz * u
    if abs(dot) > epsilon:
        p_co = plane.xyz * (-plane[3] / plane.xyz.length_squared)

        w = p0 - p_co
        fac = -(plane * w) / dot
        return p0 + (u * fac)
    else:
        return None

答案 1 :(得分:18)

您需要考虑三种情况:

  • 平面与线平行,线不在平面(无交点)
  • 平面与线(一个交点)不平行
  • 平面包含线条(线条在其上的每个点相交)

您可以用参数化的形式表达这一行,如下所示:

http://answers.yahoo.com/question/index?qid=20080830195656AA3aEBr

本讲座的前几页对飞机也是如此:

http://math.mit.edu/classes/18.02/notes/lecture5compl-09.pdf

如果平面的法线垂直于沿线的方向,那么你有一个边缘情况,需要看它是否完全相交,或者是否位于平面内。

否则,你有一个交点,可以解决它。

我知道这不是代码,但为了获得强大的解决方案,您可能希望将其放在应用程序的上下文中。

编辑:以下是一个恰好有一个交叉点的示例。假设您从第一个链接中的参数化方程开始:

x = 5 - 13t
y = 5 - 11t
z = 5 - 8t

参数t可以是任何东西。满足这些方程的所有(x, y, z)的(无限)集合构成了该行。然后,如果您有飞机的等式,请说:

x + 2y + 2z = 5

(取自here)您可以将上面xyz的等式替换为平面的等式,现在只有参数t。解决t。这是位于平面中的那条线的t的特定值。然后,您可以通过返回到线方程并替换x来解决yzt

答案 2 :(得分:11)

使用numpy和python:

#Based on http://geomalgorithms.com/a05-_intersect-1.html
from __future__ import print_function
import numpy as np

epsilon=1e-6

#Define plane
planeNormal = np.array([0, 0, 1])
planePoint = np.array([0, 0, 5]) #Any point on the plane

#Define ray
rayDirection = np.array([0, -1, -1])
rayPoint = np.array([0, 0, 10]) #Any point along the ray

ndotu = planeNormal.dot(rayDirection) 

if abs(ndotu) < epsilon:
    print ("no intersection or line is within plane")

w = rayPoint - planePoint
si = -planeNormal.dot(w) / ndotu
Psi = w + si * rayDirection + planePoint

print ("intersection at", Psi)

答案 3 :(得分:11)

这是Java中的一种方法,用于查找线和平面之间的交点。没有包含向量方法,但是它们的功能很容易说明。

/**
 * Determines the point of intersection between a plane defined by a point and a normal vector and a line defined by a point and a direction vector.
 *
 * @param planePoint    A point on the plane.
 * @param planeNormal   The normal vector of the plane.
 * @param linePoint     A point on the line.
 * @param lineDirection The direction vector of the line.
 * @return The point of intersection between the line and the plane, null if the line is parallel to the plane.
 */
public static Vector lineIntersection(Vector planePoint, Vector planeNormal, Vector linePoint, Vector lineDirection) {
    if (planeNormal.dot(lineDirection.normalize()) == 0) {
        return null;
    }

    double t = (planeNormal.dot(planePoint) - planeNormal.dot(linePoint)) / planeNormal.dot(lineDirection.normalize());
    return linePoint.plus(lineDirection.normalize().scale(t));
}

答案 4 :(得分:3)

基于this Matlab代码(减去交集检查),在Python中

# n: normal vector of the Plane 
# V0: any point that belongs to the Plane 
# P0: end point 1 of the segment P0P1
# P1:  end point 2 of the segment P0P1
n = np.array([1., 1., 1.])
V0 = np.array([1., 1., -5.])
P0 = np.array([-5., 1., -1.])
P1 = np.array([1., 2., 3.])

w = P0 - V0;
u = P1-P0;
N = -np.dot(n,w);
D = np.dot(n,u)
sI = N / D
I = P0+ sI*u
print I

结果

[-3.90909091  1.18181818 -0.27272727]

我以图形方式检查它似乎有效,

enter image description here

我相信这是一个更强大的链接共享before

的实现

答案 5 :(得分:3)

如果你有两个点p和q定义一条线,一个平面用一般的笛卡尔形式ax + by + cz + d = 0,你可以使用参数方法。

如果你需要这个用于编码目的,这里是一个javascript片段:

/**
* findLinePlaneIntersectionCoords (to avoid requiring unnecessary instantiation)
* Given points p with px py pz and q that define a line, and the plane
* of formula ax+by+cz+d = 0, returns the intersection point or null if none.
*/
function findLinePlaneIntersectionCoords(px, py, pz, qx, qy, qz, a, b, c, d) {
    var tDenom = a*(qx-px) + b*(qy-py) + c*(qz-pz);
    if (tDenom == 0) return null;

    var t = - ( a*px + b*py + c*pz + d ) / tDenom;

    return {
        x: (px+t*(qx-px)),
        y: (py+t*(qy-py)),
        z: (pz+t*(qz-pz))
    };
}

// Example (plane at y = 10  and perpendicular line from the origin)
console.log(JSON.stringify(findLinePlaneIntersectionCoords(0,0,0,0,1,0,0,1,0,-10)));

// Example (no intersection, plane and line are parallel)
console.log(JSON.stringify(findLinePlaneIntersectionCoords(0,0,0,0,0,1,0,1,0,-10)));

答案 6 :(得分:2)

这个问题已经过时了,但由于有更方便的解决方案,我认为这可能有助于某人。

当用齐次坐标表示时,平面和直线交点相当优雅,但我们假设您只想要解决方案:

存在向量4x1 p,其描述平面,使得对于平面上的任何均匀点,p ^ T * x = 0。接下来计算线L = ab ^ T - ba ^ T的plucker坐标,其中a = {point_1; 1},b = {point_2; 1},行上的4x1

compute:x = L * p = {x0,x1,x2,x3}

x_intersect =({x0,x1,x2} / x3)其中如果x3为零,则欧几里德意义上没有交集。

答案 7 :(得分:0)

仅需扩展ZGorlock's的答案,我就完成了点积,3D向量的正负缩放。这些计算的参考是Dot ProductAdd two 3D vectorsScaling。注意:Vec3D只是一个具有点:x,y和z的自定义类。

/**
 * Determines the point of intersection between a plane defined by a point and a normal vector and a line defined by a point and a direction vector.
 *
 * @param planePoint    A point on the plane.
 * @param planeNormal   The normal vector of the plane.
 * @param linePoint     A point on the line.
 * @param lineDirection The direction vector of the line.
 * @return The point of intersection between the line and the plane, null if the line is parallel to the plane.
 */
public static Vec3D lineIntersection(Vec3D planePoint, Vec3D planeNormal, Vec3D linePoint, Vec3D lineDirection) {
    //ax × bx + ay × by
    int dot = (int) (planeNormal.x * lineDirection.x + planeNormal.y * lineDirection.y);
    if (dot == 0) {
        return null;
    }

    // Ref for dot product calculation: https://www.mathsisfun.com/algebra/vectors-dot-product.html
    int dot2 = (int) (planeNormal.x * planePoint.x + planeNormal.y * planePoint.y);
    int dot3 = (int) (planeNormal.x * linePoint.x + planeNormal.y * linePoint.y);
    int dot4 = (int) (planeNormal.x * lineDirection.x + planeNormal.y * lineDirection.y);

    double t = (dot2 - dot3) / dot4;

    float xs = (float) (lineDirection.x * t);
    float ys = (float) (lineDirection.y * t);
    float zs = (float) (lineDirection.z * t);
    Vec3D lineDirectionScale = new Vec3D( xs, ys, zs);

    float xa = (linePoint.x + lineDirectionScale.x);
    float ya = (linePoint.y + lineDirectionScale.y);
    float za = (linePoint.z + lineDirectionScale.z);

    return new Vec3D(xa, ya, za);
}