-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_alignment.py
214 lines (176 loc) · 6.22 KB
/
test_alignment.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
import pytest
from alitra import Frame, Position, Positions, Transform, align_maps, align_positions
def test_align_positions_translation_only():
expected_rotation = np.array([0, 0, 0])
expected_translation = np.array([1, -1, 0])
robot_frame = Frame("robot")
robot_positions: Positions = Positions.from_array(
np.array(
[
[0, 1, 0],
[1, 1, 0],
[0, 2, 0],
]
),
frame=robot_frame,
)
asset_frame = Frame("asset")
asset_positions: Positions = Positions.from_array(
np.array(
[
[1, 0, 0],
[2, 0, 0],
[1, 1, 0],
]
),
frame=asset_frame,
)
transform: Transform = align_positions(
positions_from=robot_positions, positions_to=asset_positions, rot_axes="xyz"
)
assert np.allclose(expected_rotation, transform.rotation.as_euler("ZYX"))
assert np.allclose(expected_translation, transform.translation.to_array())
def test_align_positions_one_dimension():
expected_rotation = np.array([np.pi / 2, 0, 0])
expected_translation = np.array([0, 0, 0])
robot_frame = Frame("robot")
robot_positions: Positions = Positions.from_array(
np.array(
[
[0, 1, 0],
[0, 0, 0],
[1, 0, 0],
]
),
frame=robot_frame,
)
asset_frame = Frame("asset")
asset_positions: Positions = Positions.from_array(
np.array(
[
[-1, 0, 0],
[0, 0, 0],
[0, 1, 0],
]
),
frame=asset_frame,
)
transform: Transform = align_positions(
positions_from=robot_positions, positions_to=asset_positions, rot_axes="xyz"
)
assert np.allclose(expected_rotation, transform.rotation.as_euler("ZYX"))
assert np.allclose(expected_translation, transform.translation.to_array())
def test_align_positions_three_dimensions():
expected_rotation = np.array([np.pi / 2, np.pi / 4, np.pi / 2])
expected_translation = np.array([0, 0, 0])
hyp = np.sqrt(1 / 2)
robot_frame = Frame("robot")
robot_positions: Positions = Positions.from_array(
np.array(
[
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
]
),
frame=robot_frame,
)
asset_frame = Frame("asset")
asset_positions: Positions = Positions.from_array(
np.array(
[
[0, 0, 1],
[-hyp, -hyp, 1],
[-hyp, -hyp, 0],
]
),
frame=asset_frame,
)
transform: Transform = align_positions(
positions_from=robot_positions, positions_to=asset_positions, rot_axes="xyz"
)
assert np.allclose(expected_rotation, transform.rotation.as_euler("zyx"))
assert np.allclose(expected_translation, transform.translation.to_array())
def test_align_positions_rotation_and_translation():
expected_rotation = np.array([np.pi / 2, 0, 0])
expected_translation = np.array([2, -1, 0])
robot_frame = Frame("robot")
robot_positions: Positions = Positions.from_array(
np.array(
[
[1, 0, 0],
[2, 0, 0],
[1, 1, 0],
]
),
frame=robot_frame,
)
asset_frame = Frame("asset")
asset_positions: Positions = Positions.from_array(
np.array(
[
[2, 0, 0],
[2, 1, 0],
[1, 0, 0],
]
),
frame=asset_frame,
)
transform: Transform = align_positions(
positions_from=robot_positions, positions_to=asset_positions, rot_axes="xyz"
)
assert np.allclose(expected_rotation, transform.rotation.as_euler("ZYX"))
assert np.allclose(expected_translation, transform.translation.to_array())
def test_align_maps(robot_map, asset_map, default_position, robot_frame, asset_frame):
expected_position: Position = Position(80, 10, 0, frame=asset_frame)
transform = align_maps(map_from=robot_map, map_to=asset_map, rot_axes="z")
position_to = transform.transform_position(
positions=default_position,
from_=robot_frame,
to_=asset_frame,
)
assert np.allclose(expected_position.to_array(), position_to.to_array())
assert expected_position.frame == position_to.frame
def test_align_positions_unequal_position_length(robot_frame, asset_frame):
positions_from = Positions.from_array(
np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0]]), frame=robot_frame
)
positions_to = Positions.from_array(
np.array([[1, 0, 0], [1, 0, 0]]), frame=asset_frame
)
with pytest.raises(ValueError):
align_positions(
positions_from=positions_from,
positions_to=positions_to,
rot_axes="xyz",
)
def test_align_positions_not_enough_positions_one_rotation(robot_frame, asset_frame):
positions_from = Positions.from_array(np.array([[1, 0, 0]]), frame=robot_frame)
positions_to = Positions.from_array(np.array([[1, 0, 0]]), frame=asset_frame)
with pytest.raises(ValueError):
align_positions(
positions_from=positions_from, positions_to=positions_to, rot_axes="z"
)
def test_align_positions_not_enough_positions_three_rotations(robot_frame, asset_frame):
positions_from = Positions.from_array(
np.array([[1, 0, 0], [1, 0, 0]]), frame=robot_frame
)
positions_to = Positions.from_array(
np.array([[1, 0, 0], [1, 0, 0]]), frame=asset_frame
)
with pytest.raises(ValueError):
align_positions(
positions_from=positions_from, positions_to=positions_to, rot_axes="xyz"
)
def test_align_positions_outside_rsme_treshold(robot_frame, asset_frame):
positions_from = Positions.from_array(
np.array([[20, 10, 0], [60, 20, 0], [80, 70, 0]]), frame=robot_frame
)
positions_to = Positions.from_array(
np.array([[10, 20, 0], [30, 40, 0], [50, 60, 0]]), frame=asset_frame
)
with pytest.raises(ValueError):
align_positions(
positions_from=positions_from, positions_to=positions_to, rot_axes="z"
)