Salome HOME
7943fd1a9246cd02fca602ae807846985500e436
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkGenerateStructElement.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 "vtkGenerateStructElement.h"
21
22 #include "vtkObjectFactory.h"
23 #include "vtkInformation.h"
24 #include "vtkInformationVector.h"
25 #include "vtkUnstructuredGrid.h"
26 #include "vtkCellData.h"
27 #include "vtkIdTypeArray.h"
28
29 #include "vtkMedUtilities.h"
30 #include "vtkMedStructElement.h"
31 #include "vtkMedConstantAttribute.h"
32 #include "vtkMedVariableAttribute.h"
33
34 class vtkGenerateStructElementCache
35 {
36 public :
37   vtkGenerateStructElementCache(vtkMedStructElement* strelem, vtkUnstructuredGrid* ug)
38     {
39     for(int attid = 0; attid < strelem->GetNumberOfConstantAttribute(); attid++)
40       {
41       vtkMedConstantAttribute* att = strelem->GetConstantAttribute(attid);
42       this->cstAttribute[att->GetName()] = att;
43       }
44
45     for(int attid = 0; attid < strelem->GetNumberOfVariableAttribute(); attid++)
46       {
47       vtkMedVariableAttribute* att = strelem->GetVariableAttribute(attid);
48       vtkDataArray* array = ug->GetFieldData()->GetArray(att->GetName());
49       if(array != NULL)
50         this->varAttribute[att->GetName()] = array;
51       }
52     }
53
54   bool HasParameter(const char* name)
55     {
56     return this->cstAttribute.find(name) != this->cstAttribute.end()
57         || this->varAttribute.find(name) != this->varAttribute.end();
58     }
59
60   double GetParameter1(const char* name, vtkIdType id)
61     {
62     if(this->cstAttribute.find(name) != this->cstAttribute.end())
63       {
64       vtkMedConstantAttribute* att = this->cstAttribute[name];
65       return att->GetValues()->GetVariantValue(0).ToDouble();
66       }
67     if(this->varAttribute.find(name) != this->varAttribute.end())
68       {
69       vtkDataArray* array = this->varAttribute[name];
70       return array->GetTuple1(id);
71       }
72     return 0.0;
73     }
74
75 protected :
76   std::map<std::string, vtkMedConstantAttribute*> cstAttribute;
77   std::map<std::string, vtkDataArray*> varAttribute;
78 };
79
80 // vtkCxxRevisionMacro(vtkGenerateStructElement, "$Revision$");
81 vtkStandardNewMacro(vtkGenerateStructElement);
82
83 vtkGenerateStructElement::vtkGenerateStructElement()
84 {
85
86 }
87
88 vtkGenerateStructElement::~vtkGenerateStructElement()
89 {
90
91 }
92
93 int vtkGenerateStructElement::RequestData(vtkInformation* request,
94                           vtkInformationVector** inputVector,
95                           vtkInformationVector* outputVector)
96 {
97   vtkInformation* outInfo=outputVector->GetInformationObject(0);
98
99   vtkInformation* inputInfo=inputVector[0]->GetInformationObject(0);
100
101   vtkUnstructuredGrid* inug = vtkUnstructuredGrid::SafeDownCast(
102       inputInfo->Get(vtkDataObject::DATA_OBJECT()));
103
104   vtkUnstructuredGrid* outug = vtkUnstructuredGrid::SafeDownCast(
105       outInfo->Get(vtkDataObject::DATA_OBJECT()));
106   outug->Initialize();
107
108   vtkMedStructElement* strelem = vtkMedStructElement::SafeDownCast(
109       inug->GetInformation()->Get(vtkMedUtilities::STRUCT_ELEMENT()));
110
111   std::cout << "Inside vtkGenerateStructElement::RequestData" << std::endl;
112
113   if(strelem == NULL)
114     {
115     vtkDebugMacro("vtkGenerateStructElement needs a vtkMedStructElement information");
116     return 1;
117     }
118
119   vtkIdTypeArray* strelemindex = vtkIdTypeArray::SafeDownCast(
120       inug->GetCellData()->GetArray("STRUCT_ELEMENT_INDEX"));
121   if(strelemindex == NULL)
122     {
123     vtkDebugMacro("vtkGenerateStructElement needs some information on the structural elements");
124     return 1;
125     }
126
127   // loop over input cells.
128   // struct elements support are a set cells of same type following each other.
129   vtkIdType medid = -1;
130   vtkGenerateStructElementCache cache(strelem, inug);
131
132   std::string name = strelem->GetName();
133
134   if(name == MED_BALL_NAME)
135     {
136     std::cout << "vtkGenerateStructElement::RequestData ...  if(name == MED_BALL_NAME)" << std::endl;
137     // sanity check : is the diameter defined?
138     if(!cache.HasParameter(MED_BALL_DIAMETER))
139       {
140       vtkErrorMacro("MED_BALL elements need a diameter");
141       return 1;
142       }
143     std::cout << "inug->GetNumberOfCells() = " << inug->GetNumberOfCells() << std::endl;
144     for(vtkIdType cellId = 0; cellId < inug->GetNumberOfCells(); cellId++)
145       {
146       vtkIdType ballMedId = strelemindex->GetValue(2*cellId);
147       double balldiam = this->GetParameter1(MED_BALL_DIAMETER, ballMedId, cache);
148       std::cout << balldiam << " - ";
149       //TODO
150       //this->GenerateBall(inug, cellId, balldiam, outug);
151       }
152     std::cout << std::endl;
153     }
154   else if(name == MED_PARTICLE_NAME)
155     {
156     bool hasLabel = cache.HasParameter(MED_PARTICLE_LABEL);
157     for(vtkIdType cellId = 0; cellId < inug->GetNumberOfCells(); cellId++)
158       {
159       if(hasLabel)
160         {
161         vtkIdType particleMedId = strelemindex->GetValue(2*cellId);
162         double particlelabel = this->GetParameter1(MED_PARTICLE_LABEL, particleMedId, cache);
163
164         //TODO
165         //  this->GenerateParticle(inug, cellId, particlelabel, outug);
166         }
167       else
168         {
169         //TODO
170         //  this->GenerateParticle(inug, cellId, outug);
171         }
172       }
173     }
174   else if(name == MED_BEAM_NAME)
175     {
176     // sanity check : is the diameter defined?
177     if(!cache.HasParameter(MED_BEAM_THICKNESS))
178       {
179       vtkErrorMacro("MED_BEAM elements need a thickness");
180       return 1;
181       }
182     for(vtkIdType cellId = 0; cellId < inug->GetNumberOfCells(); cellId++)
183       {
184       vtkIdType cellmedid = strelemindex->GetValue(2*cellId);
185       if(cellmedid != medid)
186         {
187         // this means that a new beam begins
188         medid = cellmedid;
189         double thickness = this->GetParameter1(MED_BEAM_THICKNESS, medid, cache);
190
191         //TODO : generate a beam.
192         // rem : a beam can span several segments.
193
194         }
195       }
196     }
197   else
198     {
199     vtkErrorMacro("structural elements of type " << name << " are not supported");
200     }
201
202   return 1;
203 }
204
205 double  vtkGenerateStructElement::GetParameter1(const char* name,
206                               vtkIdType medid,
207                               vtkGenerateStructElementCache& cache)
208 {
209 #ifdef WIN32 //rnv : Avoid compliation error in the VS under windows.
210   return 0;
211 #endif
212 }
213
214 void  vtkGenerateStructElement::PrintSelf(ostream& os, vtkIndent indent)
215 {
216   this->Superclass::PrintSelf(os, indent);
217 }