def generateMesh(self, x, y, z, in_x, in_y, in_z): x_coord = vtk.vtkFloatArray() x_coord.InsertNextValue(x) y_coord = vtk.vtkFloatArray() y_coord.InsertNextValue(y) z_coord = vtk.vtkFloatArray() z_coord.InsertNextValue(z) grid = vtk.vtkRectilinearGrid() grid.SetDimensions(self.nx if in_x else 1, self.ny if in_y else 1, self.nz if in_z else 1) grid.SetXCoordinates(self.x_coords if in_x else x_coord); grid.SetYCoordinates(self.y_coords if in_y else y_coord); grid.SetZCoordinates(self.z_coords if in_z else z_coord); # We're going to generate all of the IDs of the cells in that rectilinear grid so we can extract them as an UnstructuredGrid # Why would we do such a thing? Because there is some bug associated with RecitilinearGrids and clipping / rendering # So, instead I'm going to use RectilinearGrid for generating the cells and then "copy" it to an UnstructuredGrid using ExtractCells num_cells = grid.GetNumberOfCells() id_list = vtk.vtkIdList() for i in xrange(num_cells): id_list.InsertNextId(i) extract = vtk.vtkExtractCells() if vtk.VTK_MAJOR_VERSION <= 5: extract.SetInput(grid) else: extract.SetInputData(grid) extract.SetCellList(id_list) return extract.GetOutput()
def cpnonzerocolor(data): CellIdList = vtk.vtkIdList() CellIdList.Reset() colors = data.GetCellData().GetScalars() count = 0 # Get indices from nonzero colored cells for i in range(data.GetNumberOfCells()): if ( colors.GetTuple(i) != (0.0, 0.0, 0.0, 0.0) ): CellIdList.InsertId(count, i) count = count + 1 # print "Number Of cell:\t",i # print "Number of cell added:\t",count else: pass # Extract cells with nonzero color from data extractor = vtk.vtkExtractCells() extractor.SetInputData(data) extractor.SetCellList(CellIdList) extractor.Modified() extractor.Update() print "Number of nodes in clipped subvolume:\t",extractor.GetOutput().GetNumberOfPoints() print "Number of egdes in clipped subvolume:\t",extractor.GetOutput().GetNumberOfCells() # rearrange the id's of the cells and points - consecutive increasing ids print "...extracted zerocolor cells." return extractor.GetOutput()
def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkExtractCells(), 'Processing.', ('vtkDataSet',), ('vtkUnstructuredGrid',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def write_unstructured_grid(filename, mesh, cdata, nEle, EFs, time, verbose, outline=False, cut=[]): writer = vtk.vtkXMLUnstructuredGridWriter() ## writer.SetDataModeToBinary() writer.SetDataModeToAscii() writer.SetFileName(filename + '.vtu') extract = vtk.vtkExtractCells() extract.SetInputData(mesh) eleList = vtk.vtkIdList() EF = cdata.GetArray('EF') for k in range(nEle): if (EFs[EF.GetValue(k)][0] < time and EFs[EF.GetValue(k)][1] >= time) or (EFs[EF.GetValue(k)][0] == 0 and time == 0): a = eleList.InsertNextId(k) extract.SetCellList(eleList) grid = extract.GetOutputPort() if outline: gf = vtk.vtkGeometryFilter() gf.SetInputConnection(grid) cpd = vtk.vtkCleanPolyData() cpd.SetInputConnection(gf.GetOutputPort()) cpd.Update() af = vtk.vtkAppendFilter() af.AddInputData(cpd.GetOutput()) grid = af.GetOutputPort() elif len(cut): plane = vtk.vtkPlane() plane.SetOrigin(cut[0]) plane.SetNormal(cut[1]) cutter = vtk.vtkCutter() cutter.SetInputConnection(grid) cutter.SetCutFunction(plane) cutter.Update() cpd = vtk.vtkCleanPolyData() cpd.SetInputConnection(cutter.GetOutputPort()) cpd.Update() af = vtk.vtkAppendFilter() af.AddInputData(cpd.GetOutput()) grid = af.GetOutputPort() writer.SetFileName(filename + '_cut.vtu') writer.SetInputConnection(grid) writer.Write() if not verbose: print('%i elements written to %s' % (eleList.GetNumberOfIds(), filename))
def extractcells(polydata, idlist=[0, 1, 2]): """Extract cells from polydata whose cellid is in idlist.""" cellids = vtk.vtkIdList() # specify cellids cellids.Initialize() for i in idlist: cellids.InsertNextId(i) extract = vtk.vtkExtractCells() # extract cells with specified cellids extract.SetInput(polydata) extract.AddCellList(cellids) extraction = extract.GetOutput() geometry = vtk.vtkGeometryFilter() # unstructured grid to polydata geometry.SetInput(extraction) return geometry.GetOutput()
interpolateAttr = 0 computeNormals = 0 # # Quadric definition quadric = vtk.vtkQuadric() quadric.SetCoefficients([.5, 1, .2, 0, .1, 0, 0, .2, 0, 0]) sample = vtk.vtkSampleFunction() sample.SetSampleDimensions(res, res, res) sample.SetImplicitFunction(quadric) sample.ComputeNormalsOn() sample.Update() # # Extract voxel cells extract = vtk.vtkExtractCells() extract.SetInputConnection(sample.GetOutputPort()) extract.AddCellRange(0, sample.GetOutput().GetNumberOfCells()) extract.Update() # Now contour the cells, using scalar tree or not stree = vtk.vtkSpanSpace() stree.SetDataSet(extract.GetOutput()) stree.SetNumberOfCellsPerBucket(1) contour = vtk.vtkContour3DLinearGrid() contour.SetInputConnection(extract.GetOutputPort()) contour.SetValue(0, 0.5) contour.SetValue(1, 0.75) contour.SetMergePoints(mergePoints) contour.SetSequentialProcessing(serialProcessing)
#res = 200 res = 50 # Test cell extraction # # Quadric definition quadric = vtk.vtkQuadric() quadric.SetCoefficients([.5,1,.2,0,.1,0,0,.2,0,0]) sample = vtk.vtkSampleFunction() sample.SetSampleDimensions(res,res,res) sample.SetImplicitFunction(quadric) sample.ComputeNormalsOff() sample.Update() # Now extract the cells: use badly formed range extract = vtk.vtkExtractCells() extract.SetInputConnection(sample.GetOutputPort()) extract.AddCellRange(0,sample.GetOutput().GetNumberOfCells()) extrMapper = vtk.vtkDataSetMapper() extrMapper.SetInputConnection(extract.GetOutputPort()) extrMapper.ScalarVisibilityOff() extrActor = vtk.vtkActor() extrActor.SetMapper(extrMapper) extrActor.GetProperty().SetColor(.8,.4,.4) # Extract interior range of cells extract2 = vtk.vtkExtractCells() extract2.SetInputConnection(sample.GetOutputPort()) extract2.AddCellRange(100,5000)
def write_vtu_diff(res, step0, step1, name, verbose=True, beams=False, vol=False, shells=False, trusses=False, disp=True): if not verbose: print('writing step %i' % (kt)) ## tstr = str(int(step1.time)).rjust(3,'0')+'_'+str(int((step1.time-int(step1.time))*100)).rjust(2,'0') ## tstr += '-'+str(int(step0.time)).rjust(3,'0')+'_'+str(int((step0.time-int(step0.time))*100)).rjust(2,'0') if vol: # write vtu for volumics: writer = vtk.vtkXMLUnstructuredGridWriter() writer.DebugOn() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + name + '_vol.vtu') pdata = res.vol.mesh.GetPointData() if step1.nodal.disp.GetNumberOfTuples() > 0: anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(3) anArray.SetName(step1.nodal.disp.GetName()) for kt in range(step1.nodal.disp.GetNumberOfTuples()): tup0 = step0.nodal.disp.GetTuple(kt) tup1 = step1.nodal.disp.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(1) anArray.SetName(step1.nodal.ppres.GetName()) for kt in range(step1.nodal.ppres.GetNumberOfTuples()): tup0 = step0.nodal.ppres.GetTuple(kt) tup1 = step1.nodal.ppres.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) for att in dir(step0.vol): if att not in ['__doc__', '__init__', '__module__']: cdata = res.vol.mesh.GetCellData() anArray = vtk.vtkFloatArray() att0 = getattr(step0.vol, att) att1 = getattr(step1.vol, att) if att1.GetNumberOfTuples() > 0: anArray.SetNumberOfComponents(att1.GetNumberOfComponents()) anArray.SetName(att1.GetName()) for kt in range(att1.GetNumberOfTuples()): tup0 = att0.GetTuple(kt) tup1 = att1.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) cdata.AddArray(anArray) ## cdata = res.vol.mesh.GetCellData() ## anArray = vtk.vtkFloatArray() ## anArray.SetNumberOfComponents(step1.vol.strain.GetNumberOfComponents()) ## anArray.SetName(step1.vol.strain.GetName()) ## for kt in range(step1.vol.strain.GetNumberOfTuples()): ## tup0 = step0.vol.strain.GetTuple(kt) ## tup1 = step1.vol.strain.GetTuple(kt) ## anArray.InsertNextTuple(tuple(map(lambda x,y:x-y,tup1,tup0))) ## cdata.AddArray(anArray) ## ## anArray = vtk.vtkFloatArray() ## anArray.SetNumberOfComponents(step1.vol.stress.GetNumberOfComponents()) ## anArray.SetName(step1.vol.stress.GetName()) ## for kt in range(step1.vol.stress.GetNumberOfTuples()): ## tup0 = step0.vol.stress.GetTuple(kt) ## tup1 = step1.vol.stress.GetTuple(kt) ## anArray.InsertNextTuple(tuple(map(lambda x,y:x-y,tup1,tup0))) ## cdata.AddArray(anArray) ## ## anArray = vtk.vtkFloatArray() ## anArray.SetNumberOfComponents(step1.vol.str_level.GetNumberOfComponents()) ## anArray.SetName(step1.vol.str_level.GetName()) ## for kt in range(step1.vol.str_level.GetNumberOfTuples()): ## tup0 = step0.vol.str_level.GetTuple(kt) ## tup1 = step1.vol.str_level.GetTuple(kt) ## anArray.InsertNextTuple(tuple(map(lambda x,y:x-y,tup1,tup0))) ## cdata.AddArray(anArray) cdata.AddArray(res.vol.EF) cdata.AddArray(res.vol.LF) cdata.AddArray(res.vol.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.vol.mesh) eleList = vtk.vtkIdList() for k in range(res.nVolumics): if res.EF[res.vol.EF.GetValue(k)][0] <= step1.time and res.EF[ res.vol.EF.GetValue(k)][1] > step1.time: a = eleList.InsertNextId(k) if not verbose: print('%i volumics written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if shells: # write vtu for shells: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + name + '_shell.vtu') pdata = res.shell.mesh.GetPointData() if step1.nodal.disp.GetNumberOfTuples() > 0: anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(3) anArray.SetName(step1.nodal.disp.GetName()) for kt in range(step1.nodal.disp.GetNumberOfTuples()): tup0 = step0.nodal.disp.GetTuple(kt) tup1 = step1.nodal.disp.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(1) anArray.SetName(step1.nodal.ppres.GetName()) for kt in range(step1.nodal.ppres.GetNumberOfTuples()): tup0 = step0.nodal.ppres.GetTuple(kt) tup1 = step1.nodal.ppres.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) for att in dir(step0.shell): if att not in ['__doc__', '__init__', '__module__']: cdata = res.shell.mesh.GetCellData() anArray = vtk.vtkFloatArray() att0 = getattr(step0.shell, att) att1 = getattr(step1.shell, att) if att1.GetNumberOfTuples() > 0: anArray.SetNumberOfComponents(att1.GetNumberOfComponents()) anArray.SetName(att1.GetName()) for kt in range(att1.GetNumberOfTuples()): tup0 = att0.GetTuple(kt) tup1 = att1.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) cdata.AddArray(anArray) cdata.AddArray(res.shell.EF) cdata.AddArray(res.shell.LF) cdata.AddArray(res.shell.mat) cdata.AddArray(step1.shell.thick) extract = vtk.vtkExtractCells() extract.SetInputData(res.shell.mesh) eleList = vtk.vtkIdList() for k in range(res.nShells): if res.EF[res.shell.EF.GetValue(k)][0] <= step1.time and res.EF[ res.shell.EF.GetValue(k)][1] > step1.time: a = eleList.InsertNextId(k) if not verbose: print('%i shells written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if trusses: # write vtu for trusses: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + name + '_truss.vtu') pdata = res.truss.mesh.GetPointData() if step1.nodal.disp.GetNumberOfTuples() > 0: anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(3) anArray.SetName(step1.nodal.disp.GetName()) for kt in range(step1.nodal.disp.GetNumberOfTuples()): tup0 = step0.nodal.disp.GetTuple(kt) tup1 = step1.nodal.disp.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) cdata = res.truss.mesh.GetCellData() for att in dir(step0.truss): if att not in ['__doc__', '__init__', '__module__']: cdata = res.truss.mesh.GetCellData() anArray = vtk.vtkFloatArray() att0 = getattr(step0.truss, att) att1 = getattr(step1.truss, att) if att1.GetNumberOfTuples() > 0: anArray.SetNumberOfComponents(att1.GetNumberOfComponents()) anArray.SetName(att1.GetName()) for kt in range(att1.GetNumberOfTuples()): tup0 = att0.GetTuple(kt) tup1 = att1.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) cdata.AddArray(anArray) cdata.AddArray(res.truss.EF) cdata.AddArray(res.truss.LF) cdata.AddArray(res.truss.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.truss.mesh) eleList = vtk.vtkIdList() for k in range(res.nTrusses): if res.EF[res.truss.EF.GetValue(k)][0] <= step1.time and res.EF[ res.truss.EF.GetValue(k)][1] > step1.time: a = eleList.InsertNextId(k) if not verbose: print('%i trusses written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if beams: # write vtu for beams: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + name + '_beam.vtu') pdata = res.beam.mesh.GetPointData() if step1.nodal.disp.GetNumberOfTuples() > 0: anArray = vtk.vtkFloatArray() anArray.SetNumberOfComponents(3) anArray.SetName(step1.nodal.disp.GetName()) for kt in range(step1.nodal.disp.GetNumberOfTuples()): tup0 = step0.nodal.disp.GetTuple(kt) tup1 = step1.nodal.disp.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) pdata.AddArray(anArray) cdata = res.beam.mesh.GetCellData() for att in dir(step0.beam): if att not in ['__doc__', '__init__', '__module__', 'dm']: cdata = res.beam.mesh.GetCellData() anArray = vtk.vtkFloatArray() att0 = getattr(step0.beam, att) att1 = getattr(step1.beam, att) if att1.GetNumberOfTuples() > 0: anArray.SetNumberOfComponents(att1.GetNumberOfComponents()) anArray.SetName(att1.GetName()) for kt in range(att1.GetNumberOfTuples()): tup0 = att0.GetTuple(kt) tup1 = att1.GetTuple(kt) anArray.InsertNextTuple( tuple(map(lambda x, y: x - y, tup1, tup0))) cdata.AddArray(anArray) cdata.AddArray(res.beam.EF) cdata.AddArray(res.beam.LF) cdata.AddArray(res.beam.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.beam.mesh) eleList = vtk.vtkIdList() for k in range(res.nBeams): if res.EF[res.beam.EF.GetValue(k)][0] <= step1.time and res.EF[ res.beam.EF.GetValue(k)][1] > step1.time: a = eleList.InsertNextId(k) if not verbose: print('%i beams written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write()
def write_vtu(res, tsteps='all', verbose=True, beams=False, vol=False, shells=False, trusses=False, disp=True): if tsteps == 'all': tsteps = res.out_steps for kt in tsteps: step = res.steps[kt] if not verbose: print('writing step %i' % (kt)) tstr = str(int(step.time)).rjust(3, '0') + '_' + str( int((step.time - int(step.time)) * 100)).rjust(2, '0') if vol: # write vtu for volumics: writer = vtk.vtkXMLUnstructuredGridWriter() writer.DebugOn() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + tstr + '_vol.vtu') pdata = res.vol.mesh.GetPointData() if step.nodal.disp.GetNumberOfTuples() > 0: pdata.AddArray(step.nodal.disp) if step.nodal.ppres.GetNumberOfTuples() > 0: pdata.AddArray(step.nodal.ppres) cdata = res.vol.mesh.GetCellData() if step.vol.strain.GetNumberOfTuples() > 0: cdata.AddArray(step.vol.strain) cdata.AddArray(step.vol.stress) cdata.AddArray(step.vol.str_level) cdata.AddArray(res.vol.EF) cdata.AddArray(res.vol.LF) cdata.AddArray(res.vol.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.vol.mesh) eleList = vtk.vtkIdList() for k in range(res.nVolumics): if res.EF[res.vol.EF.GetValue(k)][0] <= step.time and res.EF[ res.vol.EF.GetValue(k)][1] > step.time: a = eleList.InsertNextId(k) if not verbose: print('%i volumics written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if shells: # write vtu for shells: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + tstr + '_shell.vtu') pdata = res.shell.mesh.GetPointData() if step.nodal.disp.GetNumberOfTuples() > 0: pdata.AddArray(step.nodal.disp) pdata.AddArray(step.nodal.ppres) cdata = res.shell.mesh.GetCellData() cdata.AddArray(step.shell.smforce) cdata.AddArray(step.shell.smoment) cdata.AddArray(step.shell.sqforce) cdata.AddArray(res.shell.EF) cdata.AddArray(res.shell.LF) cdata.AddArray(res.shell.mat) cdata.AddArray(step.shell.thick) extract = vtk.vtkExtractCells() extract.SetInputData(res.shell.mesh) eleList = vtk.vtkIdList() for k in range(res.nShells): if res.EF[res.shell.EF.GetValue(k)][0] <= step.time and res.EF[ res.shell.EF.GetValue(k)][1] > step.time: a = eleList.InsertNextId(k) if not verbose: print('%i shells written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if trusses: # write vtu for trusses: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + tstr + '_truss.vtu') pdata = res.truss.mesh.GetPointData() if step.nodal.disp.GetNumberOfTuples() > 0: pdata.AddArray(step.nodal.disp) pdata.AddArray(step.nodal.ppres) cdata = res.truss.mesh.GetCellData() cdata.AddArray(step.truss.force) cdata.AddArray(res.truss.EF) cdata.AddArray(res.truss.LF) cdata.AddArray(res.truss.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.truss.mesh) eleList = vtk.vtkIdList() for k in range(res.nTrusses): if res.EF[res.truss.EF.GetValue(k)][0] <= step.time and res.EF[ res.truss.EF.GetValue(k)][1] > step.time: a = eleList.InsertNextId(k) if not verbose: print('%i trusses written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write() if beams: # write vtu for beams: writer = vtk.vtkXMLUnstructuredGridWriter() writer.SetDataModeToBinary() writer.SetFileName(res.problem_name + '_' + tstr + '_beam.vtu') pdata = res.beam.mesh.GetPointData() if step.nodal.disp.GetNumberOfTuples() > 0: pdata.AddArray(step.nodal.disp) pdata.AddArray(step.nodal.ppres) cdata = res.beam.mesh.GetCellData() cdata.AddArray(step.beam.force) cdata.AddArray(step.beam.moment) cdata.AddArray(res.beam.EF) cdata.AddArray(res.beam.LF) cdata.AddArray(res.beam.mat) extract = vtk.vtkExtractCells() extract.SetInputData(res.beam.mesh) eleList = vtk.vtkIdList() for k in range(res.nBeams): if res.EF[res.beam.EF.GetValue(k)][0] <= step.time and res.EF[ res.beam.EF.GetValue(k)][1] > step.time: a = eleList.InsertNextId(k) if not verbose: print('%i beams written' % (eleList.GetNumberOfIds())) extract.SetCellList(eleList) grid = extract.GetOutputPort() writer.SetInputConnection(grid) writer.Write()
sampleL.Update() # # Quadric definition quadricR = vtk.vtkQuadric() quadricR.SetCoefficients([.5,1,.2,0,.1,0,0,.2,0,0]) sampleR = vtk.vtkSampleFunction() sampleR.SetModelBounds(0,1, -1,1, -1,1) sampleR.SetSampleDimensions(int(res/2),res,res) sampleR.SetImplicitFunction(quadricR) sampleR.ComputeNormalsOn() sampleR.Update() # # Extract voxel cells extractL = vtk.vtkExtractCells() extractL.SetInputConnection(sampleL.GetOutputPort()) extractL.AddCellRange(0,sampleL.GetOutput().GetNumberOfCells()) extractL.Update() # # Extract voxel cells extractR = vtk.vtkExtractCells() extractR.SetInputConnection(sampleR.GetOutputPort()) extractR.AddCellRange(0,sampleR.GetOutput().GetNumberOfCells()) extractR.Update() # Create a composite dataset. Throw in an extra polydata which should be skipped. sphere = vtk.vtkSphereSource() sphere.SetCenter(1,0,0) sphere.Update()
contour.SetSequentialProcessing(serialProcessing) contour.SetInterpolateAttributes(0); contour.SetComputeNormals(0); contMapper = vtk.vtkPolyDataMapper() contMapper.SetInputConnection(contour.GetOutputPort()) contMapper.ScalarVisibilityOff() contActor = vtk.vtkActor() contActor.SetMapper(contMapper) contActor.GetProperty().SetColor(.8,.4,.4) # Test voxel contouring # # Extract voxel cells extract2 = vtk.vtkExtractCells() extract2.SetInputConnection(sample.GetOutputPort()) extract2.AddCellRange(0,sample.GetOutput().GetNumberOfCells()) extract2.Update() # Now contour the voxels contour2 = vtk.vtkContour3DLinearGrid() contour2.SetInputConnection(extract2.GetOutputPort()) contour2.SetValue(0, 0.5) contour2.SetValue(1, 0.9) contour2.SetMergePoints(mergePoints) contour2.SetSequentialProcessing(serialProcessing) contour2.SetInterpolateAttributes(interpolateAttr); contour2.SetComputeNormals(computeNormals); cont2Mapper = vtk.vtkPolyDataMapper()
sampleL.Update() # # Quadric definition quadricR = vtk.vtkQuadric() quadricR.SetCoefficients([.5, 1, .2, 0, .1, 0, 0, .2, 0, 0]) sampleR = vtk.vtkSampleFunction() sampleR.SetModelBounds(0, 1, -1, 1, -1, 1) sampleR.SetSampleDimensions(int(res / 2), res, res) sampleR.SetImplicitFunction(quadricR) sampleR.ComputeNormalsOn() sampleR.Update() # # Extract voxel cells extractL = vtk.vtkExtractCells() extractL.SetInputConnection(sampleL.GetOutputPort()) extractL.AddCellRange(0, sampleL.GetOutput().GetNumberOfCells()) extractL.Update() # # Extract voxel cells extractR = vtk.vtkExtractCells() extractR.SetInputConnection(sampleR.GetOutputPort()) extractR.AddCellRange(0, sampleR.GetOutput().GetNumberOfCells()) extractR.Update() # Create a composite dataset. Throw in an extra polydata which should be skipped. sphere = vtk.vtkSphereSource() sphere.SetCenter(1, 0, 0) sphere.Update()
def get_section_vol(mesh, plane, loc_syst, celldata=False, array='DISP_TRA', component=-1, mesh0=0, matlist=[], EFlist=[], LFlist=[]): cut = vtk.vtkCutter() cut.SetInputData(mesh) cut.SetCutFunction(plane) cut.Update() if celldata: output0 = cut.GetOutput() cdata = output0.GetCellData() mat = cdata.GetArray('mat') EF = cdata.GetArray('EF') LF = cdata.GetArray('LF') extract = vtk.vtkExtractCells() extract.SetInputData(output0) eleList = vtk.vtkIdList() for ke in range(output0.GetNumberOfCells()): if len(matlist) == 0 or mat.GetTuple1(ke) in matlist: if len(EFlist) == 0 or EF.GetTuple1(ke) in EFlist: if len(LFlist) == 0 or LF.GetTuple1(ke) in LFlist: a = eleList.InsertNextId(ke) extract.SetCellList(eleList) output1 = extract.GetOutputPort() geom = vtk.vtkGeometryFilter() geom.SetInputConnection(output1) geom.Update() output = geom.GetOutput() c2p = vtk.vtkCellDataToPointData() c2p.SetInputData(output) c2p.Update() pdata = c2p.GetOutput().GetPointData() else: output = cut.GetOutput() pdata = output.GetPointData() anArray = pdata.GetArray(array) if anArray is None: print('Array ' + array + ' is not found.') val = [] inel = [] crd = [[], []] orig = plane.GetOrigin() for kp in range(output.GetNumberOfPoints()): p = output.GetPoint(kp) crd_2D = project_on_plane(loc_syst, orig, p) crd[0].append(crd_2D[0]) crd[1].append(crd_2D[1]) if component == -1: val.append(anArray.GetTuple(kp)) else: val.append(anArray.GetTuple(kp)[component]) for kp in range(output.GetNumberOfPolys()): p = output.GetCell(kp) if p.GetNumberOfPoints() == 3: inel.append([p.GetPointId(kk) for kk in range(3)]) return val, crd, output