]> Creatis software - creaVtk.git/commitdiff
#3507 Undo and Redo Meshes
authorPablo Garzon <gapablo2001@gmail.com>
Tue, 18 Jul 2023 11:28:17 +0000 (13:28 +0200)
committerPablo Garzon <gapablo2001@gmail.com>
Tue, 18 Jul 2023 11:28:17 +0000 (13:28 +0200)
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx
lib/creaVtk/MeshManagerModel.cpp
lib/creaVtk/MeshManagerModel.h

index e8ed04592d092f14086788fa2cfbde74421bf682..b4190a284d048122ae203b224108aa91cfad33ff 100644 (file)
@@ -53,11 +53,19 @@ void MeshManager::Process()
     {
         meshManagerModel_Box = new MeshManagerModel_Box(this);
 
-        meshManagerModel_Box->AddMeshes_( bbGetInputMeshVector() );
-
-               meshManagerModel_Box->AddMesh_( bbGetInputMesh() );
+               if(bbGetInputMemoryMode() == false){
+                       meshManagerModel_Box->MeshMemoryModeOff();
+                       meshManagerModel_Box->AddMeshes_( bbGetInputMeshVector() );
+                       meshManagerModel_Box->AddMesh_( bbGetInputMesh() );
+                       meshManagerModel_Box->ResetHistory();
+               }
+               else{
+                       meshManagerModel_Box->MeshMemoryModeOn();
+                       meshManagerModel_Box->SetHistory(50);
+                       meshManagerModel_Box->AddMesh_( bbGetInputMesh() );
+                       meshManagerModel_Box->SaveMemoryMode();
+               }
                
-               meshManagerModel_Box->ResetHistory();
         //meshManagerModel_Box->SetMeshBase( bbGetInputMesh() );
         meshManagerModel_Box->RefreshOutputs(false);
         bbSetOutputMeshManagerModel( meshManagerModel_Box );
index 02dc68943c26769fea3333cc9fb3a286f1d4be43..39d1566a3f8fd98d6ad8dd0a4cfc56bf35d62175 100644 (file)
@@ -25,6 +25,7 @@ class bbcreaVtk_EXPORT MeshManager
 //===== 
   BBTK_DECLARE_INPUT(Mesh,vtkPolyData*);
   BBTK_DECLARE_INPUT(MeshVector, std::vector<vtkPolyData*>);
+  BBTK_DECLARE_INPUT(MemoryMode, bool);
   BBTK_DECLARE_OUTPUT(MeshBase,vtkPolyData*);
   BBTK_DECLARE_OUTPUT(MeshTemp,vtkPolyData*);
   BBTK_DECLARE_OUTPUT(MeshNames,std::vector<std::string>);
@@ -61,6 +62,7 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(MeshManager,bbtk::AtomicBlackBox);
 
   BBTK_INPUT(MeshManager,Mesh,"Mesh",vtkPolyData*,"");
   BBTK_INPUT(MeshManager,MeshVector,"Vector of Meshes",std::vector<vtkPolyData*>,"");
+  BBTK_INPUT(MeshManager,MemoryMode,"Memory Mode, default false", bool,"");
   BBTK_OUTPUT(MeshManager,MeshBase,"Mesh Base",vtkPolyData*,"");
   BBTK_OUTPUT(MeshManager,MeshTemp,"Mesh Temp",vtkPolyData*,"");
   BBTK_OUTPUT(MeshManager,MeshNames,"Mesh Names",std::vector<std::string>,"");
index cf99baf507ec3ec1682307e9b77f521f25c262c8..1be40ffb74bc0dbe960e7b41d1d6f712894d8d4e 100644 (file)
@@ -56,7 +56,14 @@ void MeshManager_tool::Process()
         {
                 bbGetInputMeshManagerModel()->SetMeshMemoryMode( bbGetInputMesh() );
         } // if Tool 35 Set memory mode
-
+               if(bbGetInputTool() == 37) // Save State Memory Mode
+        {
+               if(bbGetInputStringParam().length() > 0){       
+                               cout << " MeshManager_tool : MeshManagerModel    Executed tool 37" << endl;//bGetInputMeshManagerModel()->Save();
+                       bbGetInputMeshManagerModel()->SaveMemoryMode();
+                   //  cout << bbGetInputStringParam() << endl;
+               }
+        }
         if (bbGetInputTool()==32)  // Copy and Set
         {
                 bbGetInputMeshManagerModel()->CopySetMeshBase( bbGetInputMesh() );
index 41b97706d227b76b8a5c27aec1e8f406befcdcd4..ddf25583551f299a25eeff842c0151f02dacce49 100644 (file)
@@ -48,21 +48,27 @@ void HistoryHandler<T>::CleanHistory()
        {
                delete element;
        }
+       redoStack.clear();
        for (T* element : undoStack)
        {
                delete element;
        }
+       undoStack.clear();
 }
 
 template <class T>
 T* HistoryHandler<T>::Undo(T* state)
 {
+       if(state == NULL)
+       {
+               return NULL;
+       }
        if(!undoStack.empty())
        {
                auto lastElem = undoStack.back();
                undoStack.pop_back();
                redoStack.push_back(state);
-               return lastElem;//undoStack.back();
+               return lastElem;
        }else{
                delete state;
        }
@@ -72,6 +78,10 @@ T* HistoryHandler<T>::Undo(T* state)
 template <class T>
 T* HistoryHandler<T>::Redo(T* state)
 {
+       if(state == NULL)
+       {
+               return NULL;
+       }
        if(!redoStack.empty())
        {
                auto lastElem = redoStack.back();
@@ -80,8 +90,46 @@ T* HistoryHandler<T>::Redo(T* state)
                return lastElem;
        }else{
                delete state;
+               return NULL;
+       }
+}
+
+/**
+* To be used with RedoKeepCurrent
+* Will always maintain the current state in the undo stack as the first element
+* Useful when states are saved after actions and the initial state is saved.
+*/
+template <class T>
+T* HistoryHandler<T>::UndoKeepCurrent()
+{
+       if(undoStack.size() > 1)
+       {
+               auto lastElem = undoStack.back();
+               undoStack.pop_back();
+               redoStack.push_back(lastElem);
+               return undoStack.back();
+       }else{
+               return NULL;
+       }
+}
+
+/**
+* To be used with UndoKeepCurrent
+* Will always maintain the current state in the undo stack as the first element
+* Useful when states are saved after actions and the initial state is saved.
+*/
+template <class T>
+T* HistoryHandler<T>::RedoKeepCurrent()
+{
+       if(!redoStack.empty())
+       {
+               auto lastElem = redoStack.back();
+               redoStack.pop_back();
+               undoStack.push_back(lastElem);
+               return undoStack.back();
+       }else{
+               return NULL;
        }
-       return NULL;
 }
 
 template <class T>
@@ -90,8 +138,9 @@ void HistoryHandler<T>::Save(T* state)
        undoStack.push_back(state);
        if(undoStack.size() > maxElements)
        {
-               delete undoStack.front();
+               T* frontDel = undoStack.front();
                undoStack.pop_front();
+               delete frontDel;
        }
        if(!redoStack.empty())
        {
@@ -279,21 +328,32 @@ MeshManagerModel::MeshManagerModel()
        meshId = 0;
        history = new HistoryHandler<ManagerState>(15);
        lastModified = 0;
+       memoryMode = false;
 }
 
 MeshManagerModel::~MeshManagerModel()
 {
        DeleteAll();
+       history->CleanHistory();
+       delete history;
 }
 
 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
 {
 }
 
+void MeshManagerModel::SetHistory(int maxCapacity)
+{
+       if(history != NULL){    
+               ResetHistory();
+               delete history;
+       }
+       history = new HistoryHandler<ManagerState>(maxCapacity);
+}
+
 void MeshManagerModel::ResetHistory()
 {
        history->CleanHistory();
-       //Save();
 }
 
 int MeshManagerModel::GetNumberOfMeshes()
@@ -550,6 +610,16 @@ void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
     }
 }
 
