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