旋转矩阵

x轴旋转:
在3D坐标系中,逆时针方向通常被视为正方向,所以旋转默认是逆时针的。

prediction3dpoint = np.asarray(prediction3dpoint)
            theta = np.radians(90)
            # 构建绕X轴旋转的矩阵
            rotation_matrix = np.array([[1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)]])

            prediction3dpoint = np.dot(prediction3dpoint, rotation_matrix.T)

y轴旋转:

theta = np.radians(90)

# 构建绕Y轴旋转的矩阵
rotation_matrix = np.array([
    [np.cos(theta), 0, np.sin(theta)],
    [0, 1, 0],
    [-np.sin(theta), 0, np.cos(theta)]
])

z轴旋转:

theta = np.radians(90)

# 构建绕Z轴旋转的矩阵
rotation_matrix = np.array([
    [np.cos(theta), -np.sin(theta), 0],
    [np.sin(theta), np.cos(theta), 0],
    [0, 0, 1]
])

旋转点

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


def rotate_y_axis(point, angle_degree):
     # 将角度转换为弧度
     angle_radian = np.radians(angle_degree)

     # 旋转矩阵
     rotation_matrix = np.array([[np.cos(angle_radian), 0, np.sin(angle_radian)], [0, 1, 0], [-np.sin(angle_radian), 0, np.cos(angle_radian)]])

     # 进行矩阵乘法
     rotated_point = np.dot(rotation_matrix, point)

     return rotated_point


# 输入点的坐标
point_to_rotate = np.array([1, 2, 3])

# 绕y轴逆时针旋转90度
rotated_point = rotate_y_axis(point_to_rotate, 90)

# 创建3D图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 原始点
ax.scatter(point_to_rotate[0], point_to_rotate[1], point_to_rotate[2], c='blue', label='原始点')

# 旋转后的点
ax.scatter(rotated_point[0], rotated_point[1], rotated_point[2], c='red', label='旋转后的点')

# 设置坐标轴标签
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_zlabel('Z轴')

# 设置图例
ax.legend()

# 显示图形
plt.show()

旋转立方体:

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


def plot_cube(ax, vertices, color, label):
    ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2], c=color, marker='o', label=label)

    # 绘制线条
    edges = [[0, 1], [0, 2], [0, 4], [1, 3], [1, 5], [2, 3], [2, 6], [3, 7], [4, 5], [4, 6], [5, 7], [6, 7]]

    for edge in edges:
        ax.plot([vertices[edge[0], 0], vertices[edge[1], 0]], [vertices[edge[0], 1], vertices[edge[1], 1]], [vertices[edge[0], 2], vertices[edge[1], 2]], color=color)


def visualize_rotation(cube_vertices, rotated_cube_vertices):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 可视化旋转前的立方体
    plot_cube(ax, cube_vertices, color='b', label='Original Cube')
    ax.set_title('Original Cube')
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
    ax.set_zlabel('Z-axis')
    ax.legend()

    plt.show()

    # 清空图形
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 可视化旋转后的立方体
    plot_cube(ax, rotated_cube_vertices, color='r', label='Rotated Cube')
    ax.set_title('Rotated Cube')
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
    ax.set_zlabel('Z-axis')
    ax.legend()

    plt.show()


# 定义立方体的顶点坐标
cube_vertices = np.array([[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]])

# 定义绕Y轴的旋转矩阵(示例中旋转角度为45度)
rotation_angle = np.pi / 4
rotation_matrix = np.array([[np.cos(rotation_angle), 0, np.sin(rotation_angle)], [0, 1, 0], [-np.sin(rotation_angle), 0, np.cos(rotation_angle)]])

# 将旋转矩阵应用到立方体的顶点
rotated_cube_vertices = np.dot(rotation_matrix, cube_vertices.T).T

# 可视化旋转前后的效果
visualize_rotation(cube_vertices, rotated_cube_vertices)