|
|
|
|
|
import trimesh |
|
|
import json |
|
|
import numpy as np |
|
|
|
|
|
PI = np.pi |
|
|
|
|
|
|
|
|
MODEL_SCALE = 1 |
|
|
MARKER_FRAC = 0.02 |
|
|
|
|
|
def create_model_data(id="card"): |
|
|
file_path = f"./{id}.glb" |
|
|
save_path = f"./model_data.json" |
|
|
|
|
|
|
|
|
with open(file_path, "rb") as f: |
|
|
loaded = trimesh.load(f, file_type="glb") |
|
|
mesh = loaded.to_mesh() if isinstance(loaded, trimesh.Scene) else loaded |
|
|
|
|
|
|
|
|
mesh.apply_scale(MODEL_SCALE) |
|
|
scene = trimesh.Scene(mesh) |
|
|
|
|
|
|
|
|
obb = mesh.bounding_box_oriented |
|
|
center = obb.centroid |
|
|
ext = obb.extents |
|
|
diag = float(np.linalg.norm(ext)) + 1e-12 |
|
|
|
|
|
|
|
|
axis_len = diag * MARKER_FRAC * 6.0 |
|
|
origin_sz = diag * MARKER_FRAC * 0.6 |
|
|
r_target = diag * MARKER_FRAC * 1.2 |
|
|
r_contact0 = diag * MARKER_FRAC * 1.0 |
|
|
r_contact1 = diag * MARKER_FRAC * 1.0 |
|
|
|
|
|
red = [1.0, 0.0, 0.0, 0.5] |
|
|
green = [0.0, 1.0, 0.0, 0.5] |
|
|
blue = [0.0, 0.0, 1.0, 0.5] |
|
|
|
|
|
|
|
|
target_sphere = trimesh.creation.icosphere(subdivisions=2, radius=r_target) |
|
|
T_target = trimesh.transformations.translation_matrix(center) |
|
|
target_sphere.apply_transform(T_target) |
|
|
target_sphere.visual.vertex_colors = np.array([red] * len(target_sphere.vertices)) |
|
|
scene.add_geometry(target_sphere) |
|
|
target_points_list = [T_target.tolist()] |
|
|
|
|
|
|
|
|
contact_points_list, contact_desc = [], [] |
|
|
|
|
|
def add_contact(radius, pos, euler_xyz, desc): |
|
|
sph = trimesh.creation.icosphere(subdivisions=2, radius=radius) |
|
|
T = trimesh.transformations.translation_matrix(pos) @ \ |
|
|
trimesh.transformations.euler_matrix(*euler_xyz) |
|
|
sph.apply_transform(T) |
|
|
sph.visual.vertex_colors = np.array([red] * len(sph.vertices)) |
|
|
|
|
|
axis = trimesh.creation.axis(origin_size=origin_sz) |
|
|
|
|
|
axis.apply_transform(T) |
|
|
scene.add_geometry(axis) |
|
|
scene.add_geometry(sph) |
|
|
contact_points_list.append(T.tolist()) |
|
|
contact_desc.append(desc) |
|
|
|
|
|
ex, ey, ez = ext |
|
|
print(ext) |
|
|
print(center) |
|
|
c = center |
|
|
add_contact(r_contact0, [c[0] + 0.25 * ex, c[1], c[2] - 0.25 * ez], [0, -PI, 0], "Suction point of the upper right corner to the midpoint by half") |
|
|
add_contact(r_contact1, [c[0], c[1], c[2] + 0.25 * ez], [-PI/2, -PI, 0], "Grasp on midpoint of the bottom edge") |
|
|
add_contact(r_contact1, [c[0], c[1], c[2] - 0.25 * ez], [PI/2, PI, 0], "Grasp on midpoint of the bottom edge") |
|
|
|
|
|
orient = trimesh.creation.icosphere(subdivisions=2, radius=origin_sz) |
|
|
T_orient = trimesh.transformations.translation_matrix([c[0], c[1] + ey/2.0, c[2]]) |
|
|
orient.apply_transform(T_orient) |
|
|
orient.visual.vertex_colors = np.array([green] * len(orient.vertices)) |
|
|
scene.add_geometry(orient) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transform_matrix = trimesh.transformations.euler_matrix(0, 0, 0).tolist() |
|
|
data = { |
|
|
"center": center.tolist(), |
|
|
"extents": ext.tolist(), |
|
|
"scale": [MODEL_SCALE, MODEL_SCALE, MODEL_SCALE], |
|
|
"target_pose": target_points_list, |
|
|
"contact_points_pose": contact_points_list, |
|
|
"transform_matrix": transform_matrix, |
|
|
"functional_matrix": [], |
|
|
"orientation_point": T_orient.tolist(), |
|
|
"contact_points_group": [[0, 1]], |
|
|
"contact_points_mask": [True, True], |
|
|
"contact_points_discription": contact_desc, |
|
|
"target_point_discription": ["Object centroid."], |
|
|
"functional_point_discription": [""], |
|
|
"orientation_point_discription": ["OBB +Y reference."] |
|
|
} |
|
|
with open(save_path, "w") as f: |
|
|
json.dump(data, f, indent=4, separators=(",", ": ")) |
|
|
|
|
|
scene.show() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
create_model_data("card") |
|
|
|