查询变换球体上的最近点

时间:2017-05-02 21:31:58

标签: python geometry computational-geometry

鉴于球体的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.]

enter image description here

当查询点已经接近球体时,此解决方案很快并且运行良好,但是当它远离球体时则不然。参见上图:点p2,这将是所需的结果。

2 个答案:

答案 0 :(得分:3)

你想看看大卫艾伯利的“从点到椭圆的距离,椭圆体,或者 超椭圆” (PDF download)。 最终你会找到一个二维椭圆的四次多项式的根,和 一个三维椭球的6次多项式的根,所以这是 绝不是一个简单的问题。

鉴于这种复杂性,您可能希望寻找近似结果,例如,通过对椭球进行网格划分并寻找最近的网格顶点。

答案 1 :(得分:3)

我不知道下面的迭代过程是否有效,但直观地想到了这一点。也许值得一试。

  1. 从给定点开始,将一条线绘制到椭圆体的中心,并获得与曲面的交点。

  2. 在交叉点处构建法线平面并将给定点投影到其上。

  3. 将投影连接到中心,找到与曲面的交点并从2开始重复,直到收敛。

  4. 我无法确定是否可以保证收敛到最近点。我只能说,当有收敛时,你已经找到了给定点在表面上的正交投影。

    图片显示了第一次迭代(绿色的交叉点,橙色的投影)。

    enter image description here

    这可能相当于牛顿的迭代。