From 666f60b4ceb01ba6b2abdb4f33c8ebe6c0969608 Mon Sep 17 00:00:00 2001 From: Maarten de Jong Date: Tue, 21 Nov 2023 15:13:48 +0100 Subject: [PATCH 1/2] Add early fail for `get_normal_newell` when the provided polygon is too small (< 3) --- cjio/geom_help.py | 2 ++ tests/test_geom_help.py | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/test_geom_help.py diff --git a/cjio/geom_help.py b/cjio/geom_help.py index b66214c..bf8331e 100755 --- a/cjio/geom_help.py +++ b/cjio/geom_help.py @@ -39,6 +39,8 @@ def get_normal_newell(poly): ne = i + 1 if (ne == len(poly)): ne = 0 + if len(poly[i]) < 3 or len(poly[ne]) < 3: + return n, False n[0] += ( (poly[i][1] - poly[ne][1]) * (poly[i][2] + poly[ne][2]) ) n[1] += ( (poly[i][2] - poly[ne][2]) * (poly[i][0] + poly[ne][0]) ) n[2] += ( (poly[i][0] - poly[ne][0]) * (poly[i][1] + poly[ne][1]) ) diff --git a/tests/test_geom_help.py b/tests/test_geom_help.py new file mode 100644 index 0000000..404942b --- /dev/null +++ b/tests/test_geom_help.py @@ -0,0 +1,57 @@ +import typing as t + +import numpy as np +import numpy.typing as npt +import pytest + +from cjio.geom_help import get_normal_newell + + +@pytest.mark.parametrize( + ["poly", "expected_normal"], + [ + ( + [ + [2195013, 353200, 12283], + [2195013, 353200, 8680], + [2182302, 347931, 8680], + [2182302, 347931, 12159], + [2182302, 347931, 12178], + ], + np.array([-0.38292729, 0.92377848, 0.0]), + ), + ( + [ + [2203406, 332904, 12622], + [2203406, 332904, 8680], + [2204954, 333543, 8680], + [2204954, 333543, 12223], + [2204954, 333543, 12584], + ], + np.array([0.38156054, -0.92434385, 0.0]), + ), + ], +) +def test_get_normal_valid_poly(poly: t.List[t.List[int]], expected_normal: npt.NDArray[t.Any]) -> None: + normal, success = get_normal_newell(poly=poly) + assert success + np.testing.assert_almost_equal(actual=normal, desired=expected_normal) + + +@pytest.mark.parametrize( + ["poly", "expected_normal"], + [ + ( + [[1041, 1009, 1025, 1054, 1087]], + np.array([0.0, 0.0, 0.0]), + ), + ( + [[[1099, 1098]]], + np.array([0.0, 0.0, 0.0]), + ), + ], +) +def test_get_normal_invalid_poly(poly: t.List[t.List[int]], expected_normal: npt.NDArray[t.Any]) -> None: + normal, success = get_normal_newell(poly=poly) + assert not success + np.testing.assert_almost_equal(actual=normal, desired=expected_normal) From 234a7adc81ef4b94a972a4147fc2b44b13188441 Mon Sep 17 00:00:00 2001 From: Maarten de Jong Date: Tue, 21 Nov 2023 15:44:05 +0100 Subject: [PATCH 2/2] Update `cjvalpy` to 0.4.1 in Dockerfile to avoid build issue with 0.3.0 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index f7a18b1..e9fa62b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ RUN curl https://sh.rustup.rs -sSf | sh -s -- -y ENV PATH="/root/.cargo/bin:$PATH" RUN pip install maturin -ARG CJVALPY_VERSION="0.3.2" +ARG CJVALPY_VERSION="0.4.1" RUN curl -L -o cjvalpy.tar.gz https://github.com/cityjson/cjvalpy/archive/refs/tags/${CJVALPY_VERSION}.tar.gz && \ tar -xvf cjvalpy.tar.gz && \ cd cjvalpy-${CJVALPY_VERSION} && \