/
FemPostPipeline.cpp
373 lines (297 loc) · 14.8 KB
/
FemPostPipeline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/***************************************************************************
* Copyright (c) 2015 Stefan Tröger <stefantroeger@gmx.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#endif
#include "FemPostPipeline.h"
#include "FemMesh.h"
#include "FemMeshObject.h"
#include <Base/Console.h>
#include <App/Document.h>
#include <SMESH_Mesh.hxx>
#include <App/DocumentObjectPy.h>
#include <vtkDataSetReader.h>
#include <vtkGeometryFilter.h>
#include <vtkPointData.h>
#include <vtkStructuredGrid.h>
#include <vtkCellData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkTetra.h>
#include <vtkQuadraticTetra.h>
#include <vtkTriangle.h>
#include <vtkQuadraticTriangle.h>
#include <vtkQuad.h>
#include <vtkImageData.h>
#include <vtkRectilinearGrid.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLStructuredGridReader.h>
#include <vtkXMLRectilinearGridReader.h>
#include <vtkXMLImageDataReader.h>
using namespace Fem;
using namespace App;
PROPERTY_SOURCE(Fem::FemPostPipeline, Fem::FemPostObject)
const char* FemPostPipeline::ModeEnums[]= {"Serial","Parallel",NULL};
FemPostPipeline::FemPostPipeline()
{
ADD_PROPERTY_TYPE(Filter, (0), "Pipeline", App::Prop_None, "The filter used in in this pipeline");
ADD_PROPERTY_TYPE(Functions, (0), "Pipeline", App::Prop_Hidden, "The function provider which groups all pipeline functions");
ADD_PROPERTY_TYPE(Mode,(long(0)), "Pipeline", App::Prop_None, "Selects the pipeline data transition mode. In serial every filter"
"gets the output of the previous one as input, in parrallel every"
"filter gets the pipelien source as input.");
Mode.setEnums(ModeEnums);
}
FemPostPipeline::~FemPostPipeline()
{
}
short FemPostPipeline::mustExecute(void) const
{
return 1;
}
DocumentObjectExecReturn* FemPostPipeline::execute(void) {
//if we are in serial mode we just copy over the data of the last filter,
//but if we are in parallel we need to combine all filter results
return Fem::FemPostObject::execute();
}
bool FemPostPipeline::canRead(Base::FileInfo File) {
if (File.hasExtension("vtk") ||
File.hasExtension("vtp") ||
File.hasExtension("vts") ||
File.hasExtension("vtr") ||
File.hasExtension("vtu") ||
File.hasExtension("vti"))
return true;
return false;
}
void FemPostPipeline::read(Base::FileInfo File) {
// checking on the file
if (!File.isReadable())
throw Base::Exception("File to load not existing or not readable");
if (File.hasExtension("vtu"))
readXMLFile<vtkXMLUnstructuredGridReader>(File.filePath());
else if (File.hasExtension("vtp"))
readXMLFile<vtkXMLPolyDataReader>(File.filePath());
else if (File.hasExtension("vts"))
readXMLFile<vtkXMLStructuredGridReader>(File.filePath());
else if (File.hasExtension("vtr"))
readXMLFile<vtkXMLRectilinearGridReader>(File.filePath());
else if (File.hasExtension("vti"))
readXMLFile<vtkXMLImageDataReader>(File.filePath());
else if (File.hasExtension("vtk"))
readXMLFile<vtkDataSetReader>(File.filePath());
else
throw Base::Exception("Unknown extension");
}
// PyObject *FemPostPipeline::getPyObject()
// {
// if (PythonObject.is(Py::_None())){
// // ref counter is set to 1
// PythonObject = Py::Object(new DocumentObjectPy(this),true);
// }
// return Py::new_reference_to(PythonObject);
// }
void FemPostPipeline::onChanged(const Property* prop)
{
if(prop == &Filter || prop == &Mode) {
//we check if all connections are right and add new ones if needed
std::vector<App::DocumentObject*> objs = Filter.getValues();
if(objs.empty())
return;
std::vector<App::DocumentObject*>::iterator it = objs.begin();
FemPostFilter* filter = static_cast<FemPostFilter*>(*it);
//If we have a Input we need to ensure our filters are connected correctly
if(Input.getValue()) {
//the first filter is always connected to the input
if(filter->Input.getValue() != Input.getValue())
filter->Input.setValue(Input.getValue());
//all the others need to be connected to the previous filter or the source, dependend on the mode
++it;
for(; it != objs.end(); ++it) {
FemPostFilter* nextFilter = static_cast<FemPostFilter*>(*it);
if(Mode.getValue() == 0) { //serial mode
if( nextFilter->Input.getValue() != filter)
nextFilter->Input.setValue(filter);
}
else { //Parallel mode
if( nextFilter->Input.getValue() != Input.getValue())
nextFilter->Input.setValue(Input.getValue());
}
filter = nextFilter;
};
}
//if we have no input the filters are responsible of grabbing the pipeline data themself
else {
//the first filter must always grab the data
if(filter->Input.getValue() != NULL)
filter->Input.setValue(NULL);
//all the others need to be connected to the previous filter or grab the data, dependend on mode
++it;
for(; it != objs.end(); ++it) {
FemPostFilter* nextFilter = static_cast<FemPostFilter*>(*it);
if(Mode.getValue() == 0) { //serial mode
if( nextFilter->Input.getValue() != filter)
nextFilter->Input.setValue(filter);
}
else { //Parallel mode
if( nextFilter->Input.getValue() != NULL)
nextFilter->Input.setValue(NULL);
}
filter = nextFilter;
};
}
}
App::GeoFeature::onChanged(prop);
}
FemPostObject* FemPostPipeline::getLastPostObject() {
if(Filter.getValues().empty())
return this;
return static_cast<FemPostObject*>(Filter.getValues().back());
}
bool FemPostPipeline::holdsPostObject(FemPostObject* obj) {
std::vector<App::DocumentObject*>::const_iterator it = Filter.getValues().begin();
for(; it != Filter.getValues().end(); ++it) {
if(*it == obj)
return true;
}
return false;
}
void FemPostPipeline::load(FemResultObject* res) {
vtkSmartPointer<vtkUnstructuredGrid> grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
//first copy the mesh over
//########################
if(!res->Mesh.getValue() || !res->Mesh.getValue()->isDerivedFrom(Fem::FemMeshObject::getClassTypeId()))
return;
const FemMesh& mesh = static_cast<FemMeshObject*>(res->Mesh.getValue())->FemMesh.getValue();
SMESH_Mesh* smesh = const_cast<SMESH_Mesh*>(mesh.getSMesh());
SMESHDS_Mesh* meshDS = smesh->GetMeshDS();
const SMDS_MeshInfo& info = meshDS->GetMeshInfo();
//start with the nodes
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
SMDS_NodeIteratorPtr aNodeIter = meshDS->nodesIterator();
points->SetNumberOfPoints(info.NbNodes());
for(; aNodeIter->more(); ) {
const SMDS_MeshNode* node = aNodeIter->next();
float coords[3] = {float(node->X()), float(node->Y()), float(node->Z())};
points->SetPoint(node->GetID()-1, coords);
}
grid->SetPoints(points);
//start with 2d elements
vtkSmartPointer<vtkCellArray> triangleArray = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkCellArray> quadTriangleArray = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkCellArray> quadArray = vtkSmartPointer<vtkCellArray>::New();
SMDS_FaceIteratorPtr aFaceIter = meshDS->facesIterator();
for (;aFaceIter->more();) {
const SMDS_MeshFace* aFace = aFaceIter->next();
//triangle
if(aFace->NbNodes() == 3) {
vtkSmartPointer<vtkTriangle> tria = vtkSmartPointer<vtkTriangle>::New();
tria->GetPointIds()->SetId(0, aFace->GetNode(0)->GetID()-1);
tria->GetPointIds()->SetId(1, aFace->GetNode(1)->GetID()-1);
tria->GetPointIds()->SetId(2, aFace->GetNode(2)->GetID()-1);
triangleArray->InsertNextCell(tria);
}
//quad
else if(aFace->NbNodes() == 4) {
vtkSmartPointer<vtkQuad> quad = vtkSmartPointer<vtkQuad>::New();
quad->GetPointIds()->SetId(0, aFace->GetNode(0)->GetID()-1);
quad->GetPointIds()->SetId(1, aFace->GetNode(1)->GetID()-1);
quad->GetPointIds()->SetId(2, aFace->GetNode(2)->GetID()-1);
quadArray->InsertNextCell(quad);
}
else if (aFace->NbNodes() == 6) {
vtkSmartPointer<vtkQuadraticTriangle> tria = vtkSmartPointer<vtkQuadraticTriangle>::New();
tria->GetPointIds()->SetId(0, aFace->GetNode(0)->GetID()-1);
tria->GetPointIds()->SetId(1, aFace->GetNode(1)->GetID()-1);
tria->GetPointIds()->SetId(2, aFace->GetNode(2)->GetID()-1);
tria->GetPointIds()->SetId(3, aFace->GetNode(3)->GetID()-1);
tria->GetPointIds()->SetId(4, aFace->GetNode(4)->GetID()-1);
tria->GetPointIds()->SetId(5, aFace->GetNode(5)->GetID()-1);
quadTriangleArray->InsertNextCell(tria);
}
}
if(triangleArray->GetNumberOfCells()>0)
grid->SetCells(VTK_TRIANGLE, triangleArray);
if(quadArray->GetNumberOfCells()>0)
grid->SetCells(VTK_QUAD, quadArray);
if(quadTriangleArray->GetNumberOfCells()>0)
grid->SetCells(VTK_QUADRATIC_TRIANGLE, quadTriangleArray);
//now all volumes
vtkSmartPointer<vtkCellArray> tetraArray = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkCellArray> quadTetraArray = vtkSmartPointer<vtkCellArray>::New();
SMDS_VolumeIteratorPtr aVolIter = meshDS->volumesIterator();
for (;aVolIter->more();) {
const SMDS_MeshVolume* aVol = aVolIter->next();
//tetrahedra
if(aVol->NbNodes() == 4) {
vtkSmartPointer<vtkTetra> tetra = vtkSmartPointer<vtkTetra>::New();
tetra->GetPointIds()->SetId(0, aVol->GetNode(0)->GetID()-1);
tetra->GetPointIds()->SetId(1, aVol->GetNode(1)->GetID()-1);
tetra->GetPointIds()->SetId(2, aVol->GetNode(2)->GetID()-1);
tetra->GetPointIds()->SetId(3, aVol->GetNode(3)->GetID()-1);
tetraArray->InsertNextCell(tetra);
}
//quadratic tetrahedra
else if( aVol->NbNodes() == 10) {
vtkSmartPointer<vtkQuadraticTetra> tetra = vtkSmartPointer<vtkQuadraticTetra>::New();
tetra->GetPointIds()->SetId(0, aVol->GetNode(0)->GetID()-1);
tetra->GetPointIds()->SetId(1, aVol->GetNode(1)->GetID()-1);
tetra->GetPointIds()->SetId(2, aVol->GetNode(2)->GetID()-1);
tetra->GetPointIds()->SetId(3, aVol->GetNode(3)->GetID()-1);
tetra->GetPointIds()->SetId(4, aVol->GetNode(4)->GetID()-1);
tetra->GetPointIds()->SetId(5, aVol->GetNode(5)->GetID()-1);
tetra->GetPointIds()->SetId(6, aVol->GetNode(6)->GetID()-1);
tetra->GetPointIds()->SetId(7, aVol->GetNode(7)->GetID()-1);
tetra->GetPointIds()->SetId(8, aVol->GetNode(8)->GetID()-1);
tetra->GetPointIds()->SetId(9, aVol->GetNode(9)->GetID()-1);
quadTetraArray->InsertNextCell(tetra);
}
}
if(tetraArray->GetNumberOfCells()>0)
grid->SetCells(VTK_TETRA, tetraArray);
if(quadTetraArray->GetNumberOfCells()>0)
grid->SetCells(VTK_QUADRATIC_TETRA, quadTetraArray);
//Now copy the point data over
//############################
if(!res->StressValues.getValues().empty()) {
const std::vector<double>& vec = res->StressValues.getValues();
vtkSmartPointer<vtkDoubleArray> data = vtkSmartPointer<vtkDoubleArray>::New();
data->SetNumberOfValues(vec.size());
data->SetName("Stress");
for(size_t i=0; i<vec.size(); ++i)
data->SetValue(i, vec[i]);
grid->GetPointData()->AddArray(data);
}
if(!res->StressValues.getValues().empty()) {
const std::vector<Base::Vector3d>& vec = res->DisplacementVectors.getValues();
vtkSmartPointer<vtkDoubleArray> data = vtkSmartPointer<vtkDoubleArray>::New();
data->SetNumberOfComponents(3);
data->SetName("Displacement");
for(std::vector<Base::Vector3d>::const_iterator it=vec.begin(); it!=vec.end(); ++it) {
double tuple[] = {it->x, it->y, it->z};
data->InsertNextTuple(tuple);
}
grid->GetPointData()->AddArray(data);
}
Data.setValue(grid);
}