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