Skip to content

Commit

Permalink
minor tweaks & extra tests to cover new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
dfremont committed Dec 12, 2024
1 parent 568a6e3 commit ca508ea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/scenic/core/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/scenic/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/core/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion tests/core/test_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

0 comments on commit ca508ea

Please sign in to comment.