Salome HOME
Upgrade to paraview 5.4
[modules/gui.git] / src / VTKViewer / VTKViewer_AppendFilter.cxx
index dce05c44e0b976d6f7a9185459a53abef276711c..820d711064142933391a78b7f82d6b78c728139b 100644 (file)
@@ -1,65 +1,42 @@
-//  SALOME OBJECT : kernel of SALOME component
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  This library is free software; you can redistribute it and/or 
-//  modify it under the terms of the GNU Lesser General Public 
-//  License as published by the Free Software Foundation; either 
-//  version 2.1 of the License. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
+//  SALOME OBJECT : kernel of SALOME component
 //  File   : VTKViewer_GeometryFilter.cxx
 //  Author : 
-//  Module : SALOME
-//  $Header$
 
 #include "VTKViewer_AppendFilter.h"
 
-#include "VTKViewer_ConvexTool.h"
-
-#include <vtkSmartPointer.h>
-#include <vtkCellArray.h>
+#include <vtkCell.h>
 #include <vtkCellData.h>
-#include <vtkGenericCell.h>
-#include <vtkHexahedron.h>
-#include <vtkMergePoints.h>
+#include <vtkDataSetAttributes.h>
+#include <vtkDataSetCollection.h>
 #include <vtkObjectFactory.h>
 #include <vtkPointData.h>
-#include <vtkPolyData.h>
-#include <vtkPyramid.h>
-#include <vtkStructuredGrid.h>
-#include <vtkTetra.h>
-#include <vtkUnsignedCharArray.h>
 #include <vtkUnstructuredGrid.h>
-#include <vtkVoxel.h>
-#include <vtkWedge.h>
-#include <vtkDataSetCollection.h>
-
-#include <vector>
-#include <map>
-using namespace std;
+#include <vtkInformation.h>
+#include <vtkInformationVector.h>
 
-
-#ifdef _DEBUG_
-//static int MYDEBUG = 0;
-//static int MYDEBUGWITHFILES = 0;
-#else
-//static int MYDEBUG = 0;
-//static int MYDEBUGWITHFILES = 0;
-#endif
+#include <vtkPoints.h>
 
 #if defined __GNUC__
   #if __GNUC__ == 2
@@ -67,13 +44,12 @@ using namespace std;
   #endif
 #endif
 
-vtkCxxRevisionMacro(VTKViewer_AppendFilter, "$Revision$");
 vtkStandardNewMacro(VTKViewer_AppendFilter);
 
 VTKViewer_AppendFilter
 ::VTKViewer_AppendFilter() 
 {
-  myDoMappingFlag=false;
+  myDoMappingFlag = false;
 }
 
 VTKViewer_AppendFilter
@@ -84,7 +60,12 @@ void
 VTKViewer_AppendFilter
 ::SetDoMappingFlag(const bool theFlag)
 {
-  myDoMappingFlag=theFlag;
+  if(myDoMappingFlag == theFlag)
+    return;
+
+  myDoMappingFlag = theFlag;
+
+  this->Modified();
 }
 
 bool 
@@ -96,234 +77,215 @@ VTKViewer_AppendFilter
 
 void
 VTKViewer_AppendFilter
-::SetPoints(vtkPoints* thePoints)
+::SetSharedPointsDataSet(vtkPointSet* thePointsDataSet)
 {
-  myPoints = thePoints;
+  if(GetSharedPointsDataSet() == thePointsDataSet)
+    return;
+
+  mySharedPointsDataSet = thePointsDataSet;
+
+  Modified();
 }
 
-vtkPoints*
+vtkPointSet*
 VTKViewer_AppendFilter
-::GetPoints()
+::GetSharedPointsDataSet()
 {
-  return myPoints.GetPointer();
+  return mySharedPointsDataSet.GetPointer();
 }
 
-void
+int
 VTKViewer_AppendFilter
-::Execute()
+::RequestData(
+              vtkInformation *request,
+              vtkInformationVector **inputVector,
+              vtkInformationVector *outputVector)
 {
-  if (myPoints.GetPointer()) {
-    MakeOutput();
-  }
-  else {
-    vtkAppendFilter::Execute();
-  }
-  if (myDoMappingFlag){
+  int aRet = 0;
+  if(GetSharedPointsDataSet())
+    aRet = MakeOutput(request,inputVector,outputVector);
+  else
+    aRet = Superclass::RequestData(request,inputVector,outputVector);
+
+  if(myDoMappingFlag)
     DoMapping();
-  }
+  
+  return aRet;
 }
 
