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

Improved meshbuilder's mesh handling #37

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 7 additions & 13 deletions src/rod/builder/primitives.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
import os
lorycontixd marked this conversation as resolved.
Show resolved Hide resolved
import pathlib

import trimesh
Expand Down Expand Up @@ -64,6 +65,7 @@ def _geometry(self) -> rod.Geometry:
class MeshBuilder(PrimitiveBuilder):
mesh_path: str | pathlib.Path
scale: NDArray
is_empty: bool = False
lorycontixd marked this conversation as resolved.
Show resolved Hide resolved

def __post_init__(self) -> None:
"""
Expand All @@ -74,20 +76,12 @@ def __post_init__(self) -> None:
AssertionError: If the scale is not a 3D vector.
TypeError: If the mesh_path is not a str or pathlib.Path.
"""
mesh_path = pathlib.Path(self.mesh_path)
Copy link
Member

Choose a reason for hiding this comment

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

Last nit, this attribute could already be a pathlib.Path object:

Suggested change
mesh_path = pathlib.Path(self.mesh_path)
mesh_path = self.mesh_path if isinstance(self.mesh_path, pathlib.Path) else pathlib.Path(self.mesh_path)

Copy link
Member

Choose a reason for hiding this comment

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

Done in e1a70b0.

if not mesh_path.is_file():
raise FileNotFoundError(f"Mesh file not found at {self.mesh_path}")

if isinstance(self.mesh_path, str):
extension = pathlib.Path(self.mesh_path).suffix
elif isinstance(self.mesh_path, pathlib.Path):
extension = self.mesh_path.suffix
else:
raise TypeError(
f"Expected str or pathlib.Path for mesh_path, got {type(self.mesh_path)}"
)

self.mesh: trimesh.base.Trimesh = trimesh.load(
str(self.mesh_path),
force="mesh",
file_type=extension,
self.mesh: trimesh.base.Trimesh = trimesh.load_mesh(
file_obj=mesh_path,
)

assert self.scale.shape == (
Expand Down
14 changes: 14 additions & 0 deletions tests/test_meshbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ def test_builder_creation_custom_mesh():
), f"{builder.mesh.moment_inertia} != {mesh.moment_inertia}"

assert builder.mesh.volume == mesh.volume, f"{builder.mesh.volume} != {mesh.volume}"


def test_builder_empty_mesh():
with tempfile.TemporaryDirectory() as tmp:
with tempfile.NamedTemporaryFile(suffix=".stl", dir=tmp, delete=False) as fp:

builder = MeshBuilder(
name="test_mesh",
mesh_path=fp.name,
mass=1.0,
scale=np.array([1.0, 1.0, 1.0]),
)

assert builder.is_empty == True, f"builder.is_empty is {builder.is_empty} != True"
lorycontixd marked this conversation as resolved.
Show resolved Hide resolved
Loading