Salome HOME
CMake: replacing CMAKE_* variables by PROJECT_* variabls
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedCurvilinearGrid.cxx
1 // Copyright (C) 2010-2013  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 "vtkMedCurvilinearGrid.h"
21
22 #include "vtkMedMesh.h"
23 #include "vtkMedFile.h"
24 #include "vtkMedDriver.h"
25 #include "vtkMedFamilyOnEntityOnProfile.h"
26 #include "vtkMedFamilyOnEntity.h"
27 #include "vtkMedEntityArray.h"
28 #include "vtkMedProfile.h"
29 #include "vtkMedFamily.h"
30
31 #include "vtkObjectFactory.h"
32 #include "vtkDataArray.h"
33 #include "vtkStructuredGrid.h"
34
35 vtkCxxSetObjectMacro(vtkMedCurvilinearGrid, Coordinates, vtkDataArray);
36
37 // vtkCxxRevisionMacro(vtkMedCurvilinearGrid, "$Revision$")
38 vtkStandardNewMacro(vtkMedCurvilinearGrid)
39
40 vtkMedCurvilinearGrid::vtkMedCurvilinearGrid()
41 {
42   this->Coordinates = NULL;
43   this->NumberOfPoints = 0;
44 }
45
46 vtkMedCurvilinearGrid::~vtkMedCurvilinearGrid()
47 {
48   this->SetCoordinates(NULL);
49 }
50
51 void vtkMedCurvilinearGrid::SetDimension(int dim)
52 {
53   this->AxisSize.resize(dim);
54 }
55
56 int vtkMedCurvilinearGrid::GetDimension()
57 {
58   return this->AxisSize.size();
59 }
60
61 void  vtkMedCurvilinearGrid::SetAxisSize(int axis, med_int size)
62 {
63   if(axis < 0)
64     return;
65
66   if(axis >= this->AxisSize.size())
67     this->AxisSize.resize(axis+1);
68
69   this->AxisSize[axis] = size;
70 }
71
72 med_int vtkMedCurvilinearGrid::GetAxisSize(int axis)
73 {
74   if(axis < 0 || axis >= this->AxisSize.size())
75     return 0;
76
77   return this->AxisSize[axis];
78 }
79
80 void  vtkMedCurvilinearGrid::LoadCoordinates()
81 {
82   vtkMedDriver* driver = this->GetParentMesh()->GetParentFile()->GetMedDriver();
83   driver->LoadCoordinates(this);
84 }
85
86 int vtkMedCurvilinearGrid::IsCoordinatesLoaded()
87 {
88   return this->Coordinates != NULL && this->Coordinates->GetNumberOfTuples()
89      == this->NumberOfPoints;
90 }
91
92 double* vtkMedCurvilinearGrid::GetCoordTuple(med_int index)
93 {
94   return this->Coordinates->GetTuple(index);
95 }
96
97 vtkDataSet* vtkMedCurvilinearGrid::CreateVTKDataSet(
98     vtkMedFamilyOnEntityOnProfile* foep)
99 {
100   vtkStructuredGrid* vtkgrid = vtkStructuredGrid::New();
101
102   vtkPoints* points = vtkPoints::New();
103   vtkgrid->SetPoints(points);
104   points->Delete();
105   vtkgrid->SetDimensions(this->GetAxisSize(0),
106                          this->GetAxisSize(1),
107                          this->GetAxisSize(2));
108
109   this->LoadCoordinates();
110
111   if(this->GetDimension() == 3)
112     {
113     vtkgrid->GetPoints()->SetData(this->GetCoordinates());
114     }
115   else
116     {
117     if(this->GetDimension() == 1)
118       {
119       vtkgrid->SetDimensions(this->GetAxisSize(0),
120                              1,
121                              1);
122       }
123
124     if(this->GetDimension() == 2)
125       {
126       vtkgrid->SetDimensions(this->GetAxisSize(0),
127                              this->GetAxisSize(1),
128                              1);
129       }
130
131     vtkDataArray* coords = vtkDataArray::SafeDownCast(
132         vtkAbstractArray::CreateArray(this->GetCoordinates()->GetDataType()));
133     coords->SetNumberOfComponents(3);
134     coords->SetNumberOfTuples(this->GetNumberOfPoints());
135     vtkgrid->GetPoints()->SetData(coords);
136     coords->Delete();
137
138     med_int npts = this->GetNumberOfPoints();
139     double coord[3] = {0, 0, 0};
140     for(med_int id=0; id<npts; id++)
141       {
142       double * tuple = this->Coordinates->GetTuple(id);
143       for(int dim=0; dim<this->GetDimension(); dim++)
144         {
145         coord[dim] = tuple[dim];
146         }
147       coords->SetTuple(id, coord);
148       }
149     }
150
151   if(foep->GetProfile() != NULL)
152     {
153     foep->GetProfile()->Load();
154     vtkMedIntArray* pids = foep->GetProfile()->GetIds();
155     med_int previd = -1;
156     for(med_int pid=0; pid<pids->GetNumberOfTuples(); pid++)
157       {
158       med_int id = pids->GetValue(pid) - 1;
159       for(med_int theid=previd+1; theid<id; theid++)
160         {
161         vtkgrid->BlankCell(theid);
162         }
163
164       previd = id;
165       }
166     }
167
168   if(foep->GetFamilyOnEntity()->GetEntityArray()->GetNumberOfFamilyOnEntity() > 1)
169     {
170     med_int famid = foep->GetFamilyOnEntity()->GetFamily()->GetId();
171     vtkMedEntityArray* ea = foep->GetFamilyOnEntity()->GetEntityArray();
172     for(med_int id=0; id<vtkgrid->GetNumberOfCells(); id++)
173       {
174       if(ea->GetFamilyId(id) != famid)
175         vtkgrid->BlankCell(id);
176       }
177     }
178
179   return vtkgrid;
180 }
181
182 void vtkMedCurvilinearGrid::PrintSelf(ostream& os, vtkIndent indent)
183 {
184   this->Superclass::PrintSelf(os, indent);
185 }