+
 void 
 VTKViewer_AppendFilter
-::Reset()
+::DoMapping()
 {
-  myNodeIds.clear();
-  myCellIds.clear();
   myNodeRanges.clear();
   myCellRanges.clear();
-  myNodeMapObjIDVtkID.clear();
-  myCellMapObjIDVtkID.clear();
+
+  vtkIdType aPntStartId = 0;
+  vtkIdType aCellStartId = 0;
+
+  for(vtkIdType aDataSetId = 0; aDataSetId < this->GetNumberOfInputPorts(); ++aDataSetId){
+    vtkDataSet* aDataSet = (vtkDataSet *)(this->GetInput(aDataSetId));
+    // Do mapping of the nodes
+    if(!GetSharedPointsDataSet()){
+      vtkIdType aNbPnts = aDataSet->GetNumberOfPoints();
+      myNodeRanges.push_back(aPntStartId + aNbPnts);
+      aPntStartId += aNbPnts;
+    }
+    // Do mapping of the cells
+    vtkIdType aNbCells = aDataSet->GetNumberOfCells();
+    myCellRanges.push_back(aCellStartId + aNbCells);
+    aCellStartId += aNbCells;
+  }
 }
-//==================================================================
-// function: DoMapping
-// purpose :
-//==================================================================
-void 
-VTKViewer_AppendFilter
-::DoMapping()
+
+namespace
 {
-  int i, j, i1, i2, iNodeCnt, iCellCnt; 
-  IteratorOfDataMapOfIntegerInteger aMapIt;
-  vtkIdType aNbPnts, aNbCells, aId;
-  vtkDataSet *pDS;
-  //
-  Reset();
-  //
-  iNodeCnt=0;
-  iCellCnt=0;
-  for (i=0; i<NumberOfInputs; ++i) {
-    pDS=(vtkDataSet *)Inputs[i];
-    //
-    // Nodes
-    if (!myPoints.GetPointer()) {
-      aNbPnts=pDS->GetNumberOfPoints();
-      i1=myNodeIds.size();
-      i2=i1+aNbPnts-1;
-      myNodeRanges.push_back(i1);
-      myNodeRanges.push_back(i2);
-      //
-      for(j=0; j<aNbPnts; ++j) {
-       aId=(vtkIdType)j;
-       myNodeIds.push_back(aId);
-       //
-       aMapIt=myNodeMapObjIDVtkID.find(aId);
-       if (aMapIt==myNodeMapObjIDVtkID.end()) {
-         // if not found
-         myNodeMapObjIDVtkID[aId]=iNodeCnt;
-       }
-       ++iNodeCnt;
-      }
-    }
-    //
-    // Cells
-    aNbCells=pDS->GetNumberOfCells();
-    i1=myCellIds.size();
-    i2=i1+aNbCells-1;
-    myCellRanges.push_back(i1);
-    myCellRanges.push_back(i2);
-    for(j=0; j<aNbCells; ++j) {
-      aId=(vtkIdType)j;
-      myCellIds.push_back(aId);
-      //
-      aMapIt=myCellMapObjIDVtkID.find(aId);
-      if (aMapIt==myCellMapObjIDVtkID.end()) {
-       // if not found
-       myCellMapObjIDVtkID[aId]=iCellCnt;
-      }
-      ++iCellCnt;
-    }
+  inline
+  vtkIdType
+  GetOutputID(vtkIdType theInputID,
+              vtkIdType theInputDataSetID,
+              const VTKViewer_AppendFilter::TVectorIds& theRanges)
+  {
+    theInputID = theInputDataSetID = -1;
+
+    vtkIdType aNbInputs = theRanges.size();
+    if(theInputDataSetID < 0 || theInputDataSetID >= aNbInputs)
+      return -1;
+    
+    vtkIdType aStartId = theRanges[theInputDataSetID];
+    return aStartId + theInputID;
   }
 }
 
-//---------------------------------------------------------------
 vtkIdType
 VTKViewer_AppendFilter
-::GetPointOutputID(vtkIdType theInputID)
+::GetPointOutputID(vtkIdType theInputID,
+                   vtkIdType theInputDataSetID)
 {
-  if (myPoints.GetPointer()) {
+  if(GetSharedPointsDataSet())
     return theInputID;
-  }
-  //
-  int aVtkID=-1;
-  IteratorOfDataMapOfIntegerInteger aMapIt;
-  //
-  aMapIt=myNodeMapObjIDVtkID.find(theInputID);
-  if (aMapIt!=myNodeMapObjIDVtkID.end()) {
-    // found
-    PairOfDataMapOfIntegerInteger& aPair=(*aMapIt);
-    aVtkID=aPair.second;
-  }
-  return aVtkID;
+
+  return GetOutputID(theInputID,theInputDataSetID,myNodeRanges);
 }
 
 
-//---------------------------------------------------------------
 vtkIdType 
 VTKViewer_AppendFilter
-::GetCellOutputID(vtkIdType theInputID)
+::GetCellOutputID(vtkIdType theInputID,
+                   vtkIdType theInputDataSetID)
 {
-  int aVtkID=-1;
-  IteratorOfDataMapOfIntegerInteger aMapIt;
-  //
-  aMapIt=myCellMapObjIDVtkID.find(theInputID);
-  if (aMapIt!=myCellMapObjIDVtkID.end()) {
-    // found
-    PairOfDataMapOfIntegerInteger& aPair=(*aMapIt);
-    aVtkID=aPair.second;
-  }
-  return aVtkID;
+  if(GetSharedPointsDataSet())
+    return theInputID;
+
+  return GetOutputID(theInputID,theInputDataSetID,myCellRanges);
 }
 
 
-//---------------------------------------------------------------
-vtkIdType 
-VTKViewer_AppendFilter
-::GetPointInputID(vtkIdType theOutputID, 
-                 vtkIdType& theInputDataSetID)
+namespace
 {
-  if (myPoints.GetPointer()) {
-    theInputDataSetID=0;
-    return theOutputID;
-  }
-  //
-  int aNb, aNbRanges, aRetID, i, i1, i2, j;
-  //
-  aRetID=-1;
-  theInputDataSetID=-1;
-  //
-  aNb=myNodeIds.size();
-  if (theOutputID<0 ||  theOutputID>=aNb) {
-    return aRetID;
-  }
-  //
-  aRetID=(int)myNodeIds[theOutputID];
-  //
-  aNbRanges=myNodeRanges.size()/2;
-  for (i=0; i<aNbRanges; ++i) {
-    j=2*i;
-    i1=myNodeRanges[j];
-    i2=myNodeRanges[j+1];
-    if (theOutputID>=i1 && theOutputID<=i2) {
-      theInputDataSetID=i;
+  void
+  GetInputID(vtkIdType theOutputID,
+             vtkIdType& theInputID,
+             vtkIdType& theStartID,
+             vtkIdType& theInputDataSetID,
+             const VTKViewer_AppendFilter::TVectorIds& theRanges)
+  {
+    theInputID = theStartID = theInputDataSetID = -1;
+
+    if(theRanges.empty())
+      return;
+
+    const vtkIdType& aRangeEnd = theRanges.back();
+    if(theOutputID < 0 ||  theOutputID >= aRangeEnd)
+      return;
+
+    vtkIdType aStartId = 0;
+    vtkIdType aNbInputs = theRanges.size();
+    for(vtkIdType aDataSetId = 0; aDataSetId < aNbInputs; ++aDataSetId){
+      vtkIdType aRange = theRanges[aDataSetId];
+      if(aRange > theOutputID){
+        theInputID = theOutputID - aStartId;
+        theInputDataSetID = aDataSetId;
+        theStartID = aStartId;
+        break;
+      }
+      aStartId = aRange;
     }
   }
-  //
-  return aRetID;
 }
 
-
-//---------------------------------------------------------------
-vtkIdType 
+void 
 VTKViewer_AppendFilter
-::GetCellInputID(vtkIdType theOutputID, 
-                vtkIdType& theInputDataSetID)
+::GetPointInputID(vtkIdType theOutputID,
+                  vtkIdType& theInputID,
+                  vtkIdType& theStartID,
+                  vtkIdType& theInputDataSetID)
 {
-  int aNb, aNbRanges, aRetID, i, i1, i2, j;
-  //
-  aRetID=-1;
-  theInputDataSetID=-1;
-  //
-  aNb=myCellIds.size();
-  if (theOutputID<0 ||  theOutputID>=aNb) {
-    return aRetID;
-  }
-  //
-  aRetID=(int)myCellIds[theOutputID];
-  //
-  aNbRanges=myCellRanges.size()/2;
-  for (i=0; i<aNbRanges; ++i) {
-    j=2*i;
-    i1=myCellRanges[j];
-    i2=myCellRanges[j+1];
-    if (theOutputID>=i1 && theOutputID<=i2) {
-      theInputDataSetID=i;
-    }
+  if(GetSharedPointsDataSet()) {
+    theStartID = theInputDataSetID = 0;
+    theInputID = theOutputID;
+    return;
   }
-  //
-  return aRetID;
+
+  ::GetInputID(theOutputID,
+               theInputID,
+               theStartID,
+               theInputDataSetID,
+               myNodeRanges);
 }
 
 
-//---------------------------------------------------------------
-void 
+void
+VTKViewer_AppendFilter
+::GetCellInputID(vtkIdType theOutputID,
+                 vtkIdType& theInputID,
+                 vtkIdType& theStartID,
+                 vtkIdType& theInputDataSetID)
+{
+  ::GetInputID(theOutputID,
+               theInputID,
+               theStartID,
+               theInputDataSetID,
+               myCellRanges);
+}
+
+
+int
 VTKViewer_AppendFilter
-::MakeOutput()
+::MakeOutput(
+  vtkInformation *vtkNotUsed(request),
+  vtkInformationVector **inputVector,
+  vtkInformationVector *outputVector)
 {
   int idx;
-  vtkIdType numPts, numCells, newCellId, cellId;
-  vtkCellData *cd;
+  vtkIdType numPts, numCells, cellId;
+  // vtkCellData *cd;
   vtkIdList *ptIds;
   vtkDataSet *ds;
-  vtkUnstructuredGrid *output = this->GetOutput();
+  int numInputs = this->GetNumberOfInputConnections(0);
+  
+  // get the output info object
+  vtkInformation *outInfo = outputVector->GetInformationObject(0);
+
+  // get the ouptut
+  vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
+    outInfo->Get(vtkDataObject::DATA_OBJECT()));
   //
-  numPts = myPoints->GetNumberOfPoints();
+  numPts = mySharedPointsDataSet->GetNumberOfPoints();
   if (numPts < 1) {
-    return;
+    return 0;
   }
   //
   numCells = 0;
-  for (idx = 0; idx < this->NumberOfInputs; ++idx) {
-    ds = (vtkDataSet *)(this->Inputs[idx]);
+  vtkInformation *inInfo = 0;
+  for (idx = 0; idx < numInputs;++idx) {
+    inInfo = inputVector[0]->GetInformationObject(idx);
+    ds = 0;
+    if (inInfo)
+      {
+      ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
+      }
     if (ds != NULL)  {
       if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )  {
         continue; //no input, just skip
@@ -332,7 +294,7 @@ VTKViewer_AppendFilter
     }//if non-empty dataset
   }//for all inputs
   if (numCells < 1) {
-    return;
+    return 0;
   }
   //
   // Now can allocate memory
@@ -343,21 +305,35 @@ VTKViewer_AppendFilter
   // Append each input dataset together
   //
   // 1.points
-  output->SetPoints(myPoints.GetPointer());
+  output->SetPoints(GetSharedPointsDataSet()->GetPoints());
+  output->GetPointData()->PassData(GetSharedPointsDataSet()->GetPointData());
   // 2.cells
-  for (idx = 0; idx < this->NumberOfInputs; ++idx) {
-    ds = (vtkDataSet *)(this->Inputs[idx]);
+  for (idx = 0; idx < numInputs; ++idx) {
+    inInfo = inputVector[0]->GetInformationObject(idx);
+    ds = 0;
+    if (inInfo)
+      {
+      ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
+      }
     if (ds != NULL) {
+      
       numCells = ds->GetNumberOfCells(); 
-      cd = ds->GetCellData();
+      // cd = ds->GetCellData();
       // copy cell and cell data
       for (cellId=0; cellId<numCells; cellId++)  {
         ds->GetCellPoints(cellId, ptIds);
-        newCellId = output->InsertNextCell(ds->GetCellType(cellId), ptIds);
+        output->InsertNextCell(ds->GetCellType(cellId), ptIds);
       }
     }
   }
   //
   ptIds->Delete();
+  return 1;
 }
 
+int VTKViewer_AppendFilter::FillInputPortInformation(int, vtkInformation *info)
+{
+  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
+  info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
+  return 1;
+}