File size: 4,810 Bytes
69f15ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# model_card_scaled.py
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"

    # 1) 读模型并烘焙为单一 Trimesh
    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

    # 2) 放大卡(整体缩放)
    mesh.apply_scale(MODEL_SCALE)  # <— 放大卡,trimesh 内置缩放方法
    scene = trimesh.Scene(mesh)

    # 3) 根据放大后的尺寸自适应计算标注尺寸
    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    # 抓取点球半径(1)
    r_contact1 = diag * MARKER_FRAC * 1.0    # 抓取点球半径(2)

    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]

    # 4) Target point:取 OBB 质心
    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()]

    # 5) 随便放两个 contact(先粗标,后续你再调)
    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_length 会随 origin_size 缩放;如要强行指定:axis = trimesh.creation.axis(origin_size=origin_sz); axis.apply_scale(axis_len / (origin_sz*10))
        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")
    # 6) Orientation 参考点(小绿球)
    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)

    # 7) 世界坐标轴(可选,按比例更小)
    # world_axis = trimesh.creation.axis(origin_size=origin_sz)
    # scene.add_geometry(world_axis)

    # 8) 写 JSON(把 scale 也写成 [MODEL_SCALE]*3,与你的 motionplanner 用法保持一致)
    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")