Example #1
0
    def write(self, mesh):
        '''
        Write a mesh to an obj file.
        Assumes mesh vertices, faces, and normals are in standard python list objects.
        Does not support material files or texture coordinates
        '''
        f = open(self.filepath_, 'w')
        vertices = mesh.vertices()
        faces = mesh.triangles()
        normals = mesh.normals()

        # write human-readable header
        f.write('###########################################################\n')
        f.write('# OBJ file generated by UC Berkeley Automation Sciences Lab\n')
        f.write('#\n')
        f.write('# Num Vertices: %d\n' %(len(vertices)))
        f.write('# Num Triangles: %d\n' %(len(faces)))
        f.write('#\n')
        f.write('###########################################################\n')
        f.write('\n')

        # write the vertex list
        for v in vertices:
            f.write('v %f %f %f\n' %(v[0], v[1], v[2]))

        # write the normals list
        if normals is not None and len(normals) > 0:
            for n in normals:
                f.write('vn %f %f %f\n' %(n[0], n[1], n[2]))

        # write the normals list
        for t in faces:
            f.write('f %d %d %d\n' %(t[0]+1, t[1]+1, t[2]+1)) # convert back to 1-indexing

        f.close()
Example #2
0
 def write_mesh_3d(mesh, data):
     """ Writes mesh object to HDF5 data provided in |data| """
     data.create_dataset(MESH_VERTICES_KEY, data=np.array(mesh.vertices()))
     data.create_dataset(MESH_TRIANGLES_KEY,
                         data=np.array(mesh.triangles()))
     if mesh.normals() is not None:
         data.create_dataset(MESH_NORMALS_KEY,
                             data=np.array(mesh.normals()))
Example #3
0
def assign_components(path):
	"""
	Sets values for component attribute of all meshes in an input directory.
	Utilizes breadth first search to determine connected components and assign values accordingly.
	Returns a dictionary mapping component numbers to indexes of all the vertices contained in that component.

	path -- path to directory containing mesh objects as .obj
	"""
	mesh_files = [filename for filename in os.listdir(path) if filename[-4:] == ".obj"]
	mesh_components_list = []
	for filename in mesh_files:
		print "Assigning components: " + path + "/" + filename
		ob = obj_file.ObjFile(path + "/" + filename)
		mesh = ob.read()
		mesh.remove_unreferenced_vertices()

		# generate graph and connect nodes appropriately
		nodes = []
		for triangle in mesh.triangles():
			node_0 = Node(triangle[0])
			node_1 = Node(triangle[1])
			node_2 = Node(triangle[2])
			node_0.connect_nodes(node_1)
			node_1.connect_nodes(node_2)
			node_2.connect_nodes(node_0)
			nodes.append(node_0)
			nodes.append(node_1)
			nodes.append(node_2)

		# breadth-first-search to mark elements of each component
		component_counter = 0
		component_to_index = {}
		while nodes != []:
			q = Queue.Queue()
			q.put(nodes[0])
			while (q.qsize() != 0):
				curr = q.get()
				curr.visited = True;
				if curr in nodes:
					nodes.remove(curr)
				else:
					continue

				for child in curr.children:
					if not child.visited:
						q.put(child)

				if component_counter in component_to_index:
					if curr.index not in component_to_index[component_counter]:
						component_to_index[component_counter].append(curr.index)
				else:
					component_to_index[component_counter] = [curr.index]
			component_counter += 1
		mesh_components_list.append(component_to_index)
	return mesh_components_list
