我试图计算直线和矩形棱镜的交点。 矩形棱镜的体积由6个平面表示
s1 = np.array([[-0.25,0,0], [1,0,0]]); s2 = np.array([[0.25,0,0], [1,0,0]]);
s3 = np.array([[0,-0.15,0],[0,1,0]]); s4 = np.array([[0,0.15,0],[0,1,0]]);
s5 = np.array([[0,0,0],[0,0,1]]); s6 = np.array([[0,0,0.3],[0,0,1]])
surfaces = np.array([s1,s2,s3,s4,s5,s6])
线由点和方向表示
point, direction = generateRandomUnitVelocity()
p = point; d = direction
要确定直线是否与单个平面相交,我运行:
def intersectLinePlane(p0, u, p1, n):
w = p0 - p1
s = -1 * np.dot(n, w) / np.dot(n, u) # distance from p0 to intersection
intersectionPoint = p0 + s*u
return intersectionPoint
为了确定线在何处截取体积的每个平面,我迭代表示体积的每个平面。如果线在体积尺寸定义的范围内拦截平面,则线将拦截体积的一侧
def volumeIntersectionPoints(p0,u,planes):
points = []
for plane in planes:
point = intersectLinePlane(p0,u,plane[0],plane[1])
#Define the dimensions of the rectangular prism
if ((abs(point[0]) <= 0.25) and (abs(point[1]) <= 0.15) and (0 <= point[2] <= 0.3)):
points.append(point)
if points:
return points
据我所知,如果线路在进入和退出卷时截取了卷,那么volumeIntersectionPoints应该返回两个截取点。这不一定会发生。有时我会像预期的那样得到两个拦截点,更频繁的时候我会得到一个
#Run this 10000 to get some intersecting lines
for i in range (10000):
point, direction = generateRandomUnitVelocity()
p = point; d = direction
points = volumeIntersectionPoints(p,d,surfaces)
if points:
print("Line direction:", d, "Intersection points:", points)
print("Number of intersection points:", len(points))
输出:
Line direction: [ 0.39894237 -0.60698745 -0.68732178] Intersection points: [array([0.25 , 0.12644022, 0.23721007])]
Number of intersection points: 1
Line direction: [ 0.30552862 -0.72084221 -0.62212441] Intersection points: [array([-0.25 , 0.1291224 , 0.21573566]), array([-0.14405106, -0.12084588, 0. ])]
Number of intersection points: 2
Line direction: [ 0.22272246 -0.14895558 -0.96343497] Intersection points: [array([ 0.13732939, -0.00621236, 0. ])]
Number of intersection points: 1
Line direction: [ 0.00423704 -0.2063055 -0.97847846] Intersection points: [array([-0.18776991, -0.15 , 0.0180488 ])]
Number of intersection points: 1
Line direction: [-0.52795903 -0.54028763 -0.65524693] Intersection points: [array([-0.20020341, 0.00703934, 0. ])]
Number of intersection points: 1
我哪里出了问题
你被舍入错误愚弄了。当直线在x=0.25处与平面相交时,通常计算的点实际上是0.2500001,并且<;=0.25失败
我添加了一个
epsilon = 0.000001
,然后扩展了测试以允许该坡度,现在每条线都有两个交点相关问题 更多 >
编程相关推荐