旁注,但你所拥有的并不是 3d 椭球的最一般方程。你的方程可以改写为
A*x**2 + C*y**2 + D*x + E*y + B*x*y = - G*z**2 - F,
这意味着实际上对于每个值,z
您都会获得不同级别的 2d 椭圆,并且切片相对于z = 0
平面是对称的。这显示了您的椭圆体是如何不通用的,它有助于检查结果以确保我们得到的结果是有意义的。
假设我们采取一般观点r0 = [x0, y0, z0]
,你有
r0 @ M @ r0 + b0 @ r0 + c0 == 0
在哪里
M = [ A B/2 0
B/2 C 0
0 0 G],
b0 = [D, E, 0],
c0 = F
其中@
代表矩阵向量或向量向量乘积。
您可以使用您的函数并绘制它的 isosurface,但这将是次优的:您需要为您的函数进行网格近似,这对于足够的分辨率来说非常昂贵,并且您必须明智地选择此采样的域。
相反,您可以对数据执行主轴转换,以概括您自己链接的规范椭圆体的参数图。
第一步是对角化M
为M = V @ D @ V.T
,其中D
是对角线。因为它是一个实对称矩阵,所以这总是可能的并且V
是正交的。然后我们有
r0 @ V @ D @ V.T @ r0 + b0 @ r0 + c0 == 0
我们可以重新组合为
(V.T @ r0) @ D @ (V.T @ r0) + b0 @ V @ (V.T @ r0) + c0 == 0
这激发了辅助坐标r1 = V.T @ r0
和向量的定义b1 = b0 @ V
,我们得到
r1 @ D @ r1 + b1 @ r1 + c0 == 0.
由于D
是一个对称矩阵,其特征值d1, d2, d3
在其对角线上,所以上面是方程
d1 * x1**2 + d2 * x2**2 + d3 * x3**3 + b11 * x1 + b12 * x2 + b13 * x3 + c0 == 0
哪里r1 = [x1, x2, x3]
和b1 = [b11, b12, b13]
。
剩下的就是从 切换r1
到r2
删除线性项:
d1 * (x1 + b11/(2*d1))**2 + d2 * (x2 + b12/(2*d2))**2 + d3 * (x3 + b13/(2*d3))**2 - b11**2/(4*d1) - b12**2/(4*d2) - b13**2/(4*d3) + c0 == 0
所以我们定义
r2 = [x2, y2, z2]
x2 = x1 + b11/(2*d1)
y2 = y1 + b12/(2*d2)
z2 = z1 + b13/(2*d3)
c2 = b11**2/(4*d1) b12**2/(4*d2) b13**2/(4*d3) - c0.
对于这些,我们终于有了
d1 * x2**2 + d2 * y2**2 + d3 * z2**2 == c2,
d1/c2 * x2**2 + d2/c2 * y2**2 + d3/c2 * z2**2 == 1
这是二阶曲面的规范形式。为了使它有意义地对应于一个椭球,我们必须确保、d1
和都是严格正的。如果这得到保证,那么规范形式的半长轴是和。d2
d3
c2
sqrt(c2/d1)
sqrt(c2/d2)
sqrt(c2/d3)
所以这就是我们要做的:
- 确保参数对应于椭球体
- 为极角和方位角生成 theta 和 phi 网格
- 计算变换后的坐标
[x2, y2, z2]
- 将它们移回 (by
r2 - r1
) 以获得[x1, y1, z1]
- 将坐标转换回来,
V
得到我们感兴趣r0
的实际坐标。[x, y, z]
这是我如何实现的:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def get_transforms(A, B, C, D, E, F, G):
""" Get transformation matrix and shift for a 3d ellipsoid
Assume A*x**2 + C*y**2 + D*x + E*y + B*x*y + F + G*z**2 = 0,
use principal axis transformation and verify that the inputs
correspond to an ellipsoid.
Returns: (d, V, s) tuple of arrays
d: shape (3,) of semi-major axes in the canonical form
(X/d1)**2 + (Y/d2)**2 + (Z/d3)**2 = 1
V: shape (3,3) of the eigensystem
s: shape (3,) shift from the linear terms
"""
# construct original matrix
M = np.array([[A, B/2, 0],
[B/2, C, 0],
[0, 0, G]])
# construct original linear coefficient vector
b0 = np.array([D, E, 0])
# constant term
c0 = F
# compute eigensystem
D, V = np.linalg.eig(M)
if (D <= 0).any():
raise ValueError("Parameter matrix is not positive definite!")
# transform the shift
b1 = b0 @ V
# compute the final shift vector
s = b1 / (2 * D)
# compute the final constant term, also has to be positive
c2 = (b1**2 / (4 * D)).sum() - c0
if c2 <= 0:
print(b1, D, c0, c2)
raise ValueError("Constant in the canonical form is not positive!")
# compute the semi-major axes
d = np.sqrt(c2 / D)
return d, V, s
def get_ellipsoid_coordinates(A, B, C, D, E, F, G, n_theta=20, n_phi=40):
"""Compute coordinates of an ellipsoid on an ellipsoidal grid
Returns: x, y, z arrays of shape (n_theta, n_phi)
"""
# get canonical grid
theta,phi = np.mgrid[0:np.pi:n_theta*1j, 0:2*np.pi:n_phi*1j]
r2 = np.array([np.sin(theta) * np.cos(phi),
np.sin(theta) * np.sin(phi),
np.cos(theta)]) # shape (3, n_theta, n_phi)
# get transformation data
d, V, s = get_transforms(A, B, C, D, E, F, G) # could be *args I guess
# shift and transform back the coordinates
r1 = d[:,None,None]*r2 - s[:,None,None] # broadcast along first of three axes
r0 = (V @ r1.reshape(3, -1)).reshape(r1.shape) # shape (3, n_theta, n_phi)
return r0 # unpackable to x, y, z of shape (n_theta, n_phi)
这是一个带有椭圆体的示例并证明它有效:
A,B,C,D,E,F,G = args = 2, -1, 2, 3, -4, -3, 4
x,y,z = get_ellipsoid_coordinates(*args)
print(np.allclose(A*x**2 + C*y**2 + D*x + E*y + B*x*y + F + G*z**2, 0)) # True
从这里开始的实际绘图是微不足道的。使用此答案中的 3d 缩放技巧来保留相等的轴:
# create 3d axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# plot the data
ax.plot_wireframe(x, y, z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# scaling hack
bbox_min = np.min([x, y, z])
bbox_max = np.max([x, y, z])
ax.auto_scale_xyz([bbox_min, bbox_max], [bbox_min, bbox_max], [bbox_min, bbox_max])
plt.show()
结果如下所示:

围绕它旋转很明显,表面确实相对于z = 0
平面反射对称,这从方程中可以看出。
您可以更改函数的n_theta
和n_phi
关键字参数以生成具有不同网格的网格。有趣的是,您可以将位于单位球体上的任何r2
散点插入到函数中的定义中get_ellipsoid_coordinates
(只要该数组的第一维大小为 3),输出坐标将具有相同的形状,但它们将被转换到实际的椭球体上。
您还可以使用其他库来可视化曲面,例如 mayavi,您可以在其中绘制我们刚刚计算的曲面,或者将其与内置的等值面进行比较。