Example #4
0
    def write(self, mesh):
        '''
        Write a mesh to an obj file.
        Assumes mesh vertices, faces, and normals are in standard python list objects.
        Does not support material files or texture coordinates
        '''
        f = open(self.filepath_, 'w')
        vertices = mesh.vertices()
        faces = mesh.triangles()
        normals = mesh.normals()
        colors = mesh.colors()

        # write human-readable header
        f.write(
            '###########################################################\n')
        f.write(
            '# OBJ file generated by UC Berkeley Automation Sciences Lab\n')
        f.write('#\n')
        f.write('# Num Vertices: %d\n' % (len(vertices)))
        f.write('# Num Triangles: %d\n' % (len(faces)))
        f.write('#\n')
        f.write(
            '###########################################################\n')
        f.write('\n')

        if colors is None:
            # write the vertex list
            for v in vertices:
                f.write('v %f %f %f\n' % (v[0], v[1], v[2]))
        else:
            assert len(vertices) == colors.shape[0], 'Color dimension mismatch'
            for v, c in zip(vertices, colors):
                f.write('v %f %f %f %f %f %f\n' %
                        (v[0], v[1], v[2], c[0], c[1], c[2]))

        # write the normals list
        if normals is not None and len(normals) > 0:
            for n in normals:
                f.write('vn %f %f %f\n' % (n[0], n[1], n[2]))

        # write the normals list
        for t in faces:
            f.write(
                'f %d %d %d\n' %
                (t[0] + 1, t[1] + 1, t[2] + 1))  # convert back to 1-indexing

        f.close()

        logging.info('Wrote mesh to %s', self.filepath_)
Example #5
0
    def write(self, mesh):
        '''
        Write a mesh to an obj file.
        Assumes mesh vertices, faces, and normals are in standard python list objects.
        Does not support material files or texture coordinates
        '''
        f = open(self.filepath_, 'w')
        vertices = mesh.vertices()
        faces = mesh.triangles()
        normals = mesh.normals()
        colors = mesh.colors()

        # write human-readable header
        f.write('###########################################################\n')
        f.write('# OBJ file generated by UC Berkeley Automation Sciences Lab\n')
        f.write('#\n')
        f.write('# Num Vertices: %d\n' %(len(vertices)))
        f.write('# Num Triangles: %d\n' %(len(faces)))
        f.write('#\n')
        f.write('###########################################################\n')
        f.write('\n')

        if colors is None:
            # write the vertex list
            for v in vertices:
                f.write('v %f %f %f\n' %(v[0], v[1], v[2]))
        else:
            assert len(vertices) == colors.shape[0], 'Color dimension mismatch'
            for v, c in zip(vertices, colors):
                f.write('v %f %f %f %f %f %f\n' %(v[0], v[1], v[2], c[0], c[1], c[2]))

        # write the normals list
        if normals is not None and len(normals) > 0:
            for n in normals:
                f.write('vn %f %f %f\n' %(n[0], n[1], n[2]))

        # write the normals list
        for t in faces:
            f.write('f %d %d %d\n' %(t[0]+1, t[1]+1, t[2]+1)) # convert back to 1-indexing

        f.close()

        logging.info('Wrote mesh to %s', self.filepath_)
Example #6
0
    def write(self, mesh):
        '''
        Write a mesh to an obj file.
        Assumes mesh vertices, faces, and normals are in standard python list objects.
        Does not support material files or texture coordinates
        '''
        f = open(self.filepath_, 'w')
        vertices = mesh.vertices()
        faces = mesh.triangles()
        normals = mesh.normals()

        # write human-readable header
        f.write(
            '###########################################################\n')
        f.write(
            '# OBJ file generated by UC Berkeley Automation Sciences Lab\n')
        f.write('#\n')
        f.write('# Num Vertices: %d\n' % (len(vertices)))
        f.write('# Num Triangles: %d\n' % (len(faces)))
        f.write('#\n')
        f.write(
            '###########################################################\n')
        f.write('\n')

        # write the vertex list
        for v in vertices:
            f.write('v %f %f %f\n' % (v[0], v[1], v[2]))

        # write the normals list
        if normals is not None and len(normals) > 0:
            for n in normals:
                f.write('vn %f %f %f\n' % (n[0], n[1], n[2]))

        # write the normals list
        for t in faces:
            f.write(
                'f %d %d %d\n' %
                (t[0] + 1, t[1] + 1, t[2] + 1))  # convert back to 1-indexing

        f.close()