Skip to content

Commit 0a9c240

Browse files
author
Romain Baville
committed
Refactor SplitMesh
1 parent 4ef951a commit 0a9c240

3 files changed

Lines changed: 92 additions & 85 deletions

File tree

geos-processing/src/geos/processing/generic_processing_tools/SplitMesh.py

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141
splitMeshFilter.setLoggerHandler( yourHandler )
4242
4343
# Do calculations
44-
splitMeshFilter.applyFilter()
44+
try:
45+
splitMeshFilter.applyFilter()
46+
except ( TypeError, AttributeError ) as e:
47+
splitMeshFilter.logger.error( f"The filter {splitMeshFilter.logger.name } failed due to: { e }" )
48+
except Exception as e:
49+
mess: str = f"The filter { splitMeshFilter.logger.name } failed due to: { e }"
50+
splitMeshFilter.logger.critical( mess, exc_info=True )
4551
4652
# Get splitted mesh
4753
outputMesh: vtkUnstructuredGrid = splitMeshFilter.getOutput()
@@ -93,88 +99,80 @@ def setLoggerHandler( self: Self, handler: logging.Handler ) -> None:
9399
self.logger.warning( "The logger already has an handler, to use yours set the argument 'speHandler' to True"
94100
" during the filter initialization." )
95101

