]> Creatis software - creaVtk.git/blob - lib/creaVtk/MeshManagerModel.cpp
#3507 Undo and Redo Meshes
[creaVtk.git] / lib / creaVtk / MeshManagerModel.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sante)
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
9 #
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.
16 #
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
21 #  liability.
22 #
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 # ------------------------------------------------------------------------
26 */
27
28 #include "MeshManagerModel.h"
29
30 //
31 //HISTORY HANDLER
32 //
33 template <class T>
34 HistoryHandler<T>::HistoryHandler(int maxElements)
35 {
36         this->maxElements = maxElements;
37 }
38 template <class T>
39 HistoryHandler<T>::~HistoryHandler()
40 {
41         CleanHistory();
42 }
43
44 template <class T>
45 void HistoryHandler<T>::CleanHistory()
46 {
47         for (T* element : redoStack)
48         {
49                 delete element;
50         }
51         for (T* element : undoStack)
52         {
53                 delete element;
54         }
55 }
56
57 template <class T>
58 T* HistoryHandler<T>::Undo(T* state)
59 {
60         if(!undoStack.empty())
61         {
62                 auto lastElem = undoStack.back();
63                 undoStack.pop_back();
64                 redoStack.push_back(state);
65                 return lastElem;//undoStack.back();
66         }else{
67                 delete state;
68         }
69         return NULL;
70 }
71
72 template <class T>
73 T* HistoryHandler<T>::Redo(T* state)
74 {
75         if(!redoStack.empty())
76         {
77                 auto lastElem = redoStack.back();
78                 redoStack.pop_back();
79                 undoStack.push_back(state);
80                 return lastElem;
81         }else{
82                 delete state;
83         }
84         return NULL;
85 }
86
87 template <class T>
88 void HistoryHandler<T>::Save(T* state)
89 {
90         undoStack.push_back(state);
91         if(!redoStack.empty())
92         {
93                 for (T* element : redoStack)
94                 {
95                         delete element;
96                 } 
97                 redoStack.clear();
98         }
99 }
100
101 template <class T>
102 T* HistoryHandler<T>::GetPrevious()
103 {
104         if(!undoStack.empty()){ 
105                 return undoStack.back();
106         }
107         return NULL;
108 }
109
110 template <class T>
111 T* HistoryHandler<T>::GetNext()
112 {
113         if(!redoStack.empty()){ 
114                 return redoStack.back();
115         }
116         return NULL;
117 }
118
119 template <class T>
120 int HistoryHandler<T>::UndoSize()
121 {
122         return undoStack.size();
123 }
124
125 template <class T>
126 int HistoryHandler<T>::RedoSize()
127 {
128         return redoStack.size();
129 }
130
131 ////////////////////
132 /////////////////////// MESH MODEL
133 ////////////////////
134
135 MeshModel::MeshModel(int id)
136 {
137         _meshBase = NULL;
138     _meshTemp = NULL;
139     _meshId = id;
140     _name = "mesh-" + std::to_string(id);
141 }
142
143 MeshModel::MeshModel(vtkPolyData* mesh, int id)
144 {
145         if(mesh != NULL){       
146                 _meshBase = vtkPolyData::New();//mesh;
147                 _meshBase->DeepCopy(mesh);
148                 _meshTemp = vtkPolyData::New();
149                 _meshTemp->DeepCopy(_meshBase);
150                 _meshId = id;
151         _name = "mesh-" + std::to_string(id);
152         }
153 }
154
155 MeshModel::MeshModel(MeshModel* meshModel)
156 {
157         _meshBase = NULL;
158     _meshTemp = NULL;
159         if(meshModel->GetMeshBase() != NULL)
160         {
161                 _meshBase = vtkPolyData::New();
162                 _meshBase->DeepCopy(meshModel->GetMeshBase());
163                 ResetMeshTemp_();
164         }
165         _meshId = meshModel->GetId();
166         _name = "mesh-" + std::to_string(meshModel->GetId());
167 }
168
169 MeshModel::~MeshModel(){
170         if(_meshBase != NULL){
171                 _meshBase->Delete();
172         }
173         if(_meshTemp != NULL){
174                 _meshTemp->Delete();
175         }
176 }
177
178 void MeshModel::ResetMeshTemp_()
179 {
180     if (_meshBase!=NULL)
181     {
182         if (_meshTemp!=NULL)
183         {
184             _meshTemp->Delete();
185         } // if
186         _meshTemp = vtkPolyData::New();
187         _meshTemp->DeepCopy(_meshBase);
188     } else {
189         if (_meshTemp!=NULL)
190         {
191             _meshTemp->Delete();
192             _meshTemp = NULL;
193         }
194     }
195 }
196
197 void MeshModel::SetMeshBase(vtkPolyData* mesh)
198 {
199     if (mesh!=NULL)
200     {
201         if(_meshBase != NULL){
202                 _meshBase->Delete();
203         }
204         _meshBase = vtkPolyData::New();
205         _meshBase->DeepCopy(mesh);
206         ResetMeshTemp_();
207     } // if mesh
208 }
209
210 void MeshModel::SetMeshMemoryMode(vtkPolyData* mesh)
211 {
212         if(_meshBase != NULL)
213         {
214                 _meshBase->Delete();
215                 _meshBase = NULL;
216         }
217         if(mesh != NULL)
218         {
219                 _meshBase = vtkPolyData::New();
220                 _meshBase->DeepCopy(mesh);
221         }
222 }
223
224 void MeshModel::ResetMeshTemp()
225 {
226     ResetMeshTemp_();
227 }
228
229 void MeshModel::CopySetMeshBase(vtkPolyData* mesh)
230 {
231     if (mesh!=NULL)
232     {
233       vtkPolyData *newMesh = vtkPolyData::New();
234       newMesh->DeepCopy( mesh );
235       SetMeshBase(newMesh);
236     } // if mesh
237 }
238
239
240 vtkPolyData* MeshModel::GetMeshBase()
241 {
242    return _meshBase;
243 }
244
245 vtkPolyData* MeshModel::GetMeshTemp()
246 {
247    return _meshTemp;
248 }
249
250 int MeshModel::GetId()
251 {
252         return _meshId;
253 }
254
255 std::string MeshModel::GetName()
256 {
257         return _name;
258 }
259
260 ////////////////////
261 /////////////////////// MESH MANAGER
262 ////////////////////
263
264 MeshManagerModel::MeshManagerModel()
265 {
266         currentMesh = 0;
267         meshId = 0;
268         history = new HistoryHandler<ManagerState>(20);
269         lastModified = 0;
270 }
271
272 MeshManagerModel::~MeshManagerModel()
273 {
274         DeleteAll();
275 }
276
277 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
278 {
279 }
280
281 void MeshManagerModel::ResetHistory()
282 {
283         history->CleanHistory();
284         //Save();
285 }
286
287 int MeshManagerModel::GetNumberOfMeshes()
288 {
289         return _meshes.size();
290 }
291
292 void MeshManagerModel::AddMesh_(vtkPolyData* mesh)
293 {
294         if(mesh != NULL){
295                 _meshes.push_back(std::make_shared<MeshModel>(mesh, meshId));
296                 meshId++;
297         }
298         else{
299                 printf("PG MeshManagerModel::AddMesh Mesh is null \n");
300         }
301 }
302
303 void MeshManagerModel::AddMesh(vtkPolyData* mesh)
304 {
305         Save();
306         AddMesh_(mesh);
307         lastModified = currentMesh;
308         RefreshOutputs(true);
309 }
310
311 void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList)
312 {
313         if(!meshList.empty())
314         {
315                 MeshModel *meshModel;
316                 for(int i = 0; i < meshList.size(); i++){
317                         _meshes.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
318                         meshId++;
319                 }
320         }else{
321                 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
322         }
323 }
324
325 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList)
326 {
327         Save();
328         AddMeshes_(meshList);
329         lastModified = currentMesh;
330         RefreshOutputs(true);
331 }
332
333 void MeshManagerModel::AddEmptyMesh_()
334 {
335         _meshes.push_back(std::make_shared<MeshModel>(meshId));
336         meshId++;
337 }
338
339 void MeshManagerModel::AddEmptyMesh()
340 {
341         Save();
342         AddEmptyMesh_();
343         lastModified = currentMesh;
344         RefreshOutputs(true);
345 }
346
347 void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList)
348 {
349         if(!meshList.empty())
350         {
351                 std::vector<std::shared_ptr<MeshModel>> tmpVect;
352                 MeshModel *meshModel;
353                 for(int i = 0; i < meshList.size(); i++){
354                         tmpVect.push_back(std::make_shared<MeshModel>(meshList[i], meshId));
355                         meshId++;
356                 }
357                 _meshes.insert(_meshes.begin() + currentMesh, tmpVect.begin(), tmpVect.end());
358         }else{
359                 printf("PG MeshManagerModel::InsertMeshesAtCurrent Empty list of meshes \n");
360         }
361 }
362
363 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
364 {
365         Save();
366         InsertMeshesAtCurrent_(meshList);
367         lastModified = currentMesh;
368         RefreshOutputs(true);
369 }
370
371 void MeshManagerModel::SelectMesh(int i) 
372 {
373         if(i >= 0 && i < _meshes.size()){
374                 int prevCurrent = currentMesh;
375                 currentMesh = i;
376                 if(prevCurrent != i){
377                         RefreshOutputs(true);           
378                 }
379         }
380         else{
381                 printf("PG MeshManagerModel::SelectMesh index out of bounds \n");
382         }
383 }
384
385 void MeshManagerModel::SelectMeshByName(std::string meshName) 
386 {
387         if(!_meshes.empty()){
388                 bool found = false;
389                 for(int i = 0; i < _meshes.size() && !found; i++){
390                         if(_meshes.at(i)->GetName() == meshName){
391                                 found = true;
392                                 SelectMesh(i);
393                         }
394                 }
395         }
396 }
397
398 void MeshManagerModel::DeleteMesh_(int position)
399 {
400         if(position >= 0 && position < _meshes.size()){
401                 _meshes.erase(_meshes.begin() + position);
402                 currentMesh = currentMesh + (position <= currentMesh?-1:0);
403                 if(currentMesh < 0){
404                         currentMesh = 0;
405                 }
406         }
407 }
408
409 void MeshManagerModel::DeleteMesh(int position)
410 {
411         Save();
412         DeleteMesh_(position);
413         lastModified = currentMesh;
414         RefreshOutputs(true);
415 }
416
417 void MeshManagerModel::DeleteMeshByName(std::string meshName)
418 {
419         if(!_meshes.empty()){
420                 bool found = false;
421                 for(int i = 0; i < _meshes.size() && !found; i++){
422                         if(_meshes.at(i)->GetName() == meshName){
423                                 found = true;
424                                 DeleteMesh_(i);
425                                 RefreshOutputs(true);
426                         }
427                 }
428         }
429 }
430
431 void MeshManagerModel::DeleteCurrentMesh()
432 {
433         if(!_meshes.empty()){
434                 Save();
435                 DeleteMesh_(currentMesh);
436                 lastModified = currentMesh;
437                 RefreshOutputs(true);
438         }
439 }
440
441 void MeshManagerModel::ReplaceMesh(std::vector<vtkPolyData*> meshList)
442 {
443         Save();
444         if(GetNumberOfMeshes() >= 1)
445         {
446                 DeleteMesh_(currentMesh);
447         }
448         InsertMeshesAtCurrent_(meshList);
449         lastModified = currentMesh;
450         RefreshOutputs(true);
451 }
452
453 void MeshManagerModel::DeleteAll_()
454 {
455         if(!_meshes.empty())
456         {
457                 currentMesh = 0;
458                 _meshes.clear();
459                 RefreshOutputs(true);
460         }
461 }
462
463 void MeshManagerModel::DeleteAll()
464 {
465         Save();
466         DeleteAll_();
467         lastModified = currentMesh;
468         RefreshOutputs(true);
469 }
470
471 void MeshManagerModel::NextMesh()
472 {
473         currentMesh++;
474         if(currentMesh >= _meshes.size()){
475                 currentMesh = _meshes.size()-1;
476         }
477         RefreshOutputs(true);
478 }
479
480 void MeshManagerModel::PreviousMesh()
481 {
482         currentMesh--;
483         if(currentMesh < 0){
484                 currentMesh = 0;
485         }
486         RefreshOutputs(true);
487 }
488
489 std::shared_ptr<MeshModel> MeshManagerModel::GetMeshModel()
490 {
491         return _meshes.at(currentMesh);
492 }
493
494 vtkPolyData*  MeshManagerModel::GetMeshBase()
495 {
496         if(!_meshes.empty()){
497                 return _meshes.at(currentMesh)->GetMeshBase();
498         }
499         else{
500                 return NULL;
501         }
502 }
503
504 vtkPolyData*  MeshManagerModel::GetMeshTemp()
505 {
506         if(!_meshes.empty()){
507                 return _meshes.at(currentMesh)->GetMeshTemp();
508         }
509         else{
510                 return NULL;
511         }
512 }
513
514 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
515 {
516     if(!_meshes.empty()){
517         Save();
518         _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
519         _meshes.at(currentMesh)->SetMeshBase(mesh);
520         lastModified = currentMesh;
521         RefreshOutputs(true);
522     }else{
523         printf("PG MeshManagerModel::SetMeshBase Mesh vector is empty \n");
524     }
525 }
526
527 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
528 {
529         if(_meshes.size() > 1)
530         {
531                 DeleteAll_();
532         }
533         if(_meshes.size() == 0)
534         {
535                 AddEmptyMesh_();
536         }
537         _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
538         RefreshOutputs(true);
539 }
540
541 void MeshManagerModel::ResetMeshTemp()
542 {
543         if(!_meshes.empty())
544         {
545         _meshes.at(currentMesh)->ResetMeshTemp();
546         RefreshOutputs(true);
547     }else{
548         printf("PG MeshManagerModel::ResetMeshTemp Mesh vector is empty \n");
549     }
550 }
551
552 void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
553 {
554         if(!_meshes.empty())
555         {
556                 Save();
557                 _meshes.at(currentMesh) = std::make_shared<MeshModel>(_meshes.at(currentMesh).get());
558                 _meshes.at(currentMesh)->CopySetMeshBase(mesh);
559                 lastModified = currentMesh;
560                 RefreshOutputs(true);
561         }
562         else{
563                 printf("PG MeshManagerModel::CopySetMeshBase Mesh vector is empty \n");
564         }
565 }
566
567 std::vector<std::string> MeshManagerModel::GetMeshNames()
568 {
569         std::vector<std::string> names;
570         for(int i = 0; i < _meshes.size(); i++){
571                 names.push_back(_meshes.at(i)->GetName());
572         }
573         return names;
574 }
575
576 std::vector<vtkPolyData*> MeshManagerModel::GetAllPolyDatas()
577 {
578         std::vector<vtkPolyData*> polydatas;
579         for(int i = 0; i < _meshes.size(); i++){
580                 polydatas.push_back(_meshes.at(i)->GetMeshBase());
581         }
582         return polydatas;
583 }
584
585 int MeshManagerModel::GetCurrentMesh()
586 {
587         return currentMesh;
588 }
589
590 void MeshManagerModel::Undo()
591 {       
592         if(history->UndoSize() > 0){    
593                 RestoreState(history->Undo(new ManagerState(_meshes, meshId, lastModified)));
594                 RefreshOutputs(true);
595         }
596 }
597
598 void MeshManagerModel::Redo()
599 {
600         if(history->RedoSize() > 0)
601         {       
602                 RestoreState(history->Redo(new ManagerState(_meshes, meshId, lastModified)));
603                 RefreshOutputs(true);
604         }
605 }
606
607 void MeshManagerModel::Save()
608 {
609         history->Save(new ManagerState(_meshes, meshId, currentMesh));
610         //lastModified = currentMesh;
611 }
612
613 void MeshManagerModel::RestoreState(ManagerState* state)
614 {
615         if(state != NULL)
616         {       
617                 _meshes = state->GetMeshes();
618                 meshId = state->GetMeshId();
619                 currentMesh = state->GetModifiedPos();
620                 lastModified = state->GetModifiedPos();
621 //              cout << "PG MeshManagerModel::RestoreState currentMesh: " << currentMesh << endl;
622         }
623 }
624
625 //
626 //Manager State
627 //
628 MeshManagerModel::ManagerState::ManagerState(std::vector<std::shared_ptr<MeshModel>> meshesToSave, int meshId, int modifiedPos)
629 {
630         savedMeshes = meshesToSave;
631         savedId = meshId;
632         savedModifiedPos = modifiedPos;
633 }
634
635 MeshManagerModel::ManagerState::~ManagerState()
636 {
637 }
638
639 std::vector<std::shared_ptr<MeshModel>>& MeshManagerModel::ManagerState::GetMeshes()
640 {
641         return savedMeshes;
642 }
643
644 int MeshManagerModel::ManagerState::GetMeshId()
645 {
646         return savedId;
647 }
648 int MeshManagerModel::ManagerState::GetModifiedPos()
649 {
650         return savedModifiedPos;
651 }
652