Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vtk
- # Prepare the unstructured grid --------------------------------------------------------------------
- grid = vtk.vtkUnstructuredGrid()
- points = vtk.vtkPoints()
- points.SetNumberOfPoints(8)
- points.SetPoint(0, 0, 0, 0)
- points.SetPoint(1, 1, 0, 0)
- points.SetPoint(2, 1, 1, 0)
- points.SetPoint(3, 0, 1, 0)
- points.SetPoint(4, 0, 0, 1)
- points.SetPoint(5, 1, 0, 1)
- points.SetPoint(6, 1, 1, 1)
- points.SetPoint(7, 0, 1, 1)
- grid.SetPoints(points)
- grid.Allocate(1)
- def AddPolyhedronTo(grid):
- polyhedron = [
- [0, 3, 2, 1],
- [0, 4, 7, 3],
- [4, 5, 6, 7],
- [5, 1, 2, 6],
- [0, 1, 5, 4],
- [2, 3, 7, 6],
- ]
- polygon_faces = vtk.vtkCellArray()
- for face in polyhedron:
- q = vtk.vtkPolygon()
- q.GetPointIds().SetNumberOfIds(len(face))
- for index, edge in enumerate(face):
- q.GetPointIds().SetId(index, edge)
- polygon_faces.InsertNextCell(q)
- face_stream = vtk.vtkIdList()
- face_stream.InsertNextId(polygon_faces.GetNumberOfCells())
- vertex_list = vtk.vtkIdList()
- polygon_faces.InitTraversal()
- while polygon_faces.GetNextCell(vertex_list) == 1:
- face_stream.InsertNextId(vertex_list.GetNumberOfIds())
- for j in xrange(vertex_list.GetNumberOfIds()):
- face_stream.InsertNextId(vertex_list.GetId(j))
- grid.InsertNextCell(vtk.VTK_POLYHEDRON, face_stream)
- def AddHexahedronTo(grid):
- aHexahedron = vtk.vtkHexahedron()
- aHexahedron.GetPointIds().SetId(0, 0)
- aHexahedron.GetPointIds().SetId(1, 1)
- aHexahedron.GetPointIds().SetId(2, 2)
- aHexahedron.GetPointIds().SetId(3, 3)
- aHexahedron.GetPointIds().SetId(4, 4)
- aHexahedron.GetPointIds().SetId(5, 5)
- aHexahedron.GetPointIds().SetId(6, 6)
- aHexahedron.GetPointIds().SetId(7, 7)
- grid.InsertNextCell(aHexahedron.GetCellType(), aHexahedron.GetPointIds())
- # Choose one of them, comment the other ------------------------------------------------------------
- AddPolyhedronTo(grid)
- # AddHexahedronTo(grid)
- # Test the algorithm -------------------------------------------------------------------------------
- unstructured_grid_extractor = vtk.vtkExtractUnstructuredGrid()
- unstructured_grid_extractor.SetInputData(grid)
- unstructured_grid_extractor.SetCellMinimum(0)
- unstructured_grid_extractor.SetCellMaximum(0)
- unstructured_grid_extractor.PointClippingOff()
- unstructured_grid_extractor.CellClippingOn()
- unstructured_grid_extractor.ExtentClippingOff()
- unstructured_grid_extractor.MergingOff()
- unstructured_grid_extractor.Update()
- output = unstructured_grid_extractor.GetOutput()
- assert grid.GetCell(0).GetNumberOfPoints() == output.GetCell(0).GetNumberOfPoints()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement