Salome HOME
Copyright update 2022
[modules/gui.git] / src / VTKViewer / VTKViewer_ExtractUnstructuredGrid.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 1f665ca..4a33aad
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  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
@@ -24,7 +24,7 @@
 // Author:  Alexey PETROV
 
 #include "VTKViewer_ExtractUnstructuredGrid.h"
-#include "VTKViewer_CellLocationsArray.h"
+//#include "VTKViewer_CellLocationsArray.h"
 
 #include <vtkUnsignedCharArray.h>
 #include <vtkUnstructuredGrid.h>
 
 #define VTK_XVERSION (VTK_MAJOR_VERSION*10000+VTK_MINOR_VERSION*100+VTK_BUILD_VERSION)
 
-vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid);
+vtkStandardNewMacro(VTKViewer_ExtractUnstructuredGrid)
 
 
 VTKViewer_ExtractUnstructuredGrid::VTKViewer_ExtractUnstructuredGrid():
-  myExtractionMode(eCells), myChangeMode(ePassAll)
+  myExtractionMode(eCells), myChangeMode(ePassAll), myStoreMapping( false ), myPassAll( false )
 {}
 
 
@@ -81,16 +81,198 @@ void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping)
   }
 }
 
-vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const
+vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(vtkIdType theOutId) const
 {
-  if ( myCellIds.empty() && myCellTypes.empty() )
+  if ( myPassAll || ( myCellIds.empty() && myCellTypes.empty() ))
     return theOutId;
 
-  if ( theOutId<0 || theOutId >= (int)myOut2InId.size() )
+  if ( theOutId < 0 || theOutId >= (vtkIdType) myOut2InId.size() )
     return -1;
   return myOut2InId[theOutId];
 }
 
