三维线平面交点

问题描述 投票:26回答:8

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

3d line intersection plane
8个回答
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));
}

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。在isect_line_plane_v3()math_geom.c


为清楚起见,这里是使用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

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


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
投票

基于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的实现


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)));

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为零,则在欧几里德意义上没有交集。


0
投票

只是为了扩展ZGorlock's的答案,我已经完成了点积,加上和3D的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);
}
© www.soinside.com 2019 - 2024. All rights reserved.