Sphere和3D Line Segment相交的具体位置
Specific location of intersection between Sphere and 3D Line Segment
我知道交集是否发生的问题已经得到解答,但我一直找不到交集在哪里。
我的分类如下
Circle (alpha, beta, gamma) with radius r
Points at (a,b,c) and (d,e,f)
m = (e-b)/(d-a) slope of xy
n = (f-c)/(d-a) slope of xz
y = m(x-a) + b
z = m(x-a) + c
Combine equation of sphere and line
(x-alpha)^2 + (m(x-a) + b - beta)^2 + (n(x-a) + c - gamma)^2 = r^2
after algebra
(1+m^2+n^2)x^2 + (-2*alpha + 2*m*(-m*a + b - beta) + 2*n*(-n*a + c - gamma))x + (alpha^2 + (-m*a + b - beta)^2 + (-n*a + c - gamma)^2 - r^2) = 0
然后我尝试在二次公式中使用上面的公式,但是在 2,3,1
处使用半径为 2
的圆和从 0,0,0
到 4,4,4
的直线我得到了25.35
和 10.65
的 x 值似乎不正确。
如能帮助我找出我可能做错了什么,将不胜感激。
不失一般性,以球心为原点(如果不是,则所有点减去圆心坐标)。
球体的隐式方程为
X² + Y² + Z² = R².
线段的参数方程:
X = X0 + t X01, Y = Y0 + t Y01, Z = Z0 + t Z01.
将第二个方程代入第一个方程,
(X01² + Y01² + Z01²) t² + 2 (X01.X0 + Y01.Y0 + Z01.Z0) t + X0² + Y0² + Z0² - R² = 0
这是您求解 t
的标准二次方程。
这两个根在球体内定义了一个区间,足以与[0, 1]
相交就知道线段的有用部分。
您是在寻找球体或球体的距离吗?球体是球的表面。在球体情况下,如果整个段都在球体内,则距离大于 0。
计算端点 (D1, D2) 到球体中心 (C) 的距离并找到距离球体中心 (X) 最近的直线(不是线段)上的点就足够了。有了那个:
在球情况下:
if X is on segment:
return max(0, dist(X,C)-R)
return max(0, min(D1, D2)-R)
球体情况:
if X is on segment:
if dist(X,C) < R: # Nearest point is inside sphere
if max(D1, D2) >= R: # One of points is out
return 0 # Than segment intersects sphere
return R - max(D1, D2) # Whole segment is inside sphere, end point is nearest (because of ball convexity)
return R - dist(X,C) # Whole segment is out
if D1 < R and D2 < R: # Whole segment is in
return R - max(D1, D2)
if D1 >= R and D2 >= R: # Whole segment is out
return R - min(D1, D2)
return 0 # Segment intersects sphere
我知道交集是否发生的问题已经得到解答,但我一直找不到交集在哪里。
我的分类如下
Circle (alpha, beta, gamma) with radius r
Points at (a,b,c) and (d,e,f)
m = (e-b)/(d-a) slope of xy
n = (f-c)/(d-a) slope of xz
y = m(x-a) + b
z = m(x-a) + c
Combine equation of sphere and line
(x-alpha)^2 + (m(x-a) + b - beta)^2 + (n(x-a) + c - gamma)^2 = r^2
after algebra
(1+m^2+n^2)x^2 + (-2*alpha + 2*m*(-m*a + b - beta) + 2*n*(-n*a + c - gamma))x + (alpha^2 + (-m*a + b - beta)^2 + (-n*a + c - gamma)^2 - r^2) = 0
然后我尝试在二次公式中使用上面的公式,但是在 2,3,1
处使用半径为 2
的圆和从 0,0,0
到 4,4,4
的直线我得到了25.35
和 10.65
的 x 值似乎不正确。
如能帮助我找出我可能做错了什么,将不胜感激。
不失一般性,以球心为原点(如果不是,则所有点减去圆心坐标)。
球体的隐式方程为
X² + Y² + Z² = R².
线段的参数方程:
X = X0 + t X01, Y = Y0 + t Y01, Z = Z0 + t Z01.
将第二个方程代入第一个方程,
(X01² + Y01² + Z01²) t² + 2 (X01.X0 + Y01.Y0 + Z01.Z0) t + X0² + Y0² + Z0² - R² = 0
这是您求解 t
的标准二次方程。
这两个根在球体内定义了一个区间,足以与[0, 1]
相交就知道线段的有用部分。
您是在寻找球体或球体的距离吗?球体是球的表面。在球体情况下,如果整个段都在球体内,则距离大于 0。
计算端点 (D1, D2) 到球体中心 (C) 的距离并找到距离球体中心 (X) 最近的直线(不是线段)上的点就足够了。有了那个:
在球情况下:
if X is on segment:
return max(0, dist(X,C)-R)
return max(0, min(D1, D2)-R)
球体情况:
if X is on segment:
if dist(X,C) < R: # Nearest point is inside sphere
if max(D1, D2) >= R: # One of points is out
return 0 # Than segment intersects sphere
return R - max(D1, D2) # Whole segment is inside sphere, end point is nearest (because of ball convexity)
return R - dist(X,C) # Whole segment is out
if D1 < R and D2 < R: # Whole segment is in
return R - max(D1, D2)
if D1 >= R and D2 >= R: # Whole segment is out
return R - min(D1, D2)
return 0 # Segment intersects sphere