File size: 1,470 Bytes
55ed985
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np

def quaternion_rotation_x_counterclockwise(angle_degrees):
    angle_radians = np.radians(angle_degrees)
    w = np.cos(angle_radians / 2)
    x = np.sin(angle_radians / 2)
    y, z = 0.0, 0.0
    return np.array([x, y, z, w]).round(4).tolist()


def quaternion_rotation_y_counterclockwise(angle_degrees):
    angle_radians = np.radians(angle_degrees)
    w = np.cos(angle_radians / 2)
    y = np.sin(angle_radians / 2)
    x, z = 0.0, 0.0
    return np.array([x, y, z, w]).round(4).tolist()


def quaternion_rotation_z_counterclockwise(angle_degrees):
    angle_radians = np.radians(angle_degrees)
    w = np.cos(angle_radians / 2)
    z = np.sin(angle_radians / 2)
    x, y = 0.0, 0.0
    return np.array([x, y, z, w]).round(4).tolist()


def quaternion_multiply(q1, q2):
    x1, y1, z1, w1 = q1
    x2, y2, z2, w2 = q2
    w = w1*w2 - x1*x2 - y1*y2 - z1*z2
    x = w1*x2 + x1*w2 + y1*z2 - z1*y2
    y = w1*y2 - x1*z2 + y1*w2 + z1*x2
    z = w1*z2 + x1*y2 - y1*x2 + z1*w2
    return np.array([w, x, y, z])

    
    
angle = 180

print(f"X轴逆时针旋转{angle}度: {quaternion_rotation_x_counterclockwise(angle)}")
print(f"Y轴逆时针旋转{angle}度: {quaternion_rotation_y_counterclockwise(angle)}")
print(f"Z轴逆时针旋转{angle}度: {quaternion_rotation_z_counterclockwise(angle)}")


q_1 = np.array([1.0, 0.0, 0.0, 0.0]) 
q_2 = np.array([0.0, 0.0, 1.0, 0.0]) 

q_total = quaternion_multiply(q_2, q_1)
print(q_total.round(4).tolist())