Skip to content

Commit ab41155

Browse files
committed
[modify] transform
1 parent 15399f9 commit ab41155

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

transform.py

+42-28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# 回転の座標変換
33
import numpy as np
44
import math
5+
6+
57
# 2D case
68
def Rz2D(theta):
79
mat = np.array([[math.cos(theta), -math.sin(theta)], [math.sin(theta), math.cos(theta)]])
@@ -11,60 +13,72 @@ def Rz2D(theta):
1113

1214
# 3D case
1315
def Rx(theta):
14-
mat = np.array([[1,0,0],[0,math.cos(theta),-math.sin(theta)],[0,math.sin(theta),math.cos(theta)]])
16+
mat = np.array([[1, 0, 0], [0, math.cos(theta), -math.sin(theta)], [0, math.sin(theta), math.cos(theta)]])
1517
return mat
1618

19+
1720
def Ry(theta):
18-
mat = np.array([[math.cos(theta),0,math.sin(theta)],[0,1,0],[-math.sin(theta),0,math.cos(theta)]])
21+
mat = np.array([[math.cos(theta), 0, math.sin(theta)], [0, 1, 0], [-math.sin(theta), 0, math.cos(theta)]])
1922
return mat
2023

24+
2125
def Rz(theta):
22-
mat = np.array([[math.cos(theta),-math.sin(theta),0],[math.sin(theta),math.cos(theta),0],[0,0,1]])
26+
mat = np.array([[math.cos(theta), -math.sin(theta), 0], [math.sin(theta), math.cos(theta), 0], [0, 0, 1]])
2327
return mat
2428

29+
2530
def rpy2R(r, p, y):
26-
r = np.dot(Ry(p),Rx(r))
27-
mat = np.dot(Rz(y),r)
31+
r = np.dot(Ry(p), Rx(r))
32+
mat = np.dot(Rz(y), r)
2833
return mat
2934

30-
def quaternion2R(q):#(w,x,y,z)
31-
r = np.zeros((3,3))
32-
r[0,0]=q[0]**2+q[1]**2-q[2]**2-q[3]**2
33-
r[0,1]=2*(q[1]*q[2]-q[3]*q[0])
34-
r[0,2]=2*(q[3]*q[1]+q[2]*q[0])
35-
r[1,0]=2*(q[1]*q[2]+q[3]*q[0])
36-
r[1,1]=q[0]**2+q[2]**2-q[3]**2-q[1]**2
37-
r[1,2]=2*(q[2]*q[3]-q[1]*q[0])
38-
r[2,0]=2*(q[3]*q[1]-q[2]*q[0])
39-
r[2,1]=2*(q[2]*q[3]+q[1]*q[0])
40-
r[2,2]=q[0]**2+q[3]**2-q[1]**2-q[2]**2
35+
36+
def quaternion2R(q): # (w,x,y,z)
37+
r = np.zeros((3, 3))
38+
r[0, 0] = q[0] ** 2 + q[1] ** 2 - q[2] ** 2 - q[3] ** 2
39+
r[0, 1] = 2 * (q[1] * q[2] - q[3] * q[0])
40+
r[0, 2] = 2 * (q[3] * q[1] + q[2] * q[0])
41+
r[1, 0] = 2 * (q[1] * q[2] + q[3] * q[0])
42+
r[1, 1] = q[0] ** 2 + q[2] ** 2 - q[3] ** 2 - q[1] ** 2
43+
r[1, 2] = 2 * (q[2] * q[3] - q[1] * q[0])
44+
r[2, 0] = 2 * (q[3] * q[1] - q[2] * q[0])
45+
r[2, 1] = 2 * (q[2] * q[3] + q[1] * q[0])
46+
r[2, 2] = q[0] ** 2 + q[3] ** 2 - q[1] ** 2 - q[2] ** 2
4147
return r
4248

49+
4350
def R2quaternion(r):
4451
q = []
45-
q[0] = math.sqrt(1+r[0,0]+r[1,1]+r[2,2])
46-
q[1] = (r[2,1]-r[1,2])/4/q[0]
47-
q[2] = (r[0,2]-r[2,0])/4/q[0]
48-
q[3] = (r[1,0]-r[0,1])/4/q[0]
52+
q[0] = math.sqrt(1 + r[0, 0] + r[1, 1] + r[2, 2])
53+
q[1] = (r[2, 1] - r[1, 2]) / 4 / q[0]
54+
q[2] = (r[0, 2] - r[2, 0]) / 4 / q[0]
55+
q[3] = (r[1, 0] - r[0, 1]) / 4 / q[0]
4956
return q
5057

58+
5159
def quaternion2rpy(q):
52-
roll = np.arctan2(2.0 * (q[2]*q[3] + q[0]*q[1]), q[0]**2 - q[1]**2 - q[2]**2 + q[3]**2)
53-
pitch = np.arcsin(2.0 * (q[0]*q[2] - q[1]*q[3]))
54-
yaw = np.arctan2(2.0 * (q[1]*q[2] + q[0]*q[3]), q[0]**2 + q[1]**2 - q[2]**2 - q[3]**2)
60+
roll = np.arctan2(2.0 * (q[2] * q[3] + q[0] * q[1]), q[0] ** 2 - q[1] ** 2 - q[2] ** 2 + q[3] ** 2)
61+
pitch = np.arcsin(2.0 * (q[0] * q[2] - q[1] * q[3]))
62+
yaw = np.arctan2(2.0 * (q[1] * q[2] + q[0] * q[3]), q[0] ** 2 + q[1] ** 2 - q[2] ** 2 - q[3] ** 2)
5563
return roll, pitch, yaw
5664

65+
5766
def rpy2quaternion(roll, pitch, yaw):
5867
cosRoll = np.cos(roll / 2.0)
5968
sinRoll = np.sin(roll / 2.0)
6069
cosPitch = np.cos(pitch / 2.0)
6170
sinPitch = np.sin(pitch / 2.0)
6271
cosYaw = np.cos(yaw / 2.0)
6372
sinYaw = np.sin(yaw / 2.0)
64-
q0 = cosRoll * cosPitch * cosYaw - sinRoll * sinPitch * sinYaw
65-
q1 = sinRoll * cosPitch * cosYaw + cosRoll * sinPitch * sinYaw
66-
q2 = cosRoll * sinPitch * cosYaw - sinRoll * cosPitch * sinYaw
67-
q3 = cosRoll * cosPitch * sinYaw + sinRoll * sinPitch * cosYaw
73+
# ZYX オイラー角
74+
q0 = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw
75+
q1 = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw
76+
q2 = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw
77+
q3 = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw
78+
# XYZオイラー角
79+
# q0 = cosRoll * cosPitch * cosYaw - sinRoll * sinPitch * sinYaw
80+
# q1 = sinRoll * cosPitch * cosYaw + cosRoll * sinPitch * sinYaw
81+
# q2 = cosRoll * sinPitch * cosYaw - sinRoll * cosPitch * sinYaw
82+
# q3 = cosRoll * cosPitch * sinYaw + sinRoll * sinPitch * cosYaw
6883
q = np.array([q0, q1, q2, q3])
6984
return q
70-

0 commit comments

Comments
 (0)