我试图在三维空间中旋转一个圆柱体,将两个点连接在一起。在计算了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\u object和sphere\u object2)。你知道吗
问题是旋转似乎是正确的,但方向是错误的,这取决于球体出现在哪个位置。你知道吗
同样的方法,我们有2个旋转矩阵的二维形状,你需要选择其中的8个可能的旋转矩阵组合比符合你的标准。你知道吗
在二维情况下,需要定义哪个旋转矩阵匹配坐标系和打印库。你知道吗
在实践中,您需要选择是使用θ还是-1*θ作为上述3个旋转矩阵中的每一个,就像我们在2D情况下所做的那样。你知道吗
相关问题 更多 >
编程相关推荐