96-
def applyFilter( self: Self ) -> bool:
102+
def applyFilter( self: Self ) -> None:
97103
"""Apply the filter SplitMesh.
98104
99-
Returns:
100-
bool: True if the filter succeeded, False otherwise.
105+
Raises:
106+
TypeError: Errors due to objects with the wrong type.
107+
AttributeError: Errors with cell data.
101108
"""
102109
self.logger.info( f"Applying filter { self.logger.name }." )
103-
try:
104-
# Count the number of cells before splitting. Then we will be able to know how many new cells and points
105-
# to allocate because each cell type is splitted in a known number of new cells and points.
106-
nbCells: int = self.inputMesh.GetNumberOfCells()
107-
counts: CellTypeCounts = self._getCellCounts()
108-
if counts.getTypeCount( VTK_WEDGE ) != 0:
109-
raise TypeError( "Input mesh contains wedges that are not currently supported." )
110-
111-
nbPolygon: int = counts.getTypeCount( VTK_POLYGON )
112-
nbPolyhedra: int = counts.getTypeCount( VTK_POLYHEDRON )
113-
# Current implementation only supports meshes composed of either polygons or polyhedra
114-
if nbPolyhedra * nbPolygon != 0:
115-
raise TypeError(
116-
"Input mesh is composed of both polygons and polyhedra, but it must contains only one of the two." )
117-
118-
nbTet: int = counts.getTypeCount( VTK_TETRA ) # will divide into 8 tets
119-
nbPyr: int = counts.getTypeCount( VTK_PYRAMID ) # will divide into 6 pyramids and 4 tets so 10 new cells
120-
nbHex: int = counts.getTypeCount( VTK_HEXAHEDRON ) # will divide into 8 hexes
121-
nbTriangles: int = counts.getTypeCount( VTK_TRIANGLE ) # will divide into 4 triangles
122-
nbQuad: int = counts.getTypeCount( VTK_QUAD ) # will divide into 4 quads
123-
nbNewPoints: int = 0
124-
nbNewPoints = nbHex * 19 + nbTet * 6 + nbPyr * 9 if nbPolyhedra > 0 else nbTriangles * 3 + nbQuad * 5
125-
nbNewCells: int = nbHex * 8 + nbTet * 8 + nbPyr * 10 + nbTriangles * 4 + nbQuad * 4
126-
127-
self.points = vtkPoints()
128-
self.points.DeepCopy( self.inputMesh.GetPoints() )
129-
self.points.Resize( self.inputMesh.GetNumberOfPoints() + nbNewPoints )
130-
131-
self.cells = vtkCellArray()
132-
self.cells.AllocateExact( nbNewCells, 8 )
133-
self.originalId = vtkIdTypeArray()
134-
self.originalId.SetName( "OriginalID" )
135-
self.originalId.Allocate( nbNewCells )
136-
self.cellTypes = []
137-
138-
# Define cell type to splitting method mapping
139-
splitMethods = {
140-
VTK_HEXAHEDRON: self._splitHexahedron,
141-
VTK_TETRA: self._splitTetrahedron,
142-
VTK_PYRAMID: self._splitPyramid,
143-
VTK_TRIANGLE: self._splitTriangle,
144-
VTK_QUAD: self._splitQuad,
145-
}
146-
for c in range( nbCells ):
147-
cell: vtkCell = self.inputMesh.GetCell( c )
148-
cellType: int = cell.GetCellType()
149-
splitMethod = splitMethods.get( cellType )
150-
if splitMethod is not None:
151-
splitMethod( cell, c )
152-
else:
153-
raise TypeError(
154-
f"Cell type { vtkCellTypes.GetClassNameFromTypeId( cellType ) } is not supported." )
155-
156-
# Add points and cells
157-
self.outputMesh.SetPoints( self.points )
158-
self.outputMesh.SetCells( self.cellTypes, self.cells )
159-
160-
# Add attribute saving original cell ids
161-
cellArrays: vtkCellData = self.outputMesh.GetCellData()
162-
if cellArrays is None:
163-
raise AttributeError( "Cell data is undefined." )
164-
cellArrays.AddArray( self.originalId )
165-
166-
# Transfer all cell arrays
167-
self._transferCellArrays( self.outputMesh )
168-
self.logger.info( f"The filter { self.logger.name } succeeded." )
169-
except ( TypeError, AttributeError ) as e:
170-
self.logger.error( f"The filter {self.logger.name } failed.\n{ e }" )
171-
return False
172-
except Exception as e:
173-
mess: str = f"The filter { self.logger.name } failed.\n{ e }"
174-
self.logger.critical( mess, exc_info=True )
175-
return False
176-
177-
return True
110+
# Count the number of cells before splitting. Then we will be able to know how many new cells and points
111+
# to allocate because each cell type is splitted in a known number of new cells and points.
112+
nbCells: int = self.inputMesh.GetNumberOfCells()
113+
counts: CellTypeCounts = self._getCellCounts()
114+
if counts.getTypeCount( VTK_WEDGE ) != 0:
115+
raise TypeError( "Input mesh contains wedges that are not currently supported." )
116+
117+
nbPolygon: int = counts.getTypeCount( VTK_POLYGON )
118+
nbPolyhedra: int = counts.getTypeCount( VTK_POLYHEDRON )
119+
# Current implementation only supports meshes composed of either polygons or polyhedra
120+
if nbPolyhedra * nbPolygon != 0:
121+
raise TypeError(
122+
"Input mesh is composed of both polygons and polyhedra, but it must contains only one of the two." )
123+
124+
nbTet: int = counts.getTypeCount( VTK_TETRA ) # will divide into 8 tets
125+
nbPyr: int = counts.getTypeCount( VTK_PYRAMID ) # will divide into 6 pyramids and 4 tets so 10 new cells
126+
nbHex: int = counts.getTypeCount( VTK_HEXAHEDRON ) # will divide into 8 hexes
127+
nbTriangles: int = counts.getTypeCount( VTK_TRIANGLE ) # will divide into 4 triangles
128+
nbQuad: int = counts.getTypeCount( VTK_QUAD ) # will divide into 4 quads
129+
nbNewPoints: int = 0
130+
nbNewPoints = nbHex * 19 + nbTet * 6 + nbPyr * 9 if nbPolyhedra > 0 else nbTriangles * 3 + nbQuad * 5
131+
nbNewCells: int = nbHex * 8 + nbTet * 8 + nbPyr * 10 + nbTriangles * 4 + nbQuad * 4
132+
133+
self.points = vtkPoints()
134+
self.points.DeepCopy( self.inputMesh.GetPoints() )
135+
self.points.Resize( self.inputMesh.GetNumberOfPoints() + nbNewPoints )
136+
137+
self.cells = vtkCellArray()
138+
self.cells.AllocateExact( nbNewCells, 8 )
139+
self.originalId = vtkIdTypeArray()
140+
self.originalId.SetName( "OriginalID" )
141+
self.originalId.Allocate( nbNewCells )
142+
self.cellTypes = []
143+
144+
# Define cell type to splitting method mapping
145+
splitMethods = {
146+
VTK_HEXAHEDRON: self._splitHexahedron,
147+
VTK_TETRA: self._splitTetrahedron,
148+
VTK_PYRAMID: self._splitPyramid,
149+
VTK_TRIANGLE: self._splitTriangle,
150+
VTK_QUAD: self._splitQuad,
151+
}
152+
for c in range( nbCells ):
153+
cell: vtkCell = self.inputMesh.GetCell( c )
154+
cellType: int = cell.GetCellType()
155+
splitMethod = splitMethods.get( cellType )
156+
if splitMethod is not None:
157+
splitMethod( cell, c )
158+
else:
159+
raise TypeError( f"Cell type { vtkCellTypes.GetClassNameFromTypeId( cellType ) } is not supported." )
160+
161+
# Add points and cells
162+
self.outputMesh.SetPoints( self.points )
163+
self.outputMesh.SetCells( self.cellTypes, self.cells )
164+
165+
# Add attribute saving original cell ids
166+
cellArrays: vtkCellData = self.outputMesh.GetCellData()
167+
if cellArrays is None:
168+
raise AttributeError( "Cell data is undefined." )
169+
cellArrays.AddArray( self.originalId )
170+
171+
# Transfer all cell arrays
172+
self._transferCellArrays( self.outputMesh )
173+
self.logger.info( f"The filter { self.logger.name } succeeded." )
174+
175+
return
178176

