def read(self): ''' Read in the vertex, normal, and face lists to form a mesh. Technically this should also read in a texture but right now we don't support it ''' numVerts = 0 verts = [] norms = None faces = [] tex_coords = [] face_norms = [] f = open(self.filepath_, 'r') for line in f: # break up the line by whitespace vals = line.split() if len(vals) > 0: # look for obj tags (see http://en.wikipedia.org/wiki/Wavefront_.obj_file) if vals[0] == 'v': # add vertex v = map(float, vals[1:4]) verts.append(v) if vals[0] == 'vn': # add normal if norms is None: norms = [] n = map(float, vals[1:4]) norms.append(n) if vals[0] == 'f': # add faces (includes vertex indices, texture coordinates, and normals) vi = [] vti = [] nti = [] if vals[1].find('/') == -1: vi = map(int, vals[1:]) vi = [i - 1 for i in vi] else: for j in range(1, len(vals)): # break up like by / to read vert inds, tex coords, and normal inds val = vals[j] tokens = val.split('/') for i in range(len(tokens)): if i == 0: vi.append( int(tokens[i]) - 1) # adjust for python 0 - indexing elif i == 1: if tokens[i] != '': vti.append(int(tokens[i])) elif i == 2: nti.append(int(tokens[i])) faces.append(vi) # below two lists are currently not in use tex_coords.append(vti) face_norms.append(nti) return mesh.Mesh3D(verts, faces, norms)
def mesh_3d(data): """ Converts HDF5 data provided in dictionary |data| to a mesh object """ vertices = np.array(data[MESH_VERTICES_KEY]) triangles = np.array(data[MESH_TRIANGLES_KEY]) normals = None if MESH_NORMALS_KEY in data.keys(): normals = np.array(data[MESH_NORMALS_KEY]) return mesh.Mesh3D(vertices.tolist(), triangles.tolist(), normals=normals)
def read(self): """Reads in the .off file and returns a Mesh3D representation of that mesh. Returns ------- :obj:`Mesh3D` A Mesh3D created from the data in the .off file. """ verts = [] faces = [] f = open(self.filepath_, 'r') # parse header (NOTE: we do not support reading edges) header = f.readline() tokens = header.split() if len(tokens) == 1: header = f.readline() tokens = header.split() else: tokens = tokens[1:] num_vertices = int(tokens[0]) num_faces = int(tokens[1]) # read vertices for i in range(num_vertices): line = f.readline() tokens = line.split() vertex = [float(tokens[0]), float(tokens[1]), float(tokens[2])] verts.append(vertex) # read faces for i in range(num_faces): line = f.readline() tokens = line.split() if int(tokens[0]) != 3: raise ValueError( 'Only triangle meshes supported, but OFF file has %d-faces' % (int(tokens[0]))) face = [int(tokens[1]), int(tokens[2]), int(tokens[3])] faces.append(face) return mesh.Mesh3D(verts, faces)
def read(self): """Reads in the .obj file and returns a Mesh3D representation of that mesh. Returns ------- :obj:`Mesh3D` A Mesh3D created from the data in the .obj file. """ numVerts = 0 verts = [] norms = None faces = [] tex_coords = [] face_norms = [] f = open(self.filepath_, 'r') for line in f: # Break up the line by whitespace vals = line.split() if len(vals) > 0: # Look for obj tags (see http://en.wikipedia.org/wiki/Wavefront_.obj_file) if vals[0] == 'v': # Add vertex v = list(map(float, vals[1:4])) verts.append(v) if vals[0] == 'vn': # Add normal if norms is None: norms = [] n = list(map(float, vals[1:4])) norms.append(n) if vals[0] == 'f': # Add faces (includes vertex indices, texture coordinates, and normals) vi = [] vti = [] nti = [] if vals[1].find('/') == -1: vi = list(map(int, vals[1:])) vi = [i - 1 for i in vi] else: for j in range(1, len(vals)): # Break up like by / to read vert inds, tex coords, and normal inds val = vals[j] tokens = val.split('/') for i in range(len(tokens)): if i == 0: vi.append( int(tokens[i]) - 1) # adjust for python 0 - indexing elif i == 1: if tokens[i] != '': vti.append(int(tokens[i])) elif i == 2: nti.append(int(tokens[i])) faces.append(vi) # Below two lists are currently not in use tex_coords.append(vti) face_norms.append(nti) f.close() return mesh.Mesh3D(verts, faces, norms)
def test_yumi_gripper(): # build new mesh finger_filename = '/home/jmahler/jeff_working/GPIS/data/grippers/yumi/finger.obj' base_filename = '/home/jmahler/jeff_working/GPIS/data/grippers/yumi/base.obj' of = objf.ObjFile(finger_filename) finger1_mesh = of.read() finger2_mesh = copy.copy(finger1_mesh) of = objf.ObjFile(base_filename) base_mesh = of.read() R_finger1_world = np.eye(3) t_finger1_world = np.array([-0.025, -0.005, 0.082]) T_finger1_world = stf.SimilarityTransform3D(pose=tfx.pose(R_finger1_world, t_finger1_world), from_frame='mesh', to_frame='world') R_finger2_world = np.array([[-1,0,0], [0,-1,0], [0,0,1]]) t_finger2_world = np.array([0.025, 0.005, 0.082]) T_finger2_world = stf.SimilarityTransform3D(pose=tfx.pose(R_finger2_world, t_finger2_world), from_frame='mesh', to_frame='world') T_mesh_world = stf.SimilarityTransform3D(pose=tfx.pose(np.eye(4)), from_frame='world', to_frame='mesh') finger1_mesh = finger1_mesh.transform(T_finger1_world) finger2_mesh = finger2_mesh.transform(T_finger2_world) offset = 0 vertices = [] triangles = [] meshes = [base_mesh, finger1_mesh, finger2_mesh] for i, mesh in enumerate(meshes): vertices.extend(mesh.vertices()) offset_tris = [[t[0]+offset,t[1]+offset,t[2]+offset] for t in mesh.triangles()] triangles.extend(offset_tris) offset += len(mesh.vertices()) gripper_mesh = m.Mesh3D(vertices, triangles) gripper_mesh.center_vertices_bb() of = objf.ObjFile('/home/jmahler/jeff_working/GPIS/data/grippers/yumi/gripper.obj') #of.write(gripper_mesh) # frames of reference R_grasp_gripper = np.array([[0, 0, -1], [0, 1, 0], [1, 0, 0]]) R_mesh_gripper = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) t_mesh_gripper = np.array([0.0, -0.002, 0.058]) T_mesh_gripper = stf.SimilarityTransform3D(pose=tfx.pose(R_mesh_gripper, t_mesh_gripper), from_frame='gripper', to_frame='mesh') T_gripper_world = T_mesh_gripper.inverse().dot(T_mesh_world) T_grasp_gripper = stf.SimilarityTransform3D(pose=tfx.pose(R_grasp_gripper), from_frame='gripper', to_frame='grasp') #T_mesh_gripper.save('/home/jmahler/jeff_working/GPIS/data/grippers/yumi/T_mesh_gripper.stf') #T_grasp_gripper.save('/home/jmahler/jeff_working/GPIS/data/grippers/yumi/T_grasp_gripper.stf') gripper_params = {} gripper_params['min_width'] = 0.0 gripper_params['max_width'] = 0.05 #f = open('/home/jmahler/jeff_working/GPIS/data/grippers/yumi/params.json', 'w') #json.dump(gripper_params, f) MayaviVisualizer.plot_mesh(gripper_mesh, T_mesh_world, style='surface', color=(0,0,1)) MayaviVisualizer.plot_pose(T_gripper_world, alpha=0.05, tube_radius=0.0025, center_scale=0.005) mv.axes()