166 lines
6.1 KiB
Python
166 lines
6.1 KiB
Python
import argparse
|
|
import json
|
|
import os
|
|
import webbrowser
|
|
import numpy as np
|
|
from plyfile import PlyData
|
|
import plotly.graph_objects as go
|
|
|
|
def load_scene_data(scene_id, base_path='Dataset/FunGraph3D/FunGraph3D'):
|
|
ply_path = os.path.join(base_path, scene_id, f'{scene_id}.ply')
|
|
annot_file = os.path.join(base_path, 'FunGraph3D.annotations.json')
|
|
rel_file = os.path.join(base_path, 'FunGraph3D.relations.json')
|
|
|
|
if not os.path.exists(ply_path):
|
|
raise FileNotFoundError(f"PLY file not found: {ply_path}")
|
|
|
|
ply = PlyData.read(ply_path)
|
|
vertex = ply['vertex']
|
|
pts = np.vstack([vertex['x'], vertex['y'], vertex['z']]).T
|
|
|
|
# Check for vertex colors
|
|
if 'red' in vertex:
|
|
colors = np.vstack([vertex['red'], vertex['green'], vertex['blue']]).T / 255.0
|
|
else:
|
|
colors = np.ones_like(pts) * 0.7
|
|
|
|
with open(annot_file, 'r') as f:
|
|
all_annots = json.load(f)
|
|
with open(rel_file, 'r') as f:
|
|
all_rels = json.load(f)
|
|
|
|
scene_annots = [a for a in all_annots if a['scene_id'] == scene_id]
|
|
scene_rels = [r for r in all_rels if r['scene_id'] == scene_id]
|
|
|
|
return pts, colors, scene_annots, scene_rels
|
|
|
|
def create_sphere_wireframe(center, radius=0.50, num_pts=20):
|
|
u = np.linspace(0, 2 * np.pi, num_pts)
|
|
v = np.linspace(0, np.pi, num_pts)
|
|
x = center[0] + radius * np.outer(np.cos(u), np.sin(v))
|
|
y = center[1] + radius * np.outer(np.sin(u), np.sin(v))
|
|
z = center[2] + radius * np.outer(np.ones(np.size(u)), np.cos(v))
|
|
return x, y, z
|
|
|
|
def visualize(scene_id, placement_idx=0, output_html='scene_visualization.html'):
|
|
pts, colors, annots, rels = load_scene_data(scene_id)
|
|
|
|
# Load placements
|
|
placements_file = 'scratch/verified_placements.json'
|
|
if os.path.exists(placements_file):
|
|
with open(placements_file, 'r') as f:
|
|
all_placements = json.load(f)
|
|
scene_placements = [p for p in all_placements if p['scene'] == scene_id]
|
|
else:
|
|
scene_placements = []
|
|
|
|
if not scene_placements:
|
|
print(f"No predefined placement found for {scene_id}. Showing whole scene.")
|
|
selected = None
|
|
else:
|
|
placement_idx = min(placement_idx, len(scene_placements) - 1)
|
|
selected = scene_placements[placement_idx]
|
|
print(f"\n--- Showing Placement #{placement_idx + 1} for {scene_id} ---")
|
|
print(f"Target: {selected['target']} ({selected['part']}) at {selected['target_pos']}")
|
|
print(f"Obstacle: {selected['obstacle']} at {selected['obstacle_pos']}")
|
|
print(f"Arm Base: {selected['base_pos']} (Yaw: {selected['base_yaw_deg']}°)")
|
|
|
|
# Downsample points for fast web rendering (every 5th point)
|
|
sub_pts = pts[::5]
|
|
sub_colors = colors[::5]
|
|
|
|
fig = go.Figure()
|
|
|
|
# 1. Background Scene Point Cloud
|
|
fig.add_trace(go.Scatter3d(
|
|
x=sub_pts[:, 0],
|
|
y=sub_pts[:, 1],
|
|
z=sub_pts[:, 2],
|
|
mode='markers',
|
|
marker=dict(
|
|
size=1.5,
|
|
color=['rgb({},{},{})'.format(int(r*255), int(g*255), int(b*255)) for r,g,b in sub_colors],
|
|
opacity=0.5
|
|
),
|
|
name='Szene (Point Cloud)'
|
|
))
|
|
|
|
if selected:
|
|
tgt_pos = np.array(selected['target_pos'])
|
|
obs_pos = np.array(selected['obstacle_pos'])
|
|
base_pos = np.array(selected['base_pos'])
|
|
|
|
# 2. Target Marker (Green)
|
|
fig.add_trace(go.Scatter3d(
|
|
x=[tgt_pos[0]], y=[tgt_pos[1]], z=[tgt_pos[2]],
|
|
mode='markers+text',
|
|
marker=dict(size=8, color='green', symbol='diamond'),
|
|
text=[f"ZIEL: {selected['target']} ({selected['part']})"],
|
|
textposition="top center",
|
|
name='Zielobjekt (Target)'
|
|
))
|
|
|
|
# 3. Obstacle Marker (Red)
|
|
fig.add_trace(go.Scatter3d(
|
|
x=[obs_pos[0]], y=[obs_pos[1]], z=[obs_pos[2]],
|
|
mode='markers+text',
|
|
marker=dict(size=8, color='red', symbol='square'),
|
|
text=[f"HINDERNIS: {selected['obstacle']}"],
|
|
textposition="top center",
|
|
name='Hindernis (Obstacle)'
|
|
))
|
|
|
|
# 4. Piper Base Marker (Blue)
|
|
fig.add_trace(go.Scatter3d(
|
|
x=[base_pos[0]], y=[base_pos[1]], z=[base_pos[2]],
|
|
mode='markers+text',
|
|
marker=dict(size=9, color='blue', symbol='cross'),
|
|
text=[f"Piper Basis ({selected['base_yaw_deg']}°)"],
|
|
textposition="bottom center",
|
|
name='Piper Roboterbasis'
|
|
))
|
|
|
|
# 5. Line of Sight (Yellow line)
|
|
fig.add_trace(go.Scatter3d(
|
|
x=[base_pos[0], tgt_pos[0]],
|
|
y=[base_pos[1], tgt_pos[1]],
|
|
z=[base_pos[2], tgt_pos[2]],
|
|
mode='lines',
|
|
line=dict(color='yellow', width=5, dash='dash'),
|
|
name='Direkter Anfahrtskorridor'
|
|
))
|
|
|
|
# 6. Reachability Sphere Wireframe (~0.50m radius)
|
|
sx, sy, sz = create_sphere_wireframe(base_pos, radius=0.52)
|
|
fig.add_trace(go.Surface(
|
|
x=sx, y=sy, z=sz,
|
|
opacity=0.15,
|
|
showscale=False,
|
|
colorscale=[[0, 'blue'], [1, 'blue']],
|
|
name='Piper Arbeitsbereich (52 cm)'
|
|
))
|
|
|
|
fig.update_layout(
|
|
title=f"FunGraph3D Szene: {scene_id} | Piper Placement Visualizer",
|
|
scene=dict(
|
|
xaxis_title='X (m)',
|
|
yaxis_title='Y (m)',
|
|
zaxis_title='Z (m)',
|
|
aspectmode='data'
|
|
),
|
|
margin=dict(l=0, r=0, b=0, t=40),
|
|
legend=dict(x=0.02, y=0.98)
|
|
)
|
|
|
|
fig.write_html(output_html)
|
|
print(f"\nSaved interactive HTML view to: {output_html}")
|
|
webbrowser.open(f'file://{os.path.abspath(output_html)}')
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description="Visualize FunGraph3D scene and Piper base placements")
|
|
parser.add_argument('--scene', type=str, default='0kitchen', help='Scene ID (e.g. 0kitchen, 3kitchen, 1bathroom)')
|
|
parser.add_argument('--index', type=int, default=0, help='Placement candidate index (0, 1, 2...)')
|
|
args = parser.parse_args()
|
|
|
|
visualize(args.scene, args.index)
|