From: Pablo Garzon Date: Fri, 2 Jun 2023 12:35:35 +0000 (+0200) Subject: #3507 Undo and Redo Meshes X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=b00e934a843a756fb0d6d5f763642816df0aaff4;p=creaVtk.git #3507 Undo and Redo Meshes --- diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx index 9e64145..e8ed045 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.cxx @@ -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(); } diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h index 3b544e3..02dc689 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager.h @@ -29,6 +29,8 @@ class bbcreaVtk_EXPORT MeshManager BBTK_DECLARE_OUTPUT(MeshTemp,vtkPolyData*); BBTK_DECLARE_OUTPUT(MeshNames,std::vector); BBTK_DECLARE_OUTPUT(MeshManagerModel,MeshManagerModel*); + BBTK_DECLARE_OUTPUT(AllPolyDatas, std::vector); + 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,""); BBTK_OUTPUT(MeshManager,MeshManagerModel,"Mesh manager model",MeshManagerModel*,""); + BBTK_OUTPUT(MeshManager,AllPolyDatas,"PolyData vector (all mesh bases)",std::vector,""); + BBTK_OUTPUT(MeshManager,MeshSelection,"Current Mesh", int,""); BBTK_END_DESCRIBE_BLACK_BOX(MeshManager); //===== diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx index 546ba74..cf99baf 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.cxx @@ -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 { diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.h b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.h index d5723c0..76ba719 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.h +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkMeshManager_tool.h @@ -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,""); BBTK_INPUT(MeshManager_tool,StringParam,"String input",std::string,""); diff --git a/lib/creaVtk/MeshManagerModel.cpp b/lib/creaVtk/MeshManagerModel.cpp index c03e9e8..6af6e93 100644 --- a/lib/creaVtk/MeshManagerModel.cpp +++ b/lib/creaVtk/MeshManagerModel.cpp @@ -27,6 +27,9 @@ #include "MeshManagerModel.h" +// +//HISTORY HANDLER +// template HistoryHandler::HistoryHandler(int maxElements) { @@ -52,27 +55,31 @@ void HistoryHandler::CleanHistory() } template -T* HistoryHandler::Undo() +T* HistoryHandler::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 -T* HistoryHandler::Redo() +T* HistoryHandler::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::Save(T* state) } } +template +T* HistoryHandler::GetPrevious() +{ + if(!undoStack.empty()){ + return undoStack.back(); + } + return NULL; +} + +template +T* HistoryHandler::GetNext() +{ + if(!redoStack.empty()){ + return redoStack.back(); + } + return NULL; +} + template int HistoryHandler::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(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 meshList) void MeshManagerModel::AddMeshes(std::vector 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 meshList void MeshManagerModel::InsertMeshesAtCurrent(std::vector 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 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(_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(_meshes.at(currentMesh).get()); _meshes.at(currentMesh)->CopySetMeshBase(mesh); - Save(); + lastModified = currentMesh; RefreshOutputs(true); } else{ @@ -542,21 +573,41 @@ std::vector MeshManagerModel::GetMeshNames() return names; } +std::vector MeshManagerModel::GetAllPolyDatas() +{ + std::vector 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> meshesToSave, int meshId, int currentMesh) +MeshManagerModel::ManagerState::ManagerState(std::vector> 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; } diff --git a/lib/creaVtk/MeshManagerModel.h b/lib/creaVtk/MeshManagerModel.h index d8e6a78..f981c87 100644 --- a/lib/creaVtk/MeshManagerModel.h +++ b/lib/creaVtk/MeshManagerModel.h @@ -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 GetMeshNames(); + std::vector GetAllPolyDatas(); + int GetCurrentMesh(); //--Method template---------------------------- // void FunctionName(int& parameterA); @@ -163,17 +166,18 @@ private: std::vector> _meshes; int currentMesh; int meshId; + int lastModified; class ManagerState{ public: - ManagerState(std::vector> meshesToSave, int meshId, int currentMesh); + ManagerState(std::vector> meshesToSave, int meshId, int modifiedId); ~ManagerState(); std::vector>& GetMeshes(); int GetMeshId(); - int GetCurrentMesh(); + int GetModifiedPos(); private: std::vector> savedMeshes; - int savedCurrentMesh; + int savedModifiedPos; int savedId; };