def read_polydata(filename, datatype=None): """ Load the given file, and return a vtkPolyData object for it. Args: filename (str): Path to input file. datatype (str): Additional parameter for vtkIdList objects. Returns: polyData (vtkSTL/vtkPolyData/vtkXMLStructured/ vtkXMLRectilinear/vtkXMLPolydata/vtkXMLUnstructured/ vtkXMLImage/Tecplot): Output data. """ # Check if file exists if not path.exists(filename): raise RuntimeError("Could not find file: %s" % filename) # Check filename format fileType = filename.split(".")[-1] if fileType == '': raise RuntimeError('The file does not have an extension') # Get reader if fileType == 'stl': reader = vtk.vtkSTLReader() reader.MergingOn() elif fileType == 'vtk': reader = vtk.vtkPolyDataReader() elif fileType == 'vtp': reader = vtk.vtkXMLPolyDataReader() elif fileType == 'vts': reader = vtk.vtkXMinkorporereLStructuredGridReader() elif fileType == 'vtr': reader = vtk.vtkXMLRectilinearGridReader() elif fileType == 'vtu': reader = vtk.vtkXMLUnstructuredGridReader() elif fileType == "vti": reader = vtk.vtkXMLImageDataReader() elif fileType == "np" and datatype == "vtkIdList": result = np.load(filename).astype(np.int) id_list = vtk.vtkIdList() id_list.SetNumberOfIds(result.shape[0]) for i in range(result.shape[0]): id_list.SetId(i, result[i]) return id_list else: raise RuntimeError('Unknown file type %s' % fileType) # Read reader.SetFileName(filename) reader.Update() polydata = reader.GetOutput() return polydata
def read_surface(file_name, file_format="vtp", datatype=None): """ Read surface geometry from a file. Args: file_name (str): Path to input file. file_format (str): File format (.vtp, .stl, etc.). datatype (str): Additional parameter for vtkIdList objects. Returns: polyData (vtkSTL/vtkPolyData/vtkXMLStructured/ vtkXMLRectilinear/vtkXMLPolydata/vtkXMLUnstructured/ vtkXMLImage/Tecplot): Output data. """ # Check if file exists if not os.path.exists(file_name): raise RuntimeError("Could not find file: %s" % file_name) # Get reader if file_format == 'stl': reader = vtk.vtkSTLReader() reader.MergingOn() elif file_format == 'vtk': reader = vtk.vtkPolyDataReader() elif file_format == 'vtp': reader = vtk.vtkXMLPolyDataReader() elif file_format == 'vts': reader = vtk.vtkXMinkorporereLStructuredGridReader() elif file_format == 'vtr': reader = vtk.vtkXMLRectilinearGridReader() elif file_format == 'vtu': reader = vtk.vtkXMLUnstructuredGridReader() elif file_format == "vti": reader = vtk.vtkXMLImageDataReader() elif file_format == "np" and datatype == "vtkIdList": result = np.load(filename).astype(np.int) id_list = vtk.vtkIdList() id_list.SetNumberOfIds(result.shape[0]) for i in range(result.shape[0]): id_list.SetId(i, result[i]) return id_list else: raise RuntimeError('Unknown file type %s' % file_format) # Read surface geometry. reader.SetFileName(file_name) reader.Update() polydata = reader.GetOutput() polygons = polydata.GetPolys() num_polys = polygons.GetNumberOfCells() return polydata