From 67969bae8f7abbc05af9e0998fc0455d139775be Mon Sep 17 00:00:00 2001 From: eap Date: Fri, 11 Nov 2016 14:38:11 +0300 Subject: [PATCH] 23315: [CEA 1929] Too much memory used to display a mesh in shading and wireframe Avoid filling myOut2InId as much as possible --- .../VTKViewer_ExtractUnstructuredGrid.cxx | 243 ++++++++++++++++-- .../VTKViewer_ExtractUnstructuredGrid.h | 10 +- 2 files changed, 225 insertions(+), 28 deletions(-) diff --git a/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.cxx b/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.cxx index bd5a69f9e..bd0cdd654 100755 --- a/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.cxx +++ b/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.cxx @@ -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 = "<GetNumberOfCells() = "<GetNumberOfCells()); if(myStoreMapping){ diff --git a/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.h b/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.h index 9470ddcd4..897de6f55 100755 --- a/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.h +++ b/src/VTKViewer/VTKViewer_ExtractUnstructuredGrid.h @@ -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 TVectorId; + typedef std::vector TVectorId; typedef std::map TMapId; + typedef std::set TSetId; protected: VTKViewer_ExtractUnstructuredGrid(); @@ -109,11 +112,10 @@ protected: EExtraction myExtractionMode; EChanging myChangeMode; - typedef std::set TSetId; TSetId myCellIds; TSetId myCellTypes; - bool myStoreMapping; + bool myStoreMapping, myPassAll; TVectorId myOut2InId; //TMapId myIn2OutId; -- 2.39.2