Salome HOME
Merge branch 'origin/abn/newpy_pv4-1'
[modules/paravis.git] / src / Plugins / MEDReader / IO / vtkELNOFilter.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.
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 "vtkELNOFilter.h"
21 #include "vtkInformation.h"
22 #include "vtkInformationVector.h"
23 #include "vtkObjectFactory.h"
24 #include "vtkPolyDataAlgorithm.h"
25 #include "vtkPolyData.h"
26 #include "vtkIdTypeArray.h"
27 #include "vtkInformationQuadratureSchemeDefinitionVectorKey.h"
28 #include "vtkQuadratureSchemeDefinition.h"
29 #include "vtkUnstructuredGrid.h"
30
31 //vtkCxxRevisionMacro(vtkELNOFilter, "$Revision: 1.2.2.2 $");
32 vtkStandardNewMacro(vtkELNOFilter);
33
34 vtkELNOFilter::vtkELNOFilter()
35 {
36   this->ShrinkFactor = 0.5;
37 }
38
39 vtkELNOFilter::~vtkELNOFilter()
40 {
41 }
42
43 int vtkELNOFilter::RequestData(vtkInformation *request,
44     vtkInformationVector **input, vtkInformationVector *output)
45 {
46   vtkUnstructuredGrid *usgIn = vtkUnstructuredGrid::SafeDownCast(
47       input[0]->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
48
49   vtkPolyData *pdOut = vtkPolyData::SafeDownCast(
50       output->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
51
52   vtkDataArray* array = this->GetInputArrayToProcess(0, input);
53   vtkIdTypeArray* offsets = vtkIdTypeArray::SafeDownCast(
54       this->GetInputArrayToProcess(0, input));
55
56   if(usgIn == NULL || offsets == NULL || pdOut == NULL)
57     {
58     vtkDebugMacro("vtkELNOFilter no correctly configured : offsets = " << offsets);
59     return 1;
60     }
61
62   vtkInformation *info = offsets->GetInformation();
63   vtkInformationQuadratureSchemeDefinitionVectorKey *key =
64       vtkQuadratureSchemeDefinition::DICTIONARY();
65   if(!key->Has(info))
66     {
67     vtkDebugMacro("Dictionary is not present in array " << offsets->GetName() << " " << offsets << " Aborting." );
68     return 1;
69     }
70
71   int res = this->Superclass::RequestData(request, input, output);
72   if(res == 0)
73     {
74     return 0;
75     }
76
77   int dictSize = key->Size(info);
78   vtkQuadratureSchemeDefinition **dict =
79       new vtkQuadratureSchemeDefinition *[dictSize];
80   key->GetRange(info, dict, 0, 0, dictSize);
81
82   vtkIdType ncell = usgIn->GetNumberOfCells();
83   vtkPoints *points = pdOut->GetPoints();
84   vtkIdType start = 0;
85   for(vtkIdType cellId = 0; cellId < ncell; cellId++)
86     {
87     vtkIdType offset = offsets->GetValue(cellId);
88     int cellType = usgIn->GetCellType(cellId);
89     // a simple check to see if a scheme really exists for this cell type.
90     // should not happen if the cell type has not been modified.
91     if(dict[cellType] == NULL)
92       continue;
93     int np = dict[cellType]->GetNumberOfQuadraturePoints();
94     double center[3] = {0, 0, 0};
95     for(int id = start; id < start + np; id++)
96       {
97       double *position = points->GetPoint(id);
98       center[0] += position[0];
99       center[1] += position[1];
100       center[2] += position[2];
101       }
102     center[0] /= np;
103     center[1] /= np;
104     center[2] /= np;
105     for(int id = start; id < start + np; id++)
106       {
107       double *position = points->GetPoint(id);
108       double newpos[3];
109       newpos[0] = position[0] * this->ShrinkFactor + center[0] * (1
110           - this->ShrinkFactor);
111       newpos[1] = position[1] * this->ShrinkFactor + center[1] * (1
112           - this->ShrinkFactor);
113       newpos[2] = position[2] * this->ShrinkFactor + center[2] * (1
114           - this->ShrinkFactor);
115       points->SetPoint(id, newpos);
116       }
117     start += np;
118     }
119
120   return 1;
121 }
122
123 void vtkELNOFilter::PrintSelf(ostream& os, vtkIndent indent)
124 {
125   this->Superclass::PrintSelf(os, indent);
126
127   os << indent << "ShrinkFactor : " << this->ShrinkFactor << endl;
128 }