/
FemPostFilter.cpp
529 lines (387 loc) · 17 KB
/
FemPostFilter.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/***************************************************************************
* 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 "FemPostFilter.h"
#include "FemPostPipeline.h"
#include <Base/Console.h>
#include <App/Document.h>
#include <App/DocumentObjectPy.h>
#include <vtkFieldData.h>
#include <vtkPointData.h>
using namespace Fem;
using namespace App;
PROPERTY_SOURCE(Fem::FemPostFilter, Fem::FemPostObject)
FemPostFilter::FemPostFilter()
{
ADD_PROPERTY(Input,(0));
}
FemPostFilter::~FemPostFilter()
{
}
void FemPostFilter::addFilterPipeline(const FemPostFilter::FilterPipeline& p, std::string name) {
m_pipelines[name] = p;
}
FemPostFilter::FilterPipeline& FemPostFilter::getFilterPipeline(std::string name) {
return m_pipelines[name];
}
void FemPostFilter::setActiveFilterPipeline(std::string name) {
if(m_activePipeline != name && isValid()) {
m_activePipeline = name;
}
}
DocumentObjectExecReturn* FemPostFilter::execute(void) {
if(!m_pipelines.empty() && !m_activePipeline.empty()) {
FemPostFilter::FilterPipeline& pipe = m_pipelines[m_activePipeline];
if (m_activePipeline.length() >= 13) {
std::string LineClip = m_activePipeline.substr(0,13);
if (LineClip == "DataAlongLine") {
pipe.filterSource->SetSourceData(getInputData());
pipe.filterTarget->Update();
Data.setValue(pipe.filterTarget->GetOutputDataObject(0));
}
} else {
pipe.source->SetInputDataObject(getInputData());
pipe.target->Update();
Data.setValue(pipe.target->GetOutputDataObject(0));
}
}
return StdReturn;
}
vtkDataObject* FemPostFilter::getInputData() {
if(Input.getValue()) {
return Input.getValue<FemPostObject*>()->Data.getValue();
}
else {
//get the pipeline and use the pipelinedata
std::vector<App::DocumentObject*> objs = getDocument()->getObjectsOfType(FemPostPipeline::getClassTypeId());
for(std::vector<App::DocumentObject*>::iterator it = objs.begin(); it != objs.end(); ++it) {
if(static_cast<FemPostPipeline*>(*it)->holdsPostObject(this)) {
return static_cast<FemPostObject*>(*it)->Data.getValue();
}
}
}
return NULL;
}
PROPERTY_SOURCE(Fem::FemPostClipFilter, Fem::FemPostFilter)
FemPostClipFilter::FemPostClipFilter(void) : FemPostFilter() {
ADD_PROPERTY_TYPE(Function, (0), "Clip", App::Prop_None, "The function object which defines the clip regions");
ADD_PROPERTY_TYPE(InsideOut, (false), "Clip", App::Prop_None, "Invert the clip direction");
ADD_PROPERTY_TYPE(CutCells, (false), "Clip", App::Prop_None, "Decides if cells are cuttet and interpolated or if the cells are kept as a whole");
FilterPipeline clip;
m_clipper = vtkSmartPointer<vtkTableBasedClipDataSet>::New();
clip.source = m_clipper;
clip.target = m_clipper;
addFilterPipeline(clip, "clip");
FilterPipeline extr;
m_extractor = vtkSmartPointer<vtkExtractGeometry>::New();
extr.source = m_extractor;
extr.target = m_extractor;
addFilterPipeline(extr, "extract");
m_extractor->SetExtractInside(0);
setActiveFilterPipeline("extract");
}
FemPostClipFilter::~FemPostClipFilter() {
}
void FemPostClipFilter::onChanged(const Property* prop) {
if(prop == &Function) {
if(Function.getValue() && Function.getValue()->isDerivedFrom(FemPostFunction::getClassTypeId())) {
m_clipper->SetClipFunction(static_cast<FemPostFunction*>(Function.getValue())->getImplicitFunction());
m_extractor->SetImplicitFunction(static_cast<FemPostFunction*>(Function.getValue())->getImplicitFunction());
}
}
else if(prop == &InsideOut) {
m_clipper->SetInsideOut(InsideOut.getValue());
m_extractor->SetExtractInside( (InsideOut.getValue()) ? 1 : 0 );
}
else if(prop == &CutCells) {
if(!CutCells.getValue())
setActiveFilterPipeline("extract");
else
setActiveFilterPipeline("clip");
};
Fem::FemPostFilter::onChanged(prop);
}
short int FemPostClipFilter::mustExecute(void) const {
if(Function.isTouched() ||
InsideOut.isTouched() ||
CutCells.isTouched()) {
return 1;
}
else return App::DocumentObject::mustExecute();
}
DocumentObjectExecReturn* FemPostClipFilter::execute(void) {
if(!m_extractor->GetImplicitFunction())
return StdReturn;
return Fem::FemPostFilter::execute();
}
PROPERTY_SOURCE(Fem::FemPostDataAlongLineFilter, Fem::FemPostFilter)
FemPostDataAlongLineFilter::FemPostDataAlongLineFilter(void) : FemPostFilter() {
ADD_PROPERTY_TYPE(Point1,(Base::Vector3d(0.0,0.0,0.0)), "DataAlongLine", App::Prop_None, "The point 1 used to define end point of line");
ADD_PROPERTY_TYPE(Point2,(Base::Vector3d(0.0,0.0,1.0)), "DataAlongLine", App::Prop_None, "The point 2 used to define end point of line");
ADD_PROPERTY_TYPE(Resolution,(100), "DataAlongLine", App::Prop_None, "The number of intervals between the 2 end points of line");
ADD_PROPERTY_TYPE(XAxisData,(0), "DataAlongLine",App::Prop_None,"X axis data values used for plotting");
ADD_PROPERTY_TYPE(YAxisData,(0), "DataAlongLine",App::Prop_None,"Y axis data values used for plotting");
ADD_PROPERTY_TYPE(PlotData ,(""),"DataAlongLine",App::Prop_None,"Field used for plotting");
PlotData.setStatus(App::Property::ReadOnly, true);
XAxisData.setStatus(App::Property::ReadOnly, true);
YAxisData.setStatus(App::Property::ReadOnly, true);
FilterPipeline clip;
m_line = vtkSmartPointer<vtkLineSource>::New();
const Base::Vector3d& vec1 = Point1.getValue();
m_line->SetPoint1(vec1.x, vec1.y, vec1.z);
const Base::Vector3d& vec2 = Point2.getValue();
m_line->SetPoint2(vec2.x, vec2.y, vec2.z);
m_line->SetResolution(Resolution.getValue());
m_probe = vtkSmartPointer<vtkProbeFilter>::New();
m_probe->SetInputConnection(m_line->GetOutputPort());
m_probe->SetValidPointMaskArrayName("ValidPointArray");
m_probe->SetPassPointArrays(1);
m_probe->SetPassCellArrays(1);
// needs vtk > 6.1
#if (VTK_MAJOR_VERSION > 6) || (VTK_MINOR_VERSION > 1)
m_probe->ComputeToleranceOff();
m_probe->SetTolerance(0.01);
#endif
clip.filterSource = m_probe;
clip.filterTarget = m_probe;
addFilterPipeline(clip, "DataAlongLine");
setActiveFilterPipeline("DataAlongLine");
}
FemPostDataAlongLineFilter::~FemPostDataAlongLineFilter() {
}
DocumentObjectExecReturn* FemPostDataAlongLineFilter::execute(void) {
//recalculate the filter
return Fem::FemPostFilter::execute();
}
void FemPostDataAlongLineFilter::onChanged(const Property* prop) {
if(prop == &Point1) {
const Base::Vector3d& vec1 = Point1.getValue();
m_line->SetPoint1(vec1.x, vec1.y, vec1.z);
}
else if(prop == &Point2) {
const Base::Vector3d& vec2 = Point2.getValue();
m_line->SetPoint2(vec2.x, vec2.y, vec2.z);
}
else if(prop == &Resolution) {
m_line->SetResolution(Resolution.getValue());
}
else if(prop == &PlotData) {
GetAxisData();
}
Fem::FemPostFilter::onChanged(prop);
}
short int FemPostDataAlongLineFilter::mustExecute(void) const {
if(Point1.isTouched() ||
Point2.isTouched() ||
Resolution.isTouched()){
return 1;
}
else return App::DocumentObject::mustExecute();
}
void FemPostDataAlongLineFilter::GetAxisData() {
std::vector<double> coords;
std::vector<double> values;
vtkSmartPointer<vtkDataObject> data = m_probe->GetOutputDataObject(0);
vtkDataSet* dset = vtkDataSet::SafeDownCast(data);
vtkDataArray* pdata = dset->GetPointData()->GetArray(PlotData.getValue());
vtkDataArray *tcoords = dset->GetPointData()->GetTCoords("Texture Coordinates");
int component = 0;
const Base::Vector3d& vec1 = Point1.getValue();
const Base::Vector3d& vec2 = Point2.getValue();
const Base::Vector3d diff = vec1 - vec2;
double Len = diff.Length();
for(int i=0; i<dset->GetNumberOfPoints(); ++i) {
double value = 0;
if(pdata->GetNumberOfComponents() == 1)
value = pdata->GetComponent(i, component);
else {
for(int j=0; j<pdata->GetNumberOfComponents(); ++j)
value += std::pow(pdata->GetComponent(i, j),2);
value = std::sqrt(value);
}
values.push_back(value);
double tcoord = tcoords->GetComponent(i, component);
coords.push_back(tcoord*Len);
}
YAxisData.setValues(values);
XAxisData.setValues(coords);
}
PROPERTY_SOURCE(Fem::FemPostScalarClipFilter, Fem::FemPostFilter)
FemPostScalarClipFilter::FemPostScalarClipFilter(void) : FemPostFilter() {
ADD_PROPERTY_TYPE(Value, (0), "Clip", App::Prop_None, "The scalar value used to clip the selected field");
ADD_PROPERTY_TYPE(Scalars, (long(0)), "Clip", App::Prop_None, "The field used to clip");
ADD_PROPERTY_TYPE(InsideOut, (false), "Clip", App::Prop_None, "Invert the clip direction");
Value.setConstraints(&m_constraints);
FilterPipeline clip;
m_clipper = vtkSmartPointer<vtkTableBasedClipDataSet>::New();
clip.source = m_clipper;
clip.target = m_clipper;
addFilterPipeline(clip, "clip");
setActiveFilterPipeline("clip");
}
FemPostScalarClipFilter::~FemPostScalarClipFilter() {
}
DocumentObjectExecReturn* FemPostScalarClipFilter::execute(void) {
std::string val;
if(m_scalarFields.getEnums() && Scalars.getValue() >= 0)
val = Scalars.getValueAsString();
std::vector<std::string> array;
vtkSmartPointer<vtkDataObject> data = getInputData();
if(!data || !data->IsA("vtkDataSet"))
return StdReturn;
vtkDataSet* dset = vtkDataSet::SafeDownCast(data);
vtkPointData* pd = dset->GetPointData();
for(int i=0; i<pd->GetNumberOfArrays(); ++i) {
if(pd->GetArray(i)->GetNumberOfComponents()==1)
array.push_back(pd->GetArrayName(i));
}
App::Enumeration empty;
Scalars.setValue(empty);
m_scalarFields.setEnums(array);
Scalars.setValue(m_scalarFields);
std::vector<std::string>::iterator it = std::find(array.begin(), array.end(), val);
if(!val.empty() && it != array.end())
Scalars.setValue(val.c_str());
//recalculate the filter
return Fem::FemPostFilter::execute();
}
void FemPostScalarClipFilter::onChanged(const Property* prop) {
if(prop == &Value) {
m_clipper->SetValue(Value.getValue());
}
else if(prop == &InsideOut) {
m_clipper->SetInsideOut(InsideOut.getValue());
}
else if(prop == &Scalars && (Scalars.getValue() >= 0)) {
m_clipper->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_POINTS, Scalars.getValueAsString() );
setConstraintForField();
}
Fem::FemPostFilter::onChanged(prop);
}
short int FemPostScalarClipFilter::mustExecute(void) const {
if(Value.isTouched() ||
InsideOut.isTouched() ||
Scalars.isTouched()) {
return 1;
}
else return App::DocumentObject::mustExecute();
}
void FemPostScalarClipFilter::setConstraintForField() {
vtkSmartPointer<vtkDataObject> data = getInputData();
if(!data || !data->IsA("vtkDataSet"))
return;
vtkDataSet* dset = vtkDataSet::SafeDownCast(data);
vtkDataArray* pdata = dset->GetPointData()->GetArray(Scalars.getValueAsString());
double p[2];
pdata->GetRange(p);
m_constraints.LowerBound = p[0];
m_constraints.UpperBound = p[1];
m_constraints.StepSize = (p[1]-p[0])/100.;
}
PROPERTY_SOURCE(Fem::FemPostWarpVectorFilter, Fem::FemPostFilter)
FemPostWarpVectorFilter::FemPostWarpVectorFilter(void): FemPostFilter() {
ADD_PROPERTY_TYPE(Factor, (0), "Warp", App::Prop_None, "The factor by which the vector is added to the node positions");
ADD_PROPERTY_TYPE(Vector, (long(0)), "Warp", App::Prop_None, "The field added to the node position");
FilterPipeline warp;
m_warp = vtkSmartPointer<vtkWarpVector>::New();
warp.source = m_warp;
warp.target = m_warp;
addFilterPipeline(warp, "warp");
setActiveFilterPipeline("warp");
}
FemPostWarpVectorFilter::~FemPostWarpVectorFilter() {
}
DocumentObjectExecReturn* FemPostWarpVectorFilter::execute(void) {
std::string val;
if(m_vectorFields.getEnums() && Vector.getValue() >= 0)
val = Vector.getValueAsString();
std::vector<std::string> array;
vtkSmartPointer<vtkDataObject> data = getInputData();
if(!data || !data->IsA("vtkDataSet"))
return StdReturn;
vtkDataSet* dset = vtkDataSet::SafeDownCast(data);
vtkPointData* pd = dset->GetPointData();
for(int i=0; i<pd->GetNumberOfArrays(); ++i) {
if(pd->GetArray(i)->GetNumberOfComponents()==3)
array.push_back(pd->GetArrayName(i));
}
App::Enumeration empty;
Vector.setValue(empty);
m_vectorFields.setEnums(array);
Vector.setValue(m_vectorFields);
std::vector<std::string>::iterator it = std::find(array.begin(), array.end(), val);
if(!val.empty() && it != array.end())
Vector.setValue(val.c_str());
//recalculate the filter
return Fem::FemPostFilter::execute();
}
void FemPostWarpVectorFilter::onChanged(const Property* prop) {
if(prop == &Factor) {
m_warp->SetScaleFactor(Factor.getValue());
}
else if(prop == &Vector && (Vector.getValue() >= 0)) {
m_warp->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_POINTS, Vector.getValueAsString() );
}
Fem::FemPostFilter::onChanged(prop);
}
short int FemPostWarpVectorFilter::mustExecute(void) const {
if(Factor.isTouched() ||
Vector.isTouched()) {
return 1;
}
else return App::DocumentObject::mustExecute();
}
PROPERTY_SOURCE(Fem::FemPostCutFilter, Fem::FemPostFilter)
FemPostCutFilter::FemPostCutFilter(void) : FemPostFilter() {
ADD_PROPERTY_TYPE(Function, (0), "Cut", App::Prop_None, "The function object which defines the clip cut function");
FilterPipeline clip;
m_cutter = vtkSmartPointer<vtkCutter>::New();
clip.source = m_cutter;
clip.target = m_cutter;
addFilterPipeline(clip, "cut");
setActiveFilterPipeline("cut");
}
FemPostCutFilter::~FemPostCutFilter() {
}
void FemPostCutFilter::onChanged(const Property* prop) {
if(prop == &Function) {
if(Function.getValue() && Function.getValue()->isDerivedFrom(FemPostFunction::getClassTypeId())) {
m_cutter->SetCutFunction(static_cast<FemPostFunction*>(Function.getValue())->getImplicitFunction());
}
}
Fem::FemPostFilter::onChanged(prop);
}
short int FemPostCutFilter::mustExecute(void) const {
if(Function.isTouched()) {
return 1;
}
else return App::DocumentObject::mustExecute();
}
DocumentObjectExecReturn* FemPostCutFilter::execute(void) {
if(!m_cutter->GetCutFunction())
return StdReturn;
return Fem::FemPostFilter::execute();
}