-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
91 lines (67 loc) · 2.35 KB
/
utils.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import numpy as np
#general rotation matrices
def get_R_x(theta):
R = np.array([[1, 0, 0],
[0, np.cos(theta), -np.sin(theta)],
[0, np.sin(theta), np.cos(theta)]])
return R
def get_R_y(theta):
R = np.array([[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]])
return R
def get_R_z(theta):
R = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
return R
#calculate rotation matrix to take A vector to B vector
def Get_R(A,B):
#get unit vectors
uA = A/np.sqrt(np.sum(np.square(A)))
uB = B/np.sqrt(np.sum(np.square(B)))
#get products
dotprod = np.sum(uA * uB)
crossprod = np.sqrt(np.sum(np.square(np.cross(uA,uB)))) #magnitude
#get new unit vectors
u = uA
v = uB - dotprod*uA
v = v/np.sqrt(np.sum(np.square(v)))
w = np.cross(uA, uB)
w = w/np.sqrt(np.sum(np.square(w)))
#get change of basis matrix
C = np.array([u, v, w])
#get rotation matrix in new basis
R_uvw = np.array([[dotprod, -crossprod, 0],
[crossprod, dotprod, 0],
[0, 0, 1]])
#full rotation matrix
R = C.T @ R_uvw @ C
#print(R)
return R
#Same calculation as above using a different formalism
def Get_R2(A, B):
#get unit vectors
uA = A/np.sqrt(np.sum(np.square(A)))
uB = B/np.sqrt(np.sum(np.square(B)))
v = np.cross(uA, uB)
s = np.sqrt(np.sum(np.square(v)))
c = np.sum(uA * uB)
vx = np.array([[0, -v[2], v[1]],
[v[2], 0, -v[0]],
[-v[1], v[0], 0]])
R = np.eye(3) + vx + vx@vx*((1-c)/s**2)
return R
#decomposes given R matrix into rotation along each axis. In this case Rz @ Ry @ Rx
def Decompose_R_ZYX(R):
#decomposes as RzRyRx. Note the order: ZYX <- rotation by x first
thetaz = np.arctan2(R[1,0], R[0,0])
thetay = np.arctan2(-R[2,0], np.sqrt(R[2,1]**2 + R[2,2]**2))
thetax = np.arctan2(R[2,1], R[2,2])
return thetaz, thetay, thetax
def Decompose_R_ZXY(R):
#decomposes as RzRXRy. Note the order: ZXY <- rotation by y first
thetaz = np.arctan2(-R[0,1], R[1,1])
thetay = np.arctan2(-R[2,0], R[2,2])
thetax = np.arctan2(R[2,1], np.sqrt(R[2,0]**2 + R[2,2]**2))
return thetaz, thetay, thetax