This repository was archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoordinates.py
More file actions
68 lines (50 loc) · 1.6 KB
/
coordinates.py
File metadata and controls
68 lines (50 loc) · 1.6 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
"""Types and helpers for manipulating coordinates."""
from typing import NamedTuple, Union
import numpy as np
from numpy import arctan2, float64, linalg
AnyFloat = Union[float, float64]
_Cartesian = NamedTuple('Cartesian', (
('x', AnyFloat),
('y', AnyFloat),
('z', AnyFloat),
))
class Cartesian(_Cartesian):
"""Cartesian coordinates."""
__slots__ = ()
def tolist(self):
"""Placeholder helper to ease migration within robotd."""
return list(self)
Spherical = NamedTuple('Spherical', (
('rot_x', AnyFloat),
('rot_y', AnyFloat),
('dist', AnyFloat),
))
LegacyPolar = NamedTuple('LegacyPolar', (
('polar_x', AnyFloat),
('polar_y', AnyFloat),
('dist', AnyFloat),
))
Orientation = NamedTuple('Orientation', (
('rot_x', AnyFloat),
('rot_y', AnyFloat),
('rot_z', AnyFloat),
))
PixelCoordinate = NamedTuple('PixelCoordinate', [('x', AnyFloat), ('y', AnyFloat)])
def cartesian_to_spherical(cartesian: Cartesian) -> Spherical:
"""Convert a Cartesian coordinate into a spherical one."""
x, y, z = cartesian
return Spherical(
rot_x=arctan2(y, z),
rot_y=arctan2(x, z),
dist=linalg.norm(cartesian),
)
def cartesian_to_legacy_polar(cartesian: Cartesian) -> LegacyPolar:
"""
Convert cartesian co-ordinate space to the legacy "polar" space.
This is kept for compatibility only.
"""
cart_x, cart_y, cart_z = tuple(cartesian)
polar_dist = np.linalg.norm(cartesian)
polar_x = np.arctan2(cart_z, cart_x)
polar_y = np.arctan2(cart_z, cart_y)
return LegacyPolar(polar_x, polar_y, polar_dist)