diff --git a/surface_analyses/patches.py b/surface_analyses/patches.py index 3b0c176..f77c2e7 100644 --- a/surface_analyses/patches.py +++ b/surface_analyses/patches.py @@ -18,6 +18,8 @@ def find_patches(faces, should_be_in_patch): patches : list of np.ndarray objects. Each array contains a list of vertex indices that define a patch. """ + faces = np.asarray(faces) + should_be_in_patch = np.asarray(should_be_in_patch) assert len(should_be_in_patch.shape) == 1 assert len(faces.shape) == 2 and faces.shape[1] == 3 n_vertices = should_be_in_patch.shape[0] diff --git a/test/patches_test.py b/test/patches_test.py new file mode 100644 index 0000000..f5a1f96 --- /dev/null +++ b/test/patches_test.py @@ -0,0 +1,35 @@ +import pytest +import numpy as np + +import surface_analyses.surface as surf +import surface_analyses.patches as patches + +@pytest.fixture +def minimal(): + vertices = [[0., 0., 0.], [0., 0., 1.], [0., 1., 1.], [0., 2., 1.], [0., 2., 2.]] + faces = [[0, 1, 2], [2, 3, 4]] + s = surf.Surface(vertices, faces) + s.data['DATA'] = [-2, -1, 0, 1, 2] + return s + +def test_finds_patches_full(minimal): + p = patches.find_patches(minimal.faces, [True, True, True, True, True]) + assert len(p) == 1 + assert np.all(p[0] == [0, 1, 2, 3, 4]) + +def test_finds_patches_separate(minimal): + p = patches.find_patches(minimal.faces, [True, True, False, True, True]) + assert len(p) == 2 + assert np.all(p[0] == [0, 1]) + assert np.all(p[1] == [3, 4]) + area = minimal.vertex_areas() + assert area[p[0]].sum() == 1.0 + +def test_finds_patches_separate(minimal): + p = patches.find_patches(minimal.faces, [True, True, False, True, True]) + assert len(p) == 2 + assert np.all(p[0] == [0, 1]) + assert np.all(p[1] == [3, 4]) + area = minimal.vertex_areas() + assert area[p[0]].sum() == pytest.approx(1/3) + assert area[p[1]].sum() == pytest.approx(1/3) diff --git a/test/surface_test.py b/test/surface_test.py index 50ea511..ba6f8ba 100644 --- a/test/surface_test.py +++ b/test/surface_test.py @@ -41,3 +41,7 @@ def test_ses_gisttools(): out = surf.compute_ses_gisttools(grid, [[0.0, 0.0, 0.0]], [1.0]) # area of a unit sphere is 4*pi = 12.566, but we are not really exact assert np.isclose(out.areas().sum(), 12.5876) + +def test_triangle_area(minimal): + areas = minimal.vertex_areas() + assert np.allclose(areas, [1/6, 1/6, 1/6])