Commit 6e080682 authored by Gabriele Buondonno's avatar Gabriele Buondonno
Browse files

[meshcat_visualizer] Load geometric primitives

parent 11ca5882
from .. import libpinocchio_pywrap as pin
from ..shortcuts import buildModelsFromUrdf, createDatas
from ..rpy import npToTuple
from . import BaseVisualizer
......@@ -39,6 +40,56 @@ class MeshcatVisualizer(BaseVisualizer):
if loadModel:
self.loadViewerModel()
def loadPrimitive(self, geometry_object):
import meshcat.geometry
# Cylinders need to be rotated
R = np.array([[1., 0., 0., 0.],
[0., 0., -1., 0.],
[0., 1., 0., 0.],
[0., 0., 0., 1.]])
RotatedCylinder = type("RotatedCylinder", (meshcat.geometry.Cylinder,), {"intrinsic_transform": lambda self: R })
geom = geometry_object.geometry
if isinstance(geom, hppfcl.Capsule):
obj = RotatedCylinder(2. * geom.halfLength, geom.radius)
elif isinstance(geom, hppfcl.Cylinder):
obj = RotatedCylinder(2. * geom.halfLength, geom.radius)
elif isinstance(geom, hppfcl.Box):
obj = meshcat.geometry.Box(npToTuple(2. * geom.halfSide))
elif isinstance(geom, hppfcl.Sphere):
obj = meshcat.geometry.Sphere(geom.radius)
else:
msg = "Unsupported geometry type for %s (%s)" % (geometry_object.name, type(geom) )
warnings.warn(msg, category=UserWarning, stacklevel=2)
obj = None
return obj
def loadMesh(self, geometry_object):
import meshcat.geometry
# Mesh path is empty if Pinocchio is built without HPP-FCL bindings
if geometry_object.meshPath == "":
return None
# Get file type from filename extension.
_, file_extension = os.path.splitext(geometry_object.meshPath)
if file_extension.lower() == ".dae":
obj = meshcat.geometry.DaeMeshGeometry.from_file(geometry_object.meshPath)
elif file_extension.lower() == ".obj":
obj = meshcat.geometry.ObjMeshGeometry.from_file(geometry_object.meshPath)
elif file_extension.lower() == ".stl":
obj = meshcat.geometry.StlMeshGeometry.from_file(geometry_object.meshPath)
else:
msg = "Unknown mesh file format: {}.".format(geometry_object.meshPath)
warnings.warn(msg, category=UserWarning, stacklevel=2)
obj = None
return obj
def loadViewerGeometryObject(self, geometry_object, geometry_type, color=None):
"""Load a single geometry object"""
......@@ -48,27 +99,13 @@ class MeshcatVisualizer(BaseVisualizer):
try:
if WITH_HPP_FCL_BINDINGS and isinstance(geometry_object.geometry, hppfcl.ShapeBase):
# Geometric primitives
return
obj = self.loadPrimitive(geometry_object)
else:
# Mesh
# Path is empty if Pinocchio is built without HPP-FCL bindings
if geometry_object.meshPath == "":
return
# Get file type from filename extension.
_, file_extension = os.path.splitext(geometry_object.meshPath)
if file_extension.lower() == ".dae":
obj = meshcat.geometry.DaeMeshGeometry.from_file(geometry_object.meshPath)
elif file_extension.lower() == ".obj":
obj = meshcat.geometry.ObjMeshGeometry.from_file(geometry_object.meshPath)
elif file_extension.lower() == ".stl":
obj = meshcat.geometry.StlMeshGeometry.from_file(geometry_object.meshPath)
else:
msg = "Unknown mesh file format: {}.".format(geometry_object.meshPath)
warnings.warn(msg, category=UserWarning, stacklevel=2)
return
obj = self.loadMesh(geometry_object)
if obj is None:
return
except Exception as e:
msg = "Error while loading geometry object: {}.".format(geometry_object.name)
msg = "Error while loading geometry object: %s\nError message:\n%s" % (geometry_object.name, e)
warnings.warn(msg, category=UserWarning, stacklevel=2)
return
material = meshcat.geometry.MeshPhongMaterial()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment