-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_utils.py
More file actions
30 lines (22 loc) · 738 Bytes
/
math_utils.py
File metadata and controls
30 lines (22 loc) · 738 Bytes
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
import math
"""
import sympy
w, x, y, z = sympy.symbols('w x y z')
q = sympy.Quaternion(w, x, y, z)
q.set_norm(1)
project_gravity = sympy.Quaternion.rotate_point((0, 0, -1), q.inverse())
gx = 2 * w * y - 2 * x * z
gy = -2 * w * x - 2 * y * z
gz = -w * w + x * x + y * y - z * z
assert project_gravity == (gx, gy, gz)
"""
def project_gravity(quaternion: 'list[float]'):
w, x, y, z = quaternion # assume normalized
gx = 2 * w * y - 2 * x * z
gy = -2 * w * x - 2 * y * z
gz = -w * w + x * x + y * y - z * z
return [gx, gy, gz]
def wrap_to_pi(x: float):
# wrap ℝ to [-π, π] while preserving cos(x) and sin(x)
# (x ± math.pi) % (2 * math.pi) ± math.pi
return math.atan2(math.sin(x), math.cos(x))