-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_rotationmutator.py
More file actions
224 lines (194 loc) · 9.86 KB
/
Copy pathtest_rotationmutator.py
File metadata and controls
224 lines (194 loc) · 9.86 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
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
215
216
217
218
219
220
221
222
223
224
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import unittest
from test_util import ScanPointGeneratorTest
from scanpointgenerator.compat import range_
from scanpointgenerator.mutators import RotationMutator
from scanpointgenerator import Point
from pkg_resources import require
require("mock")
from mock import MagicMock
float_error_tolerance = 1e-12
class RandomOffsetMutatorTest(ScanPointGeneratorTest):
def test_init(self):
m = RotationMutator(["x", "y"], 30, [0., 0.])
self.assertEqual(30, m.angle)
def test_init_fails_for_invalid_axes(self):
self.assertRaises(AssertionError, RotationMutator, ["x"], 30, [0.])
self.assertRaises(AssertionError, RotationMutator, ["x", "y", "z"], 30, [0., 0., 0.])
def test_mutate_simple(self):
def point_gen():
for j in range_(10):
for k in range_(10):
pt = Point()
pt.indexes = [j, k]
pt.positions = {"x": j/10., "y": k/10.}
# TODO: generate upper & lower appropriately
pt.lower = {"x": (j-0.5)/10., "y": (k-0.5)/10.}
pt.upper = {"x": (j+0.5)/10., "y": (k+0.5)/10.}
yield pt
m = RotationMutator(["x", "y"], 30, [0., 0.])
original = [p for p in point_gen()]
mutated = [m.mutate(p, i) for i, p in enumerate(point_gen())]
for o, m in zip(original, mutated):
op_x, mp_x = o.positions["x"], m.positions["x"]
op_y, mp_y = o.positions["y"], m.positions["y"]
# TODO: test upper & lower appropriately...how should they be?
ou_x, mu_x = o.upper["x"], m.upper["x"]
ou_y, mu_y = o.upper["y"], m.upper["y"]
ol_x, ml_x = o.lower["x"], m.lower["x"]
ol_y, ml_y = o.lower["y"], m.lower["y"]
# self.assertNotEqual(op_x, mp_x)
# self.assertNotEqual(op_y, mp_y)
self.assertTrue(abs((op_x**2 + op_y**2) - (mp_x**2 + mp_y**2)) < float_error_tolerance)
for i in range(len(original) - 1):
# check distance between consecutive points is preserved
o_step = (original[i + 1].positions["x"] - original[i].positions["x"]) ** 2 + \
(original[i + 1].positions["y"] - original[i].positions["y"]) ** 2
m_step = (mutated[i + 1].positions["x"] - mutated[i].positions["x"]) ** 2 + \
(mutated[i + 1].positions["y"] - mutated[i].positions["y"]) ** 2
self.assertTrue(abs(o_step - m_step) < float_error_tolerance)
# check angle between points preserved
o_dot = (original[i + 1].positions["x"] * original[i].positions["x"]) + \
(original[i + 1].positions["y"] * original[i].positions["y"])
m_dot = (mutated[i + 1].positions["x"] * mutated[i].positions["x"]) + \
(mutated[i + 1].positions["y"] * mutated[i].positions["y"])
self.assertTrue(abs(o_dot - m_dot) < float_error_tolerance)
def test_mutate_cor(self):
def point_gen():
for j in range_(10):
for k in range_(10):
pt = Point()
pt.indexes = [j, k]
pt.positions = {"x": j/10., "y": k/10.}
pt.lower = {"x": (j-0.5)/10., "y": (k-0.5)/10.}
pt.upper = {"x": (j+0.5)/10., "y": (k+0.5)/10.}
yield pt
CoR = [2., 3.]
m = RotationMutator(["x", "y"], 30, CoR)
original = [p for p in point_gen()]
mutated = [m.mutate(p, i) for i, p in enumerate(point_gen())]
o_r = []
m_r = []
o_rel_x = []
o_rel_y = []
m_rel_x = []
m_rel_y = []
for o, m in zip(original, mutated):
op_x, mp_x = o.positions["x"], m.positions["x"]
op_y, mp_y = o.positions["y"], m.positions["y"]
ou_x, mu_x = o.upper["x"], m.upper["x"]
ou_y, mu_y = o.upper["y"], m.upper["y"]
ol_x, ml_x = o.lower["x"], m.lower["x"]
ol_y, ml_y = o.lower["y"], m.lower["y"]
# self.assertNotEqual(op_x, mp_x)
# self.assertNotEqual(op_y, mp_y)
o_rel_x += [op_x - CoR[0]]
o_rel_y += [op_y - CoR[1]]
m_rel_x += [mp_x - CoR[0]]
m_rel_y += [mp_y - CoR[1]]
o_r += [(op_x - CoR[0]) ** 2 + (op_y - CoR[1]) ** 2]
m_r += [(mp_x - CoR[0]) ** 2 + (mp_y - CoR[1]) ** 2]
self.assertTrue(abs(m_r[-1] - o_r[-1]) < float_error_tolerance)
for i in range(len(original) - 1):
# check distance between consecutive points is preserved
o_step = (original[i + 1].positions["x"] - original[i].positions["x"]) ** 2 + \
(original[i + 1].positions["y"] - original[i].positions["y"]) ** 2
m_step = (mutated[i + 1].positions["x"] - mutated[i].positions["x"]) ** 2 + \
(mutated[i + 1].positions["y"] - mutated[i].positions["y"]) ** 2
self.assertTrue(abs(o_step - m_step) < float_error_tolerance)
# check angle between points preserved
o_dot = (o_rel_x[i + 1] * o_rel_x[i]) + (o_rel_y[i + 1] * o_rel_y[i])
m_dot = (m_rel_x[i + 1] * m_rel_x[i]) + (m_rel_y[i + 1] * m_rel_y[i])
self.assertTrue(abs(o_dot - m_dot) < float_error_tolerance)
def test_mutate_90_degrees(self):
def point_gen():
for j in range_(10):
for k in range_(10):
pt = Point()
pt.indexes = [j, k]
pt.positions = {"x": j/10., "y": k/10.}
pt.lower = {"x": (j-0.5)/10., "y": (k-0.5)/10.}
pt.upper = {"x": (j+0.5)/10., "y": (k+0.5)/10.}
yield pt
m = RotationMutator(["x", "y"], 90, [0., 0.])
original = [p for p in point_gen()]
mutated = [m.mutate(p, i) for i, p in enumerate(point_gen())]
for o, m in zip(original, mutated):
op_x, mp_x = o.positions["x"], m.positions["x"]
op_y, mp_y = o.positions["y"], m.positions["y"]
ou_x, mu_x = o.upper["x"], m.upper["x"]
ou_y, mu_y = o.upper["y"], m.upper["y"]
ol_x, ml_x = o.lower["x"], m.lower["x"]
ol_y, ml_y = o.lower["y"], m.lower["y"]
# self.assertNotEqual(op_x, mp_x)
# self.assertNotEqual(op_y, mp_y)
self.assertTrue(abs((op_x**2 + op_y**2) - (mp_x**2 + mp_y**2)) < float_error_tolerance)
# rotate 90 degrees, mp_y = op_x, mp_x = -op_y
self.assertTrue(abs(op_x - mp_y) < float_error_tolerance)
self.assertTrue(abs(op_y + mp_x) < float_error_tolerance)
# check distance between consecutive points is preserved
for i in range(len(original) - 1):
o_step = (original[i + 1].positions["x"] - original[i].positions["x"]) ** 2 + \
(original[i + 1].positions["y"] - original[i].positions["y"]) ** 2
m_step = (mutated[i + 1].positions["x"] - mutated[i].positions["x"]) ** 2 + \
(mutated[i + 1].positions["y"] - mutated[i].positions["y"]) ** 2
self.assertTrue(abs(o_step - m_step) < float_error_tolerance)
def test_mutate_twice_opposite(self):
def point_gen():
for j in range_(10):
for k in range_(10):
pt = Point()
pt.indexes = [j, k]
pt.positions = {"x": j/10., "y": k/10.}
pt.lower = {"x": (j-0.5)/10., "y": (k-0.5)/10.}
pt.upper = {"x": (j+0.5)/10., "y": (k+0.5)/10.}
yield pt
m1 = RotationMutator(["x", "y"], 30, [0., 0.])
m2 = RotationMutator(["x", "y"], -30, [0., 0.])
original = [p for p in point_gen()]
mutated1 = [m1.mutate(p, i) for i, p in enumerate(point_gen())]
mutated2 = [m2.mutate(p, i) for i, p in enumerate(mutated1)]
for o, m in zip(original, mutated2):
op_x, mp_x = o.positions["x"], m.positions["x"]
op_y, mp_y = o.positions["y"], m.positions["y"]
ou_x, mu_x = o.upper["x"], m.upper["x"]
ou_y, mu_y = o.upper["y"], m.upper["y"]
ol_x, ml_x = o.lower["x"], m.lower["x"]
ol_y, ml_y = o.lower["y"], m.lower["y"]
# should be equal within floating point error
self.assertTrue(abs(mp_x - op_x) < float_error_tolerance)
self.assertTrue(abs(mu_x - ou_x) < float_error_tolerance)
self.assertTrue(abs(ml_x - ol_x) < float_error_tolerance)
self.assertTrue(abs(mp_y - op_y) < float_error_tolerance)
self.assertTrue(abs(mu_y - ou_y) < float_error_tolerance)
self.assertTrue(abs(ml_y - ol_y) < float_error_tolerance)
class TestSerialisation(unittest.TestCase):
def setUp(self):
self.l = MagicMock()
self.l_dict = MagicMock()
self.centreOfRotation = [0., 0.]
self.m = RotationMutator(["x", "y"], 45, self.centreOfRotation)
def test_to_dict(self):
self.l.to_dict.return_value = self.l_dict
expected_dict = dict()
expected_dict['typeid'] = "scanpointgenerator:mutator/RotationMutator:1.0"
expected_dict['angle'] = 45
expected_dict['axes'] = ["x", "y"]
expected_dict['centreOfRotation'] = self.centreOfRotation
d = self.m.to_dict()
self.assertEqual(expected_dict, d)
def test_from_dict(self):
_dict = dict()
_dict['angle'] = 45
_dict['axes'] = ["x", "y"]
_dict['centreOfRotation'] = self.centreOfRotation
units_dict = dict()
units_dict['x'] = 'mm'
units_dict['y'] = 'mm'
m = RotationMutator.from_dict(_dict)
self.assertEqual(45, m.angle)
self.assertEqual(self.centreOfRotation, m.centreOfRotation)
if __name__ == "__main__":
unittest.main(verbosity=2)