#include "VVTK_ViewWindow.h"
#include "SUIT_ResourceMgr.h"
+#include <vtkRenderWindow.h>
+
#ifdef _DEBUG_
static int MYDEBUG = 0;
#else
static int MYDEBUG = 0;
#endif
+namespace
+{
+ //----------------------------------------------------------------------------
+ inline
+ bool
+ IsSameField(const VISU::ColoredPrs3dHolder::BasicInput& theReferenceInput,
+ const VISU::ColoredPrs3dHolder::BasicInput& thePrs3dInput)
+ {
+ return thePrs3dInput.myResult->_is_equivalent(theReferenceInput.myResult) &&
+ thePrs3dInput.myEntity == theReferenceInput.myEntity &&
+ !strcmp(thePrs3dInput.myFieldName.in(), theReferenceInput.myFieldName.in());
+ }
+
+
+ //----------------------------------------------------------------------------
+ inline
+ bool
+ IsSameTimeStamp(const VISU::ColoredPrs3dHolder::BasicInput& theReferenceInput,
+ const VISU::ColoredPrs3dHolder::BasicInput& thePrs3dInput)
+ {
+ return IsSameField(theReferenceInput, thePrs3dInput) &&
+ thePrs3dInput.myTimeStampNumber == theReferenceInput.myTimeStampNumber;
+ }
+
+
+ //----------------------------------------------------------------------------
+ VISU::TPrs3dPtr
+ FindSameFieldPrs(const VISU::TColoredPrs3dHolderMap& theHolderMap,
+ const VISU::ColoredPrs3dHolder::BasicInput& theInput,
+ VISU::VISUType theType)
+ {
+ VISU::TColoredPrs3dHolderMap::const_iterator aHolderIter = theHolderMap.begin();
+ VISU::TColoredPrs3dHolderMap::const_iterator aHolderIterEnd = theHolderMap.end();
+ for(; aHolderIter != aHolderIterEnd; aHolderIter++){
+ const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second;
+ VISU::TLastVisitedPrsList::const_iterator aPrsIter = aPrsList.begin();
+ VISU::TLastVisitedPrsList::const_iterator aPrsIterEnd = aPrsList.end();
+ for(; aPrsIter != aPrsIterEnd; aPrsIter++){
+ VISU::TPrs3dPtr aPrs3d = *aPrsIter;
+ if(aPrs3d->GetType() != theType)
+ break;
+ VISU::ColoredPrs3dHolder::BasicInput_var anInput = aPrs3d->GetBasicInput();
+ if(IsSameField(theInput, anInput))
+ return aPrs3d;
+ }
+ }
+ return NULL;
+ }
+
+
+ //----------------------------------------------------------------------------
+ CORBA::Float
+ EstimateMemorySize(const VISU::TColoredPrs3dHolderMap& theHolderMap,
+ const VISU::ColoredPrs3dHolder::BasicInput& theInput,
+ VISU::VISUType theType,
+ const size_t theRawEstimatedMemorySize)
+ {
+ VISU::TPrs3dPtr aPrs3d = FindSameFieldPrs(theHolderMap, theInput, theType);
+ if(aPrs3d.GetPointer())
+ return aPrs3d->GetMemorySize();
+ return CORBA::Float(theRawEstimatedMemorySize/(1024.0*1024.0)); // convert to Mb
+ }
+
+
+ //----------------------------------------------------------------------------
+ bool
+ SelectPrs3dToBeDeleted(CORBA::Float theRequiredMemory,
+ const std::string& theActiveHolderEntry,
+ const VISU::TColoredPrs3dHolderMap& theHolderMap,
+ VISU::TColoredPrs3dHolderMap& theColoredPrs3dHolderMap)
+ {
+ if( theRequiredMemory < 1.0 / VTK_LARGE_FLOAT )
+ return false;
+
+ VISU::TColoredPrs3dHolderMap::const_iterator aHolderIter = theHolderMap.begin();
+ VISU::TColoredPrs3dHolderMap::const_iterator aHolderIterEnd = theHolderMap.end();
+
+ // To calculate the maximum length of the cache history among all holders
+ int anIteration = 0;
+ for( ; aHolderIter != aHolderIterEnd; aHolderIter++ ){
+ if( aHolderIter->first == theActiveHolderEntry )
+ continue;
+ const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second;
+ anIteration = QMAX( aPrsList.size() - 1, anIteration );
+ }
+
+ // To estimate what amount of memory can be obtained
+ // by cleaning of non-active holder's presentation
+ CORBA::Float aGatheredMemory = 0.0;
+ for(; anIteration > 0; anIteration--){ // To take into account only non-devices
+ aHolderIter = theHolderMap.begin();
+ for(; aHolderIter != aHolderIterEnd; aHolderIter++ ){
+ const std::string& aHolderEntry = aHolderIter->first;
+ if( aHolderEntry == theActiveHolderEntry )
+ continue;
+ const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second;
+ if( anIteration < aPrsList.size() ){
+ VISU::TPrs3dPtr aPrs3d = aPrsList[anIteration];
+ aGatheredMemory += aPrs3d->GetMemorySize();
+ theColoredPrs3dHolderMap[aHolderEntry].push_back(aPrs3d);
+ if( aGatheredMemory > theRequiredMemory )
+ return true;
+ }
+ }
+ }
+
+ // To estimate what amount of memory can be obtained
+ // by cleaning of active holder's presentation
+ if( theActiveHolderEntry != "" ){
+ aHolderIter = theHolderMap.find( theActiveHolderEntry );
+ const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second;
+ VISU::TLastVisitedPrsList::const_reverse_iterator aPrsIter = aPrsList.rbegin();
+ VISU::TLastVisitedPrsList::const_reverse_iterator aPrsIterEnd = aPrsList.rend();
+ for(; aPrsIter != aPrsIterEnd; aPrsIter++){
+ VISU::TPrs3dPtr aPrs3d = *aPrsIter;
+ // To avoid of device destruction
+ if(aPrs3d.GetPointer() == aPrsList.front().GetPointer())
+ return false;
+
+ CORBA::Float aMemory = aPrs3d->GetMemorySize();
+
+ // To prefere "move" action instead of destroy / create
+ if(aPrsIter == aPrsList.rbegin())
+ if(aMemory >= theRequiredMemory)
+ return false;
+
+ aGatheredMemory += aMemory;
+ theColoredPrs3dHolderMap[theActiveHolderEntry].push_back(aPrs3d);
+ if( aGatheredMemory > theRequiredMemory )
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+
+ //----------------------------------------------------------------------------
+ void
+ ErasePrs3d(VISU::TLastVisitedPrsList& thePrsList,
+ const VISU::TPrs3dPtr& thePrs3d)
+ {
+ VISU::TLastVisitedPrsList::iterator anIter = thePrsList.begin();
+ VISU::TLastVisitedPrsList::iterator aEndIter = thePrsList.end();
+ for(; anIter != aEndIter; anIter++){
+ VISU::TPrs3dPtr aPrs3d = *anIter;
+ if(aPrs3d.GetPointer() == thePrs3d.GetPointer())
+ thePrsList.erase(anIter);
+ }
+ }
+}
+
//----------------------------------------------------------------------------
VISU::ColoredPrs3dCache_i
VISU::ColoredPrs3dCache_i
::IsPossible(VISU::VISUType theType,
const VISU::ColoredPrs3dHolder::BasicInput& theInput,
- float& theMemoryToClear)
+ CORBA::Float& theRequiredMemory,
+ const std::string theHolderEntry)
{
- //if(MYDEBUG) cout << "VISU::ColoredPrs3dCache_i::IsPossible" << endl;
- size_t aMemory = VISU::CheckIsPossible(theType, theInput, true);
-
- if(aMemory > 0){
+ size_t aRawEstimatedMemorySize = VISU::CheckIsPossible(theType, theInput, true);
+ if(aRawEstimatedMemorySize > 0){
if(GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED){
CORBA::Float aMemoryUsed = GetMemorySize();
CORBA::Float aMemoryLimit = GetLimitedMemory();
- CORBA::Float aMemoryNeeded = CORBA::Float(aMemory/(1024.0*1024.0)); // convert to Mb
- if(MYDEBUG) cout<<"Memory needed: "<<aMemoryNeeded<<"; used: "<<aMemoryUsed<<"; limit: "<<aMemoryLimit<<endl;
+ CORBA::Float aMemoryNeeded = EstimateMemorySize(myHolderMap,
+ theInput,
+ theType,
+ aRawEstimatedMemorySize);
if( aMemoryUsed + aMemoryNeeded < aMemoryLimit )
return true;
-
- theMemoryToClear = aMemoryNeeded;
- bool ok = ClearMemory( theMemoryToClear, NULL, true );
- return ok;
+
+ theRequiredMemory = aMemoryNeeded;
+ TColoredPrs3dHolderMap aColoredPrs3dHolderMap;
+ return SelectPrs3dToBeDeleted(theRequiredMemory,
+ theHolderEntry,
+ myHolderMap,
+ aColoredPrs3dHolderMap);
}
}
- return aMemory > 0;
+ return aRawEstimatedMemorySize > 0;
}
::CreateHolder(VISU::VISUType theType,
const VISU::ColoredPrs3dHolder::BasicInput& theInput)
{
- float aMemoryToClear = 0.;
- if(IsPossible(theType, theInput, aMemoryToClear))
- {
+ CORBA::Float aRequiredMemory = 0.0;
+ if(IsPossible(theType, theInput, aRequiredMemory, "")){
VISU::ColoredPrs3dHolder_i* aHolder = new VISU::ColoredPrs3dHolder_i(theType, theInput, *this);
-
- if( aMemoryToClear > 1 / VTK_LARGE_FLOAT )
- ClearMemory( aMemoryToClear, aHolder );
-
VISU::ColoredPrs3d_i* aColoredPrs3d = CreateColoredPrs3d(theType, theInput);
std::string aComment = std::string("myComment=") + aColoredPrs3d->GetComment();
std::string aName = aColoredPrs3d->GenerateName().latin1();
aHolder->PublishInStudy(aName, aComment);
RegisterInHolder(aColoredPrs3d, aHolder);
+ if( aRequiredMemory > 1.0 / VTK_LARGE_FLOAT )
+ ClearMemory( aRequiredMemory, aHolder->GetEntry() );
return aHolder->_this();
}
return VISU::ColoredPrs3dHolder::_nil();
::FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList,
const VISU::ColoredPrs3dHolder::BasicInput& theInput)
{
- // User reverse iteration to start from last visited items
TLastVisitedPrsList::iterator anIter = theLastVisitedPrsList.begin();
TLastVisitedPrsList::iterator aEndIter = theLastVisitedPrsList.end();
- for(; anIter != aEndIter; anIter++)
- {
+ for(; anIter != aEndIter; anIter++){
TPrs3dPtr aPrs3d = *anIter;
VISU::ColoredPrs3dHolder::BasicInput_var anInput = aPrs3d->GetBasicInput();
- if(anInput->myResult->_is_equivalent(theInput.myResult) &&
- !strcmp(anInput->myMeshName.in(), theInput.myMeshName.in()) &&
- anInput->myEntity == theInput.myEntity &&
- !strcmp(anInput->myFieldName.in(), theInput.myFieldName.in()) &&
- anInput->myTimeStampNumber == theInput.myTimeStampNumber )
- {
+ if(IsSameTimeStamp(theInput, anInput)){
theLastVisitedPrsList.erase(anIter);
return aPrs3d;
}
TLastVisitedPrsList& aLastVisitedPrsList = GetLastVisitedPrsList(theHolder);
TPrs3dPtr aPrs3d = FindPrsByInput(aLastVisitedPrsList, theInput);
bool anIsCheckPossible = GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED;
- float aMemoryToClear = 0.;
+ std::string aHolderEntry = theHolder->GetEntry();
+ VISU::VISUType aPrsType = theHolder->GetPrsType();
+ CORBA::Float aRequiredMemory = 0.0;
if(aPrs3d.GetPointer()){
aLastVisitedPrsList.push_front(aPrs3d);
if(MYDEBUG) cout << "FindPrsByInput " << aPrs3d << endl;
- }else if(anIsCheckPossible && IsPossible(theHolder->GetPrsType(), theInput, aMemoryToClear)){
- if( aMemoryToClear > 1 / VTK_LARGE_FLOAT )
- ClearMemory(aMemoryToClear, theHolder);
- aPrs3d = CreatePrs(theHolder->GetPrsType(), theInput, theHolder);
+ }else if(anIsCheckPossible && IsPossible(aPrsType, theInput, aRequiredMemory, aHolderEntry)){
+ if( aRequiredMemory > 1.0 / VTK_LARGE_FLOAT )
+ ClearMemory(aRequiredMemory, aHolderEntry);
+ aPrs3d = CreatePrs(aPrsType, theInput, theHolder);
if(MYDEBUG) cout << "Created " << aPrs3d << endl;
}else{
aPrs3d = aLastVisitedPrsList.back();
aLastVisitedPrsList.push_front(aPrs3d);
if(MYDEBUG) cout << "Move only " << aPrs3d << endl;
}
- if(MYDEBUG) PrintCache();
+ //if(MYDEBUG) PrintCache();
aPrs3d->SameAs(thePrs);
aPrs3d->UpdateActors();
aNewActor->SetVisibility(true);
aNewActor->DeepCopy(anActor);
aPrs3d->SetActiveState(true);
+ //aLastVisitedPrs3d->RemoveActors();
}
- aViewWindow->Repaint();
+ //aViewWindow->Repaint();
+ aViewWindow->getRenderWindow()->Render();
return true;
}
}
VISU::ColoredPrs3dCache_i
::ClearCache(float theMemory)
{
- //if(MYDEBUG) cout << "VISU::ColoredPrs3dCache_i::ClearCache( " << theMemory << " )" << endl;
-
- float aCurrentMemory = GetMemorySize();
- ClearMemory( aCurrentMemory - theMemory, NULL );
+ CORBA::Float aCurrentMemory = GetMemorySize();
+ ClearMemory( aCurrentMemory - theMemory, "" );
}
//----------------------------------------------------------------------------
bool
VISU::ColoredPrs3dCache_i
-::ClearMemory(float theMemory, VISU::ColoredPrs3dHolder_i* theActiveHolder, bool theOnlyCheck)
+::ClearMemory(CORBA::Float theRequiredMemory,
+ const std::string& theHolderEntry)
{
- if(MYDEBUG)
- {
- cout << "VISU::ColoredPrs3dCache_i::ClearMemory( ";
- cout << (int)theOnlyCheck;
- cout << " , " << theMemory;
- if( theActiveHolder )
- cout << " , " << theActiveHolder->GetEntry().c_str();
- cout << " )" << endl;
- }
-
- if( theMemory < 1 / VTK_LARGE_FLOAT )
- return false;
-
- if(MYDEBUG) PrintCache();
-
- TColoredPrs3dHolderMap::iterator aHolderIter = myHolderMap.begin();
- TColoredPrs3dHolderMap::iterator aHolderIterEnd = myHolderMap.end();
-
- std::string anEntry;
- if( theActiveHolder )//&& myHolderMap.size() > 1 )
- anEntry = theActiveHolder->GetEntry();
-
- int anIteration = 0;
- for( ; aHolderIter != aHolderIterEnd; aHolderIter++ )
- {
- if( aHolderIter->first == anEntry )
- continue;
- TLastVisitedPrsList& aPrsList = aHolderIter->second;
- anIteration = QMAX( aPrsList.size() - 1, anIteration );
- }
- if(MYDEBUG) cout << "Iteration - " << anIteration << endl;
-
- float aMemory, aGatheredMemory = 0.;
- while( anIteration > 0 )
- {
- for( aHolderIter = myHolderMap.begin(); aHolderIter != aHolderIterEnd; aHolderIter++ )
- {
- if( aHolderIter->first == anEntry )
- continue;
-
- TLastVisitedPrsList& aPrsList = aHolderIter->second;
- if( anIteration < aPrsList.size() )
- {
- TPrs3dPtr aPrs3d = aPrsList[anIteration];
- if(MYDEBUG) cout << aPrs3d.GetPointer() << " ";
- if( !aPrs3d.GetPointer() )
- continue;
-
- aMemory = aPrs3d->GetMemorySize();
- aGatheredMemory += aMemory;
- if(MYDEBUG) cout << aMemory << " >> " << aGatheredMemory;
-
- if( !theOnlyCheck )
- aPrsList.pop_back();
-
- if( aGatheredMemory > theMemory )
- {
- if(MYDEBUG) cout << " STOP" << endl;
- if(MYDEBUG) PrintCache();
- return true;
- }
- if(MYDEBUG) cout << endl;
+ CORBA::Float aInitialMemorySize = GetMemorySize();
+ TColoredPrs3dHolderMap aHolder2PrsToBeDeletedMap;
+ SelectPrs3dToBeDeleted(theRequiredMemory, theHolderEntry, myHolderMap, aHolder2PrsToBeDeletedMap);
+ TColoredPrs3dHolderMap::const_iterator aHolderIter = aHolder2PrsToBeDeletedMap.begin();
+ TColoredPrs3dHolderMap::const_iterator anEndHolderIter = aHolder2PrsToBeDeletedMap.end();
+ for(; aHolderIter != anEndHolderIter; aHolderIter++){
+ const std::string aHolderEntry = aHolderIter->first;
+ TColoredPrs3dHolderMap::iterator anHolderMapIter = myHolderMap.find(aHolderEntry);
+ if(anHolderMapIter != myHolderMap.end()){
+ TLastVisitedPrsList& aLastVisitedPrsList = anHolderMapIter->second;
+
+ const TLastVisitedPrsList& aPrsToBeDeletedList = aHolderIter->second;
+ TLastVisitedPrsList::const_iterator anIter = aPrsToBeDeletedList.begin();
+ TLastVisitedPrsList::const_iterator aEndIter = aPrsToBeDeletedList.end();
+ for(; anIter != aEndIter; anIter++){
+ TPrs3dPtr aPrs3d = *anIter;
+ ErasePrs3d(aLastVisitedPrsList, aPrs3d);
}
}
- anIteration--;
}
-
- // clear active holder in a last order
- if( theActiveHolder )
- {
- aHolderIter = myHolderMap.find( anEntry );
-
- TLastVisitedPrsList& aPrsList = aHolderIter->second;
- TLastVisitedPrsList::reverse_iterator aPrsIter = aPrsList.rbegin();
- TLastVisitedPrsList::reverse_iterator aPrsIterEnd = aPrsList.rend();
- for(; aPrsIter != aPrsIterEnd; aPrsIter++)
- {
- TPrs3dPtr aPrs3d = *aPrsIter;
- if(MYDEBUG) cout << aPrs3d.GetPointer() << " ";
- if( !aPrs3d.GetPointer() )
- continue;
-
- aMemory = aPrs3d->GetMemorySize();
- aGatheredMemory += aMemory;
- if(MYDEBUG) cout << aMemory << " >> " << aGatheredMemory;
-
- if( !theOnlyCheck )
- aPrsList.pop_back();
-
- if( aGatheredMemory > theMemory )
- {
- if(MYDEBUG) cout << " STOP" << endl;
- if(MYDEBUG) PrintCache();
- return true;
- }
- if(MYDEBUG) cout << endl;
- }
- }
-
- if(MYDEBUG) PrintCache();
- return false;
+ CORBA::Float aCurrentMemory = GetMemorySize();
+ return (aInitialMemorySize - aCurrentMemory >= theRequiredMemory);
}