Skip to content

Commit

Permalink
--add semantic visualization in a more formal manner.
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner65 committed Jan 26, 2024
1 parent 7e99fc7 commit f9db344
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions examples/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def __init__(self, sim_settings: Dict[str, Any]) -> None:
self.debug_bullet_draw = False
# draw active contact point debug line visualizations
self.contact_debug_draw = False
# draw semantic region debug visualizations if present
self.semantic_region_debug_draw = False

# cache most recently loaded URDF file for quick-reload
self.cached_urdf = ""

Expand Down Expand Up @@ -176,7 +179,7 @@ def __init__(self, sim_settings: Dict[str, Any]) -> None:
self.replay_renderer_cfg: Optional[ReplayRendererConfiguration] = None
self.replay_renderer: Optional[ReplayRenderer] = None
self.reconfigure_sim()
self.region_colors = {}
self.debug_semantic_colors = {}

# compute NavMesh if not already loaded by the scene.
if (
Expand All @@ -190,55 +193,53 @@ def __init__(self, sim_settings: Dict[str, Any]) -> None:
logger.setLevel("INFO")
self.print_help_text()

def draw_contact_debug(self):
def draw_contact_debug(self, debug_line_render: Any):
"""
This method is called to render a debug line overlay displaying active contact points and normals.
Yellow lines show the contact distance along the normal and red lines show the contact normal at a fixed length.
"""
yellow = mn.Color4.yellow()
red = mn.Color4.red()
cps = self.sim.get_physics_contact_points()
self.sim.get_debug_line_render().set_line_width(1.5)
debug_line_render.set_line_width(1.5)
camera_position = self.render_camera.render_camera.node.absolute_translation
# only showing active contacts
active_contacts = (x for x in cps if x.is_active)
for cp in active_contacts:
# red shows the contact distance
self.sim.get_debug_line_render().draw_transformed_line(
debug_line_render.draw_transformed_line(
cp.position_on_b_in_ws,
cp.position_on_b_in_ws
+ cp.contact_normal_on_b_in_ws * -cp.contact_distance,
red,
)
# yellow shows the contact normal at a fixed length for visualization
self.sim.get_debug_line_render().draw_transformed_line(
debug_line_render.draw_transformed_line(
cp.position_on_b_in_ws,
# + cp.contact_normal_on_b_in_ws * cp.contact_distance,
cp.position_on_b_in_ws + cp.contact_normal_on_b_in_ws * 0.1,
yellow,
)
self.sim.get_debug_line_render().draw_circle(
debug_line_render.draw_circle(
translation=cp.position_on_b_in_ws,
radius=0.005,
color=yellow,
normal=camera_position - cp.position_on_b_in_ws,
)

def draw_region(self, region: Any, color: Optional[mn.Color4] = None) -> None:
def draw_region_debug(self, debug_line_render: Any) -> None:
"""
Draw a region wireframe.
Draw the semantic region wireframes.
"""

if color is None:
color = mn.Color4.magenta()

debug_line_render = self.sim.get_debug_line_render()
for edge in region.volume_edges:
debug_line_render.draw_transformed_line(
edge[0],
edge[1],
color,
)
for region in self.sim.semantic_scene.regions:
color = self.debug_semantic_colors.get(region.id, mn.Color4.magenta())
for edge in region.volume_edges:
debug_line_render.draw_transformed_line(
edge[0],
edge[1],
color,
)

def debug_draw(self):
"""
Expand All @@ -248,20 +249,18 @@ def debug_draw(self):
render_cam = self.render_camera.render_camera
proj_mat = render_cam.projection_matrix.__matmul__(render_cam.camera_matrix)
self.sim.physics_debug_draw(proj_mat)

debug_line_render = self.sim.get_debug_line_render()
if self.contact_debug_draw:
self.draw_contact_debug()
self.draw_contact_debug(debug_line_render)

if len(self.region_colors) == 0:
for region in self.sim.semantic_scene.regions:
self.region_colors[region.id] = mn.Color4(
mn.Vector3(np.random.random(3))
)
for region in self.sim.semantic_scene.regions:
# if region.contains(self.default_agent.get_state().position+mn.Vector3(0,0.3,0)):
# print(region.id)
if region.id in self.region_colors:
self.draw_region(region, color=self.region_colors[region.id])
# self.draw_region(region)
if self.semantic_region_debug_draw:
if len(self.debug_semantic_colors) != len(self.sim.semantic_scene.regions):
for region in self.sim.semantic_scene.regions:
self.debug_semantic_colors[region.id] = mn.Color4(
mn.Vector3(np.random.random(3))
)
self.draw_region_debug(debug_line_render)

def draw_event(
self,
Expand Down Expand Up @@ -502,6 +501,12 @@ def key_press_event(self, event: Application.KeyEvent) -> None:

elif key == pressed.H:
self.print_help_text()
elif key == pressed.J:
logger.info(
f"Toggle Region Draw from {self.semantic_region_debug_draw } to {not self.semantic_region_debug_draw}"
)
# Toggle visualize semantic bboxes. Currently only regions supported
self.semantic_region_debug_draw = not self.semantic_region_debug_draw

elif key == pressed.TAB:
# NOTE: (+ALT) - reconfigure without cycling scenes
Expand Down Expand Up @@ -619,7 +624,6 @@ def key_press_event(self, event: Application.KeyEvent) -> None:
elif key == pressed.V:
self.invert_gravity()
logger.info("Command: gravity inverted")

elif key == pressed.N:
# (default) - toggle navmesh visualization
# NOTE: (+ALT) - re-sample the agent position on the NavMesh
Expand Down Expand Up @@ -1002,6 +1006,7 @@ def print_help_text(self) -> None:
',': Render a Bullet collision shape debug wireframe overlay (white=active, green=sleeping, blue=wants sleeping, red=can't sleep).
'c': Run a discrete collision detection pass and render a debug wireframe overlay showing active contact points and normals (yellow=fixed length normals, red=collision distances).
(+SHIFT) Toggle the contact point debug render overlay on/off.
'j' Toggle Semantic visualization bounds (currently only Semantic Region annotations)
Object Interactions:
SPACE: Toggle physics simulation on/off.
Expand Down

0 comments on commit f9db344

Please sign in to comment.