|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies. |
| 3 | +# SPDX-FileContributor: Antoine Mazuyer, Martin Lemay |
| 4 | +import numpy as np |
| 5 | +import numpy.typing as npt |
| 6 | +from typing_extensions import Self |
| 7 | +from vtkmodules.vtkCommonDataModel import ( vtkCellTypes, VTK_TRIANGLE, VTK_QUAD, VTK_TETRA, VTK_VERTEX, VTK_POLYHEDRON, |
| 8 | + VTK_POLYGON, VTK_PYRAMID, VTK_HEXAHEDRON, VTK_WEDGE, |
| 9 | + VTK_NUMBER_OF_CELL_TYPES ) |
| 10 | + |
| 11 | +__doc__ = """ |
| 12 | +CellTypeCounts stores the number of elements of each type. |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +class CellTypeCounts(): |
| 17 | + |
| 18 | + def __init__( self: Self ) -> None: |
| 19 | + """CellTypeCounts stores the number of cells of each type.""" |
| 20 | + self._counts: npt.NDArray[ np.int64 ] = np.zeros( VTK_NUMBER_OF_CELL_TYPES, dtype=float ) |
| 21 | + |
| 22 | + def __str__( self: Self ) -> str: |
| 23 | + """Overload __str__ method. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + str: counts as string. |
| 27 | + """ |
| 28 | + return self.print() |
| 29 | + |
| 30 | + def __add__( self: Self, other: Self ) -> 'CellTypeCounts': |
| 31 | + """Addition operator. |
| 32 | +
|
| 33 | + CellTypeCounts addition consists in suming counts. |
| 34 | +
|
| 35 | + Args: |
| 36 | + other (Self): other CellTypeCounts object |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Self: new CellTypeCounts object |
| 40 | + """ |
| 41 | + assert isinstance( other, CellTypeCounts ), "Other object must be a CellTypeCounts." |
| 42 | + newCounts: CellTypeCounts = CellTypeCounts() |
| 43 | + newCounts._counts = self._counts + other._counts |
| 44 | + return newCounts |
| 45 | + |
| 46 | + def addType( self: Self, cellType: int ) -> None: |
| 47 | + """Increment the number of cell of input type. |
| 48 | +
|
| 49 | + Args: |
| 50 | + cellType (int): cell type |
| 51 | + """ |
| 52 | + self._counts[ cellType ] += 1 |
| 53 | + self._updateGeneralCounts( cellType, 1 ) |
| 54 | + |
| 55 | + def setTypeCount( self: Self, cellType: int, count: int ) -> None: |
| 56 | + """Set the number of cells of input type. |
| 57 | +
|
| 58 | + Args: |
| 59 | + cellType (int): cell type |
| 60 | + count (int): number of cells |
| 61 | + """ |
| 62 | + prevCount = self._counts[ cellType ] |
| 63 | + self._counts[ cellType ] = count |
| 64 | + self._updateGeneralCounts( cellType, count - prevCount ) |
| 65 | + |
| 66 | + def getTypeCount( self: Self, cellType: int ) -> int: |
| 67 | + """Get the number of cells of input type. |
| 68 | +
|
| 69 | + Args: |
| 70 | + cellType (int): cell type |
| 71 | +
|
| 72 | + Returns: |
| 73 | + int: number of cells |
| 74 | + """ |
| 75 | + return int( self._counts[ cellType ] ) |
| 76 | + |
| 77 | + def _updateGeneralCounts( self: Self, cellType: int, count: int ) -> None: |
| 78 | + """Update generic type counters. |
| 79 | +
|
| 80 | + Args: |
| 81 | + cellType (int): cell type |
| 82 | + count (int): count increment |
| 83 | + """ |
| 84 | + if ( cellType != VTK_POLYGON ) and ( vtkCellTypes.GetDimension( cellType ) == 2 ): |
| 85 | + self._counts[ VTK_POLYGON ] += count |
| 86 | + if ( cellType != VTK_POLYHEDRON ) and ( vtkCellTypes.GetDimension( cellType ) == 3 ): |
| 87 | + self._counts[ VTK_POLYHEDRON ] += count |
| 88 | + |
| 89 | + def print( self: Self ) -> str: |
| 90 | + """Print counts string. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + str: counts string. |
| 94 | + """ |
| 95 | + card: str = "" |
| 96 | + card += "| | |\n" |
| 97 | + card += "| - | - |\n" |
| 98 | + card += f"| **Total Number of Vertices** | {int(self._counts[VTK_VERTEX]):12} |\n" |
| 99 | + card += f"| **Total Number of Polygon** | {int(self._counts[VTK_POLYGON]):12} |\n" |
| 100 | + card += f"| **Total Number of Polyhedron** | {int(self._counts[VTK_POLYHEDRON]):12} |\n" |
| 101 | + card += f"| **Total Number of Cells** | {int(self._counts[VTK_POLYHEDRON]+self._counts[VTK_POLYGON]):12} |\n" |
| 102 | + card += "| - | - |\n" |
| 103 | + for cellType in ( VTK_TRIANGLE, VTK_QUAD ): |
| 104 | + card += f"| **Total Number of {vtkCellTypes.GetClassNameFromTypeId(cellType):<13}** | {int(self._counts[cellType]):12} |\n" |
| 105 | + for cellType in ( VTK_TETRA, VTK_PYRAMID, VTK_WEDGE, VTK_HEXAHEDRON ): |
| 106 | + card += f"| **Total Number of {vtkCellTypes.GetClassNameFromTypeId(cellType):<13}** | {int(self._counts[cellType]):12} |\n" |
| 107 | + return card |
0 commit comments