+void MeshManagerModel::MeshMemoryModeOn()
+{
+       memoryMode = true;
+}
+
+void MeshManagerModel::MeshMemoryModeOff()
+{
+       memoryMode = false;
+}
+
 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
 {
        if(_meshes.size() > 1)
@@ -561,6 +631,8 @@ void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
                AddEmptyMesh_();
        }
        _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
+       ResetHistory();
+       SaveMemoryMode();
        RefreshOutputs(true);
 }
 
@@ -616,25 +688,47 @@ int MeshManagerModel::GetCurrentMesh()
 void MeshManagerModel::Undo()
 {      
        if(history->UndoSize() > 0)
-       {       
-               RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
-               RefreshOutputs(true);
+       {
+               if(memoryMode == false){
+                       RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
+                       RefreshOutputs(true);
+               }
+               else if(history->UndoSize() > 1){
+                       RestoreStateMemoryMode(history->UndoKeepCurrent());
+               }
        }
 }
 
 void MeshManagerModel::Redo()
 {
        if(history->RedoSize() > 0)
-       {       
-               RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
-               RefreshOutputs(true);
+       {
+               if(memoryMode == false){
+                       RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
+                       RefreshOutputs(true);
+               }
+               else{
+                       RestoreStateMemoryMode(history->RedoKeepCurrent());
+               }
        }
 }
 
 void MeshManagerModel::Save()
 {
        history->Save(new ManagerState(_meshes, meshId, currentMesh));
-       //lastModified = currentMesh;
+}
+
+void MeshManagerModel::SaveMemoryMode()
+{
+       if(_meshes.size() == 1 && memoryMode)
+       {
+               std::vector<std::shared_ptr<MeshModel>> savedMesh;
+               savedMesh.push_back(std::make_shared<MeshModel>(_meshes.at(0).get()));
+               history->Save(new ManagerState(savedMesh, meshId, 0));
+       }
+       else{
+               printf("PG MeshManagerModel::SaveMemoryMode WARNING Mesh vector has invalid size or memoryMode is not set \n");
+       }
 }
 
 void MeshManagerModel::RestoreState(ManagerState* state)
@@ -645,6 +739,23 @@ void MeshManagerModel::RestoreState(ManagerState* state)
                meshId = state->GetMeshId();
                currentMesh = state->GetModifiedPos();
                lastModified = state->GetModifiedPos();
+               delete state;
+       }
+       else{
+               printf("PG MeshManagerModel::RestoreState WARNING State is NULL \n");
+       }
+}
+
+void MeshManagerModel::RestoreStateMemoryMode(ManagerState* state){
+       if(_meshes.size() == 1 && state != NULL)
+       {
+               vtkPoints* statePoints = vtkPoints::New();
+               statePoints->DeepCopy(state->GetMeshes().at(0)->GetMeshBase()->GetPoints());
+               _meshes.at(0)->GetMeshBase()->SetPoints(statePoints);
+               _meshes.at(0)->GetMeshBase()->GetPoints()->Modified();
+        _meshes.at(0)->GetMeshBase()->Modified();
+       }else{
+               printf("PG MeshManagerModel::RestoreStateMemoryMode WARNING Mesh vector has invalid size or state is NULL\n");
        }
 }
 
