x3dSurfaceWriter.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) 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 #include "x3dSurfaceWriter.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
31 #include "MeshedSurfaceProxy.H"
32 #include "surfaceWriterMethods.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace surfaceWriters
40 {
41  defineTypeName(x3dWriter);
42  addToRunTimeSelectionTable(surfaceWriter, x3dWriter, word);
43  addToRunTimeSelectionTable(surfaceWriter, x3dWriter, wordDict);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 //- A (0-1) range for colouring
54 template<class Type>
55 static inline scalar rangex(const scalarMinMax& range, const Type& val)
56 {
57  scalar x = Foam::mag(val);
58 
59  return (x - range.min()) / (range.max() - range.min());
60 }
61 
62 
63 //- A (0-1) range for colouring
64 template<>
65 inline scalar rangex(const scalarMinMax& range, const scalar& val)
66 {
67  scalar x = val;
68  return (x - range.min()) / (range.max() - range.min());
69 }
70 
71 
72 //- A (0-1) range for colouring
73 template<>
74 inline scalar rangex(const scalarMinMax& range, const label& val)
75 {
76  scalar x = val;
77  return (x - range.min()) / (range.max() - range.min());
78 }
79 
80 
81 static inline void printColour(Ostream& os, const vector& rgb)
82 {
83  os << rgb[0] << ' ' << rgb[1] << ' ' << rgb[2] << ',' << nl;
84 }
85 
86 } // End namespace Foam
87 
88 
89 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
90 
92 :
93  surfaceWriter(),
94  range_(),
95  colourTablePtr_(nullptr)
96 {}
97 
98 
100 (
101  const dictionary& options
102 )
103 :
104  surfaceWriter(options),
105  range_(),
106  colourTablePtr_(nullptr)
107 {
108  verbose_ = true;
109 
110  options.readIfPresent("range", range_);
111 
112  word tableName;
113  if (options.readIfPresent("colourMap", tableName))
114  {
115  colourTablePtr_ = colourTable::ptr(tableName);
116  if (!colourTablePtr_)
117  {
119  << "No colourMap " << tableName << " using default" << nl;
120  }
121  }
122 
123  if (!colourTablePtr_)
124  {
126  colourTablePtr_ = colourTable::ptr(colourTable::COOL_WARM);
127  }
128 
129  if (verbose_)
130  {
131  Info<< "X3D with colourMap '" << tableName << "' and range ";
132 
133  if (range_.valid())
134  {
135  Info<< range_;
136  }
137  else
138  {
139  Info<< "auto";
140  }
141  Info<< nl;
142  }
143 }
144 
145 
147 (
148  const meshedSurf& surf,
149  const fileName& outputPath,
150  bool parallel,
151  const dictionary& options
152 )
153 :
154  x3dWriter(options)
155 {
156  open(surf, outputPath, parallel);
157 }
158 
159 
161 (
162  const pointField& points,
163  const faceList& faces,
164  const fileName& outputPath,
165  bool parallel,
166  const dictionary& options
167 )
168 :
169  x3dWriter(options)
170 {
171  open(points, faces, outputPath, parallel);
172 }
173 
174 
175 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
176 
178 {
179  checkOpen();
180 
181  // Geometry: rootdir/<TIME>/surfaceName.x3d
182 
183  fileName outputFile = outputPath_;
184  if (useTimeDir() && !timeName().empty())
185  {
186  // Splice in time-directory
187  outputFile = outputPath_.path() / timeName() / outputPath_.name();
188  }
189  outputFile.ext("x3d");
190 
191  if (verbose_)
192  {
193  Info<< "Writing geometry to " << outputFile << endl;
194  }
195 
196  const meshedSurf& surf = surface();
197 
198  if (Pstream::master() || !parallel_)
199  {
200  if (!isDir(outputFile.path()))
201  {
202  mkDir(outputFile.path());
203  }
204 
206  (
207  surf.points(),
208  surf.faces()
209  ).write(outputFile, "x3d");
210  }
211 
212  wroteGeom_ = true;
213  return outputFile;
214 }
215 
216 
217 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 
219 template<class Type>
220 Foam::fileName Foam::surfaceWriters::x3dWriter::writeTemplate
221 (
222  const word& fieldName,
223  const Field<Type>& localValues
224 )
225 {
226  if (!colourTablePtr_)
227  {
228  // Write geometry only if there are no colours to use
230  << "No output colours set" << endl;
231 
232  return this->write();
233  }
234 
235  checkOpen();
236 
237  // Field: rootdir/<TIME>/<field>_surfaceName.x3d
238 
239  fileName outputFile = outputPath_.path();
240  if (useTimeDir() && !timeName().empty())
241  {
242  // Splice in time-directory
243  outputFile /= timeName();
244  }
245 
246  // Append <field>_surfaceName.usr
247  outputFile /= fieldName + '_' + outputPath_.name();
248  outputFile.ext("x3d");
249 
250  if (verbose_)
251  {
252  Info<< "Writing field " << fieldName << " to " << outputFile << endl;
253  }
254 
255  const meshedSurf& surf = surface();
256 
257  // geometry merge() implicit
258  tmp<Field<Type>> tfield = mergeField(localValues);
259 
260  if (Pstream::master() || !parallel_)
261  {
262  const auto& values = tfield();
263 
264  scalarMinMax range(range_);
265 
266  if (!range.valid())
267  {
269  }
270 
271  if (!isDir(outputFile.path()))
272  {
273  mkDir(outputFile.path());
274  }
275 
276  OFstream os(outputFile);
277 
278  writeHeader(os);
279  beginGroup(os);
280  writeAppearance(os);
281 
282  // For point field: "colorPerVetex=true"
283  os << " <IndexedFaceSet"
284  << " colorPerVertex='" << Switch(this->isPointData()) << "'"
285  << " coordIndex='" << nl;
286 
287  for (const auto& f : surf.faces())
288  {
289  for (const label vrti : f)
290  {
291  os << vrti << ' ';
292  }
293  os << "-1\n";
294  }
295  os << "'";
296 
297  // Colour indices for face fields
298  if (!this->isPointData())
299  {
300  const label nFaces = surf.faces().size();
301 
302  os << " colorIndex='";
303 
304  for (label i=0; i < nFaces; ++i)
305  {
306  os << i << ' ';
307  }
308  os << "'";
309  }
310 
311  os << " >\n"; // IndexedFaceSet
312 
313  writePoints(os, surf.points());
314 
315  os << "<Color color='" << nl;
316 
317  // writeColours(os, values, range, colorBar);
318 
319  for (const Type& val : values)
320  {
321  const scalar x = rangex(range, val);
322  vector rgb = colourTablePtr_->value(x);
323  printColour(os, rgb);
324  }
325 
326  os << "' />" << nl; // Color
327 
328  os <<
329  " </IndexedFaceSet>\n";
330 
331  endGroup(os);
332  writeFooter(os);
333  }
334 
335  wroteGeom_ = true;
336  return outputFile;
337 }
338 
339 
340 // Field writing methods
342 
343 
344 // ************************************************************************* //
Foam::colourTable::predefinedNames
static const Enum< predefinedType > predefinedNames
Enumeration names for predefinedType.
Definition: colourTable.H:108
Foam::surfaceWriters::x3dWriter
A surfaceWriter for X3D files.
Definition: x3dSurfaceWriter.H:97
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
MeshedSurfaceProxy.H
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:70
Foam::colourTable::ptr
static const colourTable * ptr(const word &tableName)
Look up pointer to colourTable by name, or nullptr on failure.
Definition: colourTables.C:88
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::meshedSurf::faces
virtual const faceList & faces() const =0
The faces used for the surface.
Foam::meshedSurf
Abstract definition of a meshed surface defined by faces and points.
Definition: meshedSurf.H:49
x3dSurfaceWriter.H
Foam::surfaceWriters::defineTypeName
defineTypeName(boundaryDataWriter)
Foam::rangex
static scalar rangex(const scalarMinMax &range, const Type &val)
A (0-1) range for colouring.
Definition: x3dSurfaceWriter.C:55
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
surfaceWriterMethods.H
Convenience macros for instantiating surfaceWriter methods.
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
OFstream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Field< vector >
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::surfaceWriters::x3dWriter::x3dWriter
x3dWriter()
Construct null.
Definition: x3dSurfaceWriter.C:91
Foam::printColour
static void printColour(Ostream &os, const vector &rgb)
Definition: x3dSurfaceWriter.C:81
Foam::meshedSurf::points
virtual const pointField & points() const =0
The points used for the surface.
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::minMaxMag
dimensioned< scalarMinMax > minMaxMag(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:330
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::colourTable::COOL_WARM
"coolToWarm"
Definition: colourTable.H:95
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::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::surfaceWriters::addToRunTimeSelectionTable
addToRunTimeSelectionTable(surfaceWriter, boundaryDataWriter, word)
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::nl
constexpr char nl
Definition: Ostream.H:372
f
labelList f(nPoints)
Foam::surfaceWriters::x3dWriter::write
virtual fileName write()
Write surface geometry to file.
Definition: x3dSurfaceWriter.C:177
Foam::Vector< scalar >
Foam::List< face >
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
points
const pointField & points
Definition: gmvOutputHeader.H:1
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76
defineSurfaceWriterWriteFields
defineSurfaceWriterWriteFields(Foam::surfaceWriters::x3dWriter)
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643