+void VTKViewer_ExtractUnstructuredGrid::BuildOut2InMap()
+{
+  if ( myPassAll || !myOut2InId.empty() ) return;
+
+  vtkUnstructuredGrid *anInput = dynamic_cast< vtkUnstructuredGrid*>( this->GetInput() );
+  if ( !anInput ) return;
+
+  // use a vector of cellTypes to avoid searching in myCellTypes map
+  // for a better performance (IPAL53103)
+  TVectorId cellTypesVec( VTK_NUMBER_OF_CELL_TYPES, -1 );
+  for ( TSetId::iterator type = myCellTypes.begin(); type != myCellTypes.end(); ++type )
+  {
+    if ( *type >= (int)cellTypesVec.size() ) cellTypesVec.resize( *type+1, -1 );
+    if ( *type > 0 )
+      cellTypesVec[ *type ] = *type;
+  }
+
+  // same code as in RequestData() excluding cells copying
+  vtkIdType aNbElems = 0;
+  if ( myExtractionMode == eCells )
+  {
+    aNbElems = anInput->GetNumberOfCells();
+    if (( myChangeMode == ePassAll ) ||
+        ( myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving))
+    {
+      myPassAll = true;
+    }
+    else
+    {
+      if ( !myCellIds.empty() && myCellTypes.empty() )
+      {
+        if ( myChangeMode == eAdding )
+        {
+          myOut2InId.reserve(myCellIds.size());
+
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+            if ( myCellIds.find(aCellId) != myCellIds.end() )
+              myOut2InId.push_back( aCellId );
+        }
+        else
+        {
+          myOut2InId.reserve( std::max( vtkIdType(0), vtkIdType(aNbElems - myCellIds.size())));
+
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+            if ( myCellIds.find(aCellId) == myCellIds.end() )
+              myOut2InId.push_back( aCellId );
+        }
+      }
+      else if ( myCellIds.empty() && !myCellTypes.empty() )
+      {
+        myOut2InId.reserve( aNbElems );
+        if ( myChangeMode == eAdding )
+        {
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          {
+            vtkIdType aType = anInput->GetCellType(aCellId);
+            if ( cellTypesVec[ aType ] == aType )
+              myOut2InId.push_back( aCellId );
+          }
+        }
+        else
+        {
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          {
+            vtkIdType aType = anInput->GetCellType(aCellId);
+            if ( cellTypesVec[ aType ] != aType )
+              myOut2InId.push_back( aCellId );
+          }
+        }
+      }
+      else if ( !myCellIds.empty() && !myCellTypes.empty())
+      {
+        myOut2InId.reserve( aNbElems );
+
+        if ( myChangeMode == eAdding )
+        {
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          {
+            vtkIdType aType = anInput->GetCellType(aCellId);
+            if ( cellTypesVec[ aType ] == aType )
+              if ( myCellIds.find(aCellId) != myCellIds.end() )
+                myOut2InId.push_back( aCellId );
+          }
+        }
+        else
+        {
+          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          {
+            vtkIdType aType = anInput->GetCellType(aCellId);
+            if ( cellTypesVec[ aType ] != aType )
+              if ( myCellIds.find(aCellId) == myCellIds.end())
+                myOut2InId.push_back( aCellId );
+          }
+        }
+      }
+    } // not ePassAll 
+  }
+  else // nodes
+  {
+    aNbElems = anInput->GetNumberOfPoints();
+
+    if (( myChangeMode == ePassAll ) ||
+        ( myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving) ||
+        ( !anInput->GetCellTypesArray() ))
+    {
+      myPassAll = true;
+    }
+    else if ( !myCellIds.empty() && myCellTypes.empty())
+    {
+      if ( myChangeMode == eAdding )
+      {
+        myOut2InId.reserve( myCellIds.size() );
+
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          if ( myCellIds.find(aCellId) != myCellIds.end())
+            myOut2InId.push_back( aCellId );
+      }
+      else
+      {
+        myOut2InId.reserve( std::max( vtkIdType(0), vtkIdType(aNbElems - myCellIds.size())));
+
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+          if ( myCellIds.find(aCellId) == myCellIds.end())
+            myOut2InId.push_back( aCellId );
+      }
+    }
+    else if ( myCellIds.empty() && !myCellTypes.empty())
+    {
+      myOut2InId.reserve( aNbElems );
+
+      if ( myChangeMode == eAdding )
+      {
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+        {
+          vtkIdType aType = anInput->GetCellType(aCellId);
+          if ( cellTypesVec[ aType ] == aType )
+            myOut2InId.push_back( aCellId );
+        }
+      }
+      else
+      {
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+        {
+          vtkIdType aType = anInput->GetCellType(aCellId);
+          if ( cellTypesVec[ aType ] != aType )
+            myOut2InId.push_back( aCellId );
+        }
+      }
+    }
+    else if ( !myCellIds.empty() && !myCellTypes.empty() )
+    {
+      if ( myChangeMode == eAdding )
+      {
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+        {
+          vtkIdType aType = anInput->GetCellType(aCellId);
+          if ( cellTypesVec[ aType ] == aType )
+            if ( myCellIds.find(aCellId) != myCellIds.end())
+              myOut2InId.push_back( aCellId );
+        }
+      }
+      else
+      {
+        for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++)
+        {
+          vtkIdType aType = anInput->GetCellType(aCellId);
+          if ( cellTypesVec[ aType ] != aType )
+            if ( myCellIds.find(aCellId) == myCellIds.end())
+              myOut2InId.push_back( aCellId );
+        }
+      }
+    }
+  } // nodes
+
+  if ((vtkIdType) myOut2InId.size() == aNbElems )
+  {
+    myPassAll = true;
+    TVectorId().swap( myOut2InId );
+  }
+  return;
+}
+
 // vtkIdType VTKViewer_ExtractUnstructuredGrid::GetOutputId(int theInId) const{
 //   if(myCellIds.empty() && myCellTypes.empty()) return theInId;
 //   TMapId::const_iterator anIter = myIn2OutId.find(theInId);
@@ -99,15 +281,15 @@ vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const
 // }
 
 
