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