使用euler角度/矩阵在3d空间中旋转?

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

我试图在3D空间中旋转圆柱体以在3D空间中将2个点连接在一起。在计算3个旋转角度后,我使用Euler矩阵将旋转应用于圆柱点的网格。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

class sphere(object):

    def __init__(self, center_x = 0.0, center_y = 0.0, center_z =0.0, radius = 1.0, resolution = 20):

        self.center_x = center_x
        self.center_y = center_y
        self.center_z = center_z
        self.radius = radius
        self.resolution = resolution   
        self.sphere = self._define_sphere()

    def _define_sphere(self):

        self.u, self.v = np.mgrid[0:2*np.pi: (self.resolution * 1j), 0:np.pi: (self.resolution * 1j)]
        self.x = self.radius * np.cos(self.u)*np.sin(self.v) + self.center_x
        self.y = self.radius * np.sin(self.u)*np.sin(self.v) + self.center_y
        self.z = self.radius * np.cos(self.v) + self.center_z

        return [self.x, self.y, self.z]

    def plot_self(self, ax):

        ax.plot_surface(self.x, self.y, self.z)

class cylinder(object):

    def __init__(self, center_x = 0.0, center_y = 0.0, center_z = 0.0, radius = 1.0, z = 1.0, resolution = 20):

        self.center_x = center_x
        self.center_y = center_y
        self.center_z = center_z
        self.radius = radius
        self.z = z
        self.resolution = resolution
        self.cylinder = self._define_cylinder()

    def _define_cylinder(self):

        self.z_values = np.linspace(0, self.z, self.resolution)
        self.theta = np.linspace(0, 2*np.pi, self.resolution)

        self.theta_mesh, self.z_grid = np.meshgrid(self.theta, self.z_values)

        self.x_grid = self.radius * np.cos(self.theta_mesh) + self.center_x
        self.y_grid = self.radius * np.sin(self.theta_mesh) + self.center_y

        return [self.x_grid, self.y_grid, self.z_grid]

    def join_points(self, x1, y1, z1, x2, y2, z2):

        dx = x1 - x2
        dy = y1 - y2
        dz = z1 - z2

        print dx,dy,dz

        distance = math.sqrt(dx**2 + dy**2 + dz**2)

        self.psi = math.atan2(dx, dy)
        self.theta = math.atan2(dx, dz)
        self.phi = 0

        self.euler = np.array([[(math.cos(self.psi)*math.cos(self.phi)) - math.cos(self.theta)*math.sin(self.phi)*math.sin(self.psi), math.cos(self.psi)*math.sin(self.phi) + math.cos(self.theta)*math.cos(self.phi)*math.sin(self.psi), math.sin(self.psi)*math.sin(self.theta)],
                               [-math.sin(self.psi)*math.cos(self.phi) - math.cos(self.theta)*math.sin(self.phi)*math.cos(self.psi), -math.sin(self.psi)*math.sin(self.phi) + math.cos(self.theta)*math.cos(self.phi)*math.cos(self.psi), math.cos(self.psi)*math.sin(self.theta)],
                               [math.sin(self.theta)*math.sin(self.phi), -math.sin(self.theta)*math.cos(self.phi), math.cos(self.theta)]])

        print self.euler

        rotation = np.dot(self.euler, np.array([self.x_grid.ravel(), self.y_grid.ravel(), self.z_grid.ravel()]))

        x,y,z = self.x_grid, self.y_grid, self.z_grid

        self.x_grid = rotation[0,:].reshape(x.shape)               
        self.y_grid = rotation[1,:].reshape(y.shape)               
        self.z_grid = rotation[2,:].reshape(z.shape)

    def plot_self(self, ax):

        ax.plot_surface(self.x_grid, self.y_grid, self.z_grid)

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.set_aspect('equal')
ax.set_xlim([-10,10])
ax.set_ylim([-10,10])
ax.set_zlim([-10,10])

cylinder_object = cylinder(0.0, 0.0, 0.0, 0.3, 12)
cylinder_object.join_points(0.0, 0.0, 0.0, 8.0, 10.0, 2.0)
cylinder_object.plot_self(ax)

sphere_object = sphere(0.0, 0.0, 0.0, 1.0, 100)
sphere_object2 = sphere(8.0, 10.0, 0.0, 1.0, 100)
sphere_object.plot_self(ax)
sphere_object2.plot_self(ax)

预期的结果是创建一个圆柱体,它将点A连接到点B(在我的例子中,是sphere_object和sphere_object2)。

问题是旋转似乎是正确的,但方向错误,取决于球体出现在哪个位置。

python numpy math 3d angle
1个回答
0
投票

对于2D形状我们有2个旋转矩阵的方式相同,您需要选择8个可能的旋转矩阵组合中的一个,而不是匹配您的标准。

Rotation Matrix 1

Rotation Matrix 2

在2D情况下,您需要定义与坐标系统匹配的旋转矩阵与绘图库。

Rotation Matrix 3

在实践中,您需要选择是否为上面的3个旋转矩阵中的每一个使用Theta或-1 * Theta,就像我们对2D情况一样。

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