-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sphere.py
353 lines (289 loc) · 15.6 KB
/
test_sphere.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""
Run the tests by executing, for all test classes:
$ python -m unittest -v test_sphere.py
or
$ python test_sphere.py
or, for individual test classes (sorted as appearing in this file):
$ python -m unittest -v test_sphere.Test_Sphere
$ python -m unittest -v test_sphere.Test_Sphere_get_mse
$ python -m unittest -v test_sphere.Test_Sphere_get_mean_signed_distance
$ python -m unittest -v test_sphere.Test_get_sphere
$ python -m unittest -v test_sphere.Test_get_best_fit_sphere
$ python -m unittest -v test_sphere.Test_get_best_fit_sphere_for_radius_range
"""
import math
import numpy as np
import unittest
from epsilon import epsilon_distance, equal_in_practice, zero_in_practice
from sphere import as_tuple_of_3_floats, get_sphere, get_best_fit_sphere, get_best_fit_sphere_for_radius_range, Sphere
from typing import Final, TypeAlias
class Test_Sphere(unittest.TestCase):
def test_GivenAnEmptyCenter_When_Sphere_ThenExceptionIsRaised(self):
radius: Final = 3.
self.assertRaises(TypeError, Sphere, None, radius)
def test_GivenA2DCenterPoint_When_Sphere_ThenExceptionIsRaised(self):
center_2_d: Final = 1, 3
radius: Final = 3.
self.assertRaises(ValueError, Sphere, center_2_d, radius)
def test_GivenAMissingRadius_When_Sphere_ThenExceptionIsRaised(self):
center: Final = 1, 3, 6
self.assertRaises(TypeError, Sphere, center)
def test_GivenAZeroRadius_When_Sphere_ThenExceptionIsRaised(self):
center: Final = 1, 3, 6
zero_radius: Final = 0
self.assertRaises(ValueError, Sphere, center, zero_radius)
def test_GivenANegativeRadius_When_Sphere_ThenExceptionIsRaised(self):
center: Final = 1, 3, 6
negative_radius: Final = -7
self.assertRaises(ValueError, Sphere, center, negative_radius)
def test_Given2SpheresCreatedEqually_WhenComparison_ThenReturnTrue(self):
center: Final = 1, 3, 6
radius: Final = 3.
self.assertEqual(Sphere(center, radius), Sphere(center, radius))
sphere: Final = Sphere(center, radius)
self.assertEqual(sphere, Sphere(center, radius))
def test_Given2SpheresCreatedNotEqually_WhenComparison_ThenReturnFalse(self):
center_a: Final = 1, 3, 6
radius: Final = 3.
self.assertNotEqual(Sphere(center_a, radius), Sphere(center_a, radius + 1.))
center_b: Final = 4, 3, 3
self.assertNotEqual(Sphere(center_a, radius), Sphere(center_b, radius))
def test_GivenSphereCreatedWithRadiusR_When_get_radius_ThenReturnR(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
# sphere.spy("sphere")
self.assertEqual(sphere.get_radius(), radius)
def test_GivenCircleCreatedWithCenterC_When_get_center_ThenReturnC(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
returned_center: Final = sphere.get_center()
equal_centers: Final = (equal_in_practice(center[0], returned_center[0])
and equal_in_practice(center[1], returned_center[1])
and equal_in_practice(center[2], returned_center[2]))
self.assertTrue(equal_centers)
def test_GivenSphereAndPointEqualToCenterPlusRadiusForZ_When_get_signed_distance_to_surface_ThenReturnZero(self):
center: Final = 1, 2, 3
radius: Final = 8
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([0, 0, radius]))
self.assertTrue(zero_in_practice(sphere.get_signed_distance_to_surface(point)))
def test_GivenSphereAndPointEqualToCenterPlus2RadiusForZ_When_get_signed_distance_to_surface_ThenReturnRadius(self):
center: Final = 1, 2, 3
radius: Final = 8
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([0, 0, 2*radius]))
self.assertTrue(equal_in_practice(sphere.get_signed_distance_to_surface(point), radius))
def test_GivenSphereAndPointEqualToCenterPlusHalfRadiusForZ_When_get_signed_distance_to_surface_ThenReturnMinusHalfRadius(self):
center: Final = 1, 2, 3
radius: Final = 8
half_radius: Final = radius/2
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([0, 0, half_radius]))
self.assertTrue(equal_in_practice(sphere.get_signed_distance_to_surface(point), -half_radius))
def test_GivenSphereAndPointEqualToCenterPlusRadiusForX_When_point_is_on_surface_ThenReturnTrue(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([radius, 0, 0]))
self.assertTrue(sphere.point_is_on_surface(point))
def test_GivenSphereAndPointAlmostEqualToCenterPlusRadiusForX_When_point_is_on_surface_ThenReturnTrue(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([radius + epsilon_distance/2., 0, 0]))
self.assertTrue(sphere.point_is_on_surface(point))
def test_GivenSphereAndPointFarEnoughToCenterPlusRadiusForX_When_point_is_on_surface_ThenReturnFalse(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
point: Final = as_tuple_of_3_floats(center + np.array([radius + 2. * epsilon_distance, 0, 0]))
self.assertFalse(sphere.point_is_on_surface(point))
TupleOf3Floats: TypeAlias = tuple[float, float, float]
def get_sample_points_on_the_surface(sphere: Sphere) -> list[TupleOf3Floats]:
# https://en.wikipedia.org/wiki/List_of_common_coordinate_transformations#From_spherical_coordinates
center: Final = sphere.get_center()
radius: Final = sphere.get_radius()
points: list[TupleOf3Floats] = []
angles_in_degrees: Final = 0, 45, 90, 135, 180, 225, 270, 315
for theta_in_degrees in angles_in_degrees:
theta = math.radians(theta_in_degrees)
for phi_in_degrees in angles_in_degrees:
phi = math.radians(phi_in_degrees)
points.append((
center[0] + radius*math.sin(theta)*math.cos(phi),
center[1] + radius*math.sin(theta)*math.sin(phi),
center[2] + radius*math.cos(theta)))
return points
class Test_Sphere_get_mse(unittest.TestCase):
def test_GivenASphereAndZeroPoints_When_get_mse_ThenExceptionIsRaised(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
self.assertRaises(ValueError, sphere.get_mse, ())
def test_GivenASphereAndPointsOnSurface_When_get_mse_ThenReturn0(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
points: Final = get_sample_points_on_the_surface(sphere)
self.assertTrue(zero_in_practice(sphere.get_mse(points)))
def test_GivenASphereAndPointsOn2xRadius_When_get_mse_ThenReturnRadiusSquared(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
two_radius: Final = 2*radius
points: Final = get_sample_points_on_the_surface(Sphere(center, two_radius))
self.assertTrue(equal_in_practice(sphere.get_mse(points), radius * radius))
class Test_Sphere_get_mean_signed_distance(unittest.TestCase):
def test_GivenASphereAndZeroPoints_When_get_mean_signed_distance_ThenExceptionIsRaised(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
self.assertRaises(ValueError, sphere.get_mean_signed_distance, ())
def test_GivenASphereAndPointsOnSurface_When_get_mean_signed_distance_ThenReturn0(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
points: Final = get_sample_points_on_the_surface(sphere)
self.assertTrue(zero_in_practice(sphere.get_mean_signed_distance(points)))
def test_GivenASphereAndPointsOn2xRadius_When_get_mean_signed_distance_ThenReturnRadius(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
two_radius: Final = 2*radius
points: Final = get_sample_points_on_the_surface(Sphere(center, two_radius))
self.assertTrue(equal_in_practice(sphere.get_mean_signed_distance(points), radius))
def test_GivenASphereAndPointsOnHalfRadius_When_get_mean_signed_distance_ThenReturnMinusHalfRadius(self):
center: Final = 1, 2, 3
radius: Final = 7
sphere: Final = Sphere(center, radius)
half_radius: Final = .5*radius
points: Final = get_sample_points_on_the_surface(Sphere(center, half_radius))
self.assertTrue(equal_in_practice(sphere.get_mean_signed_distance(points), -half_radius))
class Test_get_sphere(unittest.TestCase):
def test_GivenNotExactly4Points_When_get_sphere_ThenExceptionIsRaised(self):
point: Final = 1, 3
self.assertRaises(ValueError, get_sphere, [point])
self.assertRaises(ValueError, get_sphere, [point, point])
self.assertRaises(ValueError, get_sphere, [point, point, point])
self.assertRaises(ValueError, get_sphere, [point, point, point, point, point])
def test_GivenAPointP_4x_When_get_sphere_ThenExceptionIsRaised(self):
point: Final = 1, 3, 6
points: Final = point, point, point, point
self.assertRaises(ArithmeticError, get_sphere, points)
def test_Given4CollinearPoints_When_get_sphere_ThenExceptionIsRaised(self):
delta = 1.5
points: list[TupleOf3Floats] = []
for i in 1, 2, 3, 4:
points.append((i*delta, 0, 0))
self.assertRaises(ArithmeticError, get_sphere, points)
def test_Given4CoplanarPoints_When_get_sphere_ThenExceptionIsRaised(self):
delta: Final = 1.5
points: list[TupleOf3Floats] = []
for i in 1., 2.:
points.append((i*delta, i, 0.))
points.append((i*delta, i + 1., 0.))
self.assertRaises(ArithmeticError, get_sphere, points)
def test_Given4PointsFrom_31_136_12_106_When_get_sphere_ThenReturnTheSitesResult(self):
point_1: Final = -34.1186, 1.389, 8.5034
point_2: Final = -34.3179, 1.3719, -29.432
point_3: Final = -8.7948, -0.1148, -10.462
point_4: Final = -60.527, 5.8305, -10.423
points: Final = point_1, point_2, point_3, point_4
sphere_31_136_12_106 = get_sphere(points)
center_from_site: Final = -21.942809924607257, 113.52343730620188, -10.579341367428881
radius_from_site: Final = 114.39638504793206
sphere_from_site: Final = Sphere(center_from_site, radius_from_site)
self.assertEqual(sphere_31_136_12_106, sphere_from_site)
def test_Given4PointsFrom_39_136_10_106_When_get_sphere_ThenReturnTheSitesResult(self):
point_1: Final = -34.5737, 0.5015, 10.377
point_2: Final = -34.4944, 0.4966, -22.06
point_3: Final = -8.2244, 0.5713, -5.6182
point_4: Final = -60.996, 4.412, -5.7822
points: Final = point_1, point_2, point_3, point_4
sphere_39_136_10_106 = get_sphere(points)
center_from_site: Final = -26.681763623168056, 111.42322500511179, -5.8390596432420425
radius_from_site: Final = 112.37825558460493
sphere_from_site: Final = Sphere(center_from_site, radius_from_site)
self.assertEqual(sphere_39_136_10_106, sphere_from_site)
class Test_get_best_fit_sphere(unittest.TestCase):
def test_GivenLessThan5Points_When_get_best_fit_sphere_ThenExceptionIsRaised(self):
point: Final = 1, 2, 3
center_x_and_z: Final = 0, 0
y_range: Final = 0, 500
radius: Final = 3.4
points: list[TupleOf3Floats] = []
for _ in range(4):
points.append(point)
for use_mse in True, False:
for num_samples in range(4, 10):
self.assertRaises(
ValueError, get_best_fit_sphere, points, center_x_and_z, y_range, radius, use_mse, num_samples)
def test_Given4PointsInTopOfSphereS_When_get_best_fit_sphere_ThenResultIsS(self):
center: Final = 0, 0, 0
radius: Final = 3.4
sphere: Final = Sphere(center, radius)
center_x_and_z: Final = center[0], center[2]
y_range: Final = -1, 3
points: list[TupleOf3Floats] = []
for theta_in_degrees in 30, 60:
theta = math.radians(theta_in_degrees)
for phi_in_degrees in 0, 45, 90, 135, 180, 225, 270, 315:
phi = math.radians(phi_in_degrees)
points.append((
center[0] + radius*math.sin(theta)*math.cos(phi),
center[1] + radius*math.cos(theta),
center[2] + radius*math.sin(theta)*math.sin(phi)))
for use_mse in True, False:
for num_samples in range(4, 10):
result = get_best_fit_sphere(points, center_x_and_z, y_range, radius, use_mse, num_samples)
another_epsilon_distance = 1e-5
self.assertTrue(sphere.__eq__(result, another_epsilon_distance))
def test_Given4PointsInBottomOfSphereS_When_get_best_fit_sphere_ThenResultIsS(self):
center: Final = 0, 0, 0
radius: Final = 3.4
sphere: Final = Sphere(center, radius)
center_x_and_z: Final = center[0], center[2]
y_range: Final = -1, 10
points: list[TupleOf3Floats] = []
for theta_in_degrees in 120, 160:
theta = math.radians(theta_in_degrees)
for phi_in_degrees in 0, 45, 90, 135, 180, 225, 270, 315:
phi = math.radians(phi_in_degrees)
points.append((
center[0] + radius*math.sin(theta)*math.cos(phi),
center[1] + radius*math.cos(theta),
center[2] + radius*math.sin(theta)*math.sin(phi)))
for use_mse in True, False:
for num_samples in range(4, 10):
result = get_best_fit_sphere(points, center_x_and_z, y_range, radius, use_mse, num_samples)
another_epsilon_distance = 1e-5
self.assertTrue(sphere.__eq__(result, another_epsilon_distance))
def test_GivenNPointsAroundSphereS_When_get_best_fit_sphere_ThenResultIsS(self):
center: Final = 0, 0, 0
radius: Final = 3.4
sphere: Final = Sphere(center, radius)
center_x_and_z: Final = center[0], center[2]
y_range: Final = -10, 20
points: Final = get_sample_points_on_the_surface(sphere)
for use_mse in True, False:
for num_samples in range(4, 10):
result = get_best_fit_sphere(points, center_x_and_z, y_range, radius, use_mse, num_samples)
another_epsilon_distance = 1e-4
self.assertTrue(sphere.__eq__(result, another_epsilon_distance))
class Test_get_best_fit_sphere_for_radius_range(unittest.TestCase):
def test_GivenLessThan5Points_When_get_best_fit_sphere_for_radius_range_ThenExceptionIsRaised(self):
point: Final = 1, 2, 3
center_x_and_z: Final = 0, 0
y_range: Final = 0, 500
radius_range: Final = 3.1, 3.9
points: list[TupleOf3Floats] = []
for _ in range(4):
points.append(point)
for use_mse in True, False:
for num_samples in range(4, 10):
self.assertRaises(ValueError, get_best_fit_sphere_for_radius_range, points, center_x_and_z, y_range,
radius_range, use_mse, num_samples)
if __name__ == '__main__':
unittest.main()