【问题标题】:Using euler angles/matrix to rotate in 3d space?使用欧拉角/矩阵在 3d 空间中旋转?
【发布时间】:2019-07-07 21:58:27
【问题描述】:

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

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】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2013-01-14
      • 2019-10-13
      相关资源
      最近更新 更多