def get(self,label): """ Get either cell or point data. Cell data takes precedence over point data, i.e. this function assumes that labels are unique among cell and point data. Parameters ---------- label : str Data label. """ cell_data = self.vtk_data.GetCellData() for a in range(cell_data.GetNumberOfArrays()): if cell_data.GetArrayName(a) == label: return vtk_to_np(cell_data.GetArray(a)) point_data = self.vtk_data.GetPointData() for a in range(point_data.GetNumberOfArrays()): if point_data.GetArrayName(a) == label: return vtk_to_np(point_data.GetArray(a)) raise ValueError(f'Array "{label}" not found.')
def vtkReadVTKfile(self, filename): """ Lecture d'un fichier vtk""" # Read the source file. reader = vtk.vtkUnstructuredGridReader() reader.SetFileName(filename) reader.Update() # Needed because of GetScalarRange surface_filter = vtk.vtkDataSetSurfaceFilter() surface_filter.SetInputConnection(reader.GetOutputPort()) self.meshNormals = vtk.vtkPolyDataNormals() self.meshNormals.SetInputConnection(surface_filter.GetOutputPort()) self.meshNormals.SetFeatureAngle(60.0) self.meshNormals.ComputeCellNormalsOn() self.meshNormals.ComputePointNormalsOn() self.meshNormals.ConsistencyOn() self.meshNormals.AutoOrientNormalsOn() self.meshNormals.Update() # Sauvegarde des proprietes maillage GMSH pour calcul centerline self.gmeshNode = vtk_to_np(surface_filter.GetOutput().GetPoints().GetData()) self.gmeshTable = vtk_to_np(surface_filter.GetOutput().GetPolys().GetData()) # The quadric has nasty discontinuities from the way the edges are generated # so let's pass it though a CleanPolyDataFilter and merge any points which # are coincident, or very close self.outputMesh = vtk.vtkCleanPolyData() self.outputMesh.SetInputConnection(surface_filter.GetOutputPort()) self.outputMesh.SetTolerance(0.001)
def vtkCreateFilterIsoContour(self): """ Fonction pour le filtrage du contour""" #----------------------------------------- # Application d'un filtre de subdivision #----------------------------------------- self.subdiviseAneurysm = vtk.vtkLoopSubdivisionFilter() self.subdiviseAneurysm.SetNumberOfSubdivisions(self.valFilter) self.subdiviseAneurysm.SetInputConnection(self.aneurysmNormals.GetOutputPort()) #----------------------------------------- # Application d'un filtre decimate et smooth #----------------------------------------- self.aneurysmDecimator = vtk.vtkDecimatePro() self.aneurysmDecimator.SetInputConnection(self.subdiviseAneurysm.GetOutputPort()) self.aneurysmDecimator.SetTargetReduction(self.valDecimate) self.aneurysmDecimator.PreserveTopologyOn() self.smoothAneurysm = vtk.vtkSmoothPolyDataFilter() self.smoothAneurysm.SetInputConnection(self.aneurysmDecimator.GetOutputPort()) self.smoothAneurysm.SetNumberOfIterations(self.valSmoothIter) self.smoothAneurysm.SetRelaxationFactor(0.07) self.smoothAneurysm.SetFeatureAngle(90) self.smoothAneurysm.FeatureEdgeSmoothingOn() #----------------------------------------- # Sauvegarde des datas pour le calcul des centerlines #----------------------------------------- surface_filter = vtk.vtkDataSetSurfaceFilter() surface_filter.SetInputConnection(self.smoothAneurysm.GetOutputPort()) meshNormals = vtk.vtkPolyDataNormals() meshNormals.SetInputConnection(surface_filter.GetOutputPort()) meshNormals.Update() self.surfNode = vtk_to_np(self.smoothAneurysm.GetOutput().GetPoints().GetData()) self.surfTable = vtk_to_np(self.smoothAneurysm.GetOutput().GetPolys().GetData()) #----------------------------------------- # Realisation du mapper ppour affichage #----------------------------------------- self.aneurysmStripper = vtk.vtkStripper() self.aneurysmStripper.SetInputConnection(self.smoothAneurysm.GetOutputPort()) self.aneurysmMapper = vtk.vtkPolyDataMapper() self.aneurysmMapper.SetInputConnection(self.aneurysmStripper.GetOutputPort()) self.aneurysmMapper.ScalarVisibilityOff() self.aneurysmMapper.SetScalarRange(self.min_pixel_value, self.max_pixel_value) self.aneurysmMapper.SetScalarModeToUsePointData() self.aneurysmMapper.SetLookupTable(self.bwLut)
def get(self, label): """ Get either cell or point data. Cell data takes precedence over point data, i.e. this function assumes that labels are unique among cell and point data. Parameters ---------- label : str Data label. Returns ------- data : numpy.ndarray Data stored under the given label. """ cell_data = self.vtk_data.GetCellData() for a in range(cell_data.GetNumberOfArrays()): if cell_data.GetArrayName(a) == label: try: return vtk_to_np(cell_data.GetArray(a)) except AttributeError: vtk_array = cell_data.GetAbstractArray(a) # string array point_data = self.vtk_data.GetPointData() for a in range(point_data.GetNumberOfArrays()): if point_data.GetArrayName(a) == label: try: return vtk_to_np(point_data.GetArray(a)) except AttributeError: vtk_array = point_data.GetAbstractArray(a) # string array try: # string array return np.array([ vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues()) ]).astype(str) except UnboundLocalError: raise ValueError(f'Array "{label}" not found.')
def vtkReadVolumAll(self, filename): """ Chargement des fichiers images dicom""" #----------------------------------------- # Creation de la donnee volumique #----------------------------------------- self.vtkVolumAll = vtk.vtkVolume16Reader() self.vtkVolumAll.SetDataDimensions(self.col, self.lig) self.vtkVolumAll.SetDataByteOrderToLittleEndian() self.vtkVolumAll.SetFilePrefix(filename) self.vtkVolumAll.SetImageRange(1, self.nb_coupe) self.vtkVolumAll.SetDataSpacing(self.pixel_spacing_x, self.pixel_spacing_y, self.slice_spacing) self.SizeOfVolum = np.array([self.lig*self.pixel_spacing_x,self.col*self.pixel_spacing_y,self.slice_spacing*self.nb_coupe]) self.NumpyData = np.zeros((self.lig,self.col,self.nb_coupe),int) for i in range(self.nb_coupe): self.NumpyData[:,:,i] = vtk_to_np(self.vtkVolumAll.GetImage(i+1).GetPointData().GetArray(0)).reshape(self.lig,self.col)
def load(fname): """ Load from VTK rectilinear grid file. Parameters ---------- fname : str or or pathlib.Path Grid file to read. Valid extension is .vtr, which will be appended if not given. Returns ------- loaded : damask.Grid Grid-based geometry from file. """ v = VTK.load(fname if str(fname).endswith('.vtr') else str(fname) + '.vtr') comments = v.get_comments() cells = np.array(v.vtk_data.GetDimensions()) - 1 bbox = np.array(v.vtk_data.GetBounds()).reshape(3, 2).T for i, c in enumerate([ v.vtk_data.GetXCoordinates(), v.vtk_data.GetYCoordinates(), v.vtk_data.GetZCoordinates() ]): if not np.allclose( vtk_to_np(c), np.linspace(bbox[0][i], bbox[1][i], cells[i] + 1)): raise ValueError('regular grid spacing violated') return Grid(material=v.get('material').reshape(cells, order='F'), size=bbox[1] - bbox[0], origin=bbox[0], comments=comments)