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

Add the possibility to draw lines in MeshcatVisualizer #1141

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased Major]

## [Unreleased]
- MeshcatVisualizer: Add the possibility to draw lines in MeshcatVisualizer (https://github.com/robotology/idyntree/pull/1141)

## [10.1.0] - 2024-01-09

Expand Down
47 changes: 42 additions & 5 deletions bindings/python/visualize/meshcat_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
import warnings
from pathlib import Path

class Line:
"""
A class representing a line
"""
def __init__(self, vertices, linewidth=1.0):
self.vertices = vertices
self.linewidth = linewidth

def isBox(self):
return False

def isSphere(self):
return False

def isCylinder(self):
return False

class MeshcatVisualizer:
"""
Expand Down Expand Up @@ -102,7 +118,6 @@ def __load_primitive_geometry(geometry_object: idyn.SolidShape):
(meshcat.geometry.Cylinder,),
{"intrinsic_transform": lambda self: R},
)

if geometry_object.isCylinder():
obj = RotatedCylinder(
geometry_object.asCylinder().getLength(),
Expand All @@ -118,6 +133,9 @@ def __load_primitive_geometry(geometry_object: idyn.SolidShape):
)
elif geometry_object.isSphere():
obj = meshcat.geometry.Sphere(geometry_object.asSphere().getRadius())
elif isinstance(geometry_object, Line):
# transpose the vertices to have the correct shape
obj = meshcat.geometry.PointsGeometry(geometry_object.vertices)
else:
msg = "Unsupported geometry type (%s)" % type(geometry_object)
warnings.warn(msg, category=UserWarning, stacklevel=2)
Expand Down Expand Up @@ -370,8 +388,11 @@ def load_primitive_geometry(self, solid_shape, shape_name="iDynTree", color=None

if isinstance(obj, meshcat.geometry.Object):
self.viewer[viewer_name].set_object(obj)
elif isinstance(obj, meshcat.geometry.Geometry):
material = meshcat.geometry.MeshPhongMaterial()
elif isinstance(obj, meshcat.geometry.Geometry) or isinstance(obj, meshcat.geometry.PointsGeometry):
if not isinstance(obj, meshcat.geometry.PointsGeometry):
material = meshcat.geometry.MeshPhongMaterial()
else:
material = meshcat.geometry.MeshBasicMaterial()

mesh_color = self.__get_color_from_shape(solid_shape, color)
material.color = (
Expand All @@ -385,7 +406,13 @@ def load_primitive_geometry(self, solid_shape, shape_name="iDynTree", color=None
material.transparent = True
material.opacity = float(mesh_color[3])

self.viewer[viewer_name].set_object(obj, material)
# If it is a line we need to manually set the material
if isinstance(obj, meshcat.geometry.PointsGeometry):
material.linewidth = solid_shape.linewidth
self.viewer[viewer_name].set_object(meshcat.geometry.Line(obj, material))
else:
self.viewer[viewer_name].set_object(obj, material)

self.primitive_geometries_names.append(viewer_name)

def set_primitive_geometry_transform(
Expand Down Expand Up @@ -512,6 +539,16 @@ def load_box(self, x, y, z, shape_name="iDynTree", color=None):
solid_shape=box, shape_name=shape_name, color=color
)

def load_line(self, vertices, linewidth, shape_name="iDynTree", color=None):
Copy link
Member

Choose a reason for hiding this comment

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

I realized we could have a more debuggable default shape name (like "idyntree-line" or similar), anyhow this is not a problem just for the line.

if vertices.shape[0] != 3:
msg = "The vertices must be a 3xN matrix."
warnings.warn(msg, category=UserWarning, stacklevel=2)
return
line = Line(vertices=vertices, linewidth=linewidth)
self.load_primitive_geometry(
solid_shape=line, shape_name=shape_name, color=color
)

def delete(self, shape_name="iDynTree"):
if self.__primitive_geometry_exists(shape_name):
self.viewer[shape_name].delete()
Expand All @@ -525,5 +562,5 @@ def delete(self, shape_name="iDynTree"):
del self.traversal[shape_name]
del self.link_pos[shape_name]
else:
msg = "The object named: " + name + " does not exist."
msg = "The object named: " + shape_name + " does not exist."
warnings.warn(msg, category=UserWarning, stacklevel=2)
Loading