-inline int InsertCell(vtkUnstructuredGrid *theInput,
+inline vtkIdType InsertCell(vtkUnstructuredGrid *theInput,
                       vtkCellArray *theConnectivity,
                       vtkUnsignedCharArray* theCellTypesArray,
                       vtkIdTypeArray*& theFaces,
                       vtkIdTypeArray*& theFaceLocations,
                       vtkIdType theCellId,
-                      vtkIdList *theIdList,
+                      vtkIdList* /*theIdList*/,
                       bool theStoreMapping,
-                      vtkIdType theOutId,
+                      vtkIdType /*theOutId*/,
                       VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId/*,
                       VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId*/)
 {
@@ -142,8 +324,8 @@ inline int InsertCell(vtkUnstructuredGrid *theInput,
     theFaceLocations->InsertNextValue(theFaces->GetMaxId() + 1);
 
     // insert cell connectivity and faces stream
-    vtkIdType nfaces = 0;
-    vtkIdType*  face = 0;
+    vtkIdType       nfaces = 0;
+    const vtkIdType*  face = 0;
     vtkIdType realnpts;
     theInput->GetFaceStream(theCellId, nfaces, face);
     vtkUnstructuredGrid::DecomposeAPolyhedronCell(
@@ -164,7 +346,7 @@ inline void InsertPointCell(vtkCellArray *theConnectivity,
                             vtkIdType theCellId,
                             vtkIdList *theIdList,
                             bool theStoreMapping,
-                            vtkIdType theOutId
+                            vtkIdType /*theOutId*/
                             VTKViewer_ExtractUnstructuredGrid::TVectorId& theOut2InId/*,
                             VTKViewer_ExtractUnstructuredGrid::TMapId& theIn2OutId*/)
 {
@@ -187,15 +369,16 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
   vtkInformation *outInfo = outputVector->GetInformationObject(0);
 
   // get the input and ouptut
-  vtkUnstructuredGrid *anInput = vtkUnstructuredGrid::SafeDownCast(
-    inInfo->Get(vtkDataObject::DATA_OBJECT()));
-  vtkUnstructuredGrid *anOutput = vtkUnstructuredGrid::SafeDownCast(
-    outInfo->Get(vtkDataObject::DATA_OBJECT()));
+  vtkUnstructuredGrid *anInput =
+    vtkUnstructuredGrid::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
+  vtkUnstructuredGrid *anOutput =
+    vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
 
   //vtkUnstructuredGrid *anInput = this->GetInput();
   //vtkUnstructuredGrid *anOutput = this->GetOutput();
 
-  myOut2InId.clear();  //myIn2OutId.clear();
+  myPassAll = false;
+  TVectorId().swap( myOut2InId );
 
   // use a vector of cellTypes to avoid searching in myCellTypes map
   // for a better performance (IPAL53103)
@@ -214,22 +397,25 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
     MESSAGE("Execute - myExtractionMode = "<<myExtractionMode);
     MESSAGE("Execute - myChangeMode = "<<myChangeMode);
   }*/
-  if(myExtractionMode == eCells){
+  vtkIdType aNbElems = 0;
+  if(myExtractionMode == eCells)
+  {
+    aNbElems = anInput->GetNumberOfCells();
     if(myChangeMode == ePassAll || (myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving)){
-      if(vtkIdType aNbElems = anInput->GetNumberOfCells()){
-        if(myStoreMapping) myOut2InId.reserve(aNbElems);
+      myPassAll = true;
+      if ( aNbElems > 0 ){
         anOutput->ShallowCopy(anInput);
-        if(myStoreMapping){
-          for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
-            myOut2InId.push_back(aCellId);
-            //myIn2OutId.insert( myIn2OutId.end(), std::make_pair( aCellId, anOutId ));
-          }
-        }
+        //if(myStoreMapping) myOut2InId.reserve(aNbElems);
+        // if(myStoreMapping){
+        //   for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
+        //     myOut2InId.push_back(aCellId);
+        //     //myIn2OutId.insert( myIn2OutId.end(), std::make_pair( aCellId, anOutId ));
+        //   }
+        // }
       }
     }else{
       vtkIdList *anIdList = vtkIdList::New();
       vtkCellArray *aConnectivity = vtkCellArray::New();
-      vtkIdType aNbElems = anInput->GetNumberOfCells();
       aConnectivity->Allocate(2*aNbElems,0);
       vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
       aCellTypesArray->SetNumberOfComponents(1);
@@ -303,12 +489,13 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
           }
         }
       }
-      if((aNbElems = aConnectivity->GetNumberOfCells())){
-        VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
+      if ( vtkIdType newNbElems = aConnectivity->GetNumberOfCells() ) {
+        vtkIdTypeArray* aCellLocationsArray = vtkIdTypeArray::New();
         aCellLocationsArray->SetNumberOfComponents(1);
-        aCellLocationsArray->SetNumberOfTuples(aNbElems);
+        aCellLocationsArray->SetNumberOfTuples(newNbElems);
         aConnectivity->InitTraversal();
-        for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
+        vtkIdType const *pts(nullptr);
+        for(vtkIdType i = 0, npts; aConnectivity->GetNextCell(npts,pts); i++){
           aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
         }
 #if VTK_XVERSION > 50700
@@ -325,11 +512,13 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
       if ( newFaceLocations ) newFaceLocations->Delete();
       if ( newFaces ) newFaces->Delete();
     }
-  }else{
+  }
+  else
+  {
     vtkIdList *anIdList = vtkIdList::New();
     anIdList->SetNumberOfIds(1);
     vtkCellArray *aConnectivity = vtkCellArray::New();
-    vtkIdType aNbElems = anInput->GetNumberOfPoints();
+    aNbElems = anInput->GetNumberOfPoints();
     aConnectivity->Allocate(2*aNbElems,0);
     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
     aCellTypesArray->SetNumberOfComponents(1);
@@ -338,10 +527,11 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
     // note that it is significant only when myExtractionMode == ePoints
     if(myChangeMode == ePassAll || (myCellIds.empty() && myCellTypes.empty() && myChangeMode == eRemoving) ||
        !anInput->GetCellTypesArray()){
-      if(myStoreMapping) myOut2InId.reserve(aNbElems);
+      myPassAll = true;
+      //if(myStoreMapping) myOut2InId.reserve(aNbElems);
       for(vtkIdType aCellId = 0, anOutId = 0; aCellId < aNbElems; aCellId++,anOutId++){
         InsertPointCell(aConnectivity,aCellTypesArray,aCellId,anIdList,
-                        myStoreMapping,anOutId,myOut2InId/*,myIn2OutId*/);
+                        /*theStoreMapping=*/false,anOutId,myOut2InId/*,myIn2OutId*/);
       }
     }else if(!myCellIds.empty() && myCellTypes.empty()){
       if(myStoreMapping) myOut2InId.reserve(myCellIds.size());
@@ -401,12 +591,13 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
         }
       }
     }
-    if((aNbElems = aConnectivity->GetNumberOfCells())){
-      VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
+    if (vtkIdType newNbElems = aConnectivity->GetNumberOfCells() ) {
+      vtkIdTypeArray* aCellLocationsArray = vtkIdTypeArray::New();
       aCellLocationsArray->SetNumberOfComponents(1);
-      aCellLocationsArray->SetNumberOfTuples(aNbElems);
+      aCellLocationsArray->SetNumberOfTuples( newNbElems );
       aConnectivity->InitTraversal();
-      for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
+      vtkIdType const *pts(nullptr);
+      for(vtkIdType i = 0, npts; aConnectivity->GetNextCell(npts,pts); i++){
         aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
       }
 #if VTK_XVERSION > 50700
@@ -421,6 +612,12 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
     aConnectivity->Delete();
     anIdList->Delete();
   }
+
+  if ( aNbElems == (vtkIdType) myOut2InId.size() )
+  {
+    myPassAll = true;
+    TVectorId().swap( myOut2InId );
+  }
 /*  if(MYDEBUG){
     MESSAGE("Execute - anOutput->GetNumberOfCells() = "<<anOutput->GetNumberOfCells());
     if(myStoreMapping){