index f981c8741e9ad7b2376387b8e210a35dcfed52be..b74204c20a3010df654b1c07fbcda1ec97e6c5b2 100644 (file)
@@ -35,7 +35,9 @@ public:
        ~HistoryHandler();
        void            CleanHistory();
        StateType*      Undo(StateType* State);
+       StateType*      UndoKeepCurrent();
        StateType*      Redo(StateType* State);
+       StateType*      RedoKeepCurrent();
        void            Save(StateType* State);
        StateType*      GetPrevious();
        StateType*      GetNext();
@@ -95,11 +97,14 @@ public :
   MeshManagerModel(std::vector<vtkPolyData*> meshList);
   ~MeshManagerModel();
 
+       void SetHistory(int maxCapacity);
        void ResetHistory();
        void ResetAll();
        
     void SetMeshBase(vtkPolyData* mesh);
     void SetMeshMemoryMode(vtkPolyData* mesh);
+       void MeshMemoryModeOn();
+    void MeshMemoryModeOff();
     void CopySetMeshBase(vtkPolyData* mesh);
     void ResetMeshTemp();
     //void ResetMeshTemp_();
@@ -142,6 +147,7 @@ public :
     void NextMesh();
     void PreviousMesh();
     
+    void SaveMemoryMode();
     void Undo();
     void Redo();
     
@@ -167,6 +173,7 @@ private:
     int                                                                        currentMesh;
     int                                                                                meshId;
     int                                                                                lastModified;
+    bool                                                                       memoryMode;
     
     class ManagerState{
        public:
@@ -183,6 +190,7 @@ private:
     
     void                                                       Save();
     void                                                       RestoreState(ManagerState* state);
+    void                                                       RestoreStateMemoryMode(ManagerState* state);
     
     HistoryHandler<ManagerState>       *history;