Skip to content

Commit 041441c

Browse files
fix: Secure multiBlockDataSet parsing. (#134)
* Add the test for the functions of multiblockHelpers * Clean the doc and the variables names * Fix issue with multiblock parsing * Update the vtm to have blocks in block * fix the test to deals with block in block for the vtm * clean for ci
1 parent 2a6b5af commit 041441c

7 files changed

Lines changed: 364 additions & 310 deletions

File tree

geos-mesh/src/geos/mesh/utils/arrayHelpers.py

Lines changed: 91 additions & 102 deletions
Large diffs are not rendered by default.

geos-mesh/src/geos/mesh/utils/arrayModifiers.py

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
vtkPointSet,
1818
vtkCompositeDataSet,
1919
vtkDataObject,
20-
vtkDataObjectTreeIterator,
2120
vtkPointData,
2221
vtkCellData,
2322
)
@@ -43,10 +42,7 @@
4342
getVtkArrayTypeInMultiBlock,
4443
getNumberOfComponentsMultiBlock,
4544
)
46-
from geos.mesh.utils.multiblockHelpers import (
47-
getBlockElementIndexesFlatten,
48-
getBlockFromFlatIndex,
49-
)
45+
from geos.mesh.utils.multiblockHelpers import getBlockElementIndexesFlatten
5046

5147
__doc__ = """
5248
ArrayModifiers contains utilities to process VTK Arrays objects.
@@ -148,18 +144,13 @@ def fillPartialAttributes(
148144
)
149145

150146
# Parse the multiBlockDataSet to create and fill the attribute on blocks where it is not.
151-
iterator: vtkDataObjectTreeIterator = vtkDataObjectTreeIterator()
152-
iterator.SetDataSet( multiBlockDataSet )
153-
iterator.VisitOnlyLeavesOn()
154-
iterator.GoToFirstItem()
155-
while iterator.GetCurrentDataObject() is not None:
156-
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( iterator.GetCurrentDataObject() )
147+
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
148+
for blockIndex in elementaryBlockIndexes:
149+
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
157150
if not isAttributeInObjectDataSet( dataSet, attributeName, onPoints ) and \
158151
not createConstantAttributeDataSet( dataSet, listValues, attributeName, componentNames, onPoints, vtkDataType, logger ):
159152
return False
160153

161-
iterator.GoToNextItem()
162-
163154
return True
164155

165156

@@ -224,18 +215,18 @@ def createEmptyAttribute(
224215

225216

226217
def createConstantAttribute(
227-
object: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet, vtkDataObject ],
218+
mesh: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet, vtkDataSet ],
228219
listValues: list[ Any ],
229220
attributeName: str,
230221
componentNames: tuple[ str, ...] = (), # noqa: C408
231222
onPoints: bool = False,
232223
vtkDataType: Union[ int, None ] = None,
233224
logger: Union[ Logger, None ] = None,
234225
) -> bool:
235-
"""Create a new attribute with a constant value in the object.
226+
"""Create a new attribute with a constant value in the mesh.
236227
237228
Args:
238-
object (vtkDataObject): Object (vtkMultiBlockDataSet, vtkDataSet) where to create the attribute.
229+
mesh (Union[vtkMultiBlockDataSet, vtkDataSet]): Mesh where to create the attribute.
239230
listValues (list[Any]): List of values of the attribute for each components. It is recommended to use numpy scalar type for the values.
240231
attributeName (str): Name of the attribute.
241232
componentNames (tuple[str,...], optional): Name of the components for vectorial attributes. If one component, gives an empty tuple.
@@ -260,20 +251,15 @@ def createConstantAttribute(
260251
logger = getLogger( "createConstantAttribute", True )
261252

262253
# Deals with multiBlocksDataSets.
263-
if isinstance( object, ( vtkMultiBlockDataSet, vtkCompositeDataSet ) ):
264-
return createConstantAttributeMultiBlock( object, listValues, attributeName, componentNames, onPoints,
254+
if isinstance( mesh, ( vtkMultiBlockDataSet, vtkCompositeDataSet ) ):
255+
return createConstantAttributeMultiBlock( mesh, listValues, attributeName, componentNames, onPoints,
265256
vtkDataType, logger )
266257

267258
# Deals with dataSets.
268-
elif isinstance( object, vtkDataSet ):
269-
return createConstantAttributeDataSet( object, listValues, attributeName, componentNames, onPoints, vtkDataType,
259+
elif isinstance( mesh, vtkDataSet ):
260+
return createConstantAttributeDataSet( mesh, listValues, attributeName, componentNames, onPoints, vtkDataType,
270261
logger )
271262

272-
else:
273-
logger.error( "The mesh has to be inherited from a vtkMultiBlockDataSet or a vtkDataSet" )
274-
logger.error( f"The constant attribute { attributeName } has not been created into the mesh." )
275-
return False
276-
277263

278264
def createConstantAttributeMultiBlock(
279265
multiBlockDataSet: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet ],
@@ -287,7 +273,7 @@ def createConstantAttributeMultiBlock(
287273
"""Create a new attribute with a constant value per component on every block of the multiBlockDataSet.
288274
289275
Args:
290-
multiBlockDataSet (vtkMultiBlockDataSet | vtkCompositeDataSet): MultiBlockDataSet where to create the attribute.
276+
multiBlockDataSet (Union[vtkMultiBlockDataSet, vtkCompositeDataSet]): MultiBlockDataSet where to create the attribute.
291277
listValues (list[Any]): List of values of the attribute for each components. It is recommended to use numpy scalar type for the values.
292278
attributeName (str): Name of the attribute.
293279
componentNames (tuple[str,...], optional): Name of the components for vectorial attributes. If one component, gives an empty tuple.
@@ -334,18 +320,13 @@ def createConstantAttributeMultiBlock(
334320
)
335321

