-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayHelpers.py
More file actions
600 lines (474 loc) · 23.4 KB
/
arrayHelpers.py
File metadata and controls
600 lines (474 loc) · 23.4 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright 2023-2024 TotalEnergies.
# SPDX-FileContributor: Martin Lemay, Paloma Martinez, Romain Baville
from copy import deepcopy
import logging
import numpy as np
import numpy.typing as npt
import pandas as pd # type: ignore[import-untyped]
import vtkmodules.util.numpy_support as vnp
from typing import Optional, Union, Any
from vtkmodules.util.numpy_support import vtk_to_numpy
from vtkmodules.vtkCommonCore import vtkDataArray, vtkPoints
from vtkmodules.vtkCommonDataModel import ( vtkUnstructuredGrid, vtkFieldData, vtkMultiBlockDataSet, vtkDataSet,
vtkCompositeDataSet, vtkDataObject, vtkPointData, vtkCellData, vtkPolyData )
from vtkmodules.vtkFiltersCore import vtkCellCenters
from geos.mesh.utils.multiblockHelpers import getBlockElementIndexesFlatten
__doc__ = """
ArrayHelpers module contains several utilities methods to get information on arrays in VTK datasets.
These methods include:
- array getters, with conversion into numpy array or pandas dataframe
- boolean functions to check whether an array is present in the dataset
- bounds getter for vtu and multiblock datasets
"""
def has_array( mesh: vtkUnstructuredGrid, array_names: list[ str ] ) -> bool:
"""Checks if input mesh contains at least one of input data arrays.
Args:
mesh (vtkUnstructuredGrid): An unstructured mesh.
array_names (list[str]): List of array names.
Returns:
bool: True if at least one array is found, else False.
"""
# Check the cell data fields
data: Union[ vtkFieldData, None ]
for data in ( mesh.GetCellData(), mesh.GetFieldData(), mesh.GetPointData() ):
if data is None:
continue # type: ignore[unreachable]
for arrayName in array_names:
if data.HasArray( arrayName ):
logging.error( f"The mesh contains the array named '{arrayName}'." )
return True
return False
def getFieldType( data: vtkFieldData ) -> str:
"""Returns whether the data is "vtkFieldData", "vtkCellData" or "vtkPointData".
A vtk mesh can contain 3 types of field data:
- vtkFieldData (parent class)
- vtkCellData (inheritance of vtkFieldData)
- vtkPointData (inheritance of vtkFieldData)
Args:
data (vtkFieldData): Vtk field data.
Returns:
str: "vtkFieldData", "vtkCellData" or "vtkPointData"
"""
if not data.IsA( "vtkFieldData" ):
raise ValueError( f"data '{ data }' entered is not a vtkFieldData object." )
if data.IsA( "vtkCellData" ):
return "vtkCellData"
elif data.IsA( "vtkPointData" ):
return "vtkPointData"
else:
return "vtkFieldData"
def getArrayNames( data: vtkFieldData ) -> list[ str ]:
"""Get the names of all arrays stored in a "vtkFieldData", "vtkCellData" or "vtkPointData".
Args:
data (vtkFieldData): Vtk field data.
Returns:
list[str]: The array names in the order that they are stored in the field data.
"""
if not data.IsA( "vtkFieldData" ):
raise ValueError( f"data '{ data }' entered is not a vtkFieldData object." )
return [ data.GetArrayName( i ) for i in range( data.GetNumberOfArrays() ) ]
def getArrayByName( data: vtkFieldData, name: str ) -> Optional[ vtkDataArray ]:
"""Get the vtkDataArray corresponding to the given name.
Args:
data (vtkFieldData): Vtk field data.
name (str): Array name.
Returns:
Optional[ vtkDataArray ]: The vtkDataArray associated with the name given. None if not found.
"""
if data.HasArray( name ):
return data.GetArray( name )
logging.warning( f"No array named '{ name }' was found in '{ data }'." )
return None
def getCopyArrayByName( data: vtkFieldData, name: str ) -> Optional[ vtkDataArray ]:
"""Get the copy of a vtkDataArray corresponding to the given name.
Args:
data (vtkFieldData): Vtk field data.
name (str): Array name.
Returns:
Optional[ vtkDataArray ]: The copy of the vtkDataArray associated with the name given. None if not found.
"""
dataArray: Optional[ vtkDataArray ] = getArrayByName( data, name )
if dataArray is not None:
return deepcopy( dataArray )
return None
def getNumpyGlobalIdsArray( data: Union[ vtkCellData, vtkPointData ] ) -> Optional[ npt.NDArray[ np.int64 ] ]:
"""Get a numpy array of the GlobalIds.
Args:
data (Union[ vtkCellData, vtkPointData ]): Cell or point array.
Returns:
Optional[ npt.NDArray[ np.int64 ] ]: The numpy array of GlobalIds.
"""
global_ids: Optional[ vtkDataArray ] = data.GetGlobalIds()
if global_ids is None:
logging.warning( "No GlobalIds array was found." )
return None
return vtk_to_numpy( global_ids )
def getNumpyArrayByName( data: Union[ vtkCellData, vtkPointData ],
name: str,
sorted: bool = False ) -> Optional[ npt.NDArray ]:
"""Get the numpy array of a given vtkDataArray found by its name.
If sorted is selected, this allows the option to reorder the values wrt GlobalIds. If not GlobalIds was found,
no reordering will be perform.
Args:
data (Union[vtkCellData, vtkPointData]): Vtk field data.
name (str): Array name to sort.
sorted (bool, optional): Sort the output array with the help of GlobalIds. Defaults to False.
Returns:
Optional[ npt.NDArray ]: Sorted array.
"""
dataArray: Optional[ vtkDataArray ] = getArrayByName( data, name )
if dataArray is not None:
arr: npt.NDArray[ np.float64 ] = vtk_to_numpy( dataArray )
if sorted and ( data.IsA( "vtkCellData" ) or data.IsA( "vtkPointData" ) ):
sortArrayByGlobalIds( data, arr )
return arr
return None
def getAttributeSet( mesh: Union[ vtkMultiBlockDataSet, vtkDataSet ], onPoints: bool ) -> set[ str ]:
"""Get the set of all attributes from an mesh on points or on cells.
Args:
mesh (Any): Mesh where to find the attributes.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
set[str]: Set of attribute names present in input mesh.
"""
attributes: dict[ str, int ]
if isinstance( mesh, vtkMultiBlockDataSet ):
attributes = getAttributesFromMultiBlockDataSet( mesh, onPoints )
elif isinstance( mesh, vtkDataSet ):
attributes = getAttributesFromDataSet( mesh, onPoints )
else:
raise TypeError( "Input mesh must be a vtkDataSet or vtkMultiBlockDataSet." )
assert attributes is not None, "Attribute list is undefined."
return set( attributes.keys() ) if attributes is not None else set()
def getAttributesWithNumberOfComponents(
mesh: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet, vtkDataSet, vtkDataObject ],
onPoints: bool,
) -> dict[ str, int ]:
"""Get the dictionary of all attributes from object on points or cells.
Args:
mesh (Any): Mesh where to find the attributes.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
dict[str, int]: Dictionary where keys are the names of the attributes and values the number of components.
"""
attributes: dict[ str, int ]
if isinstance( mesh, ( vtkMultiBlockDataSet, vtkCompositeDataSet ) ):
attributes = getAttributesFromMultiBlockDataSet( mesh, onPoints )
elif isinstance( mesh, vtkDataSet ):
attributes = getAttributesFromDataSet( mesh, onPoints )
else:
raise TypeError( "Input mesh must be a vtkDataSet or vtkMultiBlockDataSet." )
return attributes
def getAttributesFromMultiBlockDataSet( multiBlockDataSet: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet ],
onPoints: bool ) -> dict[ str, int ]:
"""Get the dictionary of all attributes of object on points or on cells.
Args:
multiBlockDataSet (vtkMultiBlockDataSet | vtkCompositeDataSet): multiBlockDataSet where to find the attributes.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
dict[str, int]: Dictionary of the names of the attributes as keys, and number of components as values.
"""
attributes: dict[ str, int ] = {}
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
blockAttributes: dict[ str, int ] = getAttributesFromDataSet( dataSet, onPoints )
for attributeName, nbComponents in blockAttributes.items():
if attributeName not in attributes:
attributes[ attributeName ] = nbComponents
return attributes
def getAttributesFromDataSet( dataSet: vtkDataSet, onPoints: bool ) -> dict[ str, int ]:
"""Get the dictionary of all attributes of a vtkDataSet on points or cells.
Args:
dataSet (vtkDataSet): DataSet where to find the attributes.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
dict[str, int]: List of the names of the attributes.
"""
attributes: dict[ str, int ] = {}
data: Union[ vtkPointData, vtkCellData ]
sup: str = ""
if onPoints:
data = dataSet.GetPointData()
sup = "Point"
else:
data = dataSet.GetCellData()
sup = "Cell"
assert data is not None, f"{sup} data was not recovered."
nbAttributes: int = data.GetNumberOfArrays()
for i in range( nbAttributes ):
attributeName: str = data.GetArrayName( i )
attribute: vtkDataArray = data.GetArray( attributeName )
assert attribute is not None, f"Attribute {attributeName} is null"
nbComponents: int = attribute.GetNumberOfComponents()
attributes[ attributeName ] = nbComponents
return attributes
def isAttributeInObject( mesh: Union[ vtkMultiBlockDataSet, vtkDataSet ], attributeName: str, onPoints: bool ) -> bool:
"""Check if an attribute is in the input object.
Args:
mesh (vtkMultiBlockDataSet | vtkDataSet): Input mesh.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
bool: True if the attribute is in the table, False otherwise.
"""
if isinstance( mesh, vtkMultiBlockDataSet ):
return isAttributeInObjectMultiBlockDataSet( mesh, attributeName, onPoints )
elif isinstance( mesh, vtkDataSet ):
return isAttributeInObjectDataSet( mesh, attributeName, onPoints )
else:
raise TypeError( "Input object must be a vtkDataSet or vtkMultiBlockDataSet." )
def isAttributeInObjectMultiBlockDataSet( multiBlockDataSet: vtkMultiBlockDataSet, attributeName: str,
onPoints: bool ) -> bool:
"""Check if an attribute is in the input object.
Args:
multiBlockDataSet (vtkMultiBlockDataSet): Input multiBlockDataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
bool: True if the attribute is in the table, False otherwise.
"""
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
if isAttributeInObjectDataSet( dataSet, attributeName, onPoints ):
return True
return False
def isAttributeInObjectDataSet( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> bool:
"""Check if an attribute is in the input object.
Args:
dataSet (vtkDataSet): Input dataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
bool: True if the attribute is in the table, False otherwise.
"""
data: Union[ vtkPointData, vtkCellData ]
sup: str = ""
if onPoints:
data = dataSet.GetPointData()
sup = "Point"
else:
data = dataSet.GetCellData()
sup = "Cell"
assert data is not None, f"{ sup } data was not recovered."
return bool( data.HasArray( attributeName ) )
def isAttributeGlobal( multiBlockDataSet: vtkMultiBlockDataSet, attributeName: str, onPoints: bool ) -> bool:
"""Check if an attribute is global in the input multiBlockDataSet.
Args:
multiBlockDataSet (vtkMultiBlockDataSet): Input multiBlockDataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
bool: True if the attribute is global, False if not.
"""
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
if not isAttributeInObjectDataSet( dataSet, attributeName, onPoints ):
return False
return True
def getArrayInObject( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> npt.NDArray[ Any ]:
"""Return the numpy array corresponding to input attribute name in table.
Args:
dataSet (vtkDataSet): Input dataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
ArrayLike[Any]: The numpy array corresponding to input attribute name.
"""
vtkArray: vtkDataArray = getVtkArrayInObject( dataSet, attributeName, onPoints )
npArray: npt.NDArray[ Any ] = vnp.vtk_to_numpy( vtkArray ) # type: ignore[no-untyped-call]
return npArray
def getVtkArrayTypeInObject( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> int:
"""Return VTK type of requested array from dataset input.
Args:
dataSet (vtkDataSet): Input dataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
int: The type of the vtk array corresponding to input attribute name.
"""
array: vtkDataArray = getVtkArrayInObject( dataSet, attributeName, onPoints )
vtkArrayType: int = array.GetDataType()
return vtkArrayType
def getVtkArrayTypeInMultiBlock( multiBlockDataSet: vtkMultiBlockDataSet, attributeName: str, onPoints: bool ) -> int:
"""Return VTK type of requested array from multiblock dataset input, if existing.
Args:
multiBlockDataSet (vtkMultiBlockDataSet): Input multiBlockDataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
int: Type of the requested vtk array if existing in input multiblock dataset.
"""
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
listAttributes: set[ str ] = getAttributeSet( dataSet, onPoints )
if attributeName in listAttributes:
return getVtkArrayTypeInObject( dataSet, attributeName, onPoints )
raise AssertionError( "The vtkMultiBlockDataSet has no attribute with the name " + attributeName + "." )
def getVtkArrayInObject( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> vtkDataArray:
"""Return the array corresponding to input attribute name in table.
Args:
dataSet (vtkDataSet): Input dataSet.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
vtkDataArray: The vtk array corresponding to input attribute name.
"""
assert isAttributeInObject( dataSet, attributeName, onPoints ), f"{attributeName} is not in input mesh."
return dataSet.GetPointData().GetArray( attributeName ) if onPoints else dataSet.GetCellData().GetArray(
attributeName )
def getNumberOfComponents(
mesh: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet, vtkDataSet ],
attributeName: str,
onPoints: bool,
) -> int:
"""Get the number of components of attribute attributeName in dataSet.
Args:
mesh (vtkMultiBlockDataSet | vtkCompositeDataSet | vtkDataSet): Mesh where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
int: Number of components.
"""
if isinstance( mesh, vtkDataSet ):
return getNumberOfComponentsDataSet( mesh, attributeName, onPoints )
elif isinstance( mesh, ( vtkMultiBlockDataSet, vtkCompositeDataSet ) ):
return getNumberOfComponentsMultiBlock( mesh, attributeName, onPoints )
else:
raise AssertionError( "Object type is not managed." )
def getNumberOfComponentsDataSet( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> int:
"""Get the number of components of attribute attributeName in dataSet.
Args:
dataSet (vtkDataSet): DataSet where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
int: Number of components.
"""
array: vtkDataArray = getVtkArrayInObject( dataSet, attributeName, onPoints )
return array.GetNumberOfComponents()
def getNumberOfComponentsMultiBlock(
multiBlockDataSet: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet ],
attributeName: str,
onPoints: bool,
) -> int:
"""Get the number of components of attribute attributeName in dataSet.
Args:
multiBlockDataSet (vtkMultiBlockDataSet | vtkCompositeDataSet): multi block data Set where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
int: Number of components.
"""
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
if isAttributeInObject( dataSet, attributeName, onPoints ):
array: vtkDataArray = getVtkArrayInObject( dataSet, attributeName, onPoints )
return array.GetNumberOfComponents()
return 0
def getComponentNames(
mesh: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet, vtkDataSet, vtkDataObject ],
attributeName: str,
onPoints: bool,
) -> tuple[ str, ...]:
"""Get the name of the components of attribute attributeName in dataSet.
Args:
mesh (vtkDataSet | vtkMultiBlockDataSet | vtkCompositeDataSet | vtkDataObject): Mesh where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
tuple[str,...]: Names of the components.
"""
if isinstance( mesh, vtkDataSet ):
return getComponentNamesDataSet( mesh, attributeName, onPoints )
elif isinstance( mesh, ( vtkMultiBlockDataSet, vtkCompositeDataSet ) ):
return getComponentNamesMultiBlock( mesh, attributeName, onPoints )
else:
raise AssertionError( "Mesh type is not managed." )
def getComponentNamesDataSet( dataSet: vtkDataSet, attributeName: str, onPoints: bool ) -> tuple[ str, ...]:
"""Get the name of the components of attribute attributeName in dataSet.
Args:
dataSet (vtkDataSet): DataSet where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
tuple[str,...]: Names of the components.
"""
array: vtkDataArray = getVtkArrayInObject( dataSet, attributeName, onPoints )
componentNames: list[ str ] = []
if array.GetNumberOfComponents() > 1:
componentNames += [ array.GetComponentName( i ) for i in range( array.GetNumberOfComponents() ) ]
return tuple( componentNames )
def getComponentNamesMultiBlock(
multiBlockDataSet: Union[ vtkMultiBlockDataSet, vtkCompositeDataSet ],
attributeName: str,
onPoints: bool,
) -> tuple[ str, ...]:
"""Get the name of the components of attribute in MultiBlockDataSet.
Args:
multiBlockDataSet (vtkMultiBlockDataSet | vtkCompositeDataSet): DataSet where the attribute is.
attributeName (str): Name of the attribute.
onPoints (bool): True if attributes are on points, False if they are on cells.
Returns:
tuple[str,...]: Names of the components.
"""
elementaryBlockIndexes: list[ int ] = getBlockElementIndexesFlatten( multiBlockDataSet )
for blockIndex in elementaryBlockIndexes:
dataSet: vtkDataSet = vtkDataSet.SafeDownCast( multiBlockDataSet.GetDataSet( blockIndex ) )
if isAttributeInObject( dataSet, attributeName, onPoints ):
return getComponentNamesDataSet( dataSet, attributeName, onPoints )
return ()
def getAttributeValuesAsDF( surface: vtkPolyData, attributeNames: tuple[ str, ...] ) -> pd.DataFrame:
"""Get attribute values from input surface.
Args:
surface (vtkPolyData): Mesh where to get attribute values.
attributeNames (tuple[str,...]): Tuple of attribute names to get the values.
Returns:
pd.DataFrame: DataFrame containing property names as columns.
"""
nbRows: int = surface.GetNumberOfCells()
data: pd.DataFrame = pd.DataFrame( np.full( ( nbRows, len( attributeNames ) ), np.nan ), columns=attributeNames )
for attributeName in attributeNames:
if not isAttributeInObject( surface, attributeName, False ):
logging.warning( f"Attribute {attributeName} is not in the mesh." )
continue
array: npt.NDArray[ np.float64 ] = getArrayInObject( surface, attributeName, False )
if len( array.shape ) > 1:
for i in range( array.shape[ 1 ] ):
data[ attributeName + f"_{ i }" ] = array[ :, i ]
data.drop( columns=[ attributeName ], inplace=True )
else:
data[ attributeName ] = array
return data
def computeCellCenterCoordinates( mesh: vtkDataSet ) -> vtkDataArray:
"""Get the coordinates of Cell center.
Args:
mesh (vtkDataSet): Input surface.
Returns:
vtkPoints: Cell center coordinates.
"""
assert mesh is not None, "Surface is undefined."
filter: vtkCellCenters = vtkCellCenters()
filter.SetInputDataObject( mesh )
filter.Update()
output: vtkUnstructuredGrid = filter.GetOutputDataObject( 0 )
assert output is not None, "Cell center output is undefined."
pts: vtkPoints = output.GetPoints()
assert pts is not None, "Cell center points are undefined."
return pts.GetData()
def sortArrayByGlobalIds( data: Union[ vtkCellData, vtkPointData ], arr: npt.NDArray[ np.float64 ] ) -> None:
"""Sort an array following global Ids.
Args:
data (vtkFieldData): Global Ids array.
arr (npt.NDArray[ np.float64 ]): Array to sort.
"""
globalids: Optional[ npt.NDArray[ np.int64 ] ] = getNumpyGlobalIdsArray( data )
if globalids is not None:
arr = arr[ np.argsort( globalids ) ]
else:
logging.warning( "No sorting was performed." )