Salome HOME
Merge branch 'abn/rearch'
[modules/paravis.git] / src / Plugins / MEDReader / IO / vtkGenerateVectors.cxx
1 // Copyright (C) 2010-2014  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, or (at your option) any later version.
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 // Author : Anthony Geay
20
21 #include "vtkGenerateVectors.h"
22 #include "vtkDoubleArray.h"
23 #include "vtkInformation.h"
24 #include "vtkQuadratureSchemeDefinition.h"
25 #include "vtkInformationQuadratureSchemeDefinitionVectorKey.h"
26 #include "MEDUtilities.hxx"
27 #include "vtkFieldData.h"
28
29 #include <sstream>
30
31 const char vtkGenerateVectors::VECTOR_SUFFIX[]="_Vector";
32
33 std::string vtkGenerateVectors::SuffixFieldName(const std::string& name)
34 {
35   std::ostringstream oss; oss << name << VECTOR_SUFFIX;
36   return oss.str();
37 }
38
39 void vtkGenerateVectors::Operate(vtkFieldData *fd)
40 {
41   if(!fd)
42     return ;
43   const int nbOfArrs(fd->GetNumberOfArrays());
44   std::vector<vtkDoubleArray *> daToAppend;
45   for(int i=0;i<nbOfArrs;i++)
46     {
47       vtkDataArray *arr(fd->GetArray(i));
48       if(!arr)
49         continue;
50       vtkDoubleArray *arrc(vtkDoubleArray::SafeDownCast(arr));
51       if(!arrc)
52         continue;
53       int nbOfCompo(arrc->GetNumberOfComponents());
54       if(nbOfCompo<=1 || nbOfCompo==3)
55         continue;
56       if(nbOfCompo==2)
57         daToAppend.push_back(Operate2Compo(arrc));
58       else
59         daToAppend.push_back(OperateMoreThan3Compo(arrc));
60     }
61   for(std::vector<vtkDoubleArray *>::const_iterator it=daToAppend.begin();it!=daToAppend.end();it++)
62     {
63       vtkDoubleArray *elt(*it);
64       if(!elt)
65         continue;
66       fd->AddArray(elt);
67       elt->Delete();
68     }
69 }
70
71 vtkDoubleArray *vtkGenerateVectors::Operate2Compo(vtkDoubleArray *oldArr)
72 {
73   static const int VTK_DATA_ARRAY_FREE=vtkDataArrayTemplate<double>::VTK_DATA_ARRAY_FREE;
74   vtkDoubleArray *ret(vtkDoubleArray::New());
75   std::size_t nbOfTuples(oldArr->GetNumberOfTuples());
76   const double *inPt(oldArr->GetPointer(0));
77   double *pt((double *)malloc(nbOfTuples*3*sizeof(double)));
78   for(std::size_t i=0;i<nbOfTuples;i++)
79     {
80       pt[3*i+0]=inPt[2*i+0];
81       pt[3*i+1]=inPt[2*i+1];
82       pt[3*i+2]=0.;
83     }
84   ret->SetNumberOfComponents(3);
85   std::string newName(SuffixFieldName(oldArr->GetName()));
86   ret->SetName(newName.c_str());
87   ret->SetComponentName(0,oldArr->GetComponentName(0));
88   ret->SetComponentName(1,oldArr->GetComponentName(1));
89   ret->SetArray(pt,3*nbOfTuples,0,VTK_DATA_ARRAY_FREE);
90   UpdateInformationOfArray(oldArr,ret);
91   return ret;
92 }
93
94 vtkDoubleArray *vtkGenerateVectors::OperateMoreThan3Compo(vtkDoubleArray *oldArr)
95 {
96   static const int VTK_DATA_ARRAY_FREE=vtkDataArrayTemplate<double>::VTK_DATA_ARRAY_FREE;
97   vtkDoubleArray *ret(vtkDoubleArray::New());
98   int nbOfCompo(oldArr->GetNumberOfComponents());
99   std::size_t nbOfTuples(oldArr->GetNumberOfTuples());
100   const double *inPt(oldArr->GetPointer(0));
101   double *pt((double *)malloc(nbOfTuples*3*sizeof(double)));
102   for(std::size_t i=0;i<nbOfTuples;i++)
103     {
104       pt[3*i+0]=inPt[nbOfCompo*i+0];
105       pt[3*i+1]=inPt[nbOfCompo*i+1];
106       pt[3*i+2]=inPt[nbOfCompo*i+2];
107     }
108   ret->SetNumberOfComponents(3);
109   std::string newName(SuffixFieldName(oldArr->GetName()));
110   ret->SetName(newName.c_str());
111   ret->SetComponentName(0,oldArr->GetComponentName(0));
112   ret->SetComponentName(1,oldArr->GetComponentName(1));
113   ret->SetComponentName(2,oldArr->GetComponentName(2));
114   ret->SetArray(pt,3*nbOfTuples,0,VTK_DATA_ARRAY_FREE);
115   UpdateInformationOfArray(oldArr,ret);
116   return ret;
117 }
118
119 void vtkGenerateVectors::UpdateInformationOfArray(vtkDoubleArray *oldArr, vtkDoubleArray *arr)
120 {
121   if(oldArr->GetInformation()->Has(vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME()))
122     {
123       arr->GetInformation()->Set(vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME(),oldArr->GetInformation()->Get((vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME())));
124     }
125   if(oldArr->GetInformation()->Get(MEDUtilities::ELGA()))
126     arr->GetInformation()->Set(MEDUtilities::ELGA(),1);
127   vtkInformationQuadratureSchemeDefinitionVectorKey *key(vtkQuadratureSchemeDefinition::DICTIONARY());
128   if(key->Has(oldArr->GetInformation()))
129     {
130       int dictSize(key->Size(oldArr->GetInformation()));
131       vtkQuadratureSchemeDefinition **dict = new vtkQuadratureSchemeDefinition *[dictSize];
132       key->GetRange(oldArr->GetInformation(),dict,0,0,dictSize);
133       key->SetRange(arr->GetInformation(),dict,0,0,dictSize);
134       delete [] dict;
135     }
136   if(oldArr->GetInformation()->Get(MEDUtilities::ELNO()))
137     arr->GetInformation()->Set(MEDUtilities::ELNO(),1);
138 }