1111 vtkPlane ,
1212)
1313from vtkmodules .vtkCommonTransforms import vtkTransform
14- from vtkmodules .vtkFiltersCore import vtkAppendFilter
14+ from vtkmodules .vtkFiltersCore import (
15+ vtkAppendFilter ,
16+ vtkGenerateIds ,
17+ )
1518from vtkmodules .vtkFiltersGeneral import (
1619 vtkTransformFilter ,
1720 vtkTableBasedClipDataSet ,
@@ -419,6 +422,7 @@ def __init__(self):
419422 )
420423 # common values:
421424 self ._center_meridian = 0
425+ self ._cached_output = None
422426
423427 def SetMeridian (self , center_meridian_ ):
424428 '''
@@ -427,38 +431,80 @@ def SetMeridian(self, center_meridian_):
427431 self ._center_meridian = center_meridian_
428432 self .Modified ()
429433
434+ def GetMeridian (self ):
435+ '''
436+ Returns the central meridian
437+ '''
438+ return self ._center_meridian
439+
430440 def RequestData (self , request , inInfo , outInfo ):
431441 inData = self .GetInputData (inInfo , 0 , 0 )
432- outData = self .GetOutputData (outInfo , 0 )
433-
434- cut_meridian = self ._center_meridian + 180
435-
436- plane = vtkPlane ()
437- plane .SetOrigin ([cut_meridian , 0.0 , 0.0 ])
438- plane .SetNormal ([- 1 , 0 , 0 ])
439- # vtkClipPolyData hangs
440- clipL = vtkTableBasedClipDataSet ()
441- clipL .SetClipFunction (plane )
442- clipL .SetInputData (inData )
443- clipL .Update ()
444-
445- plane .SetNormal ([1 , 0 , 0 ])
446- clipR = vtkTableBasedClipDataSet ()
447- clipR .SetClipFunction (plane )
448- clipR .SetInputData (inData )
449- clipR .Update ()
450-
451- transFunc = vtkTransform ()
452- transFunc .Translate (- 360 , 0 , 0 )
453- transform = vtkTransformFilter ()
454- transform .SetInputData (clipR .GetOutput ())
455- transform .SetTransform (transFunc )
456- transform .Update ()
457-
458- append = vtkAppendFilter ()
459- append .AddInputData (clipL .GetOutput ())
460- append .AddInputData (transform .GetOutput ())
461- append .Update ()
462- outData .ShallowCopy (append .GetOutput ())
442+ inPoints = inData .GetPoints ()
443+ inCellArray = inData .GetCells ()
463444
445+ outData = self .GetOutputData (outInfo , 0 )
446+ if self ._cached_output and self ._cached_output .GetMTime () > inPoints .GetMTime () and \
447+ self ._cached_output .GetMTime () > inCellArray .GetMTime ():
448+ # only scalars have been added or removed
449+ cached_cell_data = self ._cached_output .GetCellData ()
450+
451+ in_cell_data = inData .GetCellData ()
452+
453+ outData .ShallowCopy (self ._cached_output )
454+ out_cell_data = outData .GetCellData ()
455+
456+ out_cell_data .Initialize ()
457+ for i in range (in_cell_data .GetNumberOfArrays ()):
458+ in_array = in_cell_data .GetArray (i )
459+ cached_array = cached_cell_data .GetArray (in_array .GetName ())
460+ if cached_array and cached_array .GetMTime () >= in_array .GetMTime ():
461+ # this scalar has been seen before
462+ # simply add a reference in the outData
463+ out_cell_data .AddArray (in_array )
464+ else :
465+ # this scalar is new
466+ # we have to fill in the additional cells resulted from the clip
467+ out_array = in_array .NewInstance ()
468+ array0 = cached_cell_data .GetArray (0 )
469+ out_array .SetNumberOfComponents (array0 .GetNumberOfComponents ())
470+ out_array .SetNumberOfTuples (array0 .GetNumberOfTuples ())
471+ out_array .SetName (in_array .GetName ())
472+ out_cell_data .AddArray (out_array )
473+ outData .cell_data [out_array .GetName ()] = inData .cell_data [i ][self ._cached_output .cell_data ['PedigreeIds' ]]
474+ else :
475+ generate_ids = vtkGenerateIds ()
476+ generate_ids .SetInputData (inData )
477+ generate_ids .PointIdsOff ()
478+ generate_ids .SetCellIdsArrayName ("PedigreeIds" )
479+
480+ cut_meridian = self ._center_meridian + 180
481+ plane = vtkPlane ()
482+ plane .SetOrigin ([cut_meridian , 0.0 , 0.0 ])
483+ plane .SetNormal ([- 1 , 0 , 0 ])
484+ # vtkClipPolyData hangs
485+ clipL = vtkTableBasedClipDataSet ()
486+ clipL .SetClipFunction (plane )
487+ clipL .SetInputConnection (generate_ids .GetOutputPort ())
488+ clipL .Update ()
489+
490+ plane .SetNormal ([1 , 0 , 0 ])
491+ clipR = vtkTableBasedClipDataSet ()
492+ clipR .SetClipFunction (plane )
493+ clipR .SetInputConnection (generate_ids .GetOutputPort ())
494+ clipR .Update ()
495+
496+ transFunc = vtkTransform ()
497+ transFunc .Translate (- 360 , 0 , 0 )
498+ transform = vtkTransformFilter ()
499+ transform .SetInputData (clipR .GetOutput ())
500+ transform .SetTransform (transFunc )
501+ transform .Update ()
502+
503+ append = vtkAppendFilter ()
504+ append .AddInputData (clipL .GetOutput ())
505+ append .AddInputData (transform .GetOutput ())
506+ append .Update ()
507+ outData .ShallowCopy (append .GetOutput ())
508+ self ._cached_output = outData .NewInstance ()
509+ self ._cached_output .ShallowCopy (outData )
464510 return 1
0 commit comments