]> Creatis software - creaVtk.git/commitdiff
#3507 Undo and Redo Meshes
authorPablo Garzon <gapablo2001@gmail.com>
Fri, 2 Jun 2023 12:35:35 +0000 (14:35 +0200)
committerPablo Garzon <gapablo2001@gmail.com>
Fri, 2 Jun 2023 12:35:35 +0000 (14:35 +0200)
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx
bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.h
lib/creaVtk/MeshManagerModel.cpp
lib/creaVtk/MeshManagerModel.h

index 9e641450ae7fd32c53140558fa4b6d4bf01e0e7c..e8ed04592d092f14086788fa2cfbde74421bf682 100644 (file)
@@ -20,6 +20,8 @@ void MeshManagerModel_Box::RefreshOutputs(bool signalBox)
     mBox->bbSetOutputMeshBase( GetMeshBase() );
     mBox->bbSetOutputMeshTemp( GetMeshTemp() );
     mBox->bbSetOutputMeshNames( GetMeshNames() );
+    mBox->bbSetOutputAllPolyDatas( GetAllPolyDatas() );
+    mBox->bbSetOutputMeshSelection( GetCurrentMesh() );
     if(signalBox==true){
                mBox->bbSignalOutputModification();
     }
index 3b544e3b2a4f853991c5a58717aa68ebf270c5c0..02dc68943c26769fea3333cc9fb3a286f1d4be43 100644 (file)
@@ -29,6 +29,8 @@ class bbcreaVtk_EXPORT MeshManager
   BBTK_DECLARE_OUTPUT(MeshTemp,vtkPolyData*);
   BBTK_DECLARE_OUTPUT(MeshNames,std::vector<std::string>);
   BBTK_DECLARE_OUTPUT(MeshManagerModel,MeshManagerModel*);
+  BBTK_DECLARE_OUTPUT(AllPolyDatas, std::vector<vtkPolyData*>);
+  BBTK_DECLARE_OUTPUT(MeshSelection, int);
   BBTK_PROCESS(Process);
   void Process();
     
@@ -63,6 +65,8 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(MeshManager,bbtk::AtomicBlackBox);
   BBTK_OUTPUT(MeshManager,MeshTemp,"Mesh Temp",vtkPolyData*,"");
   BBTK_OUTPUT(MeshManager,MeshNames,"Mesh Names",std::vector<std::string>,"");
   BBTK_OUTPUT(MeshManager,MeshManagerModel,"Mesh manager model",MeshManagerModel*,"");
+  BBTK_OUTPUT(MeshManager,AllPolyDatas,"PolyData vector (all mesh bases)",std::vector<vtkPolyData*>,"");
+  BBTK_OUTPUT(MeshManager,MeshSelection,"Current Mesh", int,"");
 
 BBTK_END_DESCRIBE_BLACK_BOX(MeshManager);
 //===== 
index 546ba7470e3cb129335ecac2011f162cc663e125..cf99baf507ec3ec1682307e9b77f521f25c262c8 100644 (file)
@@ -51,17 +51,6 @@ void MeshManager_tool::Process()
                 } // if points!=NULL
             } // Mesh!=NULL
         } // if Tool 30 Set
