Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add early fail for get_normal_newell when the provided polygon is too small (< 3) #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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} && \
Expand Down
2 changes: 2 additions & 0 deletions cjio/geom_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually not 100% sure with it being here and checking for poly elements. I had it outside before with if len(poly) < 3, but that also felt weird. lmk what would be best

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]) )
Expand Down
57 changes: 57 additions & 0 deletions tests/test_geom_help.py
Original file line number Diff line number Diff line change
@@ -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)
Loading