336322
# Parse the multiBlockDataSet to create the constant attribute on each blocks.
337-
iterator: vtkDataObjectTreeIterator = vtkDataObjectTreeIterator()
338-
iterator.SetDataSet( multiBlockDataSet )
339-
iterator.VisitOnlyLeavesOn()
340-
iterator.GoToFirstItem()
341-
while iterator.GetCurrentDataObject() is not None:
342-
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( iterator.GetCurrentDataObject() )
323+
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
324+
for blockIndex in elementaryBlockIndexes:
325+
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
343326
if not createConstantAttributeDataSet( dataSet, listValues, attributeName, componentNames, onPoints,
344327
vtkDataType, logger ):
345328
return False
346329

347-
iterator.GoToNextItem()
348-
349330
return True
350331

351332

@@ -613,13 +594,13 @@ def copyAttribute(
613594

614595
# Parse blocks of the two mesh to copy the attribute.
615596
for idBlock in elementaryBlockIndexesTo:
616-
dataSetFrom: vtkDataSet = vtkDataSet.SafeDownCast( getBlockFromFlatIndex( multiBlockDataSetFrom, idBlock ) )
597+
dataSetFrom: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetFrom.GetDataSet( idBlock ) )
617598
if dataSetFrom is None:
618599
logger.error( f"Block { idBlock } of multiBlockDataSetFrom is null." ) # type: ignore[unreachable]
619600
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
620601
return False
621602

622-
dataSetTo: vtkDataSet = vtkDataSet.SafeDownCast( getBlockFromFlatIndex( multiBlockDataSetTo, idBlock ) )
603+
dataSetTo: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSetTo.GetDataSet( idBlock ) )
623604
if dataSetTo is None:
624605
logger.error( f"Block { idBlock } of multiBlockDataSetTo is null." ) # type: ignore[unreachable]
625606
logger.error( f"The attribute { attributeNameFrom } has not been copied." )
@@ -735,15 +716,10 @@ def createCellCenterAttribute( mesh: Union[ vtkMultiBlockDataSet, vtkDataSet ],
735716
"""
736717
ret: int = 1
737718
if isinstance( mesh, vtkMultiBlockDataSet ):
738-
# initialize data object tree iterator
739-
iterator: vtkDataObjectTreeIterator = vtkDataObjectTreeIterator()
740-
iterator.SetDataSet( mesh )
741-
iterator.VisitOnlyLeavesOn()
742-
iterator.GoToFirstItem()
743-
while iterator.GetCurrentDataObject() is not None:
744-
block: vtkDataSet = vtkDataSet.SafeDownCast( iterator.GetCurrentDataObject() )
745-
ret *= int( doCreateCellCenterAttribute( block, cellCenterAttributeName ) )
746-
iterator.GoToNextItem()
719+
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( mesh )
720+
for blockIndex in elementaryBlockIndexes:
721+
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( mesh.GetDataSet( blockIndex ) )
722+
ret *= int( doCreateCellCenterAttribute( dataSet, cellCenterAttributeName ) )
747723
elif isinstance( mesh, vtkDataSet ):
748724
ret = int( doCreateCellCenterAttribute( mesh, cellCenterAttributeName ) )
749725
else:

0 commit comments

Comments
 (0)