22from paraview .util .vtkAlgorithm import *
33from vtkmodules .vtkCommonCore import (
44 vtkPoints ,
5+ vtkUnsignedCharArray ,
56)
67from vtkmodules .vtkCommonDataModel import (
78 vtkCellArray ,
@@ -61,6 +62,44 @@ def ProcessPoint(point, radius):
6162 return [x , y , z ]
6263
6364
65+ def add_cell_arrays (inData , outData , cached_output ):
66+ """
67+ Adds arrays not modified in inData to outData.
68+ New arrays (or arrays modified) values are
69+ set using the PedigreeIds because the number of values
70+ in the new array (just read from the file) is different
71+ than the number of values in the arrays already processed through he
72+ pipeline.
73+ """
74+ pedigreeIds = cached_output .cell_data ["PedigreeIds" ]
75+ if pedigreeIds is None :
76+ print_error ("Error: no PedigreeIds array" )
77+ return
78+ cached_cell_data = cached_output .GetCellData ()
79+ in_cell_data = inData .GetCellData ()
80+ outData .ShallowCopy (cached_output )
81+ out_cell_data = outData .GetCellData ()
82+
83+ out_cell_data .Initialize ()
84+ for i in range (in_cell_data .GetNumberOfArrays ()):
85+ in_array = in_cell_data .GetArray (i )
86+ cached_array = cached_cell_data .GetArray (in_array .GetName ())
87+ if cached_array and cached_array .GetMTime () >= in_array .GetMTime ():
88+ # this scalar has been seen before
89+ # simply add a reference in the outData
90+ out_cell_data .AddArray (cached_array )
91+ else :
92+ # this scalar is new
93+ # we have to fill in the additional cells resulted from the clip
94+ out_array = in_array .NewInstance ()
95+ array0 = cached_cell_data .GetArray (0 )
96+ out_array .SetNumberOfComponents (array0 .GetNumberOfComponents ())
97+ out_array .SetNumberOfTuples (array0 .GetNumberOfTuples ())
98+ out_array .SetName (in_array .GetName ())
99+ out_cell_data .AddArray (out_array )
100+ outData .cell_data [out_array .GetName ()] = inData .cell_data [i ][pedigreeIds ]
101+
102+
64103@smproxy .filter ()
65104@smproperty .input (name = "Input" )
66105@smdomain .datatype (
@@ -440,7 +479,7 @@ def __init__(self):
440479 self .trim_lon = [0 , 0 ]
441480 self .trim_lat = [0 , 0 ]
442481 self .cached_cell_centers = None
443- self .cached_ghosts = None
482+ self ._cached_output = None
444483
445484 def SetTrimLongitude (self , left , right ):
446485 if left < 0 or left > 180 or right < 0 or right > 180 :
@@ -469,7 +508,6 @@ def RequestData(self, request, inInfo, outInfo):
469508 outData .ShallowCopy (inData )
470509 return 1
471510
472- outData .ShallowCopy (inData )
473511 if self .cached_cell_centers and self .cached_cell_centers .GetMTime () >= max (
474512 inData .GetPoints ().GetMTime (), inData .GetCells ().GetMTime ()
475513 ):
@@ -492,27 +530,41 @@ def RequestData(self, request, inInfo, outInfo):
492530 # get the numpy array for cell centers
493531 cc = numpy_support .vtk_to_numpy (cell_centers )
494532
495- if self .cached_ghosts and self .cached_ghosts .GetMTime () >= max (
533+ if self ._cached_output and self ._cached_output .GetMTime () >= max (
496534 self .GetMTime (), inData .GetPoints ().GetMTime (), cell_centers .GetMTime ()
497535 ):
498- ghost = self .cached_ghosts
536+ outData .ShallowCopy (self ._cached_output )
537+ add_cell_arrays (inData , outData , self ._cached_output )
499538 else :
500- # import pdb;pdb.set_trace()
539+ # add PedigreeIds
540+ generate_ids = vtkGenerateIds ()
541+ generate_ids .SetInputData (inData )
542+ generate_ids .PointIdsOff ()
543+ generate_ids .SetCellIdsArrayName ("PedigreeIds" )
544+ generate_ids .Update ()
545+ outData .ShallowCopy (generate_ids .GetOutput ())
546+ # we have to deep copy the cell array because we modify it
547+ # with RemoveGhostCells
548+ cells = vtkCellArray ()
549+ cell_types = vtkUnsignedCharArray ()
550+ cells .DeepCopy (outData .GetCells ())
551+ cell_types .DeepCopy (outData .GetCellTypesArray ())
552+ outData .SetCells (cell_types , cells )
553+
501554 # compute the new bounds by trimming the inData bounds
502555 bounds = list (inData .GetBounds ())
503556 bounds [0 ] = bounds [0 ] + self .trim_lon [0 ]
504557 bounds [1 ] = bounds [1 ] - self .trim_lon [1 ]
505558 bounds [2 ] = bounds [2 ] + self .trim_lat [0 ]
506559 bounds [3 ] = bounds [3 ] - self .trim_lat [1 ]
507560
508- # add hidden cells based on bounds
561+ # add HIDDENCELL based on bounds
509562 outside_mask = (
510563 (cc [:, 0 ] < bounds [0 ])
511564 | (cc [:, 0 ] > bounds [1 ])
512565 | (cc [:, 1 ] < bounds [2 ])
513566 | (cc [:, 1 ] > bounds [3 ])
514567 )
515-
516568 # Create ghost array (0 = visible, HIDDENCELL = invisible)
517569 ghost_np = np .where (
518570 outside_mask , vtkDataSetAttributes .HIDDENCELL , 0
@@ -521,11 +573,11 @@ def RequestData(self, request, inInfo, outInfo):
521573 # Convert to VTK and add to output
522574 ghost = numpy_support .numpy_to_vtk (ghost_np )
523575 ghost .SetName (vtkDataSetAttributes .GhostArrayName ())
524- # the previous cached_ghosts, if any,
525- # is available for garbage collection after this assignment
526- self .cached_ghosts = ghost
527- outData .GetCellData ().AddArray (ghost )
576+ outData .GetCellData ().AddArray (ghost )
577+ outData .RemoveGhostCells ()
528578
579+ self ._cached_output = outData .NewInstance ()
580+ self ._cached_output .ShallowCopy (outData )
529581 return 1
530582
531583
@@ -600,34 +652,7 @@ def RequestData(self, request, inInfo, outInfo):
600652 and self ._cached_output .GetCells ().GetMTime ()
601653 >= inData .GetCells ().GetMTime ()
602654 ):
603- # only scalars have been added or removed
604- cached_cell_data = self ._cached_output .GetCellData ()
605-
606- in_cell_data = inData .GetCellData ()
607-
608- outData .ShallowCopy (self ._cached_output )
609- out_cell_data = outData .GetCellData ()
610-
611- out_cell_data .Initialize ()
612- for i in range (in_cell_data .GetNumberOfArrays ()):
613- in_array = in_cell_data .GetArray (i )
614- cached_array = cached_cell_data .GetArray (in_array .GetName ())
615- if cached_array and cached_array .GetMTime () >= in_array .GetMTime ():
616- # this scalar has been seen before
617- # simply add a reference in the outData
618- out_cell_data .AddArray (cached_array )
619- else :
620- # this scalar is new
621- # we have to fill in the additional cells resulted from the clip
622- out_array = in_array .NewInstance ()
623- array0 = cached_cell_data .GetArray (0 )
624- out_array .SetNumberOfComponents (array0 .GetNumberOfComponents ())
625- out_array .SetNumberOfTuples (array0 .GetNumberOfTuples ())
626- out_array .SetName (in_array .GetName ())
627- out_cell_data .AddArray (out_array )
628- outData .cell_data [out_array .GetName ()] = inData .cell_data [i ][
629- self ._cached_output .cell_data ["PedigreeIds" ]
630- ]
655+ add_cell_arrays (inData , outData , self ._cached_output )
631656 else :
632657 generate_ids = vtkGenerateIds ()
633658 generate_ids .SetInputData (inData )
0 commit comments