Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 151 additions & 155 deletions geos-processing/src/geos/processing/generic_processing_tools/SplitMesh.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Antoine Mazuyer, Martin Lemay
import logging

from typing_extensions import Self
from vtkmodules.util.vtkAlgorithm import VTKPythonAlgorithmBase
from vtkmodules.vtkCommonCore import (
vtkInformation,
vtkInformationVector,
vtkIntArray,
)
from vtkmodules.vtkCommonDataModel import ( vtkUnstructuredGrid, vtkCell, vtkTable, vtkCellTypes, VTK_VERTEX )
from vtkmodules.vtkCommonCore import vtkIntArray
from vtkmodules.vtkCommonDataModel import vtkUnstructuredGrid, vtkCell, vtkTable, vtkCellTypes, VTK_VERTEX

from geos.mesh.model.CellTypeCounts import CellTypeCounts
from geos.mesh.stats.meshQualityMetricHelpers import getAllCellTypes
from geos.utils.Logger import ( Logger, getLogger )

__doc__ = """
CellTypeCounterEnhanced module is a vtk filter that computes cell type counts.
Expand All @@ -25,86 +23,108 @@
from geos.processing.pre_processing.CellTypeCounterEnhanced import CellTypeCounterEnhanced

# Filter inputs
input: vtkUnstructuredGrid
inputMesh: vtkUnstructuredGrid
speHandler: bool # defaults to False

# Instantiate the filter
cellTypeCounterEnhancedFilter: CellTypeCounterEnhanced = CellTypeCounterEnhanced()
cellTypeCounterEnhancedFilter: CellTypeCounterEnhanced = CellTypeCounterEnhanced( inputMesh, speHandler )

# Set input data object
cellTypeCounterEnhancedFilter.SetInputDataObject(input)
# Set the handler of yours (only if speHandler is True).
yourHandler: logging.Handler
cellTypeCounterEnhancedFilter.setLoggerHandler( yourHandler )

# Do calculations
cellTypeCounterEnhancedFilter.Update()
cellTypeCounterEnhancedFilter.applyFilter()

# Get counts
# Get result
counts: CellTypeCounts = cellTypeCounterEnhancedFilter.GetCellTypeCountsObject()
outputTable: vtkTable = cellTypeCounterEnhancedFilter.getOutput()
"""

loggerTitle: str = "Cell Type Counter Enhanced"

class CellTypeCounterEnhanced( VTKPythonAlgorithmBase ):

def __init__( self ) -> None:
"""CellTypeCounterEnhanced filter computes mesh stats."""
super().__init__( nInputPorts=1, nOutputPorts=1, inputType="vtkUnstructuredGrid", outputType="vtkTable" )
self._counts: CellTypeCounts = CellTypeCounts()
class CellTypeCounterEnhanced():

def FillInputPortInformation( self: Self, port: int, info: vtkInformation ) -> int:
"""Inherited from VTKPythonAlgorithmBase::RequestInformation.
def __init__(
self: Self,
inputMesh: vtkUnstructuredGrid,
speHandler: bool = False,
) -> None:
"""CellTypeCounterEnhanced filter computes mesh stats.

Args:
port (int): Input port
info (vtkInformationVector): Info

Returns:
int: 1 if calculation successfully ended, 0 otherwise.
inputMesh (vtkUnstructuredGrid): The input mesh.
speHandler (bool, optional): True to use a specific handler, False to use the internal handler.
Defaults to False.
"""
if port == 0:
info.Set( self.INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid" )
return 1
self.inputMesh: vtkUnstructuredGrid = inputMesh
self.outTable: vtkTable = vtkTable()
self._counts: CellTypeCounts = CellTypeCounts()

def RequestData(
self: Self,
request: vtkInformation, # noqa: F841
inInfoVec: list[ vtkInformationVector ], # noqa: F841
outInfoVec: vtkInformationVector,
) -> int:
"""Inherited from VTKPythonAlgorithmBase::RequestData.
# Logger.
self.logger: Logger
if not speHandler:
self.logger = getLogger( loggerTitle, True )
else:
self.logger = logging.getLogger( loggerTitle )
self.logger.setLevel( logging.INFO )
self.logger.propagate = False

