Files

108 lines
4.2 KiB
Python

import json, os
from plyfile import PlyData
import numpy as np
base_path = 'Dataset/FunGraph3D/FunGraph3D'
annot_file = os.path.join(base_path, 'FunGraph3D.annotations.json')
rel_file = os.path.join(base_path, 'FunGraph3D.relations.json')
with open(annot_file, 'r') as f:
annots = json.load(f)
with open(rel_file, 'r') as f:
relations = json.load(f)
# Piper Arm reach: ~0.62 m max reach, ideal work distance ~0.35 - 0.50 m
# Arm base should be positioned roughly 0.40 - 0.55m in front of the target/obstacle cluster
results = []
scenes_to_check = [
'0kitchen', '10kitchen', '12kitchen', '3kitchen', '5kitchen', '6kitchen',
'1bathroom', '8bathroom', '13bathroom', '2livingroom', '4livingroom', '7bedroom'
]
for s_id in scenes_to_check:
ply_path = os.path.join(base_path, s_id, f'{s_id}.ply')
if not os.path.exists(ply_path):
continue
ply = PlyData.read(ply_path)
vertex = ply['vertex']
pts = np.vstack([vertex['x'], vertex['y'], vertex['z']]).T
s_annots = [a for a in annots if a['scene_id'] == s_id]
s_rels = [r for r in relations if r['scene_id'] == s_id]
obj_dict = {}
for a in s_annots:
idx = a['indices']
if len(idx) > 0:
obj_pts = pts[idx]
centroid = obj_pts.mean(axis=0)
bbox_min = obj_pts.min(axis=0)
bbox_max = obj_pts.max(axis=0)
bbox_size = bbox_max - bbox_min
else:
centroid = np.zeros(3)
bbox_min = np.zeros(3)
bbox_max = np.zeros(3)
bbox_size = np.zeros(3)
obj_dict[a['annot_id']] = {
'id': a['annot_id'],
'label': a['label'],
'centroid': centroid,
'bbox_size': bbox_size,
'bbox_min': bbox_min,
'bbox_max': bbox_max,
}
# Find all target-part relations
for r in s_rels:
part_id = r['first_node_annot_id']
parent_id = r['second_node_annot_id']
part = obj_dict.get(part_id)
parent = obj_dict.get(parent_id)
if not part or not parent:
continue
# Target position is the functional subpart (or parent)
tgt_pos = part['centroid'] if np.any(part['centroid']) else parent['centroid']
# Look for nearby objects that could act as obstacles (between 0.15m and 0.65m away)
for other_id, other in obj_dict.items():
if other_id == part_id or other_id == parent_id:
continue
# Avoid direct subparts of the same parent
if any(r2['second_node_annot_id'] == parent_id and r2['first_node_annot_id'] == other_id for r2 in s_rels):
continue
obs_pos = other['centroid']
dist = np.linalg.norm(tgt_pos - obs_pos)
# Interesting obstacle: close to target (0.15m to 0.55m)
if 0.15 <= dist <= 0.55:
# Vector from obstacle to target
vec_obs_tgt = tgt_pos - obs_pos
# Suggested base position: placed such that obstacle lies partially in line of reach
# Approach vector roughly towards target, e.g. offsetting in XY plane
approach_dir = -vec_obs_tgt / (np.linalg.norm(vec_obs_tgt) + 1e-6)
# Position arm base ~0.45m away along approach direction
suggested_base = tgt_pos + approach_dir * 0.48
# Align base height with floor / counter level
suggested_base[2] = min(tgt_pos[2], obs_pos[2]) - 0.20
results.append({
'scene': s_id,
'target_parent': parent['label'],
'target_part': part['label'],
'affordance': r['description'],
'target_pos': np.round(tgt_pos, 3).tolist(),
'obstacle_label': other['label'],
'obstacle_pos': np.round(obs_pos, 3).tolist(),
'dist_m': round(float(dist), 3),
'suggested_arm_base': np.round(suggested_base, 3).tolist()
})
print(f"Found {len(results)} potential obstacle-target scenarios across scenes.")
with open('scratch/scenario_candidates.json', 'w') as f:
json.dump(results, f, indent=2)