|
15 | 15 | from burnman.utils.math import independent_row_indices |
16 | 16 | from burnman.utils.math import array_to_rational_matrix |
17 | 17 | from burnman.utils.math import complete_basis, generate_complete_basis |
| 18 | +from burnman.utils.plotting import visual_center_of_polygon |
18 | 19 |
|
19 | 20 |
|
20 | 21 | class test_utils(BurnManTest): |
@@ -258,6 +259,47 @@ def test_generate_complete_basis(self): |
258 | 259 | c2 = generate_complete_basis(incomplete_basis, array) |
259 | 260 | np.testing.assert_allclose(c, c2, rtol=1e-5) |
260 | 261 |
|
| 262 | + def test_visual_center_square(self): |
| 263 | + square = [np.array([[0, 0], [10, 0], [10, 10], [0, 10]])] |
| 264 | + center = visual_center_of_polygon(square, precision=0.01) |
| 265 | + self.assertTrue(np.allclose(center, [5, 5], atol=0.1)) |
| 266 | + |
| 267 | + def test_visual_center_with_distance(self): |
| 268 | + square = [np.array([[0, 0], [10, 0], [10, 10], [0, 10]])] |
| 269 | + center, dist = visual_center_of_polygon( |
| 270 | + square, precision=0.01, with_distance=True |
| 271 | + ) |
| 272 | + self.assertTrue(np.allclose(center, [5, 5], atol=0.1)) |
| 273 | + self.assertTrue(abs(dist - 5.0) < 0.2) |
| 274 | + |
| 275 | + def test_visual_center_narrow_rectangle(self): |
| 276 | + rect = [np.array([[0, 0], [100, 0], [100, 1], [0, 1]])] |
| 277 | + center, dist = visual_center_of_polygon( |
| 278 | + rect, precision=0.01, with_distance=True |
| 279 | + ) |
| 280 | + self.assertTrue(np.allclose(center[1], 0.5, atol=0.05)) |
| 281 | + self.assertTrue(dist < 1.0) |
| 282 | + |
| 283 | + def test_visual_center_invalid_input_shape(self): |
| 284 | + invalid = [np.array([[1, 2, 3]])] |
| 285 | + with self.assertRaises(ValueError): |
| 286 | + visual_center_of_polygon(invalid) |
| 287 | + |
| 288 | + def test_visual_center_tiny_square(self): |
| 289 | + tiny = [np.array([[0, 0], [0.001, 0], [0.001, 0.001], [0, 0.001]])] |
| 290 | + center = visual_center_of_polygon(tiny) |
| 291 | + self.assertTrue(np.allclose(center, [0.0005, 0.0005], atol=1e-4)) |
| 292 | + |
| 293 | + def test_visual_center_long_square(self): |
| 294 | + tiny = [np.array([[0, 0], [1, 0], [1, 0.001], [0, 0.001]])] |
| 295 | + center = visual_center_of_polygon(tiny) |
| 296 | + self.assertTrue(np.allclose(center, [0.5, 0.0005], atol=1e-4)) |
| 297 | + |
| 298 | + def test_visual_center_area_zero(self): |
| 299 | + tiny = [np.array([[0, 0], [0.01, 0.1], [0, 0]])] |
| 300 | + center = visual_center_of_polygon(tiny) |
| 301 | + self.assertTrue(np.allclose(center, [0.005, 0.05], atol=1e-4)) |
| 302 | + |
261 | 303 |
|
262 | 304 | if __name__ == "__main__": |
263 | 305 | unittest.main() |
0 commit comments