我有平面的四个角顶点以及线段的起点和终点的顶点。我希望能够检查该线是否与 3-D 空间中的平面相交。
我最初使用这个 https://www.youtube.com/watch?v=_W3aVWsMp14 视频来进行数学计算,将其转换为参数,然后求解。
但是,我对如何实际求解和格式化方程有点迷失。
import numpy as np
from sympy import Point3D, Plane, Line3D
import sympy as sp
def find_intersection_point(line_vertices, plane_vertices):
t, tt = sp.symbols('t tt')
line_start = np.array(line_vertices[0])
line_end = np.array(line_vertices[1])
line_direction = line_end - line_start
line_x = line_start[0]*t + line_direction[0]
line_y = line_start[1]*t + line_direction[1]
line_z = line_start[2]*t + line_direction[2]
print(line_x)
print(line_y)
print(line_z)
# Find two vectors in the plane
vector1 = np.subtract(plane_vertices[1], plane_vertices[0])
vector2 = np.subtract(plane_vertices[2], plane_vertices[0])
# Find the normal vector
normal_vector = np.cross(vector1, vector2)
print("normal vector: ", normal_vector)
# normal_vector[0]*(line_x-plane_vertices)
def get_line_vetices(line):
line_vertices = [[0 for i in range(3)] for j in range(2)]
line_vertices = np.empty((2, 3))
for i in range(2):
line_vertices[i][0] = line.Shape.Vertexes[i].X
line_vertices[i][1] = line.Shape.Vertexes[i].Y
line_vertices[i][2] = line.Shape.Vertexes[i].Z
return line_vertices
def get_plane_vertices(plane):
plane_vertices = [[0 for i in range(3)] for j in range(4)]
plane_vertices = np.empty((4, 3))
for i in range(4):
plane_vertices[i][0] = plane.Shape.Vertexes[i].X
plane_vertices[i][1] = plane.Shape.Vertexes[i].Y
plane_vertices[i][2] = plane.Shape.Vertexes[i].Z
return plane_vertices
# Define the vertices of the plane as [x, y, z]
plane_vertices = [
[-26.551074, 29.194627000000004, 0.0],
[45.091893, 29.194627000000004, 0.0],
[45.091893, -34.722119000000006, 0.0],
[-26.551074, -34.722119000000006, 0.0]
]
plane_vertices = np.array(plane_vertices)
# Define the start and end points of the line segment
line_vertices = [
[-112.944015, -52.98405, 0.0],
[136.40151699999996, 39.730347999999985, 0.0]
]
line_vertices = np.array(line_vertices)
find_intersection_point(line_vertices, plane_vertices)
下面的代码是我正在尝试但最终没有成功的另一种方法。
# plane Points
a1 = Point3D(-26.551074, 29.194627000000004, 0.0)
a2 = Point3D(45.091893, 29.194627000000004, 0.0)
a3 = Point3D(45.091893, -34.722119000000006, 0.0)
# line Points
p0 = Point3D(-112.944015, -52.98405, 0.0) # point in line
line_direction = np.subtract(line_vertices[0], line_vertices[1])
print(line_direction)
# create plane and line
plane = Plane(a1, a2, a3)
line = Line3D(p0, direction_ratio=line_direction)
print(f"plane equation: {plane.equation()}")
print(f"line equation: {line.equation()}")
# find intersection:
# Find intersection
intr = plane.intersection(line)
if intr:
intersection = intr[0]
print("It intersects")
print(f"Intersection: {intersection}")
else:
print("No intersection found.")
有了三个平面点
a1,a2,a3
和线点p0, p1
,您可以使用向量来制作参数方程
a12 = a2 - a1
a13 = a3 - a1
p01 = p1 - p0
飞机:
x = a1.x + u * a12.x + v * a13.x
y = a1.y + u * a12.y + v * a13.y
z = a1.z + u * a12.z + v * a13.z
线路:
x = p0.x + t * p01.x
y = p0.y + t * p01.y
z = p0.z + t * p01.z
现在将直线方程代入平面方程:
a1.x + u * a12.x + v * a13.x - p0.x - t * p01.x = 0
a1.y + u * a12.y + v * a13.y - p0.y - t * p01.y = 0
a1.z + u * a12.z + v * a13.z - p0.z - t * p01.z = 0
并求解该线性方程组的未知数
u, v, t
如果系统判别式为零,则这条线位于平面内或平行于平面。
否则你就有了交点的参数并且可以计算它。
如果您有射线,请检查该参数
t >= 0
t
参数是否在 0..1
u,v
参数。对于由 a1,a2,a3
点(和隐式四个顶点)定义的矩形(更一般 - 平行四边形)来说,参数应在 0..1
范围内