functionObjectCloud.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // OpenFOAM includes
29 #include "functionObjectCloud.H"
30 #include "fvMesh.H"
31 #include "runTimePostProcessing.H"
33 
34 // VTK includes
35 #include "vtkActor.h"
36 #include "vtkPolyData.h"
37 #include "vtkPolyDataMapper.h"
38 #include "vtkProperty.h"
39 #include "vtkRenderer.h"
40 #include "vtkSmartPointer.h"
41 
42 // VTK Readers
43 #include "vtkPolyDataReader.h"
44 #include "vtkXMLPolyDataReader.h"
45 
46 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 namespace functionObjects
51 {
52 namespace runTimePostPro
53 {
54  defineTypeName(functionObjectCloud);
55  addToRunTimeSelectionTable(pointData, functionObjectCloud, dictionary);
56 }
57 }
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
62 
63 namespace
64 {
65 
66 static vtkSmartPointer<vtkPolyData> getPolyDataFile(const Foam::fileName& fName)
67 {
68  // Very simple - we only support vtp files, which are expected to have
69  // the scaling and colouring fields.
70 
72 
73  if (fName.ext() == "vtp")
74  {
76 
77  reader->SetFileName(fName.c_str());
78  reader->Update();
79  dataset = reader->GetOutput();
80 
81  return dataset;
82  }
83 
84  return dataset;
85 }
86 
87 } // End anonymous namespace
88 
89 
90 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
91 
93 (
94  const runTimePostProcessing& parent,
95  const dictionary& dict,
96  const HashPtrTable<Function1<vector>>& colours
97 )
98 :
99  pointData(parent, dict, colours),
100  functionObjectBase(parent, dict, colours),
101  cloudName_(dict.get<word>("cloud")),
102  inputFileName_(),
103  colourFieldName_(dict.get<word>("colourField")),
104  actor_()
105 {}
106 
107 
108 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
109 
112 {}
113 
114 
115 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
116 
119 (
120  const scalar position,
121  vtkRenderer* renderer
122 )
123 {
124  if (!visible_)
125  {
126  return false;
127  }
128 
130 
131  bool good = true;
132 
133  // The vtkCloud stores 'file' via the stateFunctionObject
134  // (lookup by cloudName).
135  // It only generates VTP format, which means there is a single file
136  // containing all fields.
137 
138  if (Pstream::master())
139  {
140  inputFileName_ = getFileName("file", cloudName_);
141 
142  if (inputFileName_.size())
143  {
144  polyData = getPolyDataFile(inputFileName_);
145 
146  if (!polyData || polyData->GetNumberOfPoints() == 0)
147  {
148  good = false;
149 
151  << "Could not read "<< inputFileName_ << nl
152  << "Only VTK (.vtp) files are supported"
153  << endl;
154  }
155  else
156  {
157  DebugInfo
158  << " Resolved cloud file " << inputFileName_ << endl;
159  }
160  }
161  else
162  {
163  good = false;
164 
166  << "Unable to find function object " << functionObjectName_
167  << " output for field " << fieldName_
168  << ". Cloud will not be processed"
169  << endl;
170  }
171  }
172  else
173  {
174  inputFileName_.clear();
175  }
176 
177  reduce(good, andOp<bool>());
178 
179  if (!good)
180  {
181  return false;
182  }
183 
184  // Only render on master
185  if (!renderer || !Pstream::master())
186  {
187  return true;
188  }
189 
190 
191  // Rendering
192 
194 
195  {
196  fieldSummary scaleFieldInfo =
197  queryFieldSummary(fieldName_, polyData);
198 
199  fieldSummary colourFieldInfo =
200  queryFieldSummary(colourFieldName_, polyData);
201 
202  DebugInfo
203  << " Field " << fieldName_ << ' ' << scaleFieldInfo.info() << nl
204  << " Field " << colourFieldName_ << ' ' << colourFieldInfo.info()
205  << endl;
206 
207 
208  // No reduction
209 
211 
212  actor_->SetMapper(mapper);
213 
215 
216  addGlyphs
217  (
218  position,
219  fieldName_, scaleFieldInfo, // scaling
220  colourFieldName_, colourFieldInfo, // colour
221  maxGlyphLength_,
222  polyData,
223  actor_,
224  renderer
225  );
226 
227  renderer->AddActor(actor_);
228  }
229 
230  return true;
231 }
232 
233 
236 (
237  const scalar position,
238  vtkRenderer* renderer
239 )
240 {
241  // File source
242  addGeometryFromFile(position, renderer);
243 }
244 
245 
247 (
248  const scalar position
249 )
250 {
251  if (actor_)
252  {
253  const vector colour = pointColour_->value(position);
254 
255  vtkProperty* prop = actor_->GetProperty();
256 
257  prop->SetOpacity(opacity(position));
258 
259  prop->SetColor(colour[0], colour[1], colour[2]);
260  }
261 }
262 
263 
265 {
267  {
268  if (inputFileName_.size() && Foam::rm(inputFileName_))
269  {
270  inputFileName_.clear();
271  return true;
272  }
273  }
274 
275  return false;
276 }
277 
278 
279 // ************************************************************************* //
functionObjectCloud.H
Foam::functionObjects::runTimePostPro::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surface, contourFilter, dictionary)
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::runTimePostPro::functionObjectCloud::addGeometryFromFile
bool addGeometryFromFile(const scalar position, vtkRenderer *renderer)
Add cloud to scene (using file source)
Definition: functionObjectCloud.C:119
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary
General field characteristics.
Definition: fieldVisualisationBase.H:172
Foam::functionObjects::runTimePostPro::functionObjectCloud::updateActors
virtual void updateActors(const scalar position)
Update actors.
Definition: functionObjectCloud.C:247
vtkSmartPointer
Definition: runTimePostProcessing.H:148
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::functionObjects::runTimePostPro::pointData
Visualisation of point data.
Definition: pointData.H:89
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:56
Foam::functionObjects::runTimePostPro::functionObjectBase
Base class for function object visualisation.
Definition: functionObjectBase.H:86
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:51
Foam::functionObjects::runTimePostProcessing
Generate images during run-time.
Definition: runTimePostProcessing.H:170
Foam::functionObjects::runTimePostPro::functionObjectCloud::clear
virtual bool clear()
Clear files used to create the object(s)
Definition: functionObjectCloud.C:264
Foam::functionObjects::runTimePostPro::fieldVisualisationBase::fieldSummary::info
InfoProxy< fieldSummary > info() const
Definition: fieldVisualisationBase.H:213
Foam::andOp
Definition: ops.H:233
Foam::functionObjects::runTimePostPro::functionObjectBase::clear
virtual bool clear()
Clear files used to create the object(s)
Definition: functionObjectBase.C:88
Foam::functionObjects::runTimePostPro::functionObjectCloud::addGeometryToScene
virtual void addGeometryToScene(const scalar position, vtkRenderer *renderer)
Add cloud to scene.
Definition: functionObjectCloud.C:236
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:228
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::runTimePostPro::functionObjectCloud::functionObjectCloud
functionObjectCloud(const functionObjectCloud &)=delete
No copy construct.
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::functionObjects::runTimePostPro::functionObjectCloud::~functionObjectCloud
virtual ~functionObjectCloud()
Destructor.
Definition: functionObjectCloud.C:111
runTimePostProcessing.H
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>.
Definition: HashPtrTable.H:54
Foam::Vector< scalar >
Foam::functionObjects::runTimePostPro::defineTypeName
defineTypeName(contourFilter)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294