Salome HOME
Merge from V6_main (04/10/2012)
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedMesh.cxx
1 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "vtkMedMesh.h"
21
22 #include "vtkObjectFactory.h"
23 #include "vtkSmartPointer.h"
24 #include "vtkDataArray.h"
25
26 #include "vtkMedFamily.h"
27 #include "vtkMedFamilyOnEntity.h"
28 #include "vtkMedGroup.h"
29 #include "vtkMedGrid.h"
30 #include "vtkMedUtilities.h"
31 #include "vtkMedIntArray.h"
32 #include "vtkMedFile.h"
33
34 #include <sstream>
35
36 vtkCxxGetObjectVectorMacro(vtkMedMesh, CellFamily, vtkMedFamily);
37 vtkCxxSetObjectVectorMacro(vtkMedMesh, CellFamily, vtkMedFamily);
38 vtkCxxGetObjectVectorMacro(vtkMedMesh, PointFamily, vtkMedFamily);
39 vtkCxxSetObjectVectorMacro(vtkMedMesh, PointFamily, vtkMedFamily);
40
41 vtkCxxGetObjectVectorMacro(vtkMedMesh, PointGroup, vtkMedGroup);
42 vtkCxxSetObjectVectorMacro(vtkMedMesh, PointGroup, vtkMedGroup);
43 vtkCxxGetObjectVectorMacro(vtkMedMesh, CellGroup, vtkMedGroup);
44 vtkCxxSetObjectVectorMacro(vtkMedMesh, CellGroup, vtkMedGroup);
45
46 vtkCxxSetObjectMacro(vtkMedMesh, ParentFile, vtkMedFile);
47
48 vtkCxxRevisionMacro(vtkMedMesh, "$Revision$")
49 vtkStandardNewMacro(vtkMedMesh)
50
51 vtkMedMesh::vtkMedMesh()
52 {
53   this->GridStep = new vtkMedComputeStepMap<vtkMedGrid> ();
54   this->Name = NULL;
55   this->UniversalName = NULL;
56   this->Description = NULL;
57   this->TimeUnit = NULL;
58   this->CellFamily = new vtkObjectVector<vtkMedFamily> ();
59   this->PointFamily = new vtkObjectVector<vtkMedFamily> ();
60   this->PointGroup = new vtkObjectVector<vtkMedGroup> ();
61   this->CellGroup = new vtkObjectVector<vtkMedGroup> ();
62   this->AxisName = vtkStringArray::New();
63   this->AxisUnit = vtkStringArray::New();
64   this->AxisName->SetNumberOfValues(3);
65   this->AxisUnit->SetNumberOfValues(3);
66   this->MedIterator = -1;
67   this->MeshType = MED_UNDEF_MESH_TYPE;
68   this->StructuredGridType = MED_UNDEF_GRID_TYPE;
69   this->ParentFile = NULL;
70   this->AxisType = MED_CARTESIAN;
71   this->SortingType = MED_SORT_DTIT;
72   this->MeshDimension = 3;
73   this->SpaceDimension = 3;
74   this->IsSupportMesh = 0;
75 }
76
77 vtkMedMesh::~vtkMedMesh()
78 {
79   this->SetName(NULL);
80   this->SetUniversalName(NULL);
81   this->SetDescription(NULL);
82   delete this->CellFamily;
83   delete this->PointFamily;
84   delete this->PointGroup;
85   delete this->CellGroup;
86   this->AxisName->Delete();
87   this->AxisUnit->Delete();
88   delete this->GridStep;
89 }
90
91 vtkMedGroup* vtkMedMesh::GetOrCreateGroup(int pointOrCell, const char* name)
92 {
93   if(pointOrCell == vtkMedUtilities::OnCell)
94     {
95     for(int g = 0; g < this->CellGroup->size(); g++)
96       {
97       vtkMedGroup* group = this->CellGroup->at(g);
98       if(group != NULL && strcmp(name, group->GetName()) == 0)
99         {
100         return group;
101         }
102       }
103     vtkMedGroup* group = vtkMedGroup::New();
104     this->CellGroup->push_back(group);
105     //group->SetPointOrCell(vtkMedUtilities::OnCell);
106     group->SetName(name);
107     group->Delete();
108     return group;
109     }
110   else
111     {
112     for(int g = 0; g < this->PointGroup->size(); g++)
113       {
114       vtkMedGroup* group = this->PointGroup->at(g);
115       if(group != NULL && strcmp(name, group->GetName()) == 0)
116         {
117         return group;
118         }
119       }
120     vtkMedGroup* group = vtkMedGroup::New();
121     this->CellGroup->push_back(group);
122     //group->SetPointOrCell(vtkMedUtilities::OnPoint);
123     group->SetName(name);
124     group->Delete();
125     return group;
126     }
127   return NULL;
128 }
129
130 int vtkMedMesh::GetNumberOfFamily()
131 {
132   return this->GetNumberOfCellFamily() + this->GetNumberOfPointFamily();
133 }
134
135 vtkMedFamily* vtkMedMesh::GetFamily(int index)
136 {
137   if(index < 0)
138     return NULL;
139   if(index < this->GetNumberOfCellFamily())
140     return this->GetCellFamily(index);
141   else if(index < GetNumberOfFamily())
142     return this->GetPointFamily(index - this->GetNumberOfCellFamily());
143   else return NULL;
144 }
145
146 vtkMedFamily* vtkMedMesh::GetOrCreateCellFamilyById(med_int id)
147 {
148   for(int i = 0; i < this->GetNumberOfCellFamily(); i++)
149     {
150     vtkMedFamily* family = this->GetCellFamily(i);
151     if(family->GetId() == id)
152       {
153       return family;
154       }
155     }
156   vtkMedFamily* family = vtkMedFamily::New();
157   family->SetId(id);
158   vtkstd::ostringstream sstr;
159   sstr << "UNDEFINED_CELL_FAMILY_" << id;
160   family->SetName(sstr.str().c_str());
161   family->SetPointOrCell(vtkMedUtilities::OnCell);
162   family->SetMedIterator(-1);
163   this->AppendCellFamily(family);
164   family->Delete();
165   return family;
166 }
167
168 void  vtkMedMesh::SetNumberOfAxis(int naxis)
169 {
170   this->AxisName->SetNumberOfValues(naxis);
171   this->AxisUnit->SetNumberOfValues(naxis);
172 }
173
174 int  vtkMedMesh::GetNumberOfAxis()
175 {
176   return this->AxisName->GetNumberOfValues();
177 }
178
179 vtkMedFamily* vtkMedMesh::GetOrCreatePointFamilyById(med_int id)
180 {
181   for(int i = 0; i < this->GetNumberOfPointFamily(); i++)
182     {
183     vtkMedFamily* family = this->GetPointFamily(i);
184
185     if(family->GetId() == id)
186       return family;
187     }
188   vtkMedFamily* family = vtkMedFamily::New();
189   family->SetId(id);
190   vtkstd::ostringstream sstr;
191   sstr << "UNDEFINED_POINT_FAMILY_" << id;
192   family->SetName(sstr.str().c_str());
193   family->SetPointOrCell(vtkMedUtilities::OnPoint);
194   this->AppendPointFamily(family);
195   family->Delete();
196   return family;
197 }
198
199 void  vtkMedMesh::AddGridStep(vtkMedGrid* grid)
200 {
201   this->GridStep->AddObject(grid->GetComputeStep(), grid);
202 }
203
204 void  vtkMedMesh::ClearGridStep()
205 {
206   this->GridStep->clear();
207 }
208
209 vtkMedGrid* vtkMedMesh::GetGridStep(const vtkMedComputeStep& cs)
210 {
211   return this->GridStep->GetObject(cs);
212 }
213
214 vtkMedGrid* vtkMedMesh::FindGridStep(const vtkMedComputeStep& cs,
215                                      int strategy)
216 {
217   return this->GridStep->FindObject(cs, strategy);
218 }
219
220 void  vtkMedMesh::GatherGridTimes(std::set<med_float>& timeset)
221 {
222   this->GridStep->GatherTimes(timeset);
223 }
224
225 void  vtkMedMesh::GatherGridIterations(med_float time,
226                                        std::set<med_int>& iterationset)
227 {
228   this->GridStep->GatherIterations(time, iterationset);
229 }
230
231 void  vtkMedMesh::ClearMedSupports()
232 {
233   med_int stepnb = this->GridStep->GetNumberOfObject();
234   for(med_int stepid = 0; stepid<stepnb; stepid++ )
235     {
236     vtkMedGrid* grid = this->GridStep->GetObject(stepid);
237     grid->ClearMedSupports();
238     }
239 }
240
241 med_int vtkMedMesh::GetNumberOfGridStep()
242 {
243   return this->GridStep->GetNumberOfObject();
244 }
245
246 vtkMedGrid* vtkMedMesh::GetGridStep(med_int id)
247 {
248   return this->GridStep->GetObject(id);
249 }
250
251 void  vtkMedMesh::GatherMedEntities(std::set<vtkMedEntity>& entities)
252 {
253   vtkMedGrid* firstStep = this->GetGridStep(0);
254   if(firstStep == NULL)
255     return;
256   
257   firstStep->GatherMedEntities(entities);
258 }
259
260 void vtkMedMesh::PrintSelf(ostream& os, vtkIndent indent)
261 {
262   this->Superclass::PrintSelf(os, indent);
263   PRINT_IVAR(os, indent, MedIterator);
264   PRINT_OBJECT_VECTOR(os, indent, CellFamily);
265   PRINT_OBJECT_VECTOR(os, indent, PointFamily);
266   PRINT_OBJECT_VECTOR(os, indent, PointGroup);
267   PRINT_OBJECT_VECTOR(os, indent, CellGroup);
268
269 }