HugoVoxx commited on
Commit
4e5ad97
·
verified ·
1 Parent(s): 54f445b

Delete ag4masses/alphageometry/numericals_test.py

Browse files
ag4masses/alphageometry/numericals_test.py DELETED
@@ -1,313 +0,0 @@
1
- # Copyright 2023 DeepMind Technologies Limited
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
-
16
- """Unit testing for the geometry numericals code."""
17
-
18
- import unittest
19
-
20
- from absl.testing import absltest
21
- import numericals as nm
22
-
23
- np = nm.np
24
-
25
- unif = nm.unif
26
- Point = nm.Point
27
- Line = nm.Line
28
- Circle = nm.Circle
29
- HalfLine = nm.HalfLine
30
-
31
- line_circle_intersection = nm.line_circle_intersection
32
- line_line_intersection = nm.line_line_intersection
33
-
34
- check_coll = nm.check_coll
35
- check_eqangle = nm.check_eqangle
36
-
37
- random_points = nm.random_points
38
- ang_between = nm.ang_between
39
- head_from = nm.head_from
40
-
41
-
42
- class NumericalTest(unittest.TestCase):
43
-
44
- def test_sketch_ieq_triangle(self):
45
- a, b, c = nm.sketch_ieq_triangle([])
46
- self.assertAlmostEqual(a.distance(b), b.distance(c))
47
- self.assertAlmostEqual(c.distance(a), b.distance(c))
48
-
49
- def test_sketch_2l1c(self):
50
- p = nm.Point(0.0, 0.0)
51
- pi = np.pi
52
- anga = unif(-0.4 * pi, 0.4 * pi)
53
- a = Point(np.cos(anga), np.sin(anga))
54
- angb = unif(0.6 * pi, 1.4 * pi)
55
- b = Point(np.cos(angb), np.sin(angb))
56
-
57
- angc = unif(anga + 0.05 * pi, angb - 0.05 * pi)
58
- c = Point(np.cos(angc), np.sin(angc)) * unif(0.2, 0.8)
59
-
60
- x, y, z, i = nm.sketch_2l1c([a, b, c, p])
61
- self.assertTrue(check_coll([x, c, a]))
62
- self.assertTrue(check_coll([y, c, b]))
63
- self.assertAlmostEqual(z.distance(p), 1.0)
64
- self.assertTrue(check_coll([p, i, z]))
65
- self.assertTrue(Line(i, x).is_perp(Line(c, a)))
66
- self.assertTrue(Line(i, y).is_perp(Line(c, b)))
67
- self.assertAlmostEqual(i.distance(x), i.distance(y))
68
- self.assertAlmostEqual(i.distance(x), i.distance(z))
69
-
70
- def test_sketch_3peq(self):
71
- a, b, c = random_points(3)
72
- x, y, z = nm.sketch_3peq([a, b, c])
73
-
74
- self.assertTrue(check_coll([a, b, x]))
75
- self.assertTrue(check_coll([a, c, y]))
76
- self.assertTrue(check_coll([b, c, z]))
77
- self.assertTrue(check_coll([x, y, z]))
78
- self.assertAlmostEqual(z.distance(x), z.distance(y))
79
-
80
- def test_sketch_aline(self):
81
- a, b, c, d, e = random_points(5)
82
- ex = nm.sketch_aline([a, b, c, d, e])
83
- self.assertIsInstance(ex, HalfLine)
84
- self.assertEqual(ex.tail, e)
85
- x = ex.head
86
- self.assertAlmostEqual(ang_between(b, a, c), ang_between(e, d, x))
87
-
88
- def test_sketch_amirror(self):
89
- a, b, c = random_points(3)
90
- bx = nm.sketch_amirror([a, b, c])
91
- self.assertIsInstance(bx, HalfLine)
92
- assert bx.tail == b
93
- x = bx.head
94
-
95
- ang1 = ang_between(b, a, c)
96
- ang2 = ang_between(b, c, x)
97
- self.assertAlmostEqual(ang1, ang2)
98
-
99
- def test_sketch_bisect(self):
100
- a, b, c = random_points(3)
101
- line = nm.sketch_bisect([a, b, c])
102
- self.assertAlmostEqual(b.distance(line), 0.0)
103
-
104
- l = a.perpendicular_line(line)
105
- x = line_line_intersection(l, Line(b, c))
106
- self.assertAlmostEqual(a.distance(line), x.distance(line))
107
-
108
- d, _ = line_circle_intersection(line, Circle(b, radius=1))
109
- ang1 = ang_between(b, a, d)
110
- ang2 = ang_between(b, d, c)
111
- self.assertAlmostEqual(ang1, ang2)
112
-
113
- def test_sketch_bline(self):
114
- a, b = random_points(2)
115
- l = nm.sketch_bline([a, b])
116
- self.assertTrue(Line(a, b).is_perp(l))
117
- self.assertAlmostEqual(a.distance(l), b.distance(l))
118
-
119
- def test_sketch_cc_tangent(self):
120
- o = Point(0.0, 0.0)
121
- w = Point(1.0, 0.0)
122
-
123
- ra = unif(0.0, 0.6)
124
- rb = unif(0.4, 1.0)
125
-
126
- a = unif(0.0, np.pi)
127
- b = unif(0.0, np.pi)
128
-
129
- a = o + ra * Point(np.cos(a), np.sin(a))
130
- b = w + rb * Point(np.sin(b), np.cos(b))
131
-
132
- x, y, z, t = nm.sketch_cc_tangent([o, a, w, b])
133
- xy = Line(x, y)
134
- zt = Line(z, t)
135
- self.assertAlmostEqual(o.distance(xy), o.distance(a))
136
- self.assertAlmostEqual(o.distance(zt), o.distance(a))
137
- self.assertAlmostEqual(w.distance(xy), w.distance(b))
138
- self.assertAlmostEqual(w.distance(zt), w.distance(b))
139
-
140
- def test_sketch_circle(self):
141
- a, b, c = random_points(3)
142
- circle = nm.sketch_circle([a, b, c])
143
- self.assertAlmostEqual(circle.center.distance(a), 0.0)
144
- self.assertAlmostEqual(circle.radius, b.distance(c))
145
-
146
- def test_sketch_e5128(self):
147
- b = Point(0.0, 0.0)
148
- c = Point(0.0, 1.0)
149
- ang = unif(-np.pi / 2, 3 * np.pi / 2)
150
- d = head_from(c, ang, 1.0)
151
- a = Point(unif(0.5, 2.0), 0.0)
152
-
153
- e, g = nm.sketch_e5128([a, b, c, d])
154
- ang1 = ang_between(a, b, d)
155
- ang2 = ang_between(e, a, g)
156
- self.assertAlmostEqual(ang1, ang2)
157
-
158
- def test_sketch_eq_quadrangle(self):
159
- a, b, c, d = nm.sketch_eq_quadrangle([])
160
- self.assertAlmostEqual(a.distance(d), c.distance(b))
161
- ac = Line(a, c)
162
- assert ac.diff_side(b, d), (ac(b), ac(d))
163
- bd = Line(b, d)
164
- assert bd.diff_side(a, c), (bd(a), bd(c))
165
-
166
- def test_sketch_eq_trapezoid(self):
167
- a, b, c, d = nm.sketch_eq_trapezoid([])
168
- assert Line(a, b).is_parallel(Line(c, d))
169
- self.assertAlmostEqual(a.distance(d), b.distance(c))
170
-
171
- def test_sketch_eqangle3(self):
172
- points = random_points(5)
173
- x = nm.sketch_eqangle3(points).sample_within(points)[0]
174
- a, b, d, e, f = points
175
- self.assertTrue(check_eqangle([x, a, x, b, d, e, d, f]))
176
-
177
- def test_sketch_eqangle2(self):
178
- a, b, c = random_points(3)
179
- x = nm.sketch_eqangle2([a, b, c])
180
- ang1 = ang_between(a, b, x)
181
- ang2 = ang_between(c, x, b)
182
- self.assertAlmostEqual(ang1, ang2)
183
-
184
- def test_sketch_edia_quadrangle(self):
185
- a, b, c, d = nm.sketch_eqdia_quadrangle([])
186
- assert Line(a, c).diff_side(b, d)
187
- assert Line(b, d).diff_side(a, c)
188
- self.assertAlmostEqual(a.distance(c), b.distance(d))
189
-
190
- def test_sketch_isos(self):
191
- a, b, c = nm.sketch_isos([])
192
- self.assertAlmostEqual(a.distance(b), a.distance(c))
193
- self.assertAlmostEqual(ang_between(b, a, c), ang_between(c, b, a))
194
-
195
- def test_sketch_quadrange(self):
196
- a, b, c, d = nm.sketch_quadrangle([])
197
- self.assertTrue(Line(a, c).diff_side(b, d))
198
- self.assertTrue(Line(b, d).diff_side(a, c))
199
-
200
- def test_sketch_r_trapezoid(self):
201
- a, b, c, d = nm.sketch_r_trapezoid([])
202
- self.assertTrue(Line(a, b).is_perp(Line(a, d)))
203
- self.assertTrue(Line(a, b).is_parallel(Line(c, d)))
204
- self.assertTrue(Line(a, c).diff_side(b, d))
205
- self.assertTrue(Line(b, d).diff_side(a, c))
206
-
207
- def test_sketch_r_triangle(self):
208
- a, b, c = nm.sketch_r_triangle([])
209
- self.assertTrue(Line(a, b).is_perp(Line(a, c)))
210
-
211
- def test_sketch_rectangle(self):
212
- a, b, c, d = nm.sketch_rectangle([])
213
- self.assertTrue(Line(a, b).is_perp(Line(b, c)))
214
- self.assertTrue(Line(b, c).is_perp(Line(c, d)))
215
- self.assertTrue(Line(c, d).is_perp(Line(d, a)))
216
-
217
- def test_sketch_reflect(self):
218
- a, b, c = random_points(3)
219
- x = nm.sketch_reflect([a, b, c])
220
- self.assertTrue(Line(a, x).is_perp(Line(b, c)))
221
- self.assertAlmostEqual(x.distance(Line(b, c)), a.distance(Line(b, c)))
222
-
223
- def test_sketch_risos(self):
224
- a, b, c = nm.sketch_risos([])
225
- self.assertAlmostEqual(a.distance(b), a.distance(c))
226
- self.assertTrue(Line(a, b).is_perp(Line(a, c)))
227
-
228
- def test_sketch_rotaten90(self):
229
- a, b = random_points(2)
230
- x = nm.sketch_rotaten90([a, b])
231
- self.assertAlmostEqual(a.distance(x), a.distance(b))
232
- self.assertTrue(Line(a, x).is_perp(Line(a, b)))
233
- d = Point(0.0, 0.0)
234
- e = Point(0.0, 1.0)
235
- f = Point(1.0, 0.0)
236
- self.assertAlmostEqual(ang_between(d, e, f), ang_between(a, b, x))
237
-
238
- def test_sketch_rotatep90(self):
239
- a, b = random_points(2)
240
- x = nm.sketch_rotatep90([a, b])
241
- self.assertAlmostEqual(a.distance(x), a.distance(b))
242
- self.assertTrue(Line(a, x).is_perp(Line(a, b)))
243
- d = Point(0.0, 0.0)
244
- e = Point(0.0, 1.0)
245
- f = Point(1.0, 0.0)
246
- self.assertAlmostEqual(ang_between(d, f, e), ang_between(a, b, x))
247
-
248
- def test_sketch_s_angle(self):
249
- a, b = random_points(2)
250
- y = unif(0.0, np.pi)
251
- bx = nm.sketch_s_angle([a, b, y / np.pi * 180])
252
- self.assertIsInstance(bx, HalfLine)
253
- self.assertEqual(bx.tail, b)
254
- x = bx.head
255
-
256
- d = Point(1.0, 0.0)
257
- e = Point(0.0, 0.0)
258
- f = Point(np.cos(y), np.sin(y))
259
- self.assertAlmostEqual(ang_between(e, d, f), ang_between(b, a, x))
260
-
261
- def test_sketch_shift(self):
262
- a, b, c = random_points(3)
263
- x = nm.sketch_shift([a, b, c])
264
- self.assertTrue((b - a).close(x - c))
265
-
266
- def test_sketch_square(self):
267
- a, b = random_points(2)
268
- c, d = nm.sketch_square([a, b])
269
- self.assertTrue(Line(a, b).is_perp(Line(b, c)))
270
- self.assertTrue(Line(b, c).is_perp(Line(c, d)))
271
- self.assertTrue(Line(c, d).is_perp(Line(d, a)))
272
- self.assertAlmostEqual(a.distance(b), b.distance(c))
273
-
274
- def test_sketch_isquare(self):
275
- a, b, c, d = nm.sketch_isquare([])
276
- self.assertTrue(Line(a, b).is_perp(Line(b, c)))
277
- self.assertTrue(Line(b, c).is_perp(Line(c, d)))
278
- self.assertTrue(Line(c, d).is_perp(Line(d, a)))
279
- self.assertAlmostEqual(a.distance(b), b.distance(c))
280
-
281
- def test_sketch_trapezoid(self):
282
- a, b, c, d = nm.sketch_trapezoid([])
283
- self.assertTrue(Line(a, b).is_parallel(Line(c, d)))
284
- self.assertTrue(Line(a, c).diff_side(b, d))
285
- self.assertTrue(Line(b, d).diff_side(a, c))
286
-
287
- def test_sketch_triangle(self):
288
- a, b, c = nm.sketch_triangle([])
289
- self.assertFalse(check_coll([a, b, c]))
290
-
291
- def test_sketch_triangle12(self):
292
- a, b, c = nm.sketch_triangle12([])
293
- self.assertAlmostEqual(a.distance(b) * 2, a.distance(c))
294
-
295
- def test_sketch_trisect(self):
296
- a, b, c = random_points(3)
297
- x, y = nm.sketch_trisect([a, b, c])
298
- self.assertAlmostEqual(ang_between(b, a, x), ang_between(b, x, y))
299
- self.assertAlmostEqual(ang_between(b, x, y), ang_between(b, y, c))
300
- self.assertAlmostEqual(ang_between(b, a, x) * 3, ang_between(b, a, c))
301
-
302
- def test_sketch_trisegment(self):
303
- a, b = random_points(2)
304
- x, y = nm.sketch_trisegment([a, b])
305
- self.assertAlmostEqual(
306
- a.distance(x) + x.distance(y) + y.distance(b), a.distance(b)
307
- )
308
- self.assertAlmostEqual(a.distance(x), x.distance(y))
309
- self.assertAlmostEqual(x.distance(y), y.distance(b))
310
-
311
-
312
- if __name__ == '__main__':
313
- absltest.main()