]> Creatis software - creaVtk.git/blob - lib/creaVtk/MeshManagerModel.cpp
677727c6057a054b06ecb097298ba36977c1f78a
[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 /////////////////////// MESH MODEL
32 ////////////////////
33
34 MeshModel::MeshModel(int id)
35 {
36         _meshBase = NULL;
37     _meshTemp = NULL;
38     _meshId = id;
39     _name = "mesh-" + std::to_string(id);
40 }
41
42 MeshModel::MeshModel(vtkPolyData* mesh, int id)
43 {
44         if(mesh != NULL){       
45                 _meshBase = mesh;
46                 _meshTemp = vtkPolyData::New();
47                 _meshTemp->DeepCopy(_meshBase);
48                 _meshId = id;
49         _name = "mesh-" + std::to_string(id);
50         }
51 }
52
53 MeshModel::~MeshModel(){
54         if(_meshBase != NULL){
55                 _meshBase->Delete();
56         }
57         if(_meshTemp != NULL){
58                 _meshTemp->Delete();
59         }
60 }
61
62 void MeshModel::ResetMeshTemp_()
63 {
64     if (_meshBase!=NULL)
65     {
66         if (_meshTemp!=NULL)
67         {
68             _meshTemp->Delete();
69         } // if
70         _meshTemp = vtkPolyData::New();
71         _meshTemp->DeepCopy(_meshBase);
72     } else {
73         _meshTemp=NULL;
74     }
75 }
76
77 void MeshModel::SetMeshBase(vtkPolyData* mesh)
78 {
79     if (mesh!=NULL)
80     {
81         _meshBase = mesh;
82         ResetMeshTemp_();
83     } // if mesh
84 }
85
86 void MeshModel::SetMeshMemoryMode(vtkPolyData* mesh)
87 {
88     _meshBase = mesh;
89 }
90
91 void MeshModel::ResetMeshTemp()
92 {
93     ResetMeshTemp_();
94 }
95
96 void MeshModel::CopySetMeshBase(vtkPolyData* mesh)
97 {
98     if (mesh!=NULL)
99     {
100       vtkPolyData *newMesh = vtkPolyData::New();
101       newMesh->DeepCopy( mesh );
102       SetMeshBase(newMesh);
103     } // if mesh
104 }
105
106
107 vtkPolyData*  MeshModel::GetMeshBase()
108 {
109    return _meshBase;
110 }
111
112 vtkPolyData*  MeshModel::GetMeshTemp()
113 {
114    return _meshTemp;
115 }
116
117 int MeshModel::GetId()
118 {
119         return _meshId;
120 }
121
122 std::string MeshModel::GetName()
123 {
124         return _name;
125 }
126
127 ////////////////////
128 /////////////////////// MESH MANAGER
129 ////////////////////
130
131 MeshManagerModel::MeshManagerModel()
132 {
133         //MeshModel* firstMesh = new MeshModel();
134         //_meshes.push_back(firstMesh);
135         currentMesh = 0;
136         meshId = 0;
137 }
138
139 MeshManagerModel::~MeshManagerModel()
140 {
141         DeleteAll();
142 }
143
144 void MeshManagerModel::RefreshOutputs(bool signalBox) // virtual
145 {
146 }
147
148 int MeshManagerModel::GetNumberOfMeshes(){
149         return _meshes.size();
150 }
151
152 void MeshManagerModel::AddMesh_(vtkPolyData* mesh){
153         if(mesh != NULL){
154                 MeshModel *meshModel = new MeshModel(mesh, meshId);
155                 _meshes.push_back(meshModel);
156                 meshId++;
157         }
158         else{
159                 printf("PG MeshManagerModel::AddMesh Mesh is null \n");
160         }
161 }
162
163 void MeshManagerModel::AddMesh(vtkPolyData* mesh){
164         AddMesh_(mesh);
165         RefreshOutputs(true);
166 }
167
168 void MeshManagerModel::AddMeshes_(std::vector<vtkPolyData*> meshList){
169         if(!meshList.empty())
170         {
171                 MeshModel *meshModel;
172                 for(int i = 0; i < meshList.size(); i++){
173                         meshModel = new MeshModel(meshList[i], meshId);
174                         _meshes.push_back(meshModel);
175                         meshId++;
176                 }
177         }else{
178                 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
179         }
180 }
181
182 void MeshManagerModel::AddMeshes(std::vector<vtkPolyData*> meshList){
183         AddMeshes_(meshList);
184         RefreshOutputs(true);
185 }
186
187 void MeshManagerModel::AddEmptyMesh_(){
188         MeshModel *meshModel = new MeshModel(meshId);
189         _meshes.push_back(meshModel);
190         meshId++;
191 }
192
193 void MeshManagerModel::AddEmptyMesh(){
194         AddEmptyMesh_();
195         RefreshOutputs(true);
196 }
197
198 void MeshManagerModel::InsertMeshesAtCurrent_(std::vector<vtkPolyData*> meshList)
199 {
200         if(!meshList.empty())
201         {
202                 std::vector<MeshModel*> tmpVect;
203                 MeshModel *meshModel;
204                 for(int i = 0; i < meshList.size(); i++){
205                         meshModel = new MeshModel(meshList[i], meshId);
206                         tmpVect.push_back(meshModel);
207                         meshId++;
208                 }
209                 _meshes.insert(_meshes.begin() + currentMesh, tmpVect.begin(), tmpVect.end());
210         }else{
211                 printf("PG MeshManagerModel::AddMeshes Empty list of meshes \n");
212         }
213 }
214
215 void MeshManagerModel::InsertMeshesAtCurrent(std::vector<vtkPolyData*> meshList)
216 {
217         InsertMeshesAtCurrent_(meshList);
218         RefreshOutputs(true);
219 }
220
221 void MeshManagerModel::SelectMesh(int i) {
222         if(i >= 0 && i < _meshes.size()){
223                 int prevCurrent = currentMesh;
224                 currentMesh = i;
225                 if(prevCurrent != i){
226                         RefreshOutputs(true);           
227                 }
228         }
229         else{
230                 printf("PG MeshManagerModel::SelectMesh index out of bounds \n");
231         }
232 }
233
234 void MeshManagerModel::SelectMeshByName(std::string meshName) {
235         if(!_meshes.empty()){
236                 bool found = false;
237                 for(int i = 0; i < _meshes.size() && !found; i++){
238                         if(_meshes.at(i)->GetName() == meshName){
239                                 found = true;
240                                 SelectMesh(i);
241                         }
242                 }
243         }
244 }
245
246 void MeshManagerModel::DeleteMesh(int position){
247         if(position >= 0 && position < _meshes.size()){
248                 delete _meshes.at(position);
249                 _meshes.erase(_meshes.begin() + position);
250                 currentMesh = currentMesh + (position <= currentMesh?-1:0);
251                 if(currentMesh < 0){
252                         currentMesh = 0;
253                 }
254                 RefreshOutputs(true);
255         }
256 }
257
258 void MeshManagerModel::DeleteMeshByName(std::string meshName){
259         if(!_meshes.empty()){
260                 bool found = false;
261                 for(int i = 0; i < _meshes.size() && !found; i++){
262                         if(_meshes.at(i)->GetName() == meshName){
263                                 found = true;
264                                 delete _meshes.at(i);
265                                 _meshes.erase(_meshes.begin() + i);
266                                 currentMesh = currentMesh + (i <= currentMesh?-1:0);
267                                 if(currentMesh < 0){
268                                         currentMesh = 0;
269                                 }
270                                 RefreshOutputs(true);
271                         }
272                 }
273         }
274 }
275
276 void MeshManagerModel::DeleteCurrentMesh(){
277         if(!_meshes.empty()){   
278                 delete _meshes.at(currentMesh);
279                 _meshes.erase(_meshes.begin() + currentMesh);
280                 currentMesh--;
281                 if(currentMesh < 0){
282                         currentMesh = 0;
283                 }
284                 RefreshOutputs(true);
285         }
286 }
287
288 void MeshManagerModel::DeleteAll(){
289         if(!_meshes.empty()){   
290                 for (MeshModel* element : _meshes)
291                 {
292                         delete element;
293                 } 
294                 _meshes.clear();
295                 RefreshOutputs(true);
296         }
297 }
298
299 void MeshManagerModel::NextMesh(){
300         currentMesh++;
301         if(currentMesh >= _meshes.size()){
302                 currentMesh = _meshes.size()-1;
303         }
304         RefreshOutputs(true);
305 }
306
307 void MeshManagerModel::PreviousMesh(){
308         currentMesh--;
309         if(currentMesh < 0){
310                 currentMesh = 0;
311         }
312         RefreshOutputs(true);
313 }
314
315 MeshModel* MeshManagerModel::GetMeshModel(){
316         return _meshes.at(currentMesh);
317 }
318
319 vtkPolyData*  MeshManagerModel::GetMeshBase()
320 {
321         if(!_meshes.empty()){
322                 return _meshes.at(currentMesh)->GetMeshBase();
323         }
324         else{
325                 return NULL;
326         }
327 }
328
329 vtkPolyData*  MeshManagerModel::GetMeshTemp()
330 {
331         if(!_meshes.empty()){
332                 return _meshes.at(currentMesh)->GetMeshTemp();
333         }
334         else{
335                 return NULL;
336         }
337 }
338
339 void MeshManagerModel::SetMeshBase(vtkPolyData* mesh)
340 {
341     if(!_meshes.empty()){
342         _meshes.at(currentMesh)->SetMeshBase(mesh);
343         RefreshOutputs(true);
344     }else{
345         printf("PG MeshManagerModel::SetMeshMemoryMode Mesh vector is empty \n");
346     }
347 }
348
349 void MeshManagerModel::SetMeshMemoryMode(vtkPolyData* mesh)
350 {
351         //if(!_meshes.empty()){
352         if(_meshes.size() > 1){
353                 DeleteAll();
354         }
355         if(_meshes.size() == 0){
356                 AddEmptyMesh_();
357         }
358         _meshes.at(currentMesh)->SetMeshMemoryMode(mesh);
359         RefreshOutputs(true);
360     //}else{
361     //  printf("PG MeshManagerModel::SetMeshMemoryMode Mesh vector is empty \n");
362     //}
363 }
364
365 void MeshManagerModel::ResetMeshTemp()
366 {
367         if(!_meshes.empty()){
368         _meshes.at(currentMesh)->ResetMeshTemp();
369         RefreshOutputs(true);
370     }else{
371         printf("PG MeshManagerModel::ResetMeshTemp Mesh vector is empty \n");
372     }
373     
374 }
375
376 void MeshManagerModel::CopySetMeshBase(vtkPolyData* mesh)
377 {
378         if(!_meshes.empty()){
379                 _meshes.at(currentMesh)->CopySetMeshBase(mesh);
380                 RefreshOutputs(true);
381         }
382         else{
383                 printf("PG MeshManagerModel::CopySetMeshBase Mesh vector is empty \n");
384         }
385 }
386
387 std::vector<std::string> MeshManagerModel::GetMeshNames()
388 {
389         std::vector<std::string> names;
390         for(int i = 0; i < _meshes.size(); i++){
391                 names.push_back(_meshes.at(i)->GetName());
392         }
393         return names;
394 }