179177
def getOutput( self: Self ) -> vtkUnstructuredGrid:
180178
"""Get the splitted mesh computed."""
@@ -190,8 +188,7 @@ def _getCellCounts( self: Self ) -> CellTypeCounts:
190188
self.inputMesh, self.speHandler )
191189
if self.speHandler and len( cellTypeCounterEnhancedFilter.logger.handlers ) == 0:
192190
cellTypeCounterEnhancedFilter.setLoggerHandler( self.handler )
193-
if not cellTypeCounterEnhancedFilter.applyFilter():
194-
raise
191+
cellTypeCounterEnhancedFilter.applyFilter()
195192
return cellTypeCounterEnhancedFilter.GetCellTypeCountsObject()
196193

197194
def _addMidPoint( self: Self, ptA: int, ptB: int ) -> int:
@@ -447,6 +444,9 @@ def _transferCellArrays( self: Self, splittedMesh: vtkUnstructuredGrid ) -> None
447444
448445
Args:
449446
splittedMesh (vtkUnstructuredGrid): Splitted mesh.
447+
448+
Raises:
449+
AttributeError: Errors with cell data.
450450
"""
451451
cellData: vtkCellData = self.inputMesh.GetCellData()
452452
if cellData is None:

geos-processing/tests/test_SplitMesh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_single_cell_split( test_case: TestCase ) -> None:
143143
"""
144144
cellTypeName: str = vtkCellTypes.GetClassNameFromTypeId( test_case.cellType )
145145
splitMeshFilter: SplitMesh = SplitMesh( test_case.mesh )
146-
assert splitMeshFilter.applyFilter()
146+
splitMeshFilter.applyFilter()
147147
output: vtkUnstructuredGrid = splitMeshFilter.getOutput()
148148
assert output is not None, "Output mesh is undefined."
149149
pointsOut: vtkPoints = output.GetPoints()
@@ -234,7 +234,7 @@ def test_multi_cells_mesh_split() -> None:
234234

235235
# Apply the split filter
236236
splitMeshFilter = SplitMesh( input_mesh )
237-
assert splitMeshFilter.applyFilter()
237+
splitMeshFilter.applyFilter()
238238
output: vtkUnstructuredGrid = splitMeshFilter.getOutput()
239239
assert output is not None, "Output mesh should be defined"
240240

@@ -296,7 +296,7 @@ def test_multi_polygon_mesh_split() -> None:
296296

297297
# Apply the split filter
298298
splitMeshFilter = SplitMesh( input_mesh )
299-
assert splitMeshFilter.applyFilter()
299+
splitMeshFilter.applyFilter()
300300
output: vtkUnstructuredGrid = splitMeshFilter.getOutput()
301301
assert output is not None, "Output mesh should be defined"
302302

geos-pv/src/geos/pv/plugins/PVSplitMesh.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ def ApplyFilter( self: Self, inputMesh: vtkPointSet, outputMesh: vtkPointSet ) -
5454
splitMeshFilter: SplitMesh = SplitMesh( inputMesh, True )
5555
if len( splitMeshFilter.logger.handlers ) == 0:
5656
splitMeshFilter.setLoggerHandler( VTKHandler() )
57-
if splitMeshFilter.applyFilter():
57+
58+
try:
59+
splitMeshFilter.applyFilter()
5860
outputMesh.ShallowCopy( splitMeshFilter.getOutput() )
61+
except ( TypeError, AttributeError ) as e:
62+
splitMeshFilter.logger.error( f"The filter {splitMeshFilter.logger.name } failed due to:\n{ e }" )
63+
except Exception as e:
64+
mess: str = f"The filter { splitMeshFilter.logger.name } failed due to:\n{ e }"
65+
splitMeshFilter.logger.critical( mess, exc_info=True )
5966

6067
return

0 commit comments

Comments
 (0)