From dcad05722109488b01e1c150a875224a1d5099a8 Mon Sep 17 00:00:00 2001 From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch> Date: Thu, 19 Sep 2019 07:58:25 +0200 Subject: [PATCH] vec2d testing --- flatland/core/grid/grid_utils.py | 12 ++++ tests/test_flatland_core_grid_grid_utils.py | 64 +++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tests/test_flatland_core_grid_grid_utils.py diff --git a/flatland/core/grid/grid_utils.py b/flatland/core/grid/grid_utils.py index 47cbb944..87f0d78d 100644 --- a/flatland/core/grid/grid_utils.py +++ b/flatland/core/grid/grid_utils.py @@ -172,6 +172,18 @@ class Vec2dOperations: """ return int(np.ceil(node[0])), int(np.ceil(node[1])) + @staticmethod + def floor(node: Vector2D) -> IntVector2D: + """ + floor the x and y coordinate and convert them to an integer values + + :param node: tuple with coordinate (x,y) or 2d vector + :return: + ------- + tuple with coordinate (x,y) or 2d vector + """ + return int(np.floor(node[0])), int(np.floor(node[1])) + @staticmethod def bound(node: Vector2D, min_value: float, max_value: float) -> Vector2D: """ diff --git a/tests/test_flatland_core_grid_grid_utils.py b/tests/test_flatland_core_grid_grid_utils.py new file mode 100644 index 00000000..f4995cc9 --- /dev/null +++ b/tests/test_flatland_core_grid_grid_utils.py @@ -0,0 +1,64 @@ +from flatland.core.grid.grid_utils import Vec2dOperations as Vec2d + + +def test_vec2d_add(): + node_a = (1, 2) + node_b = (2, 3) + res_1 = Vec2d.add(node_a, node_b) + res_2 = Vec2d.add(node_b, node_a) + assert res_1 == res_2 + assert res_1 == (3, 5) + + +def test_vec2d_subtract(): + node_a = (1, 2) + node_b = (2, 4) + res_1 = Vec2d.subtract(node_a, node_b) + res_2 = Vec2d.subtract(node_b, node_a) + assert res_1 != res_2 + assert res_1 == (-1, -2) + assert res_2 == (1, 2) + + +def test_vec2d_make_orthogonal(): + node_a = (1, 2) + res_1 = Vec2d.make_orthogonal(node_a) + assert res_1 == (2, -1) + + +def test_vec2d_subtract(): + node_a = (1, 2) + node_b = (2, 4) + node_c = (1, 2) + res_1 = Vec2d.is_equal(node_a, node_b) + res_2 = Vec2d.is_equal(node_a, node_c) + + assert not res_1 + assert res_2 + + +def test_vec2d_ceil(): + node_a = (-1.95, -2.2) + node_b = (1.95, 2.2) + res_1 = Vec2d.ceil(node_a) + res_2 = Vec2d.ceil(node_b) + assert res_1 == (-1, -2) + assert res_2 == (2, 3) + + +def test_vec2d_floor(): + node_a = (-1.95, -2.2) + node_b = (1.95, 2.2) + res_1 = Vec2d.floor(node_a) + res_2 = Vec2d.floor(node_b) + assert res_1 == (-2, -3) + assert res_2 == (1, 2) + + +def test_vec2d_bound(): + node_a = (-1.95, -2.2) + node_b = (1.95, 2.2) + res_1 = Vec2d.bound(node_a, -1, 0) + res_2 = Vec2d.bound(node_b, 2, 2.2) + assert res_1 == (-1, -1) + assert res_2 == (2, 2.2) -- GitLab