Salome HOME
Copyright update 2022
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkELNOSurfaceFilter.cxx
1 // Copyright (C) 2010-2022  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
20 #include "vtkELNOSurfaceFilter.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 #include "vtkPVGeometryFilter.h"
31 #include "vtkShrinkFilter.h"
32 #include "vtkSmartPointer.h"
33 #include "vtkPointData.h"
34 #include "vtkCellData.h"
35 #include "vtkIdList.h"
36
37 //vtkCxxRevisionMacro(vtkELNOSurfaceFilter, "$Revision$")
38 //;
39 vtkStandardNewMacro(vtkELNOSurfaceFilter)
40
41 vtkELNOSurfaceFilter::vtkELNOSurfaceFilter()
42 {
43 }
44
45 vtkELNOSurfaceFilter::~vtkELNOSurfaceFilter()
46 {
47 }
48
49 int vtkELNOSurfaceFilter::RequestData(vtkInformation * /*request*/, vtkInformationVector **input, vtkInformationVector *output)
50 {
51   vtkUnstructuredGrid *usgIn=vtkUnstructuredGrid::SafeDownCast(
52       input[0]->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
53
54   vtkUnstructuredGrid *usgOut=vtkUnstructuredGrid::SafeDownCast(
55       output->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));
56
57   vtkIdTypeArray* usg_offsets=vtkIdTypeArray::SafeDownCast(
58       this->GetInputArrayToProcess(0, input));
59
60   if(usgIn==NULL||usg_offsets==NULL||usgOut==NULL)
61     {
62     vtkDebugMacro("vtkELNOSurfaceFilter no correctly configured : offsets = " << usg_offsets);
63     return 1;
64     }
65
66   // first shrink the input
67   vtkUnstructuredGrid* usgInClone=usgIn->NewInstance();
68
69   usgInClone->ShallowCopy(usgIn);
70
71   vtkSmartPointer<vtkPVGeometryFilter> geomFilter=vtkSmartPointer<
72       vtkPVGeometryFilter>::New();
73   geomFilter->SetInputData(usgInClone);
74   geomFilter->SetPassThroughCellIds(1);
75   geomFilter->SetPassThroughPointIds(1);
76   geomFilter->SetUseOutline(0);
77   geomFilter->Update();
78
79   vtkPolyData* surface=vtkPolyData::SafeDownCast(geomFilter->GetOutput());
80   vtkIdTypeArray* originalCellIds=vtkIdTypeArray::SafeDownCast(
81       surface->GetCellData()->GetArray("vtkOriginalCellIds"));
82   vtkIdTypeArray* originalPointIds=vtkIdTypeArray::SafeDownCast(
83       surface->GetPointData()->GetArray("vtkOriginalPointIds"));
84
85   if( originalCellIds == NULL )
86   {
87     vtkErrorMacro("vtkPVGeometryFilter return NULL 'vtkOriginalCellIds' array");
88     return 0;
89   }
90
91   if(originalPointIds==NULL)
92   {
93     vtkErrorMacro("It appears that your dataset is not reduced using vtkPVGeometryFilter (NULL 'vtkOriginalPointIds).\n==================================================================================================\nProbably your dataset is not 3D.\nIf it is not a 3D dataset you are expected to use ELNO Mesh filter instead of ELNO Surface filter.\n==================================================================================================\n");
94     return 0;
95   }
96
97   vtkSmartPointer<vtkShrinkFilter> shrink=
98       vtkSmartPointer<vtkShrinkFilter>::New();
99   shrink->SetInputConnection(geomFilter->GetOutputPort(0));
100   shrink->SetShrinkFactor(0.9999);
101   shrink->Update();
102
103   vtkUnstructuredGrid* shrinked=shrink->GetOutput();
104
105   usgInClone->Delete();
106
107   usgOut->ShallowCopy(shrinked);
108
109   vtkIdTypeArray* offsets=vtkIdTypeArray::SafeDownCast(
110       shrinked->GetCellData()->GetArray(usg_offsets->GetName()));
111
112   // now copy ELNO data. Start by verifying if it is possible to
113   // shallow copy the array.
114   vtkInformation *info=offsets->GetInformation();
115   vtkInformationQuadratureSchemeDefinitionVectorKey *key=
116       vtkQuadratureSchemeDefinition::DICTIONARY();
117   if(!key->Has(info))
118     {
119     vtkDebugMacro("Dictionary is not present in array " << offsets->GetName()
120                   << " " << offsets << " Aborting." );
121     return 0;
122     }
123   int dictSize=key->Size(info);
124   vtkQuadratureSchemeDefinition **dict=
125       new vtkQuadratureSchemeDefinition *[dictSize];
126   key->GetRange(info, dict, 0, 0, dictSize);
127
128   vtkIdType ncell=shrinked->GetNumberOfCells();
129
130   vtkFieldData* fielddata=usgIn->GetFieldData();
131   vtkIdList *ids=vtkIdList::New();
132   vtkIdList *surfaceIds=vtkIdList::New();
133   vtkIdList *originalIds=vtkIdList::New();
134   for(int index=0; index<fielddata->GetNumberOfArrays(); index++)
135     {
136     vtkDataArray* data=fielddata->GetArray(index);
137     if(data==NULL)
138       continue;
139
140     vtkInformation* info=data->GetInformation();
141     const char* arrayOffsetName=info->Get(
142         vtkQuadratureSchemeDefinition::QUADRATURE_OFFSET_ARRAY_NAME());
143
144     if(arrayOffsetName == NULL ||
145        strcmp(arrayOffsetName, offsets->GetName())!=0)
146       {
147       usgOut->GetFieldData()->AddArray(data);
148
149       continue;
150       }
151
152     vtkDataArray* newArray=data->NewInstance();
153     newArray->SetName(data->GetName());
154     usgOut->GetPointData()->AddArray(newArray);
155     newArray->SetNumberOfComponents(data->GetNumberOfComponents());
156     newArray->SetNumberOfTuples(usgOut->GetNumberOfPoints());
157     newArray->CopyComponentNames(data);
158     newArray->Delete();
159
160     for(vtkIdType cellId=0; cellId<ncell; cellId++)
161       {
162       vtkIdType offset=offsets->GetValue(cellId);
163
164       vtkIdType originalCellId=originalCellIds->GetValue(cellId);
165       int originalCellType=usgIn->GetCellType(originalCellId);
166
167       shrinked->GetCellPoints(cellId, ids);
168       surface->GetCellPoints(cellId, surfaceIds);
169
170       for(int id=0; id<ids->GetNumberOfIds(); id++)
171         {
172         vtkIdType surfaceId=surfaceIds->GetId(id);
173         vtkIdType shrinkedId=ids->GetId(id);
174         vtkIdType originalPointId = originalPointIds->GetValue(surfaceId);
175
176         usgIn->GetCellPoints(originalCellId, originalIds);
177         int originalLocalId=-1;
178         for(int li=0; li<originalIds->GetNumberOfIds(); li++)
179           {
180           if(originalPointId==originalIds->GetId(li))
181             {
182             originalLocalId=li;
183             break;
184             }
185           }
186         if(originalLocalId==-1)
187           {
188           originalLocalId=0;
189           vtkErrorMacro("cannot find original id");
190           }
191
192         const double * w=dict[originalCellType]->GetShapeFunctionWeights(
193             originalLocalId);
194         int j;
195         for(j=0; j<dict[originalCellType]->GetNumberOfNodes(); j++)
196           {
197           if(w[j]==1.0)
198             break;
199           }
200         if(j==dict[originalCellType]->GetNumberOfNodes())
201           {
202             //vtkErrorMacro("cannot find elno weigth.");
203           j=id;
204           }
205         newArray->SetTuple(shrinkedId, offset+j, data);
206         }
207       }
208     }
209
210   ids->FastDelete();
211   surfaceIds->FastDelete();
212   originalIds->FastDelete();
213   delete[] dict;
214
215   return 1;
216 }
217
218 void vtkELNOSurfaceFilter::PrintSelf(ostream& os, vtkIndent indent)
219 {
220   this->Superclass::PrintSelf(os, indent);
221 }