def writeMeshFile(triangles, filename, binary=True, verbose=False): """Write mesh file. The output format is determined by file name extension. Files can be written in binary (default) and ASCII format.""" outformat = path.splitext(options.outfilename)[1].strip('.') # set writer based on filename extension if outformat=='stl': write = vtk.vtkSTLWriter() elif outformat=='vtk': write = vtk.vtkPolyDataWriter() elif outformat=='obj': write = vtk.vtkMNIObjectWriter() elif outformat=='tag': write = vtk.vtkMNITagPointWriter() else: raise ValueError('cannot write outpur format' + outformat) write.SetInputConnection(triangles.GetOutputPort()) if outformat!='tag': if binary: if verbose: print 'setting ouptut to binary' write.SetFileTypeToBinary() else: if verbose: print 'setting ouptut to ascii' write.SetFileTypeToASCII() write.SetFileName(filename) err = write.Write() if err != 1: raise IOError('failed to write') if verbose: print "wrote", filename pass
def writeMeshFile(triangles, filename, binary=True, verbose=False): """Write mesh file. The output format is determined by file name extension. Files can be written in binary (default) and ASCII format.""" outformat = path.splitext(options.outfilename)[1].strip('.') # set writer based on filename extension if outformat == 'stl': write = vtk.vtkSTLWriter() elif outformat == 'vtk': write = vtk.vtkPolyDataWriter() elif outformat == 'obj': write = vtk.vtkMNIObjectWriter() elif outformat == 'tag': write = vtk.vtkMNITagPointWriter() else: raise ValueError('cannot write outpur format' + outformat) write.SetInputConnection(triangles.GetOutputPort()) if outformat != 'tag': if binary: if verbose: print 'setting ouptut to binary' write.SetFileTypeToBinary() else: if verbose: print 'setting ouptut to ascii' write.SetFileTypeToASCII() write.SetFileName(filename) err = write.Write() if err != 1: raise IOError('failed to write') if verbose: print "wrote", filename pass
def save_polydata(polydata, file_name, binary=False, color_array_name=None, is_mni_obj=False): """Save a vtk polydata to a supported format file. Save formats can be VTK, FIB, PLY, STL and XML. Parameters ---------- polydata : vtkPolyData file_name : string binary : bool color_array_name: ndarray is_mni_obj : bool """ # get file extension (type) file_extension = file_name.split(".")[-1].lower() if file_extension == "vtk": writer = vtk.vtkPolyDataWriter() elif file_extension == "fib": writer = vtk.vtkPolyDataWriter() elif file_extension == "ply": writer = vtk.vtkPLYWriter() elif file_extension == "stl": writer = vtk.vtkSTLWriter() elif file_extension == "xml": writer = vtk.vtkXMLPolyDataWriter() elif file_extension == "obj": if is_mni_obj: writer = vtk.vtkMNIObjectWriter() else: # vtkObjWriter not available on python # vtk.vtkOBJWriter() raise IOError("OBJ Writer not available. MNI obj is the only" " available writer so set mni_tag option to True") else: raise IOError("Unknown extension ({})".format(file_extension)) writer.SetFileName(file_name) writer = set_input(writer, polydata) if color_array_name is not None and file_extension == "ply": writer.SetArrayName(color_array_name) if binary: writer.SetFileTypeToBinary() writer.Update() writer.Write()
def save_polydata(polydata, file_name, binary=False, color_array_name=None): """Save a vtk polydata to a supported format file. Save formats can be VTK, FIB, PLY, STL and XML. Parameters ---------- polydata : vtkPolyData file_name : string binary : bool color_array_name: ndarray """ # get file extension (type) file_extension = file_name.split(".")[-1].lower() poly_writer = { "vtk": vtk.vtkPolyDataWriter, "vtp": vtk.vtkXMLPolyDataWriter, "fib": vtk.vtkPolyDataWriter, "ply": vtk.vtkPLYWriter, "stl": vtk.vtkSTLWriter, "xml": vtk.vtkXMLPolyDataWriter } if file_extension in poly_writer.keys(): writer = poly_writer.get(file_extension)() elif file_extension == "obj": # Special case, since there is two obj format find_keyword = file_name.lower().split(".") if "mni" in find_keyword or "mnc" in find_keyword: writer = vtk.vtkMNIObjectWriter() else: raise IOError("Wavefront obj requires a scene \n" " for MNI obj, use '.mni.obj' extension") else: raise IOError("." + file_extension + " is not supported by FURY") writer.SetFileName(file_name) writer = set_input(writer, polydata) if color_array_name is not None and file_extension == "ply": writer.SetArrayName(color_array_name) if binary: writer.SetFileTypeToBinary() writer.Update() writer.Write()
print(n) # Extract the output and save it to new folder for i in range(n): mesh = out_block.GetBlock(i) # get id of said id = str(int(df_apoe_f.iloc[i, :].id_alomar)) # save to disk rd = vtk.vtkPolyDataWriter() f1 = mesh_aligned_l + 'out_mesh_' + str(id) + '_l.vtk' rd.SetInputData(mesh) rd.SetFileName(f1) rd.Write() # SAVE IT ALSO AS .OBJ obj = vtk.vtkMNIObjectWriter() f1 = mesh_aligned_l + 'out_mesh_' + str(id) + '_l.obj' obj.SetInputData(mesh) obj.SetFileName(f1) obj.Write() # Get the mean mesh and save it polydata = vtk.vtkPolyData() mean_pts_l = pcra.GetMeanPoints() test = ExtractVTKPoints(mean_pts_l) np.save("mean_pts_l.npy", test) polydata.SetPoints(mean_pts_l) # polydata.SetPoints(points_l) polydata.SetPolys(X_left[2].GetPolys())
property1 = asciiReader.GetProperty() if (asciiReader.CanReadFile(str(filename)) != 0): asciiReader.SetFileName(str(filename)) # this is just to remove the normals, to increase coverage, # i.e. by forcing the writer to generate normals removeNormals = vtk.vtkClipClosedSurface() removeNormals.SetInputConnection(asciiReader.GetOutputPort()) # this is to make triangle strips, also to increase coverage, # because it forces the writer to decompose the strips stripper = vtk.vtkStripper() stripper.SetInputConnection(removeNormals.GetOutputPort()) # test binary writing and reading for polygons binaryWriter = vtk.vtkMNIObjectWriter() binaryWriter.SetInputConnection(stripper.GetOutputPort()) binaryWriter.SetFileName("mni-surface-mesh-binary.obj") binaryWriter.SetProperty(property0) binaryWriter.SetFileTypeToBinary() binaryWriter.Write() binaryReader = vtk.vtkMNIObjectReader() binaryReader.SetFileName("mni-surface-mesh-binary.obj") property2 = binaryReader.GetProperty() # make a polyline object with color scalars scalars = vtk.vtkCurvatures() scalars.SetInputConnection(asciiReader.GetOutputPort())
property1 = asciiReader.GetProperty() if (asciiReader.CanReadFile(str(filename)) != 0): asciiReader.SetFileName(str(filename)) # this is just to remove the normals, to increase coverage, # i.e. by forcing the writer to generate normals removeNormals = vtk.vtkClipClosedSurface() removeNormals.SetInputConnection(asciiReader.GetOutputPort()) # this is to make triangle strips, also to increase coverage, # because it forces the writer to decompose the strips stripper = vtk.vtkStripper() stripper.SetInputConnection(removeNormals.GetOutputPort()) # test binary writing and reading for polygons binaryWriter = vtk.vtkMNIObjectWriter() binaryWriter.SetInputConnection(stripper.GetOutputPort()) binaryWriter.SetFileName("mni-surface-mesh-binary.obj") binaryWriter.SetProperty(property0) binaryWriter.SetFileTypeToBinary() binaryWriter.Write() binaryReader = vtk.vtkMNIObjectReader() binaryReader.SetFileName("mni-surface-mesh-binary.obj") property2 = binaryReader.GetProperty() # make a polyline object with color scalars scalars = vtk.vtkCurvatures() scalars.SetInputConnection(asciiReader.GetOutputPort())