From ca508ea56800239a5b102ed30444a2c8c6afec86 Mon Sep 17 00:00:00 2001 From: Daniel Fremont Date: Wed, 11 Dec 2024 20:07:33 -0800 Subject: [PATCH] minor tweaks & extra tests to cover new lines --- src/scenic/core/regions.py | 9 ++++++--- src/scenic/core/utils.py | 5 ++++- tests/core/test_regions.py | 12 ++++++++++++ tests/core/test_shapes.py | 15 ++++++++++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/scenic/core/regions.py b/src/scenic/core/regions.py index 33fc987f8..c541106c2 100644 --- a/src/scenic/core/regions.py +++ b/src/scenic/core/regions.py @@ -835,6 +835,11 @@ def __init__( if isLazy(self): return + if not isinstance(mesh, (trimesh.primitives.Primitive, trimesh.base.Trimesh)): + raise TypeError( + f"Got unexpected mesh parameter of type {type(mesh).__name__}" + ) + # Apply scaling, rotation, and translation, if any if self.rotation is not None: angles = self.rotation._trimeshEulerAngles() @@ -936,9 +941,7 @@ def mesh(self): elif isinstance(mesh, trimesh.base.Trimesh): mesh = mesh.copy(include_visual=False) else: - raise TypeError( - f"Got unexpected mesh parameter of type {type(mesh).__name__}" - ) + assert False, f"mesh of invalid type {type(mesh).__name__}" # Center mesh unless disabled if self.centerMesh: diff --git a/src/scenic/core/utils.py b/src/scenic/core/utils.py index 638f58b47..68ab3c374 100644 --- a/src/scenic/core/utils.py +++ b/src/scenic/core/utils.py @@ -338,7 +338,10 @@ def findMeshInteriorPoint(mesh, num_samples=None): midPt = (surfacePt + endPt) / 2.0 if mesh.contains([midPt])[0]: return midPt - return surfacePt + + # Should never get here with reasonable geometry, but we return a surface + # point just in case. + return surfacePt # pragma: no cover class DefaultIdentityDict: diff --git a/tests/core/test_regions.py b/tests/core/test_regions.py index 03b832e6e..5228e20d9 100644 --- a/tests/core/test_regions.py +++ b/tests/core/test_regions.py @@ -295,6 +295,13 @@ def test_mesh_region_fromFile(getAssetPath): ) +def test_mesh_region_invalid_mesh(): + with pytest.raises(TypeError): + MeshVolumeRegion(42) + with pytest.raises(TypeError): + MeshSurfaceRegion(42) + + def test_mesh_volume_region_zero_dimension(): for dims in ((0, 1, 1), (1, 0, 1), (1, 1, 0)): with pytest.raises(ValueError): @@ -354,6 +361,11 @@ def test_mesh_boundingPolygon(getAssetPath, pytestconfig): assertPolygonsEqual(bp.polygons, poly) shape = MeshShape(BoxRegion(dimensions=(1, 2, 3)).mesh) + r = MeshVolumeRegion(shape.mesh, dimensions=(2, 4, 2), _shape=shape) + bp = r.boundingPolygon + poly = shapely.geometry.Polygon([(-1, 2), (1, 2), (1, -2), (-1, -2)]) + assertPolygonsEqual(bp.polygons, poly) + o = Orientation.fromEuler(0, 0, math.pi / 4) r = MeshVolumeRegion(shape.mesh, dimensions=(2, 4, 2), rotation=o, _shape=shape) bp = r.boundingPolygon diff --git a/tests/core/test_shapes.py b/tests/core/test_shapes.py index 95e257f8f..a27fd6b3d 100644 --- a/tests/core/test_shapes.py +++ b/tests/core/test_shapes.py @@ -3,7 +3,8 @@ import pytest -from scenic.core.shapes import BoxShape, MeshShape +from scenic.core.regions import BoxRegion +from scenic.core.shapes import BoxShape, CylinderShape, MeshShape def test_shape_fromFile(getAssetPath): @@ -21,3 +22,15 @@ def test_invalid_dimension(badDim): BoxShape(dimensions=dims) with pytest.raises(ValueError): BoxShape(scale=badDim) + + +def test_circumradius(): + s = CylinderShape(dimensions=(3, 1, 17)) # dimensions don't matter + assert s._circumradius == pytest.approx(math.sqrt(2) / 2) + + +def test_interiorPoint(): + s = MeshShape(BoxRegion().difference(BoxRegion(dimensions=(0.1, 0.1, 0.1))).mesh) + pt = s._interiorPoint + assert all(-0.5 <= coord <= 0.5 for coord in pt) + assert not all(-0.05 <= coord <= 0.05 for coord in pt)