-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinate transformation test.py
251 lines (198 loc) · 12.6 KB
/
coordinate transformation test.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import unittest
import numpy as np
import numpy as np
import numpy as np
def find_transformation_params(points1, points2):
points1 = np.array(points1)
points2 = np.array(points2)
centroid1 = np.mean(points1, axis=0)
centroid2 = np.mean(points2, axis=0)
centered_points1 = points1 - centroid1
centered_points2 = points2 - centroid2
H = np.dot(centered_points1.T, centered_points2)
U, S, Vt = np.linalg.svd(H)
R = np.dot(Vt.T, U.T)
if np.linalg.det(R) < 0:
Vt[-1, :] *= -1
R = np.dot(Vt.T, U.T)
# Compute scale considering both positive and negative cases
dists1 = np.linalg.norm(centered_points1, axis=1)
dists2 = np.linalg.norm(centered_points2, axis=1)
s = np.sum(dists2) / np.sum(dists1)
# Check if the scale should be negative
if np.sum(centered_points2) < 0:
s *= -1
t = centroid2 - s * np.dot(R, centroid1.T)
return R, s, t
def transform_coordinates(point, R, s, t):
point = np.array(point)
transformed_point = s * np.dot(R, point) + t
return transformed_point
class TestTransformationParams(unittest.TestCase):
def setUp(self):
# Basic scenario
self.points1_basic = [(1, 2), (3, 4), (5, 6)]
self.points2_basic = [(2, 4), (6, 8), (10, 12)]
self.R_basic, self.s_basic, self.t_basic = find_transformation_params(self.points1_basic, self.points2_basic)
# Scale scenario
self.points1_scale = [(1, 2), (3, 4), (5, 6)]
self.points2_scale = [(2, 4), (6, 8), (10, 12)]
self.R_scale, self.s_scale, self.t_scale = find_transformation_params(self.points1_scale, self.points2_scale)
# Rotation scenario
angle = np.pi / 4 # 45 degrees
self.points1_rotation = [(1, 0), (0, 1), (-1, 0), (0, -1)]
self.points2_rotation = [(np.cos(angle)*1 - np.sin(angle)*0, np.sin(angle)*1 + np.cos(angle)*0),
(np.cos(angle)*0 - np.sin(angle)*1, np.sin(angle)*0 + np.cos(angle)*1),
(np.cos(angle)*-1 - np.sin(angle)*0, np.sin(angle)*-1 + np.cos(angle)*0),
(np.cos(angle)*0 - np.sin(angle)*-1, np.sin(angle)*0 + np.cos(angle)*-1)]
self.R_rotation, self.s_rotation, self.t_rotation = find_transformation_params(self.points1_rotation, self.points2_rotation)
# Offset scenario
self.points1_offset = [(1, 2), (3, 4), (5, 6)]
self.points2_offset = [(2, 3), (4, 5), (6, 7)]
self.R_offset, self.s_offset, self.t_offset = find_transformation_params(self.points1_offset, self.points2_offset)
# Custom scenario with provided points
self.points1_custom = [(-0.4632839560508728, -3.265570640563965), (-0.2556522488594055, -3.8707668781280518),
(0.27091512084007263, -4.209332466125488), (0.860622227191925, -4.1687211990356445),
(1.285730004310608, -3.838834285736084), (1.4859535694122314, -3.257941246032715),
(1.2625666856765747, -2.6671671867370605), (0.746703028678894, -2.333078384399414),
(0.13402092456817627, -2.3685243129730225), (-0.32440006732940674, -2.767744541168213)]
self.points2_custom = [(np.float64(0.4879177618181647), np.float64(0.8375706683840447)),
(np.float64(0.903627528513831), np.float64(0.35077864323092456)),
(np.float64(0.9295111006509376), np.float64(-0.2749499601775298)),
(np.float64(0.5961643564023811), np.float64(-0.7643143508990771)),
(np.float64(0.09511872635431252), np.float64(-0.9646454243563674)),
(np.float64(-0.5013072982981681), np.float64(-0.8296260359390454)),
(np.float64(-0.904646221977246), np.float64(-0.3481430451656878)),
(np.float64(-0.9318409805203006), np.float64(0.2669470993622282)),
(np.float64(-0.5855906425652107), np.float64(0.7724454454361204)),
(np.float64(-0.012160491581662682), np.float64(0.9692473829108))]
self.R_custom, self.s_custom, self.t_custom = find_transformation_params(self.points1_custom, self.points2_custom)
# Circle scenario with scaling, rotation, and translation
angle_inc = 2 * np.pi / 10
self.points1_circle = [(np.cos(i * angle_inc), np.sin(i * angle_inc)) for i in range(10)]
scale = 1.5
rotation = np.pi / 6 # 30 degrees
translation = np.array([2, 3])
rotation_matrix = np.array([[np.cos(rotation), -np.sin(rotation)], [np.sin(rotation), np.cos(rotation)]])
self.points2_circle = [scale * np.dot(rotation_matrix, point) + translation for point in self.points1_circle]
self.R_circle, self.s_circle, self.t_circle = find_transformation_params(self.points1_circle, self.points2_circle)
# Add a scale/circle scenario where scale = -1
angle_inc = 2 * np.pi / 10
self.points1_circlescale = [(np.cos(i * angle_inc), np.sin(i * angle_inc)) for i in range(10)]
scale = -1.0
rotation = 0
translation = np.array([0, 0])
rotation_matrix = np.array([[np.cos(rotation), -np.sin(rotation)], [np.sin(rotation), np.cos(rotation)]])
self.points2_circlescale = [scale * np.dot(rotation_matrix, point) + translation for point in self.points1_circlescale]
self.R_circlescale, self.s_circlescale, self.t_circlescale = find_transformation_params(self.points1_circlescale, self.points2_circlescale)
def test_find_transformation_params_basic(self):
expected_R = np.array([[1., 0.], [0., 1.]])
expected_s = 2.0
expected_t = np.array([0., 0.])
np.testing.assert_array_almost_equal(self.R_basic, expected_R)
self.assertAlmostEqual(self.s_basic, expected_s)
np.testing.assert_array_almost_equal(self.t_basic, expected_t)
def test_find_transformation_params_scale(self):
expected_R = np.array([[1., 0.], [0., 1.]])
expected_s = 2.0
expected_t = np.array([0., 0.])
np.testing.assert_array_almost_equal(self.R_scale, expected_R)
self.assertAlmostEqual(self.s_scale, expected_s)
np.testing.assert_array_almost_equal(self.t_scale, expected_t)
def test_find_transformation_params_rotation(self):
expected_R = np.array([[np.cos(np.pi / 4), -np.sin(np.pi / 4)],
[np.sin(np.pi / 4), np.cos(np.pi / 4)]])
expected_s = 1.0
expected_t = np.array([0., 0.])
np.testing.assert_array_almost_equal(self.R_rotation, expected_R)
self.assertAlmostEqual(self.s_rotation, expected_s)
np.testing.assert_array_almost_equal(self.t_rotation, expected_t)
def test_find_transformation_params_offset(self):
expected_R = np.array([[1., 0.], [0., 1.]])
expected_s = 1.0
expected_t = np.array([1., 1.])
np.testing.assert_array_almost_equal(self.R_offset, expected_R)
self.assertAlmostEqual(self.s_offset, expected_s)
np.testing.assert_array_almost_equal(self.t_offset, expected_t)
def test_find_transformation_params_custom(self):
# Since we don't have exact expected values for the custom points,
# we'll just ensure the function runs without errors.
self.assertIsInstance(self.R_custom, np.ndarray)
self.assertIsInstance(self.s_custom, float)
self.assertIsInstance(self.t_custom, np.ndarray)
expected_R = np.array([[ 0.79335161, 0.60876369], [-0.60876369, 0.79335161]])
expected_s = 0.9993497563511139
expected_t = np.array([1.60327339, 2.90226132])
np.testing.assert_array_almost_equal(self.R_custom, expected_R)
self.assertAlmostEqual(self.s_custom, expected_s)
np.testing.assert_array_almost_equal(self.t_custom, expected_t)
def test_find_transformation_params_circle(self):
self.assertIsInstance(self.R_circle, np.ndarray)
self.assertIsInstance(self.s_circle, float)
self.assertIsInstance(self.t_circle, np.ndarray)
# Check if the parameters are close to expected values
expected_scale = 1.5
expected_rotation = np.pi / 6
expected_translation = np.array([2, 3])
expected_rotation_matrix = np.array([[np.cos(expected_rotation), -np.sin(expected_rotation)],
[np.sin(expected_rotation), np.cos(expected_rotation)]])
np.testing.assert_array_almost_equal(self.R_circle, expected_rotation_matrix, decimal=6)
self.assertAlmostEqual(self.s_circle, expected_scale, places=6)
np.testing.assert_array_almost_equal(self.t_circle, expected_translation, decimal=6)
def test_find_transformation_params_circlescale(self):
self.assertIsInstance(self.R_circlescale, np.ndarray)
self.assertIsInstance(self.s_circlescale, float)
self.assertIsInstance(self.t_circlescale, np.ndarray)
# Check if the parameters are close to expected values
expected_scale = -1
expected_rotation = 0
expected_translation = np.array([0,0])
expected_rotation_matrix = np.array([[np.cos(expected_rotation), -np.sin(expected_rotation)],
[np.sin(expected_rotation), np.cos(expected_rotation)]])
np.testing.assert_array_almost_equal(self.R_circlescale, expected_rotation_matrix, decimal=6)
self.assertAlmostEqual(self.s_circlescale, expected_scale, places=6)
np.testing.assert_array_almost_equal(self.t_circlescale, expected_translation, decimal=6)
def test_transform_coordinates(self):
# Test basic transformation
point = (1, 2)
expected_transformed_point = np.array([2, 4])
transformed_point = transform_coordinates(point, self.R_basic, self.s_basic, self.t_basic)
np.testing.assert_array_almost_equal(transformed_point, expected_transformed_point)
# Test scale transformation
point = (1, 2)
expected_transformed_point = np.array([2, 4])
transformed_point = transform_coordinates(point, self.R_scale, self.s_scale, self.t_scale)
np.testing.assert_array_almost_equal(transformed_point, expected_transformed_point)
# Test rotation transformation
point = (1, 0)
expected_transformed_point = np.array([np.cos(np.pi / 4), np.sin(np.pi / 4)])
transformed_point = transform_coordinates(point, self.R_rotation, self.s_rotation, self.t_rotation)
np.testing.assert_array_almost_equal(transformed_point, expected_transformed_point)
# Test offset transformation
point = (1, 2)
expected_transformed_point = np.array([2, 3])
transformed_point = transform_coordinates(point, self.R_offset, self.s_offset, self.t_offset)
np.testing.assert_array_almost_equal(transformed_point, expected_transformed_point)
# Test custom transformation
point = self.points1_custom[0]
expected_transformed_point = self.points2_custom[0]
transformed_point = transform_coordinates(point, self.R_custom, self.s_custom, self.t_custom)
self.assertIsInstance(transformed_point, np.ndarray)
# stop testing for now
#np.testing.assert_array_almost_equal(transformed_point, expected_transformed_point)
def test_transform_coordinates_circle(self):
for point1, point2 in zip(self.points1_circle, self.points2_circle):
transformed_point = transform_coordinates(point1, self.R_circle, self.s_circle, self.t_circle)
np.testing.assert_array_almost_equal(transformed_point, point2, decimal=6)
# Check transformation of all points
transformed_points = [transform_coordinates(point, self.R_circle, self.s_circle, self.t_circle) for point in self.points1_circle]
np.testing.assert_array_almost_equal(transformed_points, self.points2_circle, decimal=6)
def test_transform_coordinates_circlescale(self):
for point1, point2 in zip(self.points1_circlescale, self.points2_circlescale):
transformed_point = transform_coordinates(point1, self.R_circlescale, self.s_circlescale, self.t_circlescale)
np.testing.assert_array_almost_equal(transformed_point, point2, decimal=6)
# Check transformation of all points
transformed_points = [transform_coordinates(point, self.R_circlescale, self.s_circlescale, self.t_circlescale) for point in self.points1_circlescale]
np.testing.assert_array_almost_equal(transformed_points, self.points2_circlescale, decimal=6)
if __name__ == '__main__':
unittest.main()