鉴于球体的4x4变换矩阵和空间中的点,我想找到球体表面上的最近点。
通常我会在点和球体的中心之间追踪一条直线,并使用球体的半径来得到我的解,但在这里我正在处理一个非均匀缩放的球体。这是Python中的一个简单示例:
import numpy as np
from numpy.core.umath_tests import inner1d
# 4x4 transform matrix of a sphere with the following components:
# Scale XYZ = 1,5,1 (Scaled only in Y axis to keep this example simple)
# Rotation XYZ = 0,0,45 (Simple tilt for this example)
# Position XYZ = -1,3,0 (Position along XY plane, again for simplicity)
M = np.array([[ 0.70710678, 0.70710678, 0. , 0. ],
[-3.53553391, 3.53553391, 0. , 0. ],
[ 0. , 0. , 1. , 0. ],
[-1. , 3. , 0. , 1. ]])
# Query point p0
p0 = np.array([-2,6,0])
# Transform the point into a unit sphere
I = np.linalg.inv(M)
p1 = np.array(p)-M[3,:3]
p1 = np.dot(p1,I)
# Normalize the point so it is on the surface of the unit sphere
mag = np.sqrt(inner1d(p1,p1)) # magnitude
p1 /= mag
# Transform back into 3D space
p1 = np.dot(p1,M[:3,:3]) + M[3,:3] #result [-1.65653216, 4.96959649, 0.]
当查询点已经接近球体时,此解决方案很快并且运行良好,但是当它远离球体时则不然。参见上图:点p2,这将是所需的结果。
答案 0 :(得分:3)
你想看看大卫艾伯利的“从点到椭圆的距离,椭圆体,或者 超椭圆” (PDF download)。 最终你会找到一个二维椭圆的四次多项式的根,和 一个三维椭球的6次多项式的根,所以这是 绝不是一个简单的问题。
鉴于这种复杂性,您可能希望寻找近似结果,例如,通过对椭球进行网格划分并寻找最近的网格顶点。
答案 1 :(得分:3)