Salome HOME
23315: [CEA 1929] Too much memory used to display a mesh in shading and wireframe
authoreap <eap@opencascade.com>
Fri, 11 Nov 2016 11:38:11 +0000 (14:38 +0300)
committereap <eap@opencascade.com>
Fri, 11 Nov 2016 11:38:11 +0000 (14:38 +0300)
  Avoid filling myOut2InId as much as possible

src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.cxx
src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.h

index bd5a69f9e5e5666c7e227593f82d4ed0b1b8b961..bd0cdd654497a38580cc3586566ea52e88dae137 100755 (executable)
@@ -83,7 +83,7 @@ void VTKViewer_ExtractUnstructuredGrid::SetStoreMapping(int theStoreMapping)
 
 vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const
 {
-  if ( myCellIds.empty() && myCellTypes.empty() )
+  if ( myPassAll || ( myCellIds.empty() && myCellTypes.empty() ))
     return theOutId;
 
   if ( theOutId<0 || theOutId >= (int)myOut2InId.size() )
@@ -91,6 +91,188 @@ vtkIdType VTKViewer_ExtractUnstructuredGrid::GetInputId(int theOutId) const
   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);
@@ -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,10 +489,10 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
           }
         }
       }
-      if((aNbElems = aConnectivity->GetNumberOfCells())){
+      if ( vtkIdType newNbElems = aConnectivity->GetNumberOfCells() ) {
         VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
         aCellLocationsArray->SetNumberOfComponents(1);
-        aCellLocationsArray->SetNumberOfTuples(aNbElems);
+        aCellLocationsArray->SetNumberOfTuples(newNbElems);
         aConnectivity->InitTraversal();
         for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
           aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
@@ -325,11 +511,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 +526,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,10 +590,10 @@ int VTKViewer_ExtractUnstructuredGrid::RequestData(vtkInformation *vtkNotUsed(re
         }
       }
     }
-    if((aNbElems = aConnectivity->GetNumberOfCells())){
+    if (vtkIdType newNbElems = aConnectivity->GetNumberOfCells() ) {
       VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
       aCellLocationsArray->SetNumberOfComponents(1);
-      aCellLocationsArray->SetNumberOfTuples(aNbElems);
+      aCellLocationsArray->SetNumberOfTuples( newNbElems );
       aConnectivity->InitTraversal();
       for(vtkIdType i = 0, *pts, npts; aConnectivity->GetNextCell(npts,pts); i++){
         aCellLocationsArray->SetValue(i,aConnectivity->GetTraversalLocation(npts));
@@ -421,6 +610,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){
index 9470ddcd43aa0d8bcedf658dbd183500cd823add..897de6f556600abf212e96e8aeff2bed42fee6a4 100755 (executable)
@@ -92,13 +92,16 @@ public:
   //! Get \a myStoreMapping
   int GetStoreMapping(){ return myStoreMapping; }
 
-  //! Gets the input id by output id.
+  //! Computes a map out IDs to in IDs. Call it before GetInputId()!!!
+  void BuildOut2InMap();
+  //! Gets the input id by output id. Call BuildOut2InMap() before
   vtkIdType GetInputId(int theOutId) const;
   //! Gets the output id by input id.
   //vtkIdType GetOutputId(int theInId) const;
 
-  typedef std::vector<vtkIdType> TVectorId;
+  typedef std::vector<vtkIdType>        TVectorId;
   typedef std::map<vtkIdType,vtkIdType> TMapId;
+  typedef std::set<vtkIdType>           TSetId;
 
 protected:
   VTKViewer_ExtractUnstructuredGrid();
@@ -109,11 +112,10 @@ protected:
   EExtraction myExtractionMode;
   
   EChanging myChangeMode;
-  typedef std::set<vtkIdType> TSetId;
   TSetId myCellIds;
   TSetId myCellTypes;
 
-  bool myStoreMapping;
+  bool myStoreMapping, myPassAll;
   TVectorId myOut2InId;
   //TMapId myIn2OutId;