使用欧拉角/矩阵在3D空间中旋转?

时间:2019-02-13 23:10:02

标签: python numpy math 3d angle

我正在尝试在3d空间中旋转圆柱体,以将2个点在3D空间中连接在一起。在计算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)。

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

1 个答案:

答案 0 :(得分:0)

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

Rotation Matrix 1

Rotation Matrix 2

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

Rotation Matrix 3

实际上,您需要像对2D情况一样选择对上面的3个旋转矩阵分别使用Theta还是-1 * Theta。