-        
-        if (bbGetInputTool()==31)  // Set/Update PolyData Reference
-        {
-            if (bbGetInputMesh()!=NULL){
-                vtkPoints   *points = bbGetInputMesh()->GetPoints();
-                if (points!=NULL)
-                {
-                    bbGetInputMeshManagerModel()->UpdateMeshReference( bbGetInputMesh() );
-                } // if points!=NULL
-            } // Mesh!=NULL
-        } // if Tool 30 Set
 
         if (bbGetInputTool()==35)  // Set memory mode
         {
index d5723c0d22cb0773230265d15c6a5d25af7af9cb..76ba719e2999fcfa98dac4f41064d16033f40883 100644 (file)
@@ -40,7 +40,7 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(MeshManager_tool,bbtk::AtomicBlackBox);
   BBTK_DESCRIPTION("No Description.");
   BBTK_CATEGORY("empty");
 
-  BBTK_INPUT(MeshManager_tool,Tool,"(default 0) 0:Nothing  10:Undo  20:ReDo  30:Set  31:Update polydata ref  32:Copy and Set  35:Set memory mode  40:ResetMeshTemp     50: Set Array of meshes  60:Select mesh by name  70:Delete current mesh",int,"");
+  BBTK_INPUT(MeshManager_tool,Tool,"(default 0) 0:Nothing  10:Undo  20:ReDo  30:Set  32:Copy and Set  35:Set memory mode  40:ResetMeshTemp     50: Set Array of meshes  60:Select mesh by name  70:Delete current mesh",int,"");
   BBTK_INPUT(MeshManager_tool,Mesh,"Mesh",vtkPolyData*,"");
   BBTK_INPUT(MeshManager_tool,Meshes,"Input meshes, required for tool 50",std::vector<vtkPolyData*>,"");
   BBTK_INPUT(MeshManager_tool,StringParam,"String input",std::string,"");
index c03e9e8b2268567199c8b62237d72bd8fb469b35..6af6e932b42bbd438df149c21b3ff977ebb7a03b 100644 (file)
@@ -27,6 +27,9 @@
 
 #include "MeshManagerModel.h"
 
+//
+//HISTORY HANDLER
+//
 template <class T>
 HistoryHandler<T>::HistoryHandler(int maxElements)
 {
@@ -52,27 +55,31 @@ void HistoryHandler<T>::CleanHistory()
 }
 
 template <class T>
-T* HistoryHandler<T>::Undo()
+T* HistoryHandler<T>::Undo(T* state)
 {
-       if(!undoStack.empty() && undoStack.size()>1)
+       if(!undoStack.empty())
        {
                auto lastElem = undoStack.back();
                undoStack.pop_back();
-               redoStack.push_back(lastElem);
-               return undoStack.back();
+               redoStack.push_back(state);
+               return lastElem;//undoStack.back();
+       }else{
+               delete state;
        }
        return NULL;
 }
 
 template <class T>
-T* HistoryHandler<T>::Redo()
+T* HistoryHandler<T>::Redo(T* state)
 {
        if(!redoStack.empty())
        {
                auto lastElem = redoStack.back();
                redoStack.pop_back();
-               undoStack.push_back(lastElem);
+               undoStack.push_back(state);
                return lastElem;
+       }else{
+               delete state;
        }
        return NULL;
 }
@@ -91,6 +98,24 @@ void HistoryHandler<T>::Save(T* state)
        }
 }
 
+template <class T>
+T* HistoryHandler<T>::GetPrevious()
+{
+       if(!undoStack.empty()){ 
+               return undoStack.back();
+       }
+       return NULL;
+}
+
+template <class T>
+T* HistoryHandler<T>::GetNext()
+{
+       if(!redoStack.empty()){ 
+               return redoStack.back();
+       }
+       return NULL;
+}
+
 template <class T>
 int HistoryHandler<T>::UndoSize()
 {
@@ -118,7 +143,8 @@ MeshModel::MeshModel(int id)
 MeshModel::MeshModel(vtkPolyData* mesh, int id)
 {
        if(mesh != NULL){       
-               _meshBase = mesh;
+               _meshBase = vtkPolyData::New();//mesh;
+               _meshBase->DeepCopy(mesh);
                _meshTemp = vtkPolyData::New();
                _meshTemp->DeepCopy(_meshBase);
                _meshId = id;
@@ -175,24 +201,24 @@ void MeshModel::SetMeshBase(vtkPolyData* mesh)
        if(_meshBase != NULL){
                _meshBase->Delete();
        }
-        _meshBase = mesh;
+        _meshBase = vtkPolyData::New();
+        _meshBase->DeepCopy(mesh);
         ResetMeshTemp_();
     } // if mesh
 }
 
 void MeshModel::SetMeshMemoryMode(vtkPolyData* mesh)
 {
-       //if(_meshBase != NULL)
-       //{
-       //      _meshBase->Delete();
-       //      _meshBase = NULL;
-       //}
-       //if (_meshTemp != NULL)
-    //{
-    //    _meshTemp->Delete();
-    //    _meshTemp = NULL;
-       //}
-       _meshBase = mesh;
+       if(_meshBase != NULL)
+       {
+               _meshBase->Delete();
+               _meshBase = NULL;
+       }
+       if(mesh != NULL)
+       {
+               _meshBase = vtkPolyData::New();
+               _meshBase->DeepCopy(mesh);
+       }
 }
 
 void MeshModel::ResetMeshTemp()
@@ -240,6 +266,7 @@ MeshManagerModel::MeshManagerModel()
        currentMesh = 0;
        meshId = 0;
        history = new HistoryHandler<ManagerState>(20);
+       lastModified = 0;
 }
 
 MeshManagerModel::~MeshManagerModel()
