单位向量旋转矩阵变换后的变形点云

问题描述 投票:0回答:0

我试图将点云绕轴旋转一定角度而不扭曲其形状。每当我将其旋转非正交角度(任何不是 90 度的倍数)时,它都会变形。我看到了这篇文章并且我确保使用单位向量作为轴,但它仍然被扭曲。

import numpy as np
import math
import matplotlib.pyplot as plt

def rotation_matrix(axis, radian):
    ax, ay, az = axis[0], axis[1], axis[2]
    s = math.sin(radian)
    c = math.cos(radian)
    u = 1 - c
    return ( ( ax*ax*u + c,    ax*ay*u - az*s, ax*az*u + ay*s ),
             ( ay*ax*u + az*s, ay*ay*u + c,    ay*az*u - ax*s ),
             ( az*ax*u - ay*s, az*ay*u + ax*s, az*az*u + c    ) )

def get_ax():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_ylim3d(-5, 5)
    ax.set_xlim3d(-5, 5)
    ax.set_zlim3d(-5, 5)
    return ax

# torus parameters
major_radius = 1.5
minor_radius = 2.9
density = 15
theta = np.linspace(0, 2 * np.pi, density)
phi = np.linspace(0, 2*np.pi, density)
Theta, Phi = np.meshgrid(theta, phi)

# parametric equations for the torus
x = (major_radius + minor_radius * np.cos(Theta)) * np.cos(Phi)
y = (major_radius + minor_radius * np.cos(Theta)) * np.sin(Phi)
z = minor_radius * np.sin(Theta)

# coordinate array
original_points = np.array([x.flatten(), y.flatten(), z.flatten()])

angles = np.array([0, 45, 90])

for angle in angles:

    # get R matrix for unit vec axis and angle
    R = rotation_matrix([0,1,0], np.radians(angle))

    # rotate points by R and plot
    rotated_points = np.dot(R, original_points)
    rotated_x = rotated_points[0]
    rotated_y = rotated_points[1]
    rotated_z = rotated_points[2]
    ax = get_ax()
    ax.scatter(rotated_x, rotated_y, rotated_z, color='black')
    plt.show()

这里是45度。它应该是一个对称的形状。倾斜头部时更容易看到扭曲。

python 3d rotation linear-algebra
© www.soinside.com 2019 - 2024. All rights reserved.