Salome HOME
Merge from V6_main (04/10/2012)
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedCurvilinearGrid.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 "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
106   vtkgrid->SetDimensions(this->GetAxisSize(0),
107                          this->GetAxisSize(1),
108                          this->GetAxisSize(2));
109
110   this->LoadCoordinates();
111
112   if(this->GetDimension() == 3)
113     {
114     vtkgrid->GetPoints()->SetData(this->GetCoordinates());
115     }
116   else
117     {
118     vtkDataArray* coords = vtkDataArray::SafeDownCast(
119         vtkAbstractArray::CreateArray(this->GetCoordinates()->GetDataType()));
120     coords->SetNumberOfComponents(3);
121     coords->SetNumberOfTuples(this->GetNumberOfPoints());
122     vtkgrid->GetPoints()->SetData(coords);
123     coords->Delete();
124
125     med_int npts = this->GetNumberOfPoints();
126     double coord[3] = {0, 0, 0};
127     for(med_int id=0; id<npts; id++)
128       {
129       double * tuple = this->Coordinates->GetTuple(id);
130       for(int dim=0; dim<this->GetDimension(); dim++)
131         {
132         coord[dim] = tuple[dim];
133         }
134       coords->SetTuple(id, coord);
135       }
136     }
137
138   if(foep->GetProfile() != NULL)
139     {
140     foep->GetProfile()->Load();
141     vtkMedIntArray* pids = foep->GetProfile()->GetIds();
142     med_int previd = -1;
143     for(med_int pid=0; pid<pids->GetNumberOfTuples(); pid++)
144       {
145       med_int id = pids->GetValue(pid) - 1;
146       for(med_int theid=previd+1; theid<id; theid++)
147         {
148         vtkgrid->BlankCell(theid);
149         }
150
151       previd = id;
152       }
153     }
154
155   if(foep->GetFamilyOnEntity()->GetEntityArray()->GetNumberOfFamilyOnEntity() > 1)
156     {
157     med_int famid = foep->GetFamilyOnEntity()->GetFamily()->GetId();
158     vtkMedEntityArray* ea = foep->GetFamilyOnEntity()->GetEntityArray();
159     for(med_int id=0; id<vtkgrid->GetNumberOfCells(); id++)
160       {
161       if(ea->GetFamilyId(id) != famid)
162         vtkgrid->BlankCell(id);
163       }
164     }
165
166   return vtkgrid;
167 }
168
169 void vtkMedCurvilinearGrid::PrintSelf(ostream& os, vtkIndent indent)
170 {
171   this->Superclass::PrintSelf(os, indent);
172 }