-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.go
More file actions
135 lines (111 loc) · 3.72 KB
/
euler.go
File metadata and controls
135 lines (111 loc) · 3.72 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package nvector
import (
"math"
)
// eulerThreshold is a small number used to avoid Euler angle singularities.
// This number was chosen by calculating (math.Nextafter(1, 2) - 1).
const eulerThreshold = 2.220446049250313e-16 * 10
// EulerXYZ is a set of Euler angles in XYZ order.
type EulerXYZ struct {
X, Y, Z float64
}
// EulerZYX is a set of Euler angles in ZYX order.
type EulerZYX struct {
Z, Y, X float64
}
// EulerXYZToRotationMatrix converts Euler angles in XYZ order to a rotation matrix.
//
// See: https://github.com/FFI-no/n-vector/blob/f77f43d18ddb6b8ea4e1a8bb23a53700af965abb/nvector/xyz2R.m
func EulerXYZToRotationMatrix(a EulerXYZ) Matrix {
cz := math.Cos(a.Z)
sz := math.Sin(a.Z)
cy := math.Cos(a.Y)
sy := math.Sin(a.Y)
cx := math.Cos(a.X)
sx := math.Sin(a.X)
return Matrix{
cy * cz, -cy * sz, sy,
sy*sx*cz + cx*sz, -sy*sx*sz + cx*cz, -cy * sx,
-sy*cx*cz + sx*sz, sy*cx*sz + sx*cz, cy * cx,
}
}
// EulerZYXToRotationMatrix converts Euler angles in ZYX order to a rotation matrix.
//
// See: https://github.com/FFI-no/n-vector/blob/f77f43d18ddb6b8ea4e1a8bb23a53700af965abb/nvector/zyx2R.m
func EulerZYXToRotationMatrix(a EulerZYX) Matrix {
cz := math.Cos(a.Z)
sz := math.Sin(a.Z)
cy := math.Cos(a.Y)
sy := math.Sin(a.Y)
cx := math.Cos(a.X)
sx := math.Sin(a.X)
return Matrix{
cz * cy, -sz*cx + cz*sy*sx, sz*sx + cz*sy*cx,
sz * cy, cz*cx + sz*sy*sx, -cz*sx + sz*sy*cx,
-sy, cy * sx, cy * cx,
}
}
// RotationMatrixToEulerXYZ converts a rotation matrix to Euler angles in XYZ order.
//
// See: https://github.com/FFI-no/n-vector/blob/f77f43d18ddb6b8ea4e1a8bb23a53700af965abb/nvector/R2xyz.m
func RotationMatrixToEulerXYZ(r Matrix) EulerXYZ {
// cy is based on as many elements as possible, to average out numerical
// errors. It is selected as the positive square root since y: [-pi/2 pi/2]
cy := math.Sqrt((math.Pow(r.XX, 2) +
math.Pow(r.XY, 2) +
math.Pow(r.YZ, 2) +
math.Pow(r.ZZ, 2)) / 2,
)
var a EulerXYZ
// Check if (close to) Euler angle singularity:
if cy > eulerThreshold {
// Outside singularity:
// atan2: [-pi pi]
a.Z = math.Atan2(-r.XY, r.XX)
a.X = math.Atan2(-r.YZ, r.ZZ)
sy := r.XZ
a.Y = math.Atan2(sy, cy)
} else {
// In singularity (or close to), i.e. y = +pi/2 or -pi/2:
// Selecting y = +-pi/2, with correct sign
a.Y = math.Copysign(math.Pi/2, r.XZ)
// Only the sum/difference of x and z is now given, choosing x = 0:
a.X = 0
// Lower left 2x2 elements of R_AB now only consists of sin_z and cos_z.
// Using the two whose signs are the same for both singularities:
a.Z = math.Atan2(r.YX, r.YY)
}
return a
}
// RotationMatrixToEulerZYX converts a rotation matrix to Euler angles in ZYX order.
//
// See: https://github.com/FFI-no/n-vector/blob/f77f43d18ddb6b8ea4e1a8bb23a53700af965abb/nvector/R2zyx.m
func RotationMatrixToEulerZYX(r Matrix) EulerZYX {
// cy is based on as many elements as possible, to average out numerical
// errors. It is selected as the positive square root since y: [-pi/2 pi/2]
cy := math.Sqrt((math.Pow(r.XX, 2) +
math.Pow(r.YX, 2) +
math.Pow(r.ZY, 2) +
math.Pow(r.ZZ, 2)) / 2,
)
var a EulerZYX
// Check if (close to) Euler angle singularity:
if cy > eulerThreshold {
// Outside singularity:
// atan2: [-pi pi]
a.Z = math.Atan2(r.YX, r.XX)
a.X = math.Atan2(r.ZY, r.ZZ)
sy := -r.ZX
a.Y = math.Atan2(sy, cy)
} else {
// In singularity (or close to), i.e. y = +pi/2 or -pi/2:
// Selecting y = +-pi/2, with correct sign
a.Y = math.Copysign(math.Pi/2, r.ZX)
// Only the sum/difference of x and z is now given, choosing x = 0:
a.X = 0
// Upper right 2x2 elements of R_AB now only consists of sin_z and cos_z.
// Using the two whose signs are the same for both singularities:
a.Z = math.Atan2(-r.XY, r.YY)
}
return a
}