我正在尝试编写两个函数来将笛卡尔坐标转换为球坐标,反之亦然。以下是我用于转换的方程式(也可以在此维基百科页面上找到):
和
这是我的spherical_to_cartesian功能:
def spherical_to_cartesian(theta, phi):
x = math.cos(phi) * math.sin(theta)
y = math.sin(phi) * math.sin(theta)
z = math.cos(theta)
return x, y, z
Run Code Online (Sandbox Code Playgroud)
这是我的cartesian_to_spherical功能:
def cartesian_to_spherical(x, y, z):
theta = math.atan2(math.sqrt(x ** 2 + y ** 2), z)
phi = math.atan2(y, x) if x >= 0 else math.atan2(y, x) + math.pi
return theta, phi
Run Code Online (Sandbox Code Playgroud)
并且,这是驱动程序代码:
>>> t, p = 27.500, 7.500
>>> x, y, z = spherical_to_cartesian(t, p)
>>> print(f"Cartesian coordinates:\tx={x}\ty={y}\tz={z}")
Cartesian coordinates: x=0.24238129061573832 …Run Code Online (Sandbox Code Playgroud) python geometry polar-coordinates cartesian-coordinates spherical-coordinate
我在单位球面上有几个点,它们根据https://www.cmu.edu/biolphys/deserno/pdf/sphere_equi.pdf 中描述的算法分布(并在下面的代码中实现)。在这些点中的每一个上,我都有一个值,在我的特定情况下,它表示 1 减去一个小错误。[0, 0.1]如果这很重要,则错误在,所以我的值在[0.9, 1].
可悲的是,计算错误是一个代价高昂的过程,我无法根据需要计算尽可能多的点。不过,我希望我的情节看起来像我在绘制“连续”的东西。所以我想为我的数据拟合一个插值函数,以便能够根据需要采样尽可能多的点。
经过一点研究,我发现scipy.interpolate.SmoothSphereBivariateSpline似乎完全符合我的要求。但我不能让它正常工作。
问题:我可以用什么来插值(样条、线性插值,目前什么都可以)我在单位球体上的数据?答案可以是“您误用了scipy.interpolation,这是执行此操作的正确方法”或“此其他功能更适合您的问题”。
应该可以执行numpy并scipy安装的示例代码:
import typing as ty
import numpy
import scipy.interpolate
def get_equidistant_points(N: int) -> ty.List[numpy.ndarray]:
"""Generate approximately n points evenly distributed accros the 3-d sphere.
This function tries to find approximately n points (might be a little less
or more) that are evenly distributed accros the 3-dimensional unit sphere.
The algorithm used is described in
https://www.cmu.edu/biolphys/deserno/pdf/sphere_equi.pdf.
""" …Run Code Online (Sandbox Code Playgroud)