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();
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);
332 referencePoint = {0, 0, 0};
335 MeshManagerModel::~MeshManagerModel()
338 history->CleanHistory();
342 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
346 void MeshManagerModel::SetHistory(int maxCapacity)
352 history = new HistoryHandler<ManagerState>(maxCapacity);
355 void MeshManagerModel::ResetHistory()
357 history->CleanHistory();
360 void MeshManagerModel::SetReferencePoint(std::vector<double> point)
362 referencePoint = point;
365 std::vector<double> MeshManagerModel::GetReferencePoint()
367 return referencePoint;
370 int MeshManagerModel::GetNumberOfMeshes()
372 return _meshes.size();
375 void MeshManagerModel::AddMesh_(vtkPolyData* mesh)
379 _meshes.push_back(std::make_shared<MeshModel>(mesh, meshId));
382 printf("PG MeshManagerModel::AddMesh Mesh is null \n");
386 void MeshManagerModel::AddMesh(vtkPolyData* mesh)
390 lastModified = currentMesh;
391 RefreshOutputs(true);
394 void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList)
396 if(!meshList.empty())
398 MeshModel *meshModel;
399 for(int i = 0; i < meshList.size(); i++){
400 _meshes.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
404 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
408 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList)
411 AddMeshes_(meshList);
412 lastModified = currentMesh;
413 RefreshOutputs(true);
416 void MeshManagerModel::AddEmptyMesh_()
418 _meshes.push_back(std::make_shared<MeshModel>(meshId));
422 void MeshManagerModel::AddEmptyMesh()
426 lastModified = currentMesh;
427 RefreshOutputs(true);
430 void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList)
432 if(!meshList.empty())
434 std::vector<std::shared_ptr<MeshModel>> tmpVect;
435 MeshModel *meshModel;
436 for(int i = 0; i < meshList.size(); i++){
437 tmpVect.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
440 _meshes.insert(_meshes.begin() + currentMesh, tmpVect.begin(), tmpVect.end());
442 printf("PG MeshManagerModel::InsertMeshesAtCurrent Empty list of meshes \n");
446 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
449 InsertMeshesAtCurrent_(meshList);
450 lastModified = currentMesh;
451 RefreshOutputs(true);
454 void MeshManagerModel::SelectMesh(int i)
456 if(i >= 0 && i < _meshes.size())
458 int prevCurrent = currentMesh;
462 RefreshOutputs(true);
466 printf("PG MeshManagerModel::SelectMesh index out of bounds \n");
470 void MeshManagerModel::SelectMeshByName(std::string meshName)
475 for(int i = 0; i < _meshes.size() && !found; i++){
476 if(_meshes.at(i)->GetName() == meshName)
485 void MeshManagerModel::DeleteMesh_(int position)
487 if(position >= 0 && position < _meshes.size())
489 _meshes.erase(_meshes.begin() + position);
490 currentMesh = currentMesh + (position <= currentMesh?-1:0);
498 void MeshManagerModel::DeleteMesh(int position)
501 DeleteMesh_(position);
502 lastModified = currentMesh;
503 RefreshOutputs(true);
506 void MeshManagerModel::DeleteMeshByName(std::string meshName)
511 for(int i = 0; i < _meshes.size() && !found; i++){
512 if(_meshes.at(i)->GetName() == meshName)
516 RefreshOutputs(true);
522 void MeshManagerModel::DeleteCurrentMesh()
527 DeleteMesh_(currentMesh);
528 lastModified = currentMesh;
529 RefreshOutputs(true);
533 void MeshManagerModel::ReplaceMesh(std::vector<vtkPolyData*> meshList)
536 if(GetNumberOfMeshes() >= 1)
538 DeleteMesh_(currentMesh);
540 InsertMeshesAtCurrent_(meshList);
541 lastModified = currentMesh;
542 RefreshOutputs(true);
545 void MeshManagerModel::DeleteAll_()
551 RefreshOutputs(true);
555 void MeshManagerModel::DeleteAll()
559 lastModified = currentMesh;
560 RefreshOutputs(true);
563 void MeshManagerModel::NextMesh()
566 if(currentMesh >= _meshes.size())
568 currentMesh = _meshes.size()-1;
570 RefreshOutputs(true);
573 void MeshManagerModel::PreviousMesh()
580 RefreshOutputs(true);
583 std::shared_ptr<MeshModel> MeshManagerModel::GetMeshModel()
585 return _meshes.at(currentMesh);
588 vtkPolyData* MeshManagerModel::GetMeshBase()
592 return _meshes.at(currentMesh)->GetMeshBase();
599 vtkPolyData* MeshManagerModel::GetMeshTemp()
603 return _meshes.at(currentMesh)->GetMeshTemp();
610 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
615 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
616 _meshes.at(currentMesh)->SetMeshBase(mesh);
617 lastModified = currentMesh;
618 RefreshOutputs(true);
620 printf("PG MeshManagerModel::SetMeshBase Mesh vector is empty \n");
624 void MeshManagerModel::MeshMemoryModeOn()
629 void MeshManagerModel::MeshMemoryModeOff()
634 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
636 if(_meshes.size() > 1)
640 if(_meshes.size() == 0)
644 _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
647 RefreshOutputs(true);
650 void MeshManagerModel::ResetMeshTemp()
654 _meshes.at(currentMesh)->ResetMeshTemp();
655 RefreshOutputs(true);
657 printf("PG MeshManagerModel::ResetMeshTemp Mesh vector is empty \n");
661 void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
666 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
667 _meshes.at(currentMesh)->CopySetMeshBase(mesh);
668 lastModified = currentMesh;
669 RefreshOutputs(true);
672 printf("PG MeshManagerModel::CopySetMeshBase Mesh vector is empty \n");
676 std::vector<std::string> MeshManagerModel::GetMeshNames()
678 std::vector<std::string> names;
679 for(int i = 0; i < _meshes.size(); i++){
680 names.push_back(_meshes.at(i)->GetName());
685 std::vector<vtkPolyData*> MeshManagerModel::GetAllPolyDatas()
687 std::vector<vtkPolyData*> polydatas;
688 for(int i = 0; i < _meshes.size(); i++){
689 polydatas.push_back(_meshes.at(i)->GetMeshBase());
694 int MeshManagerModel::GetCurrentMesh()
699 void MeshManagerModel::Undo()
701 if(history->UndoSize() > 0)
703 if(memoryMode == false){
704 RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified, referencePoint)));
705 RefreshOutputs(true);
707 else if(history->UndoSize() > 1){
708 RestoreStateMemoryMode(history->UndoKeepCurrent());
713 void MeshManagerModel::Redo()
715 if(history->RedoSize() > 0)
717 if(memoryMode == false){
718 RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified, referencePoint)));
719 RefreshOutputs(true);
722 RestoreStateMemoryMode(history->RedoKeepCurrent());
727 void MeshManagerModel::Save()
729 history->Save(new ManagerState(_meshes, meshId, currentMesh, referencePoint));
732 void MeshManagerModel::SaveMemoryMode()
734 if(_meshes.size() == 1 && memoryMode)
736 std::vector<std::shared_ptr<MeshModel>> savedMesh;
737 savedMesh.push_back(std::make_shared<MeshModel>(_meshes.at(0).get()));
738 history->Save(new ManagerState(savedMesh, meshId, 0, referencePoint));
741 printf("PG MeshManagerModel::SaveMemoryMode WARNING Mesh vector has invalid size or memoryMode is not set \n");
745 void MeshManagerModel::RestoreState(ManagerState* state)
749 _meshes = state->GetMeshes();
750 meshId = state->GetMeshId();
751 currentMesh = state->GetModifiedPos();
752 lastModified = state->GetModifiedPos();
756 printf("PG MeshManagerModel::RestoreState WARNING State is NULL \n");
760 void MeshManagerModel::RestoreStateMemoryMode(ManagerState* state){
761 if(_meshes.size() == 1 && state != NULL)
763 vtkPoints* statePoints = vtkPoints::New();
764 statePoints->DeepCopy(state->GetMeshes().at(0)->GetMeshBase()->GetPoints());
765 _meshes.at(0)->GetMeshBase()->SetPoints(statePoints);
766 _meshes.at(0)->GetMeshBase()->GetPoints()->Modified();
767 _meshes.at(0)->GetMeshBase()->Modified();
768 referencePoint = state->GetReferencePoint();
770 printf("PG MeshManagerModel::RestoreStateMemoryMode WARNING Mesh vector has invalid size or state is NULL\n");
777 MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos, std::vector<double> refPoint)
779 savedMeshes = meshesToSave;
781 savedModifiedPos = modifiedPos;
782 referencePoint = refPoint;
785 MeshManagerModel::ManagerState::~ManagerState()
790 std::vector<std::shared_ptr<MeshModel>>& MeshManagerModel::ManagerState::GetMeshes()
795 int MeshManagerModel::ManagerState::GetMeshId()
799 int MeshManagerModel::ManagerState::GetModifiedPos()
801 return savedModifiedPos;
804 std::vector<double>& MeshManagerModel::ManagerState::GetReferencePoint()
806 return referencePoint;