def setLoggerHandler( self: Self, handler: logging.Handler ) -> None:
"""Set a specific handler for the filter logger.

In this filter 4 log levels are use, .info, .error, .warning and .critical,
be sure to have at least the same 4 levels.

Args:
request (vtkInformation): Request
inInfoVec (list[vtkInformationVector]): Input objects
outInfoVec (vtkInformationVector): Output objects
handler (logging.Handler): The handler to add.
"""
if len( self.logger.handlers ) == 0:
self.logger.addHandler( handler )
else:
self.logger.warning( "The logger already has an handler, to use yours set the argument 'speHandler'"
" to True during the filter initialization." )

def applyFilter( self: Self ) -> bool:
"""Apply CellTypeCounterEnhanced filter.

Returns:
int: 1 if calculation successfully ended, 0 otherwise.
bool: True if the filter succeeded, False otherwise.
"""
inData: vtkUnstructuredGrid = self.GetInputData( inInfoVec, 0, 0 )
outTable: vtkTable = vtkTable.GetData( outInfoVec, 0 )
assert inData is not None, "Input mesh is undefined."
assert outTable is not None, "Output table is undefined."

# compute cell type counts
self._counts.reset()
self._counts.setTypeCount( VTK_VERTEX, inData.GetNumberOfPoints() )
for i in range( inData.GetNumberOfCells() ):
cell: vtkCell = inData.GetCell( i )
self._counts.addType( cell.GetCellType() )

# create output table
# first reset output table
outTable.RemoveAllRows()
outTable.RemoveAllColumns()
outTable.SetNumberOfRows( 1 )

# create columns per types
for cellType in getAllCellTypes():
array: vtkIntArray = vtkIntArray()
array.SetName( vtkCellTypes.GetClassNameFromTypeId( cellType ) )
array.SetNumberOfComponents( 1 )
array.SetNumberOfValues( 1 )
array.SetValue( 0, self._counts.getTypeCount( cellType ) )
outTable.AddColumn( array )
return 1
self.logger.info( f"Apply filter { self.logger.name }." )
try:
# compute cell type counts
self._counts.reset()
self._counts.setTypeCount( VTK_VERTEX, self.inputMesh.GetNumberOfPoints() )
for i in range( self.inputMesh.GetNumberOfCells() ):
cell: vtkCell = self.inputMesh.GetCell( i )
self._counts.addType( cell.GetCellType() )

# create output table
# first reset output table
self.outTable.RemoveAllRows()
self.outTable.RemoveAllColumns()
self.outTable.SetNumberOfRows( 1 )

# create columns per types
for cellType in getAllCellTypes():
array: vtkIntArray = vtkIntArray()
array.SetName( vtkCellTypes.GetClassNameFromTypeId( cellType ) )
array.SetNumberOfComponents( 1 )
array.SetNumberOfValues( 1 )
array.SetValue( 0, self._counts.getTypeCount( cellType ) )
self.outTable.AddColumn( array )
self.logger.info( f"The filter { self.logger.name } succeeded." )
except TypeError as e:
self.logger.error( f"The filter { self.logger.name } failed.\n{ e }" )
return False
except Exception as e:
mess: str = f"The filter { self.logger.name } failed.\n{ e }"
self.logger.critical( mess, exc_info=True )
return False

return True

def GetCellTypeCountsObject( self: Self ) -> CellTypeCounts:
"""Get CellTypeCounts object.
Expand All @@ -113,3 +133,7 @@ def GetCellTypeCountsObject( self: Self ) -> CellTypeCounts:
CellTypeCounts: CellTypeCounts object.
"""
return self._counts

def getOutput( self: Self ) -> vtkTable:
"""Get the computed vtkTable."""
return self.outTable
Loading