Skip to content

Commit

Permalink
read_obj tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yijiangh committed May 18, 2021
1 parent 8d62c8b commit 763ed8c
Show file tree
Hide file tree
Showing 7 changed files with 1,254 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Unreleased
- Apply `HideOutput` to pybullet IK error printouts in `inverse_kinematics_helper`
- ``motion_planners`` module up-to-date with `commit e6f23053e<https://github.com/caelan/motion-planners/commit/e6f23053e441af091b898b7f56c6fee48223be48>`_.

**Fixed**
- Fixed `read_obj` returns empty dict if obj file does not start with objects (``o object_name``)

0.5.1
----------

Expand Down
22 changes: 17 additions & 5 deletions src/pybullet_planning/interfaces/geometry/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,21 @@ def get_connected_components(vertices, edges):
clusters.append(frozenset(cluster))
return clusters

# TODO consider using: https://github.com/nschloe/meshio ?
# ! doesn't work if there's no line starts with 'o'
def read_obj(path, decompose=True):
"""Read meshes from an obj file.
Parameters
----------
path : string
file path to the obj file
decompose : bool, optional
read meshes as separate component or not, by default True
Returns
-------
``Mesh`` or dict(group name : Mesh)
return dict if decompose = True
"""
mesh = Mesh([], [])
meshes = {}
vertices = []
Expand All @@ -77,6 +89,7 @@ def read_obj(path, decompose=True):
if not tokens:
continue
if tokens[0] == 'o':
# separate different components (groups)
name = tokens[1]
mesh = Mesh([], [])
meshes[name] = mesh
Expand All @@ -91,9 +104,8 @@ def read_obj(path, decompose=True):
mesh.faces.append(face)
if not decompose:
return Mesh(vertices, faces)
#if not meshes:
# # TODO: ensure this still works if no objects
# meshes[None] = mesh
if not meshes:
meshes[None] = mesh
#new_meshes = {}
# TODO: make each triangle a separate object
for name, mesh in meshes.items():
Expand Down
2 changes: 2 additions & 0 deletions src/pybullet_planning/motion_planners/prm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import operator
import time

from ..utils.shared_const import RED
from ..interfaces.geometry.camera import apply_alpha
from .utils import INF, get_pairs, merge_dicts, flatten

__all__ = [
Expand Down
36 changes: 36 additions & 0 deletions tests/test_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import pybullet
import warnings
import pybullet_planning as pp
from pybullet_planning import load_pybullet, connect, wait_for_user, LockRenderer, has_gui, WorldSaver, HideOutput, \
reset_simulation, disconnect, set_camera_pose, has_gui, wait_if_gui
from pybullet_planning import create_obj, create_attachment, Attachment
Expand Down Expand Up @@ -124,3 +125,38 @@ def test_clone_body(viewer, workspace_path, ee_path):
set_pose(c_ee_c, move_pose)

wait_if_gui()

@pytest.mark.create_body
@pytest.mark.parametrize("file_format",[
('obj'),
('stl'),
# ('dae'),
# ('ply'),
]
)
def test_create_body(viewer, file_format):
here = os.path.dirname(__file__)
path = os.path.join(here, 'test_data', 'link_4.' + file_format)
connect(use_gui=viewer)
try:
with HideOutput():
body = create_obj(path)
wait_if_gui('Body created.')
finally:
disconnect()


@pytest.mark.read_obj
def test_create_body(viewer):
here = os.path.dirname(__file__)
path = os.path.join(here, 'test_data', 'box_obstacle.obj')
connect(use_gui=viewer)
try:
mesh = pp.read_obj(path, decompose=False)
assert(len(mesh.vertices)==48)
assert(len(mesh.faces)==6)
meshes = pp.read_obj(path, decompose=True)
assert(len(meshes[None].vertices)==48)
assert(len(meshes[None].faces)==6)
finally:
disconnect()
343 changes: 343 additions & 0 deletions tests/test_data/link_4.dae

Large diffs are not rendered by default.

Loading

0 comments on commit 763ed8c

Please sign in to comment.