| | import trimesh
|
| | import json
|
| | import numpy as np
|
| |
|
| |
|
| | PI = np.pi
|
| |
|
| | def create_model_data(id):
|
| | file_path = f"./textured{id}.obj"
|
| | save_path = f"./model_data{id}.json"
|
| |
|
| |
|
| |
|
| |
|
| | with open(file_path, 'rb') as file_obj:
|
| | mesh = trimesh.load(file_obj, file_type='obj')
|
| |
|
| | scene = trimesh.Scene(mesh)
|
| |
|
| | oriented_bounding_box = mesh.bounding_box_oriented
|
| | red_color = [1.0, 0.0, 0.0, 0.5]
|
| | green_color = [0.0, 1.0, 0.0, 0.5]
|
| | blue_color = [0.0, 0.0, 1.0, 0.5]
|
| | scale = [0.,0.,0.]
|
| |
|
| | shape = oriented_bounding_box.extents.tolist()
|
| |
|
| | print(shape)
|
| | target_sphere = trimesh.creation.icosphere(subdivisions=2, radius=0.1)
|
| | target_trans_matrix_sphere = trimesh.transformations.translation_matrix([-0.07, -0.5, 0.35])
|
| |
|
| | target_sphere.apply_transform(target_trans_matrix_sphere)
|
| | target_sphere.visual.vertex_colors = np.array([red_color] * len(target_sphere.vertices))
|
| | target_points_list = [target_trans_matrix_sphere.tolist()]
|
| |
|
| |
|
| | target_sphere_2 = trimesh.creation.icosphere(subdivisions=2, radius=0.05)
|
| | target_trans_matrix_sphere_2 = trimesh.transformations.translation_matrix([-0.35, 0.25, 0.22])
|
| |
|
| | target_sphere_2.apply_transform(target_trans_matrix_sphere_2)
|
| | target_sphere_2.visual.vertex_colors = np.array([red_color] * len(target_sphere_2.vertices))
|
| | target_points_list.append(target_trans_matrix_sphere_2.tolist())
|
| |
|
| | target_point_description_list = [
|
| | 'The base of the kettle.',
|
| | 'The spout of the kettle.'
|
| | ]
|
| |
|
| | scene.add_geometry(target_sphere)
|
| | scene.add_geometry(target_sphere_2)
|
| |
|
| | contact_points_list = []
|
| | contact_point_discription_list = []
|
| | orientation_point_list = []
|
| | functional_matrix = []
|
| |
|
| | def add_contact_point(point_radius, pose:list, euler:list, discription: str):
|
| | contact_sphere = trimesh.creation.icosphere(subdivisions=2, radius=point_radius)
|
| | contact_trans_matrix_sphere = trimesh.transformations.translation_matrix(pose) @ trimesh.transformations.euler_matrix(euler[0], euler[1], euler[2])
|
| | contact_sphere.apply_transform(contact_trans_matrix_sphere)
|
| | contact_sphere.visual.vertex_colors = np.array([red_color] * len(contact_sphere.vertices))
|
| | contact_points_list.append(contact_trans_matrix_sphere.tolist())
|
| |
|
| | axis = trimesh.creation.axis(axis_length=2)
|
| | axis.apply_transform(contact_trans_matrix_sphere)
|
| | scene.add_geometry(axis)
|
| | scene.add_geometry(contact_sphere)
|
| | contact_point_discription_list.append(discription)
|
| |
|
| |
|
| | alpha = 0.95
|
| | delta = 0.05
|
| |
|
| |
|
| |
|
| | add_contact_point(0.02, [0.2, 0.18, 0.45], [0, PI, 0], "Grasping the side of the kettle.")
|
| | add_contact_point(0.02, [0.2, 0.18, 0.48], [0, PI, 0], "Grasping the side of the kettle.")
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | transform_matrix = trimesh.transformations.euler_matrix(0,0,0)
|
| | transform_matrix = transform_matrix.tolist()
|
| |
|
| |
|
| | orientation_point = trimesh.creation.icosphere(subdivisions=2, radius=0.1)
|
| | orientation_point_sphere = trimesh.transformations.translation_matrix([0,oriented_bounding_box.extents[1]/2,0])
|
| | orientation_point.apply_transform(orientation_point_sphere)
|
| | orientation_point.visual.vertex_colors = np.array([green_color] * len(orientation_point.vertices))
|
| | orientation_point_list = orientation_point_sphere.tolist()
|
| | scene.add_geometry(orientation_point)
|
| |
|
| |
|
| | axis1 = trimesh.creation.axis(axis_length=1.5)
|
| | axis1.apply_transform(transform_matrix)
|
| |
|
| | functional_trans_matrix_list = []
|
| | functional_point_discription_list = ['The functional matrix of the base', 'The functional matrix of the spout']
|
| | functional_sphere = trimesh.creation.icosphere(subdivisions=2, radius=0.05)
|
| | functional_trans_matrix_sphere = trimesh.transformations.translation_matrix([0,0,0]) @ trimesh.transformations.euler_matrix(PI / 2, 0 ,0)
|
| | functional_sphere.apply_transform(functional_trans_matrix_sphere)
|
| | functional_sphere.visual.vertex_colors = np.array([blue_color] * len(functional_sphere.vertices))
|
| | functional_matrix = functional_trans_matrix_sphere.tolist()
|
| | axis = trimesh.creation.axis(axis_length=1.5)
|
| | axis.apply_transform(functional_trans_matrix_sphere)
|
| | scene.add_geometry(axis)
|
| | scene.add_geometry(functional_sphere)
|
| | functional_trans_matrix_list.append(functional_matrix)
|
| | functional_sphere_2 = trimesh.creation.icosphere(subdivisions=2, radius=0.05)
|
| | functional_trans_matrix_sphere_2 = trimesh.transformations.translation_matrix([1,0,0]) @ trimesh.transformations.euler_matrix(0, -PI / 2 ,0)
|
| | functional_sphere_2.apply_transform(functional_trans_matrix_sphere_2)
|
| | functional_sphere_2.visual.vertex_colors = np.array([blue_color] * len(functional_sphere_2.vertices))
|
| | functional_matrix_2 = functional_trans_matrix_sphere_2.tolist()
|
| | axis = trimesh.creation.axis(axis_length=1.5)
|
| | axis.apply_transform(functional_trans_matrix_sphere_2)
|
| | scene.add_geometry(axis)
|
| | scene.add_geometry(functional_sphere_2)
|
| | functional_trans_matrix_list.append(functional_matrix_2)
|
| |
|
| | assert(len(functional_trans_matrix_list) == len(functional_point_discription_list))
|
| | data = {
|
| | 'center': oriented_bounding_box.centroid.tolist(),
|
| | 'extents': oriented_bounding_box.extents.tolist(),
|
| | 'scale': scale,
|
| | 'target_pose': target_points_list,
|
| | 'contact_points_pose' : contact_points_list,
|
| | 'transform_matrix': transform_matrix,
|
| | "functional_matrix": functional_trans_matrix_list,
|
| | 'orientation_point': orientation_point_list,
|
| | 'contact_points_group': [[0]],
|
| | 'contact_points_mask': [True],
|
| | 'contact_points_discription': contact_point_discription_list,
|
| | 'target_point_discription': target_point_description_list,
|
| | 'functional_point_discription': functional_point_discription_list,
|
| | 'orientation_point_discription': [""]
|
| | }
|
| | with open(save_path, 'w') as json_file:
|
| | json.dump(data, json_file, indent=4, separators=(',', ': '))
|
| |
|
| |
|
| | axis = trimesh.creation.axis(axis_length=1.5,origin_size= 0.05)
|
| |
|
| | scene.show()
|
| |
|
| | if __name__ == "__main__":
|
| | id = ""
|
| | create_model_data(id) |