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 ,
@@ -510,6 +513,7 @@ def __init__(self):
510513 )
511514 # common values:
512515 self ._center_meridian = 0
516+ self ._cached_output = None
513517
514518 def SetMeridian (self , center_meridian_ ):
515519 '''
@@ -518,38 +522,80 @@ def SetMeridian(self, center_meridian_):
518522 self ._center_meridian = center_meridian_
519523 self .Modified ()
520524
525+ def GetMeridian (self ):
526+ '''
527+ Returns the central meridian
528+ '''
529+ return self ._center_meridian
530+
521531 def RequestData (self , request , inInfo , outInfo ):
522532 inData = self .GetInputData (inInfo , 0 , 0 )
523- outData = self .GetOutputData (outInfo , 0 )
524-
525- cut_meridian = self ._center_meridian + 180
526-
527- plane = vtkPlane ()
528- plane .SetOrigin ([cut_meridian , 0.0 , 0.0 ])
529- plane .SetNormal ([- 1 , 0 , 0 ])
530- # vtkClipPolyData hangs
531- clipL = vtkTableBasedClipDataSet ()
532- clipL .SetClipFunction (plane )
533- clipL .SetInputData (inData )
534- clipL .Update ()
535-
536- plane .SetNormal ([1 , 0 , 0 ])
537- clipR = vtkTableBasedClipDataSet ()
538- clipR .SetClipFunction (plane )
539- clipR .SetInputData (inData )
540- clipR .Update ()
541-
542- transFunc = vtkTransform ()
543- transFunc .Translate (- 360 , 0 , 0 )
544- transform = vtkTransformFilter ()
545- transform .SetInputData (clipR .GetOutput ())
546- transform .SetTransform (transFunc )
547- transform .Update ()
548-
549- append = vtkAppendFilter ()
550- append .AddInputData (clipL .GetOutput ())
551- append .AddInputData (transform .GetOutput ())
552- append .Update ()
553- outData .ShallowCopy (append .GetOutput ())
533+ inPoints = inData .GetPoints ()
534+ inCellArray = inData .GetCells ()
554535
536+ outData = self .GetOutputData (outInfo , 0 )
537+ if self ._cached_output and self ._cached_output .GetMTime () > inPoints .GetMTime () and \
538+ self ._cached_output .GetMTime () > inCellArray .GetMTime ():
539+ # only scalars have been added or removed
540+ cached_cell_data = self ._cached_output .GetCellData ()
541+
542+ in_cell_data = inData .GetCellData ()
543+
544+ outData .ShallowCopy (self ._cached_output )
545+ out_cell_data = outData .GetCellData ()
546+
547+ out_cell_data .Initialize ()
548+ for i in range (in_cell_data .GetNumberOfArrays ()):
549+ in_array = in_cell_data .GetArray (i )
550+ cached_array = cached_cell_data .GetArray (in_array .GetName ())
551+ if cached_array and cached_array .GetMTime () >= in_array .GetMTime ():
552+ # this scalar has been seen before
553+ # simply add a reference in the outData
554+ out_cell_data .AddArray (in_array )
555+ else :
556+ # this scalar is new
557+ # we have to fill in the additional cells resulted from the clip
558+ out_array = in_array .NewInstance ()
559+ array0 = cached_cell_data .GetArray (0 )
560+ out_array .SetNumberOfComponents (array0 .GetNumberOfComponents ())
561+ out_array .SetNumberOfTuples (array0 .GetNumberOfTuples ())
562+ out_array .SetName (in_array .GetName ())
563+ out_cell_data .AddArray (out_array )
564+ outData .cell_data [out_array .GetName ()] = inData .cell_data [i ][self ._cached_output .cell_data ['PedigreeIds' ]]
565+ else :
566+ generate_ids = vtkGenerateIds ()
567+ generate_ids .SetInputData (inData )
568+ generate_ids .PointIdsOff ()
569+ generate_ids .SetCellIdsArrayName ("PedigreeIds" )
570+
571+ cut_meridian = self ._center_meridian + 180
572+ plane = vtkPlane ()
573+ plane .SetOrigin ([cut_meridian , 0.0 , 0.0 ])
574+ plane .SetNormal ([- 1 , 0 , 0 ])
575+ # vtkClipPolyData hangs
576+ clipL = vtkTableBasedClipDataSet ()
577+ clipL .SetClipFunction (plane )
578+ clipL .SetInputConnection (generate_ids .GetOutputPort ())
579+ clipL .Update ()
580+
581+ plane .SetNormal ([1 , 0 , 0 ])
582+ clipR = vtkTableBasedClipDataSet ()
583+ clipR .SetClipFunction (plane )
584+ clipR .SetInputConnection (generate_ids .GetOutputPort ())
585+ clipR .Update ()
586+
587+ transFunc = vtkTransform ()
588+ transFunc .Translate (- 360 , 0 , 0 )
589+ transform = vtkTransformFilter ()
590+ transform .SetInputData (clipR .GetOutput ())
591+ transform .SetTransform (transFunc )
592+ transform .Update ()
593+
594+ append = vtkAppendFilter ()
595+ append .AddInputData (clipL .GetOutput ())
596+ append .AddInputData (transform .GetOutput ())
597+ append .Update ()
598+ outData .ShallowCopy (append .GetOutput ())
599+ self ._cached_output = outData .NewInstance ()
600+ self ._cached_output .ShallowCopy (outData )
555601 return 1
0 commit comments