如何将三维密度图的主轴与笛卡尔坐标轴对齐?

问题描述 投票:3回答:1

我有一个n x n x n numpy数组,其中包含立方网格上的密度值。我试图将密度图的惯性主轴与网格的笛卡尔x,y,z轴对齐。到目前为止,我有以下内容:

import numpy as np
from scipy import ndimage

def center_rho(rho):
    """Move density map so its center of mass aligns with the center of the grid"""
    rhocom = np.array(ndimage.measurements.center_of_mass(rho))
    gridcenter = np.array(rho.shape)/2.
    shift = gridcenter-rhocom
    rho = ndimage.interpolation.shift(rho,shift,order=1,mode='wrap')
    return rho

def inertia_tensor(rho,side):
    """Calculate the moment of inertia tensor for the given density map."""
    halfside = side/2.
    n = rho.shape[0]
    x_ = np.linspace(-halfside,halfside,n)
    x,y,z = np.meshgrid(x_,x_,x_,indexing='ij')
    Ixx = np.sum(rho*(y**2 + z**2))
    Iyy = np.sum(rho*(x**2 + z**2))
    Izz = np.sum(rho*(x**2 + y**2))
    Ixy = -np.sum(rho*x*y)
    Iyz = -np.sum(rho*y*z)
    Ixz = -np.sum(rho*x*z)
    I = np.array([[Ixx, Ixy, Ixz],
                  [Ixy, Iyy, Iyz],
                  [Ixz, Iyz, Izz]])
    return I

def principal_axes(I):
    """Calculate the principal inertia axes and order them in ascending order."""
    w,v = np.linalg.eigh(I)
    return w,v

#number of grid points along side
n = 10
#note n <= 3 produces unit eigenvectors, not sure why
#in practice, n typically between 10 and 50
np.random.seed(1)
rho = np.random.random(size=(n,n,n))
side = 1. #physical width of box, set to 1.0 for simplicity

rho = center_rho(rho)
I = inertia_tensor(rho,side)
PAw, PAv = principal_axes(I)

#print magnitude and direction of principal axes
print "Eigenvalues/eigenvectors before rotation:"
for i in range(3):
    print PAw[i], PAv[:,i]

#sanity check that I = R * D * R.T
#where R is the rotation matrix and D is the diagonalized matrix of eigenvalues
D = np.eye(3)*PAw
print np.allclose(np.dot(PAv,np.dot(D,PAv.T)),I)

#rotate rho to align principal axes with cartesian axes
newrho = ndimage.interpolation.affine_transform(rho,PAv.T,order=1,mode='wrap')

#recalculate principal axes
newI = inertia_tensor(newrho,side)
newPAw, newPAv = principal_axes(newI)

#print magnitude and direction of new principal axes
print "Eigenvalues/eigenvectors before rotation:"
for i in range(3):
    print newPAw[i], newPAv[:,i]

在这里我假设惯性张量的特征向量定义旋转矩阵(基于this question和谷歌结果如this webpage似乎是正确的?)但是这并没有给我正确的结果。

我希望印刷的矩阵是:

[1 0 0]
[0 1 0]
[0 0 1]

(这可能是错的)但是甚至没有开始使用单位向量。我得到的是:

Eigenvalues/eigenvectors before rotation:
102.405523732 [-0.05954221 -0.8616362   0.5040216 ]
103.177395578 [-0.30020273  0.49699978  0.81416801]
104.175688943 [-0.95201526 -0.10283129 -0.288258  ]
True
Eigenvalues/eigenvectors after rotation:
104.414931478 [ 0.38786    -0.90425086  0.17859172]
104.731536038 [-0.74968553 -0.19676735  0.63186566]
106.151322662 [-0.53622405 -0.37896304 -0.75422197]

我不确定问题是我的代码还是我对旋转主轴的假设,但任何帮助都会受到赞赏。

python numpy image-rotation ndimage
1个回答
0
投票

Here是我开发的用于进行此类对齐的代码的链接。

给定一组具有坐标(x,y,z)的散点,目标是将与最小特征值相关联的特征向量与3D笛卡尔轴的X轴和与Y轴的中间特征值相关联的特征向量相匹配。来自相同的3D笛卡尔轴。

为此,我按照以下步骤操作:

  1. 通过执行:(x-xmn,y-ymn,z-zmn)将具有质心(xmn,ymn,zmn)的点集转换为具有质心(0,0,0)的新的点集。
  2. 计算与最小特征值(min_eigen)相关联的特征向量的xy投影与笛卡尔轴中的x轴之间的角度THETA(绕z旋转)。在获得得到的tetha之后,旋转给定theta的min_eigen,使其包含在xy平面中。我们称之为结果向量:rotz
  3. 计算rotz和x轴之间的角度PHI,以便围绕y轴执行旋转。一旦获得phi,就施加旋转以使y轴旋转。通过该最后一次旋转,与中间特征向量(medium_eigen)相关联的特征向量然后在笛卡尔轴的yz逼近中,因此我们将需要找到medium_eigen与笛卡尔轴的y轴之间的角度。
  4. 计算medium_eigen和y轴之间的角度ALPHA。围绕x轴aaaand应用旋转:完成!

注意:将步骤1,2,3应用到您的点集后,您必须重新计算3D_SVD(3D_single值分解)并从结果的特征向量集中重新计算,然后使用新的medium_eigen实现第4步。

我真的希望这会有所帮助。

旋转通过此处定义的旋转矩阵实现:Rotating a Vector in 3D Space

© www.soinside.com 2019 - 2024. All rights reserved.