diff --git a/surfa/mesh/mesh.py b/surfa/mesh/mesh.py index f99bcbe..59b26c2 100644 --- a/surfa/mesh/mesh.py +++ b/surfa/mesh/mesh.py @@ -531,31 +531,35 @@ def face_to_vertex_overlay(self, overlay, method='mean'): raise ValueError(f'expected overlay to have {self.nfaces} points to match ' f'the number of mesh faces, but instead got {overlay.shape[0]}') overlay = cast_overlay(overlay) - + shape = (self.nvertices, overlay.nframes) + # Scalar overlays are stored as (nfaces,), whereas the accumulation + # buffer always has an explicit frame axis. Restore that axis before + # the indexed reductions so that the values broadcast exactly. + values = np.asarray(overlay).reshape(self.nfaces, overlay.nframes) method = str(method).lower() if method == 'mean': buffer = np.zeros(shape) - np.add.at(buffer, self.faces[:, 0], overlay) - np.add.at(buffer, self.faces[:, 1], overlay) - np.add.at(buffer, self.faces[:, 2], overlay) + np.add.at(buffer, self.faces[:, 0], values) + np.add.at(buffer, self.faces[:, 1], values) + np.add.at(buffer, self.faces[:, 2], values) buffer /= np.bincount(self.faces.flat).reshape(-1, 1) elif method == 'max': buffer = np.full(shape, overlay.min(), dtype=overlay.dtype) - np.maximum.at(buffer, self.faces[:, 0], overlay) - np.maximum.at(buffer, self.faces[:, 1], overlay) - np.maximum.at(buffer, self.faces[:, 2], overlay) + np.maximum.at(buffer, self.faces[:, 0], values) + np.maximum.at(buffer, self.faces[:, 1], values) + np.maximum.at(buffer, self.faces[:, 2], values) elif method == 'min': buffer = np.full(shape, overlay.max(), dtype=overlay.dtype) - np.minimum.at(buffer, self.faces[:, 0], overlay) - np.minimum.at(buffer, self.faces[:, 1], overlay) - np.minimum.at(buffer, self.faces[:, 2], overlay) + np.minimum.at(buffer, self.faces[:, 0], values) + np.minimum.at(buffer, self.faces[:, 1], values) + np.minimum.at(buffer, self.faces[:, 2], values) elif method == 'sum': buffer = np.zeros(shape, dtype=overlay.dtype) - np.add.at(buffer, self.faces[:, 0], overlay) - np.add.at(buffer, self.faces[:, 1], overlay) - np.add.at(buffer, self.faces[:, 2], overlay) + np.add.at(buffer, self.faces[:, 0], values) + np.add.at(buffer, self.faces[:, 1], values) + np.add.at(buffer, self.faces[:, 2], values) else: raise ValueError(f'unknown method `{method}`, expected one of: mean/min/max/sum') diff --git a/test/test_mesh_overlay.py b/test/test_mesh_overlay.py new file mode 100644 index 0000000..1b3791a --- /dev/null +++ b/test/test_mesh_overlay.py @@ -0,0 +1,43 @@ +import numpy as np +import pytest +import surfa as sf + + +def _octahedron(): + vertices = np.array([ + [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1], + ], dtype=float) + faces = np.array([ + [0, 2, 4], [2, 1, 4], [1, 3, 4], [3, 0, 4], + [2, 0, 5], [1, 2, 5], [3, 1, 5], [0, 3, 5], + ]) + return sf.Mesh(vertices, faces) + + +def _reference(mesh, values, method): + """Per-vertex reduction over incident faces, computed with a plain loop.""" + reduce = {'mean': np.mean, 'sum': np.sum, 'max': np.max, 'min': np.min}[method] + out = [] + for v in range(mesh.nvertices): + incident = np.any(mesh.faces == v, axis=1) + out.append(reduce(values[incident], axis=0)) + return np.asarray(out) + + +@pytest.mark.parametrize('method', ['mean', 'sum', 'max', 'min']) +def test_face_to_vertex_overlay_single_frame(method): + mesh = _octahedron() + values = np.arange(mesh.nfaces, dtype=float) + result = mesh.face_to_vertex_overlay(sf.Overlay(values), method=method) + assert result.nframes == 1 + assert result.shape[0] == mesh.nvertices + np.testing.assert_allclose(np.asarray(result).ravel(), _reference(mesh, values, method)) + + +@pytest.mark.parametrize('method', ['mean', 'sum', 'max', 'min']) +def test_face_to_vertex_overlay_multi_frame(method): + mesh = _octahedron() + values = np.stack([np.arange(mesh.nfaces), 2.0 * np.arange(mesh.nfaces)], axis=1) + result = mesh.face_to_vertex_overlay(sf.Overlay(values), method=method) + assert result.shape == (mesh.nvertices, 2) + np.testing.assert_allclose(np.asarray(result), _reference(mesh, values, method))