@@ -254,13 +281,7 @@ void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
 void MeshManagerModel::ResetHistory()
 {
        history->CleanHistory();
-       Save();
-}
-
-void MeshManagerModel::UpdateMeshReference(vtkPolyData* mesh)
-{
-       _meshes.at(currentMesh)->SetMeshBase(mesh);
-       RefreshOutputs(true);
+       //Save();
 }
 
 int MeshManagerModel::GetNumberOfMeshes()
@@ -281,8 +302,9 @@ void MeshManagerModel::AddMesh_(vtkPolyData* mesh)
 
 void MeshManagerModel::AddMesh(vtkPolyData* mesh)
 {
-       AddMesh_(mesh);
        Save();
+       AddMesh_(mesh);
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -302,8 +324,9 @@ void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList)
 
 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList)
 {
-       AddMeshes_(meshList);
        Save();
+       AddMeshes_(meshList);
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -315,8 +338,9 @@ void MeshManagerModel::AddEmptyMesh_()
 
 void MeshManagerModel::AddEmptyMesh()
 {
-       AddEmptyMesh_();
        Save();
+       AddEmptyMesh_();
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -338,8 +362,9 @@ void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList
 
 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
 {
-       InsertMeshesAtCurrent_(meshList);
        Save();
+       InsertMeshesAtCurrent_(meshList);
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -383,8 +408,9 @@ void MeshManagerModel::DeleteMesh_(int position)
 
 void MeshManagerModel::DeleteMesh(int position)
 {
-       DeleteMesh_(position);
        Save();
+       DeleteMesh_(position);
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -405,20 +431,22 @@ void MeshManagerModel::DeleteMeshByName(std::string meshName)
 void MeshManagerModel::DeleteCurrentMesh()
 {
        if(!_meshes.empty()){
-               DeleteMesh_(currentMesh);
                Save();
+               DeleteMesh_(currentMesh);
+               lastModified = currentMesh;
                RefreshOutputs(true);
        }
 }
 
 void MeshManagerModel::ReplaceMesh(std::vector<vtkPolyData*> meshList)
 {
+       Save();
        if(GetNumberOfMeshes() >= 1)
        {
                DeleteMesh_(currentMesh);
        }
        InsertMeshesAtCurrent_(meshList);
-       Save();
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -434,8 +462,9 @@ void MeshManagerModel::DeleteAll_()
 
 void MeshManagerModel::DeleteAll()
 {
-       DeleteAll_();
        Save();
+       DeleteAll_();
+       lastModified = currentMesh;
        RefreshOutputs(true);
 }
 
@@ -485,9 +514,10 @@ vtkPolyData*  MeshManagerModel::GetMeshTemp()
 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
 {
     if(!_meshes.empty()){
+        Save();
         _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
         _meshes.at(currentMesh)->SetMeshBase(mesh);
-        Save();
+        lastModified = currentMesh;
         RefreshOutputs(true);
     }else{
        printf("PG MeshManagerModel::SetMeshBase Mesh vector is empty \n");
@@ -523,9 +553,10 @@ void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
 {
        if(!_meshes.empty())
        {
+               Save();
                _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
                _meshes.at(currentMesh)->CopySetMeshBase(mesh);
-               Save();
+               lastModified = currentMesh;
                RefreshOutputs(true);
        }
        else{
@@ -542,21 +573,41 @@ std::vector<std::string> MeshManagerModel::GetMeshNames()
        return names;
 }
 
+std::vector<vtkPolyData*> MeshManagerModel::GetAllPolyDatas()
+{
+       std::vector<vtkPolyData*> polydatas;
+       for(int i = 0; i < _meshes.size(); i++){
+               polydatas.push_back(_meshes.at(i)->GetMeshBase());
+       }
+       return polydatas;
+}
+
+int MeshManagerModel::GetCurrentMesh()
+{
+       return currentMesh;
+}
+
 void MeshManagerModel::Undo()
 {      
-       RestoreState(history->Undo());
-       RefreshOutputs(true);
+       if(history->UndoSize() > 0){    
+               RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
+               RefreshOutputs(true);
+       }
 }
 
 void MeshManagerModel::Redo()
 {
-       RestoreState(history->Redo());
-       RefreshOutputs(true);
+       if(history->RedoSize() > 0)
+       {       
+               RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
+               RefreshOutputs(true);
+       }
 }
 
 void MeshManagerModel::Save()
 {
        history->Save(new ManagerState(_meshes, meshId, currentMesh));
+       //lastModified = currentMesh;
 }
 
 void MeshManagerModel::RestoreState(ManagerState* state)
@@ -565,18 +616,20 @@ void MeshManagerModel::RestoreState(ManagerState* state)
        {       
                _meshes = state->GetMeshes();
                meshId = state->GetMeshId();
-               currentMesh = state->GetCurrentMesh();
+               currentMesh = state->GetModifiedPos();
+               lastModified = state->GetModifiedPos();
+//             cout << "PG MeshManagerModel::RestoreState currentMesh: " << currentMesh << endl;
        }
 }
 
 //
 //Manager State
 //
-MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int currentMesh)
+MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos)
 {
        savedMeshes = meshesToSave;
        savedId = meshId;
-       savedCurrentMesh = currentMesh;
+       savedModifiedPos = modifiedPos;
 }
 
 MeshManagerModel::ManagerState::~ManagerState()
@@ -592,8 +645,8 @@ int MeshManagerModel::ManagerState::GetMeshId()
 {
        return savedId;
 }
-int MeshManagerModel::ManagerState::GetCurrentMesh()
+int MeshManagerModel::ManagerState::GetModifiedPos()
 {
-       return savedCurrentMesh;
+       return savedModifiedPos;
 }
 
index d8e6a780a52814ed6cd5a22087b093262f364f7c..f981c8741e9ad7b2376387b8e210a35dcfed52be 100644 (file)
@@ -34,9 +34,11 @@ public:
        HistoryHandler(int maxElements);
        ~HistoryHandler();
        void            CleanHistory();
-       StateType*      Undo();
-       StateType*      Redo();
+       StateType*      Undo(StateType* State);
+       StateType*      Redo(StateType* State);
        void            Save(StateType* State);
+       StateType*      GetPrevious();
+       StateType*      GetNext();
        int             UndoSize();
        int             RedoSize();
        
@@ -96,7 +98,6 @@ public :
        void ResetHistory();
        void ResetAll();
        
-       void UpdateMeshReference(vtkPolyData* mesh);
     void SetMeshBase(vtkPolyData* mesh);
     void SetMeshMemoryMode(vtkPolyData* mesh);
     void CopySetMeshBase(vtkPolyData* mesh);
@@ -145,6 +146,8 @@ public :
     void Redo();
     
     std::vector<std::string> GetMeshNames();
+    std::vector<vtkPolyData*> GetAllPolyDatas();
+    int  GetCurrentMesh();
 
 //--Method template----------------------------
 //  void FunctionName(int& parameterA);
@@ -163,17 +166,18 @@ private:
     std::vector<std::shared_ptr<MeshModel>> _meshes;
     int                                                                        currentMesh;
     int                                                                                meshId;
+    int                                                                                lastModified;
     
     class ManagerState{
        public:
-               ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int currentMesh);
+               ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedId);
                ~ManagerState();
                std::vector<std::shared_ptr<MeshModel>>& GetMeshes();
                int GetMeshId();
-               int GetCurrentMesh();
+               int GetModifiedPos();
        private:
                std::vector<std::shared_ptr<MeshModel>> savedMeshes;
-               int savedCurrentMesh;
+               int savedModifiedPos;
                int savedId;
     };