2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
28 #include "MeshManagerModel.h"
34 HistoryHandler<T>::HistoryHandler(int maxElements)
36 this->maxElements = maxElements;
39 HistoryHandler<T>::~HistoryHandler()
45 void HistoryHandler<T>::CleanHistory()
47 for (T* element : redoStack)
52 for (T* element : undoStack)
60 T* HistoryHandler<T>::Undo(T* state)
66 if(!undoStack.empty())
68 auto lastElem = undoStack.back();
70 redoStack.push_back(state);
79 T* HistoryHandler<T>::Redo(T* state)
85 if(!redoStack.empty())
87 auto lastElem = redoStack.back();
89 undoStack.push_back(state);
98 * To be used with RedoKeepCurrent
99 * Will always maintain the current state in the undo stack as the first element
100 * Useful when states are saved after actions and the initial state is saved.
103 T* HistoryHandler<T>::UndoKeepCurrent()
105 if(undoStack.size() > 1)
107 auto lastElem = undoStack.back();
108 undoStack.pop_back();
109 redoStack.push_back(lastElem);
110 return undoStack.back();
117 * To be used with UndoKeepCurrent
118 * Will always maintain the current state in the undo stack as the first element
119 * Useful when states are saved after actions and the initial state is saved.
122 T* HistoryHandler<T>::RedoKeepCurrent()
124 if(!redoStack.empty())
126 auto lastElem = redoStack.back();
127 redoStack.pop_back();
128 undoStack.push_back(lastElem);
129 return undoStack.back();
136 void HistoryHandler<T>::Save(T* state)
138 undoStack.push_back(state);
139 if(undoStack.size() > maxElements)
141 T* frontDel = undoStack.front();
142 undoStack.pop_front();
145 if(!redoStack.empty())
147 for (T* element : redoStack)
156 T* HistoryHandler<T>::GetPrevious()
158 if(!undoStack.empty())
160 return undoStack.back();
166 T* HistoryHandler<T>::GetNext()
168 if(!redoStack.empty())
170 return redoStack.back();
176 int HistoryHandler<T>::UndoSize()
178 return undoStack.size();
182 int HistoryHandler<T>::RedoSize()
184 return redoStack.size();
188 /////////////////////// MESH MODEL
191 MeshModel::MeshModel(int id)
196 _name = "mesh-" + std::to_string(id);
199 MeshModel::MeshModel(vtkPolyData* mesh, int id)
203 _meshBase = vtkPolyData::New();//mesh;
204 _meshBase->DeepCopy(mesh);
205 _meshTemp = vtkPolyData::New();
206 _meshTemp->DeepCopy(_meshBase);
208 _name = "mesh-" + std::to_string(id);
212 MeshModel::MeshModel(MeshModel* meshModel)
216 if(meshModel->GetMeshBase() != NULL)
218 _meshBase = vtkPolyData::New();
219 _meshBase->DeepCopy(meshModel->GetMeshBase());
222 _meshId = meshModel->GetId();
223 _name = "mesh-" + std::to_string(meshModel->GetId());
226 MeshModel::~MeshModel()
228 if(_meshBase != NULL)
232 if(_meshTemp != NULL)
238 void MeshModel::ResetMeshTemp_()
246 _meshTemp = vtkPolyData::New();
247 _meshTemp->DeepCopy(_meshBase);
257 void MeshModel::SetMeshBase(vtkPolyData* mesh)
261 if(_meshBase != NULL)
265 _meshBase = vtkPolyData::New();
266 _meshBase->DeepCopy(mesh);
271 void MeshModel::SetMeshMemoryMode(vtkPolyData* mesh)
273 if(_meshBase != NULL)
280 _meshBase = vtkPolyData::New();
281 _meshBase->DeepCopy(mesh);
285 void MeshModel::ResetMeshTemp()
290 void MeshModel::CopySetMeshBase(vtkPolyData* mesh)
294 vtkPolyData *newMesh = vtkPolyData::New();
295 newMesh->DeepCopy( mesh );
296 SetMeshBase(newMesh);
301 vtkPolyData* MeshModel::GetMeshBase()
306 vtkPolyData* MeshModel::GetMeshTemp()
311 int MeshModel::GetId()
316 std::string MeshModel::GetName()
322 /////////////////////// MESH MANAGER
325 MeshManagerModel::MeshManagerModel()
329 history = new HistoryHandler<ManagerState>(15);
334 MeshManagerModel::~MeshManagerModel()
337 history->CleanHistory();
341 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
345 void MeshManagerModel::SetHistory(int maxCapacity)
351 history = new HistoryHandler<ManagerState>(maxCapacity);
354 void MeshManagerModel::ResetHistory()
356 history->CleanHistory();
359 int MeshManagerModel::GetNumberOfMeshes()
361 return _meshes.size();
364 void MeshManagerModel::AddMesh_(vtkPolyData* mesh)
368 _meshes.push_back(std::make_shared<MeshModel>(mesh, meshId));
371 printf("PG MeshManagerModel::AddMesh Mesh is null \n");
375 void MeshManagerModel::AddMesh(vtkPolyData* mesh)
379 lastModified = currentMesh;
380 RefreshOutputs(true);
383 void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList)
385 if(!meshList.empty())
387 MeshModel *meshModel;
388 for(int i = 0; i < meshList.size(); i++){
389 _meshes.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
393 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
397 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList)
400 AddMeshes_(meshList);
401 lastModified = currentMesh;
402 RefreshOutputs(true);
405 void MeshManagerModel::AddEmptyMesh_()
407 _meshes.push_back(std::make_shared<MeshModel>(meshId));
411 void MeshManagerModel::AddEmptyMesh()
415 lastModified = currentMesh;
416 RefreshOutputs(true);
419 void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList)
421 if(!meshList.empty())
423 std::vector<std::shared_ptr<MeshModel>> tmpVect;
424 MeshModel *meshModel;
425 for(int i = 0; i < meshList.size(); i++){
426 tmpVect.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
429 _meshes.insert(_meshes.begin() + currentMesh, tmpVect.begin(), tmpVect.end());
431 printf("PG MeshManagerModel::InsertMeshesAtCurrent Empty list of meshes \n");
435 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
438 InsertMeshesAtCurrent_(meshList);
439 lastModified = currentMesh;
440 RefreshOutputs(true);
443 void MeshManagerModel::SelectMesh(int i)
445 if(i >= 0 && i < _meshes.size())
447 int prevCurrent = currentMesh;
451 RefreshOutputs(true);
455 printf("PG MeshManagerModel::SelectMesh index out of bounds \n");
459 void MeshManagerModel::SelectMeshByName(std::string meshName)
464 for(int i = 0; i < _meshes.size() && !found; i++){
465 if(_meshes.at(i)->GetName() == meshName)
474 void MeshManagerModel::DeleteMesh_(int position)
476 if(position >= 0 && position < _meshes.size())
478 _meshes.erase(_meshes.begin() + position);
479 currentMesh = currentMesh + (position <= currentMesh?-1:0);
487 void MeshManagerModel::DeleteMesh(int position)
490 DeleteMesh_(position);
491 lastModified = currentMesh;
492 RefreshOutputs(true);
495 void MeshManagerModel::DeleteMeshByName(std::string meshName)
500 for(int i = 0; i < _meshes.size() && !found; i++){
501 if(_meshes.at(i)->GetName() == meshName)
505 RefreshOutputs(true);
511 void MeshManagerModel::DeleteCurrentMesh()
516 DeleteMesh_(currentMesh);
517 lastModified = currentMesh;
518 RefreshOutputs(true);
522 void MeshManagerModel::ReplaceMesh(std::vector<vtkPolyData*> meshList)
525 if(GetNumberOfMeshes() >= 1)
527 DeleteMesh_(currentMesh);
529 InsertMeshesAtCurrent_(meshList);
530 lastModified = currentMesh;
531 RefreshOutputs(true);
534 void MeshManagerModel::DeleteAll_()
540 RefreshOutputs(true);
544 void MeshManagerModel::DeleteAll()
548 lastModified = currentMesh;
549 RefreshOutputs(true);
552 void MeshManagerModel::NextMesh()
555 if(currentMesh >= _meshes.size())
557 currentMesh = _meshes.size()-1;
559 RefreshOutputs(true);
562 void MeshManagerModel::PreviousMesh()
569 RefreshOutputs(true);
572 std::shared_ptr<MeshModel> MeshManagerModel::GetMeshModel()
574 return _meshes.at(currentMesh);
577 vtkPolyData* MeshManagerModel::GetMeshBase()
581 return _meshes.at(currentMesh)->GetMeshBase();
588 vtkPolyData* MeshManagerModel::GetMeshTemp()
592 return _meshes.at(currentMesh)->GetMeshTemp();
599 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
604 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
605 _meshes.at(currentMesh)->SetMeshBase(mesh);
606 lastModified = currentMesh;
607 RefreshOutputs(true);
609 printf("PG MeshManagerModel::SetMeshBase Mesh vector is empty \n");
613 void MeshManagerModel::MeshMemoryModeOn()
618 void MeshManagerModel::MeshMemoryModeOff()
623 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
625 if(_meshes.size() > 1)
629 if(_meshes.size() == 0)
633 _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
636 RefreshOutputs(true);
639 void MeshManagerModel::ResetMeshTemp()
643 _meshes.at(currentMesh)->ResetMeshTemp();
644 RefreshOutputs(true);
646 printf("PG MeshManagerModel::ResetMeshTemp Mesh vector is empty \n");
650 void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
655 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
656 _meshes.at(currentMesh)->CopySetMeshBase(mesh);
657 lastModified = currentMesh;
658 RefreshOutputs(true);
661 printf("PG MeshManagerModel::CopySetMeshBase Mesh vector is empty \n");
665 std::vector<std::string> MeshManagerModel::GetMeshNames()
667 std::vector<std::string> names;
668 for(int i = 0; i < _meshes.size(); i++){
669 names.push_back(_meshes.at(i)->GetName());
674 std::vector<vtkPolyData*> MeshManagerModel::GetAllPolyDatas()
676 std::vector<vtkPolyData*> polydatas;
677 for(int i = 0; i < _meshes.size(); i++){
678 polydatas.push_back(_meshes.at(i)->GetMeshBase());
683 int MeshManagerModel::GetCurrentMesh()
688 void MeshManagerModel::Undo()
690 if(history->UndoSize() > 0)
692 if(memoryMode == false){
693 RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
694 RefreshOutputs(true);
696 else if(history->UndoSize() > 1){
697 RestoreStateMemoryMode(history->UndoKeepCurrent());
702 void MeshManagerModel::Redo()
704 if(history->RedoSize() > 0)
706 if(memoryMode == false){
707 RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
708 RefreshOutputs(true);
711 RestoreStateMemoryMode(history->RedoKeepCurrent());
716 void MeshManagerModel::Save()
718 history->Save(new ManagerState(_meshes, meshId, currentMesh));
721 void MeshManagerModel::SaveMemoryMode()
723 if(_meshes.size() == 1 && memoryMode)
725 std::vector<std::shared_ptr<MeshModel>> savedMesh;
726 savedMesh.push_back(std::make_shared<MeshModel>(_meshes.at(0).get()));
727 history->Save(new ManagerState(savedMesh, meshId, 0));
730 printf("PG MeshManagerModel::SaveMemoryMode WARNING Mesh vector has invalid size or memoryMode is not set \n");
734 void MeshManagerModel::RestoreState(ManagerState* state)
738 _meshes = state->GetMeshes();
739 meshId = state->GetMeshId();
740 currentMesh = state->GetModifiedPos();
741 lastModified = state->GetModifiedPos();
745 printf("PG MeshManagerModel::RestoreState WARNING State is NULL \n");
749 void MeshManagerModel::RestoreStateMemoryMode(ManagerState* state){
750 if(_meshes.size() == 1 && state != NULL)
752 vtkPoints* statePoints = vtkPoints::New();
753 statePoints->DeepCopy(state->GetMeshes().at(0)->GetMeshBase()->GetPoints());
754 _meshes.at(0)->GetMeshBase()->SetPoints(statePoints);
755 _meshes.at(0)->GetMeshBase()->GetPoints()->Modified();
756 _meshes.at(0)->GetMeshBase()->Modified();
758 printf("PG MeshManagerModel::RestoreStateMemoryMode WARNING Mesh vector has invalid size or state is NULL\n");
765 MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos)
767 savedMeshes = meshesToSave;
769 savedModifiedPos = modifiedPos;
772 MeshManagerModel::ManagerState::~ManagerState()
777 std::vector<std::shared_ptr<MeshModel>>& MeshManagerModel::ManagerState::GetMeshes()
782 int MeshManagerModel::ManagerState::GetMeshId()
786 int MeshManagerModel::ManagerState::GetModifiedPos()
788 return savedModifiedPos;