1. tuned arrow mesh drawing

2. centralixzed arrow config in layout.py
3. seperate mesh render and display resolution to speed up (~40 fps)
This commit is contained in:
ykcheungab
2026-06-19 14:28:28 +08:00
parent 3fd9b839a9
commit 2e5c047d1e
19 changed files with 980 additions and 147 deletions
+60 -11
View File
@@ -10,7 +10,13 @@ from orisys.finger.mesh import UVSurfaceMesh
from orisys.finger.viewer import flow_magnitude_vertex_colors, scalar_map_vertex_colors
from .contact_arrow import ContactArrowOverlay
from .mesh_camera import apply_view_from_world_to_cam, camera_look_at_from_world_to_cam
from .mesh_camera import (
apply_filament_sun_light,
apply_shade_factors,
apply_view_from_world_to_cam,
camera_look_at_from_world_to_cam,
precompute_shade_factors,
)
def _numpy_o3d_image_to_rgb(image) -> np.ndarray:
@@ -22,13 +28,17 @@ def _numpy_o3d_image_to_rgb(image) -> np.ndarray:
return arr.astype(np.uint8)
def _make_unlit_material(o3d):
def _make_lit_material(o3d):
material = o3d.visualization.rendering.MaterialRecord()
material.shader = "defaultUnlit"
material.shader = "defaultLit"
material.base_color = (1.0, 1.0, 1.0, 1.0)
return material
def _shade_vertex_colors(mesh, colors: np.ndarray, shade_factors) -> np.ndarray:
return apply_shade_factors(colors, shade_factors)
def _mesh_to_o3d(o3d, vertices: np.ndarray, triangles: np.ndarray, colors: np.ndarray):
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(vertices.astype(np.float64))
@@ -70,6 +80,8 @@ class _FilamentOffscreenBackend:
world_to_cam: np.ndarray | None,
lookat: np.ndarray,
show_contact_arrow: bool,
mesh_lighting=None,
contact_arrow_style=None,
):
import open3d as o3d
@@ -83,10 +95,26 @@ class _FilamentOffscreenBackend:
self.value_max = value_max
self.world_to_cam = world_to_cam
self.lookat = lookat
self._contact_arrow = ContactArrowOverlay(mesh, enabled=show_contact_arrow)
self.mesh_lighting = mesh_lighting
self._shade_factors = (
precompute_shade_factors(mesh.rest_vertices, mesh.triangles, mesh_lighting)
if mesh_lighting is not None
else None
)
self._contact_arrow = ContactArrowOverlay(
mesh,
enabled=show_contact_arrow,
style=contact_arrow_style,
)
self._renderer = o3d.visualization.rendering.OffscreenRenderer(self.width, self.height)
self._renderer.scene.set_background([0.05, 0.05, 0.08, 1.0])
self._material = _make_unlit_material(o3d)
if mesh_lighting is not None:
apply_filament_sun_light(
self._renderer,
direction=mesh_lighting.direction,
intensity=mesh_lighting.intensity,
)
self._material = _make_lit_material(o3d)
self._mesh_added = False
self._arrow_added = False
self._apply_camera()
@@ -111,6 +139,13 @@ class _FilamentOffscreenBackend:
self.height = height
self._renderer = self._o3d.visualization.rendering.OffscreenRenderer(self.width, self.height)
self._renderer.scene.set_background([0.05, 0.05, 0.08, 1.0])
if self.mesh_lighting is not None:
apply_filament_sun_light(
self._renderer,
direction=self.mesh_lighting.direction,
intensity=self.mesh_lighting.intensity,
)
self._material = _make_lit_material(self._o3d)
self._mesh_added = False
self._arrow_added = False
self._apply_camera()
@@ -172,6 +207,8 @@ class _HiddenVisualizerBackend:
world_to_cam: np.ndarray | None,
lookat: np.ndarray,
show_contact_arrow: bool,
mesh_lighting=None,
contact_arrow_style=None,
):
import open3d as o3d
@@ -185,7 +222,17 @@ class _HiddenVisualizerBackend:
self.value_max = value_max
self.world_to_cam = world_to_cam
self.lookat = lookat
self._contact_arrow = ContactArrowOverlay(mesh, enabled=show_contact_arrow)
self.mesh_lighting = mesh_lighting
self._shade_factors = (
precompute_shade_factors(mesh.rest_vertices, mesh.triangles, mesh_lighting)
if mesh_lighting is not None
else None
)
self._contact_arrow = ContactArrowOverlay(
mesh,
enabled=show_contact_arrow,
style=contact_arrow_style,
)
self.vis = None
self.tri_mesh = None
self._create_window()
@@ -202,6 +249,7 @@ class _HiddenVisualizerBackend:
value_min=self.value_min,
value_max=self.value_max,
)
colors = _shade_vertex_colors(self.mesh, colors, self._shade_factors)
self.vis = o3d.visualization.Visualizer()
self.vis.create_window(
window_name="Orisys Finger Mesh (embedded)",
@@ -222,9 +270,9 @@ class _HiddenVisualizerBackend:
render_option = self.vis.get_render_option()
render_option.mesh_show_back_face = True
render_option.background_color = np.array([0.05, 0.05, 0.08])
if hasattr(render_option, "light_on"):
render_option.light_on = False
render_option.background_color = np.array([0.05, 0.05, 0.08])
if self.world_to_cam is not None:
apply_view_from_world_to_cam(self.vis, self.world_to_cam, lookat=self.lookat)
@@ -261,11 +309,9 @@ class _HiddenVisualizerBackend:
flow=flow,
scalar_map=scalar_map,
)
vertices = self.mesh.rest_vertices
colors = _shade_vertex_colors(self.mesh, colors, self._shade_factors)
self.tri_mesh.vertices = o3d.utility.Vector3dVector(vertices.astype(np.float64))
self.tri_mesh.vertex_colors = o3d.utility.Vector3dVector(colors.astype(np.float64))
self.tri_mesh.compute_vertex_normals()
self.vis.update_geometry(self.tri_mesh)
arrow_result = self._contact_arrow.compute(centroid, centroid_found, fnormal)
@@ -293,7 +339,6 @@ def _create_backend(mesh: UVSurfaceMesh, **kwargs):
except RuntimeError:
return _HiddenVisualizerBackend(mesh, **kwargs)
class OffscreenFingerMeshRenderer:
"""Render finger UV mesh + contact arrow to an RGB image for Qt embedding."""
@@ -310,6 +355,8 @@ class OffscreenFingerMeshRenderer:
world_to_cam: np.ndarray | None = None,
lookat: np.ndarray | None = None,
show_contact_arrow: bool = True,
mesh_lighting=None,
contact_arrow_style=None,
):
lookat = lookat if lookat is not None else mesh.rest_vertices.mean(axis=0)
self._backend = _create_backend(
@@ -323,6 +370,8 @@ class OffscreenFingerMeshRenderer:
world_to_cam=world_to_cam,
lookat=lookat,
show_contact_arrow=show_contact_arrow,
mesh_lighting=mesh_lighting,
contact_arrow_style=contact_arrow_style,
)
@property