1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
from vtkmodules.vtkCommonCore import (
vtkIdList,
vtkPoints,
)
from vtkmodules.vtkCommonDataModel import (
VTK_POLYHEDRON,
vtkPlane,
vtkUnstructuredGrid,
)
from vtkmodules.vtkFiltersCore import vtkCutter
from vtkmodules.vtkIOXML import (
vtkXMLPolyDataWriter,
vtkXMLUnstructuredGridWriter,
)
from vtkmodules.test import Testing
class TestPolyhedronCutter(Testing.vtkTest):
def setUp(self):
self.vtk_grid = vtkUnstructuredGrid()
# vertices of the non-convex polyhedron
vtk_points = vtkPoints()
vtk_points.InsertNextPoint(0.0, 0.0, 0.0)
vtk_points.InsertNextPoint(4.0, 0.0, 0.0)
vtk_points.InsertNextPoint(4.0, 2.0, 0.0)
vtk_points.InsertNextPoint(3.0, 2.0, 0.0)
vtk_points.InsertNextPoint(3.0, 1.0, 0.0)
vtk_points.InsertNextPoint(1.0, 1.0, 0.0)
vtk_points.InsertNextPoint(1.0, 2.0, 0.0)
vtk_points.InsertNextPoint(0.0, 2.0, 0.0)
vtk_points.InsertNextPoint(0.0, 0.0, 1.0)
vtk_points.InsertNextPoint(4.0, 0.0, 1.0)
vtk_points.InsertNextPoint(4.0, 2.0, 1.0)
vtk_points.InsertNextPoint(3.0, 2.0, 1.0)
vtk_points.InsertNextPoint(3.0, 1.0, 1.0)
vtk_points.InsertNextPoint(1.0, 1.0, 1.0)
vtk_points.InsertNextPoint(1.0, 2.0, 1.0)
vtk_points.InsertNextPoint(0.0, 2.0, 1.0)
self.vtk_grid.SetPoints(vtk_points)
# connectivity of the polyhedron
ids = vtkIdList()
ids.InsertNextId(10) # no. of faces
ids.InsertNextId(8) # face 1 no. of verts
ids.InsertNextId(0)
ids.InsertNextId(1)
ids.InsertNextId(2)
ids.InsertNextId(3)
ids.InsertNextId(4)
ids.InsertNextId(5)
ids.InsertNextId(6)
ids.InsertNextId(7)
ids.InsertNextId(8) # face 2 no. of verts
ids.InsertNextId(8)
ids.InsertNextId(9)
ids.InsertNextId(10)
ids.InsertNextId(11)
ids.InsertNextId(12)
ids.InsertNextId(13)
ids.InsertNextId(14)
ids.InsertNextId(15)
ids.InsertNextId(4)
ids.InsertNextId(0)
ids.InsertNextId(1)
ids.InsertNextId(9)
ids.InsertNextId(8)
ids.InsertNextId(4)
ids.InsertNextId(1)
ids.InsertNextId(2)
ids.InsertNextId(10)
ids.InsertNextId(9)
ids.InsertNextId(4)
ids.InsertNextId(2)
ids.InsertNextId(3)
ids.InsertNextId(11)
ids.InsertNextId(10)
ids.InsertNextId(4)
ids.InsertNextId(3)
ids.InsertNextId(4)
ids.InsertNextId(12)
ids.InsertNextId(11)
ids.InsertNextId(4)
ids.InsertNextId(4)
ids.InsertNextId(5)
ids.InsertNextId(13)
ids.InsertNextId(12)
ids.InsertNextId(4)
ids.InsertNextId(5)
ids.InsertNextId(6)
ids.InsertNextId(14)
ids.InsertNextId(13)
ids.InsertNextId(4)
ids.InsertNextId(6)
ids.InsertNextId(7)
ids.InsertNextId(15)
ids.InsertNextId(14)
ids.InsertNextId(4)
ids.InsertNextId(7)
ids.InsertNextId(0)
ids.InsertNextId(8)
ids.InsertNextId(15)
self.vtk_grid.InsertNextCell(VTK_POLYHEDRON, ids)
writer = vtkXMLUnstructuredGridWriter()
writer.SetInputData(self.vtk_grid)
writer.SetFileName('my_grid.vtu')
writer.Write()
def testCutterTriangles(self):
cutter = vtkCutter()
cutter.GenerateTrianglesOn() # triangle generation is enabled!
cutter_function = vtkPlane()
cutter_function.SetOrigin(0.0, 0.0, 0.5)
cutter_function.SetNormal(0.0, 0.0, 1.0)
cutter.SetCutFunction(cutter_function)
cutter.SetInputData(self.vtk_grid)
cutter.Update()
surface = cutter.GetOutput()
writer = vtkXMLPolyDataWriter()
writer.SetInputData(surface)
writer.SetFileName('surface_tri.vtp')
writer.Write()
self.assertTrue(surface.GetNumberOfCells() > 1)
def testCutterNoTriangles(self):
cutter = vtkCutter()
cutter.GenerateTrianglesOff() # triangle generation is disabled!
cutter_function = vtkPlane()
cutter_function.SetOrigin(0.0, 0.0, 0.5)
cutter_function.SetNormal(0.0, 0.0, 1.0)
cutter.SetCutFunction(cutter_function)
cutter.SetInputData(self.vtk_grid)
cutter.Update()
surface = cutter.GetOutput()
writer = vtkXMLPolyDataWriter()
writer.SetInputData(surface)
writer.SetFileName('surface_poly.vtp')
writer.Write()
self.assertEqual(surface.GetNumberOfCells(), 1)
if __name__ == "__main__":
Testing.main([(TestPolyhedronCutter, 'test')])
|