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