geometrySurface.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 "geometrySurface.H"
30 #include "stringOps.H"
31 #include "foamVtkTools.H"
32 #include "MeshedSurfaces.H"
33 #include "runTimePostProcessing.H"
35 
36 // VTK includes
37 #include "vtkActor.h"
38 #include "vtkCellArray.h"
39 #include "vtkCellData.h"
40 #include "vtkDoubleArray.h"
41 #include "vtkPointData.h"
42 #include "vtkPoints.h"
43 #include "vtkPolyData.h"
44 #include "vtkPolyDataMapper.h"
45 #include "vtkProperty.h"
46 #include "vtkRenderer.h"
47 #include "vtkSmartPointer.h"
48 
49 // VTK Readers
50 #include "vtkOBJReader.h"
51 #include "vtkSTLReader.h"
52 #include "vtkPolyDataReader.h"
53 #include "vtkXMLPolyDataReader.h"
54 
55 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 namespace functionObjects
60 {
61 namespace runTimePostPro
62 {
63  defineTypeNameAndDebug(geometrySurface, 0);
64  addToRunTimeSelectionTable(surface, geometrySurface, dictionary);
65 }
66 }
67 }
68 
69 
70 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
71 
72 namespace
73 {
74 
75 static vtkSmartPointer<vtkPolyData> getPolyDataFile(const Foam::fileName& fName)
76 {
77  // Not extremely elegant...
79 
80  if ("vtk" == fName.ext())
81  {
83 
84  reader->SetFileName(fName.c_str());
85  reader->Update();
86  dataset = reader->GetOutput();
87 
88  return dataset;
89  }
90 
91  if ("vtp" == fName.ext())
92  {
94 
95  reader->SetFileName(fName.c_str());
96  reader->Update();
97  dataset = reader->GetOutput();
98 
99  return dataset;
100  }
101 
102  if ("obj" == fName.ext())
103  {
104  auto reader = vtkSmartPointer<vtkOBJReader>::New();
105 
106  reader->SetFileName(fName.c_str());
107  reader->Update();
108  dataset = reader->GetOutput();
109 
110  return dataset;
111  }
112 
113  if ("stl" == fName.ext() || "stlb" == fName.ext())
114  {
115  auto reader = vtkSmartPointer<vtkSTLReader>::New();
116 
117  reader->SetFileName(fName.c_str());
118  reader->Update();
119  dataset = reader->GetOutput();
120 
121  return dataset;
122  }
123 
124 
125  // Fallback to using OpenFOAM to read the surface and convert afterwards
126  Foam::meshedSurface surf(fName);
127 
128  dataset = Foam::vtk::Tools::Patch::mesh(surf);
129 
130  dataset->GetCellData()->SetNormals
131  (
133  );
134 
135  return dataset;
136 }
137 
138 } // End anonymous namespace
139 
140 
141 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
142 
144 (
145  const scalar position,
146  vtkRenderer* renderer,
147  const fileName& fName
148 ) const
149 {
150  // Master-only, since that is where the files are.
151  if (!visible_ || !renderer || !Pstream::master())
152  {
153  return;
154  }
155 
156  if (representation_ == rtGlyph)
157  {
159  << "Glyph representation not available for " << typeName
160  << " object" << exit(FatalError);
161  }
162 
163  DebugInfo << " Add geometry surface: " << fName << nl;
164 
165  auto surf = getPolyDataFile(fName);
166 
167  if (!surf || !surf->GetNumberOfPoints())
168  {
170  << "Could not read "<< fName << nl
171  << exit(FatalError);
172  }
173 
175 
176  mapper->ScalarVisibilityOff();
177 
178  mapper->SetInputData(surf);
179 
180  addFeatureEdges(renderer, surf);
181 
182  surfaceActor_->SetMapper(mapper);
183 
184  setRepresentation(surfaceActor_);
185 
186  renderer->AddActor(surfaceActor_);
187 }
188 
189 
190 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
191 
193 (
194  const runTimePostProcessing& parent,
195  const dictionary& dict,
196  const HashPtrTable<Function1<vector>>& colours
197 )
198 :
199  surface(parent, dict, colours),
200  fileNames_()
201 {
202  dict.readEntry("files", fileNames_);
203 }
204 
205 
207 (
208  const runTimePostProcessing& parent,
209  const dictionary& dict,
210  const HashPtrTable<Function1<vector>>& colours,
211  const List<fileName>& fileNames
212 )
213 :
214  surface(parent, dict, colours),
215  fileNames_(fileNames)
216 {}
217 
218 
219 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
220 
222 (
223  const scalar position,
224  vtkRenderer* renderer
225 )
226 {
227  if (!visible_)
228  {
229  return;
230  }
231 
232  for (fileName f : fileNames_) // Use a copy
233  {
234  f.expand();
235  addGeometryToScene(position, renderer, f);
236  }
237 }
238 
239 
241 (
242  const scalar position
243 )
244 {
245  if (!visible_)
246  {
247  return;
248  }
249 
250  surface::updateActors(position);
251 
252  surfaceActor_->GetProperty()->SetOpacity(opacity(position));
253 
254  vector sc = surfaceColour_->value(position);
255  surfaceActor_->GetProperty()->SetColor(sc[0], sc[1], sc[2]);
256 
257  vector ec = edgeColour_->value(position);
258  surfaceActor_->GetProperty()->SetEdgeColor(ec[0], ec[1], ec[2]);
259 }
260 
261 
263 {
264  // Note: do not remove geometry files
265  // - often static files used for other purposes as well (eg meshing)
266 
267  return true;
268 }
269 
270 
271 // ************************************************************************* //
Foam::functionObjects::runTimePostPro::defineTypeNameAndDebug
defineTypeNameAndDebug(geometrySurface, 0)
Foam::functionObjects::runTimePostPro::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surface, contourFilter, dictionary)
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
foamVtkTools.H
Foam::functionObjects::runTimePostPro::geometrySurface::updateActors
virtual void updateActors(const scalar position)
Update actors.
Definition: geometrySurface.C:241
Foam::vtk::Tools::Patch::mesh
static vtkSmartPointer< vtkPolyData > mesh(const PatchType &p)
Convert patch points/faces to vtkPolyData.
Definition: foamVtkToolsTemplates.C:92
Foam::vtk::Tools::Patch::faceNormals
static vtkSmartPointer< vtkFloatArray > faceNormals(const PatchType &p)
Convert patch face normals to vtkFloatArray.
Definition: foamVtkToolsTemplates.C:122
Foam::functionObjects::runTimePostPro::geometrySurface::geometrySurface
geometrySurface(const geometrySurface &)=delete
No copy construct.
Foam::addGeometryToScene
static void addGeometryToScene(PtrList< Type > &objects, const scalar position, vtkRenderer *renderer)
Definition: runTimePostProcessing.C:81
vtkSmartPointer
Definition: runTimePostProcessing.H:148
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::updateActors
static void updateActors(PtrList< Type > &objects, const scalar position)
Definition: runTimePostProcessing.C:102
Foam::functionObjects::runTimePostProcessing
Generate images during run-time.
Definition: runTimePostProcessing.H:170
Foam::functionObjects::runTimePostPro::surface
Visualisation of surface data with additional routines for handling parallel distributed data.
Definition: surface.H:180
Foam::functionObjects::runTimePostPro::geometrySurface::clear
virtual bool clear()
Clear files used to create the object(s) - no-op.
Definition: geometrySurface.C:262
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
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
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
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
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::List< fileName >
MeshedSurfaces.H
Foam::MeshedSurface< face >
stringOps.H
Foam::functionObjects::runTimePostPro::geometrySurface::addGeometryToScene
void addGeometryToScene(const scalar position, vtkRenderer *renderer, const fileName &fName) const
Add surface (file) to scene.
Definition: geometrySurface.C:144
geometrySurface.H