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};
333 referenceNormal = {0, 0, 1};
336 MeshManagerModel::~MeshManagerModel()
339 history->CleanHistory();
343 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
347 void MeshManagerModel::SetHistory(int maxCapacity)
353 history = new HistoryHandler<ManagerState>(maxCapacity);
356 void MeshManagerModel::ResetHistory()
358 history->CleanHistory();
361 void MeshManagerModel::SetReferencePoint(std::vector<double> point)
363 referencePoint = point;
366 std::vector<double> MeshManagerModel::GetReferencePoint()
368 return referencePoint;
371 void MeshManagerModel::SetReferenceNormal(std::vector<double> normal)
373 referenceNormal = normal;
376 std::vector<double> MeshManagerModel::GetReferenceNormal()
378 return referenceNormal;
381 int MeshManagerModel::GetNumberOfMeshes()
383 return _meshes.size();
386 void MeshManagerModel::AddMesh_(vtkPolyData* mesh)
390 _meshes.push_back(std::make_shared<MeshModel>(mesh, meshId));
393 printf("PG MeshManagerModel::AddMesh Mesh is null \n");
397 void MeshManagerModel::AddMesh(vtkPolyData* mesh)
401 lastModified = currentMesh;
402 RefreshOutputs(true);
405 void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList)
407 if(!meshList.empty())
409 MeshModel *meshModel;
410 for(int i = 0; i < meshList.size(); i++){
411 _meshes.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
415 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
419 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList)
422 AddMeshes_(meshList);
423 lastModified = currentMesh;
424 RefreshOutputs(true);
427 void MeshManagerModel::AddEmptyMesh_()
429 _meshes.push_back(std::make_shared<MeshModel>(meshId));
433 void MeshManagerModel::AddEmptyMesh()
437 lastModified = currentMesh;
438 RefreshOutputs(true);
441 void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList)
443 if(!meshList.empty())
445 std::vector<std::shared_ptr<MeshModel>> tmpVect;
446 MeshModel *meshModel;
447 for(int i = 0; i < meshList.size(); i++){
448 tmpVect.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
451 _meshes.insert(_meshes.begin() + currentMesh, tmpVect.begin(), tmpVect.end());
453 printf("PG MeshManagerModel::InsertMeshesAtCurrent Empty list of meshes \n");
457 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
460 InsertMeshesAtCurrent_(meshList);
461 lastModified = currentMesh;
462 RefreshOutputs(true);
465 void MeshManagerModel::SelectMesh(int i)
467 if(i >= 0 && i < _meshes.size())
469 int prevCurrent = currentMesh;
473 RefreshOutputs(true);
477 printf("PG MeshManagerModel::SelectMesh index out of bounds \n");
481 void MeshManagerModel::SelectMeshByName(std::string meshName)
486 for(int i = 0; i < _meshes.size() && !found; i++){
487 if(_meshes.at(i)->GetName() == meshName)
496 void MeshManagerModel::DeleteMesh_(int position)
498 if(position >= 0 && position < _meshes.size())
500 _meshes.erase(_meshes.begin() + position);
501 currentMesh = currentMesh + (position <= currentMesh?-1:0);
509 void MeshManagerModel::DeleteMesh(int position)
512 DeleteMesh_(position);
513 lastModified = currentMesh;
514 RefreshOutputs(true);
517 void MeshManagerModel::DeleteMeshByName(std::string meshName)
522 for(int i = 0; i < _meshes.size() && !found; i++){
523 if(_meshes.at(i)->GetName() == meshName)
527 RefreshOutputs(true);
533 void MeshManagerModel::DeleteCurrentMesh()
538 DeleteMesh_(currentMesh);
539 lastModified = currentMesh;
540 RefreshOutputs(true);
544 void MeshManagerModel::ReplaceMesh(std::vector<vtkPolyData*> meshList)
547 if(GetNumberOfMeshes() >= 1)
549 DeleteMesh_(currentMesh);
551 InsertMeshesAtCurrent_(meshList);
552 lastModified = currentMesh;
553 RefreshOutputs(true);
556 void MeshManagerModel::DeleteAll_()
562 RefreshOutputs(true);
566 void MeshManagerModel::DeleteAll()
570 lastModified = currentMesh;
571 RefreshOutputs(true);
574 void MeshManagerModel::NextMesh()
577 if(currentMesh >= _meshes.size())
579 currentMesh = _meshes.size()-1;
581 RefreshOutputs(true);
584 void MeshManagerModel::PreviousMesh()
591 RefreshOutputs(true);
594 std::shared_ptr<MeshModel> MeshManagerModel::GetMeshModel()
596 return _meshes.at(currentMesh);
599 vtkPolyData* MeshManagerModel::GetMeshBase()
603 return _meshes.at(currentMesh)->GetMeshBase();
610 vtkPolyData* MeshManagerModel::GetMeshTemp()
614 return _meshes.at(currentMesh)->GetMeshTemp();
621 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
626 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
627 _meshes.at(currentMesh)->SetMeshBase(mesh);
628 lastModified = currentMesh;
629 RefreshOutputs(true);
631 printf("PG MeshManagerModel::SetMeshBase Mesh vector is empty \n");
635 void MeshManagerModel::MeshMemoryModeOn()
640 void MeshManagerModel::MeshMemoryModeOff()
645 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
647 if(_meshes.size() > 1)
651 if(_meshes.size() == 0)
655 _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
658 RefreshOutputs(true);
661 void MeshManagerModel::ResetMeshTemp()
665 _meshes.at(currentMesh)->ResetMeshTemp();
666 RefreshOutputs(true);
668 printf("PG MeshManagerModel::ResetMeshTemp Mesh vector is empty \n");
672 void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
677 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
678 _meshes.at(currentMesh)->CopySetMeshBase(mesh);
679 lastModified = currentMesh;
680 RefreshOutputs(true);
683 printf("PG MeshManagerModel::CopySetMeshBase Mesh vector is empty \n");
687 std::vector<std::string> MeshManagerModel::GetMeshNames()
689 std::vector<std::string> names;
690 for(int i = 0; i < _meshes.size(); i++){
691 names.push_back(_meshes.at(i)->GetName());
696 std::vector<vtkPolyData*> MeshManagerModel::GetAllPolyDatas()
698 std::vector<vtkPolyData*> polydatas;
699 for(int i = 0; i < _meshes.size(); i++){
700 polydatas.push_back(_meshes.at(i)->GetMeshBase());
705 int MeshManagerModel::GetCurrentMesh()
710 void MeshManagerModel::Undo()
712 if(history->UndoSize() > 0)
714 if(memoryMode == false){
715 RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
716 RefreshOutputs(true);
718 else if(history->UndoSize() > 1){
719 RestoreStateMemoryMode(history->UndoKeepCurrent());
724 void MeshManagerModel::Redo()
726 if(history->RedoSize() > 0)
728 if(memoryMode == false){
729 RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
730 RefreshOutputs(true);
733 RestoreStateMemoryMode(history->RedoKeepCurrent());
738 void MeshManagerModel::Save()
740 history->Save(new ManagerState(_meshes, meshId, currentMesh));
743 void MeshManagerModel::SaveMemoryMode()
745 if(_meshes.size() == 1 && memoryMode)
747 std::vector<std::shared_ptr<MeshModel>> savedMesh;
748 savedMesh.push_back(std::make_shared<MeshModel>(_meshes.at(0).get()));
749 history->Save(new ManagerState(savedMesh, meshId, 0, referencePoint, referenceNormal));
752 printf("PG MeshManagerModel::SaveMemoryMode WARNING Mesh vector has invalid size or memoryMode is not set \n");
756 void MeshManagerModel::RestoreState(ManagerState* state)
760 _meshes = state->GetMeshes();
761 meshId = state->GetMeshId();
762 currentMesh = state->GetModifiedPos();
763 lastModified = state->GetModifiedPos();
767 printf("PG MeshManagerModel::RestoreState WARNING State is NULL \n");
771 void MeshManagerModel::RestoreStateMemoryMode(ManagerState* state){
772 if(_meshes.size() == 1 && state != NULL)
774 vtkPoints* statePoints = vtkPoints::New();
775 statePoints->DeepCopy(state->GetMeshes().at(0)->GetMeshBase()->GetPoints());
776 _meshes.at(0)->GetMeshBase()->SetPoints(statePoints);
777 _meshes.at(0)->GetMeshBase()->GetPoints()->Modified();
778 _meshes.at(0)->GetMeshBase()->Modified();
779 referencePoint = state->GetReferencePoint();
780 referenceNormal = state->referenceNormal;
782 printf("PG MeshManagerModel::RestoreStateMemoryMode WARNING Mesh vector has invalid size or state is NULL\n");
789 MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos)
791 savedMeshes = meshesToSave;
793 savedModifiedPos = modifiedPos;
796 MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos, std::vector<double> refPoint, std::vector<double> refNormal)
798 savedMeshes = meshesToSave;
800 savedModifiedPos = modifiedPos;
801 referencePoint = refPoint;
802 referenceNormal = refNormal;
805 MeshManagerModel::ManagerState::~ManagerState()
810 std::vector<std::shared_ptr<MeshModel>>& MeshManagerModel::ManagerState::GetMeshes()
815 int MeshManagerModel::ManagerState::GetMeshId()
819 int MeshManagerModel::ManagerState::GetModifiedPos()
821 return savedModifiedPos;
824 std::vector<double>& MeshManagerModel::ManagerState::GetReferencePoint()
826 return referencePoint;