gltfCoordSetWriter.H
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) 2021-2022 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 Class
27  Foam::coordSetWriters::gltfWriter
28 
29 Description
30  A coordSet(s) writer in glTF v2 format, which is particularly
31  useful for writing track data.
32 
33  Two files are generated:
34  - filename.bin : a binary file containing all scene entities
35  - filename.gltf : a JSON file that ties fields to the binary data
36 
37  The output can contain both geometry and fields, with additional support
38  for colours using a user-supplied colour map, and animation of particle
39  tracks.
40 
41  Controls are provided via the optional formatOptions dictionary.
42 
43  For non-particle track data:
44 
45  \verbatim
46  formatOptions
47  {
48  // Apply colours flag (yes | no ) [optional]
49  colours yes;
50 
51  // List of options per field
52  fieldInfo
53  {
54  p
55  {
56  // Colour map [optional]
57  colourMap <colourMap>;
58 
59  // Colour map minimum and maximum limits [optional]
60  // Uses field min and max if not specified
61  min 0;
62  max 1;
63 
64  // Alpha channel [optional] (<scalar>)
65  alpha 0.5;
66  }
67  }
68  }
69  \verbatim
70 
71  For particle tracks:
72 
73  \verbatim
74  formatOptions
75  {
76  // Apply colours flag (yes | no) [optional]
77  colours yes;
78 
79  // Animate tracks (yes | no) [optional]
80  animate yes;
81 
82  // Animation properties [optional]
83  animationInfo
84  {
85  // Colour map [optional]
86  colourMap <colourMap>;
87 
88  // Colour [optional] (<vector> | uniform | field)
89  colour (1 0 0); // RGB in range [0-1]
90 
91  //colour uniform;
92  //colourValue (1 0 0); // RGB in range [0-1]
93 
94  //colour field;
95  //colourField d;
96 
97  // Colour map minimum and maximum limits [optional]
98  // Note: for colour = field option
99  // Uses field min and max if not specified
100  min 0;
101  max 1;
102 
103  // Alpha channel [optional] (<scalar>)
104  alpha 0.5;
105  }
106  }
107  \endverbatim
108 
109 Note
110  When writing particle animations, the particle field and colour properties
111  correspond to initial particle state (first data point) and cannot be
112  animated (limitation of the file format).
113 
114  For more information on the specification see
115  https://www.khronos.org/registry/glTF/
116 
117 SourceFiles
118  gltfCoordSetWriter.C
119 
120 \*---------------------------------------------------------------------------*/
121 
122 #ifndef Foam_coordSetWriters_gltfWriter_H
123 #define Foam_coordSetWriters_gltfWriter_H
124 
125 #include "coordSetWriter.H"
126 #include "colourTable.H"
127 #include "foamGltfFwd.H"
128 #include "MinMax.H"
129 
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
131 
132 namespace Foam
133 {
134 namespace coordSetWriters
135 {
137 /*---------------------------------------------------------------------------*\
138  Class gltfWriter Declaration
139 \*---------------------------------------------------------------------------*/
140 
141 class gltfWriter
142 :
143  public coordSetWriter
144 {
145 public:
146 
147  // Enumerations
148 
149  //- Field option used for colours
150  enum class fieldOption : char
151  {
152  NONE,
153  UNIFORM,
154  FIELD
155  };
156 
157 
158  //- Strings corresponding to the field options
160 
161 
162 private:
163 
164  // Private Data
165 
166  //- Backend output
168 
169  //- Flag to animate - for particle tracks only
170  bool animate_;
171 
172  //- Flag to add field colours
173  bool colour_;
174 
175  //- Animation colour option
176  fieldOption animateColourOption_;
177 
178  //- Animation colour field name
179  word animateColourName_;
180 
181  //- Animation colour value
182  vector animateColourValue_;
183 
184  //- Local field information
185  const dictionary fieldInfoDict_;
186 
187  //- Animation information
188  const dictionary animationDict_;
189 
190  //- The mesh indices in glTF scene
191  labelList meshes_;
192 
193 
194  // Private Member Functions
195 
196  //- Return the colour map name
197  word getColourMap(const dictionary& dict) const;
198 
199  //- Return the colour table corresponding to the colour map
200  const colourTable& getColourTable(const dictionary& dict) const;
201 
202  //- Return the named min/max field limits (from sub-dictionary)
203  scalarMinMax getFieldLimits(const word& fieldName) const;
204 
205  //- Setup animation colour or field to search for
206  void setupAnimationColour();
207 
208  //- Return the alpha field for mesh values
209  tmp<scalarField> getAlphaField(const dictionary& dict) const;
210 
211 
212  //- Templated write operation (static tracks)
213  template<class Type>
214  fileName writeTemplate
215  (
216  const word& fieldName,
217  const UPtrList<const Field<Type>>& fieldPtrs
218  );
219 
220  //- Write animated tracks
221  template<class Type>
222  fileName writeTemplate_animate
223  (
224  const word& fieldName,
225  const UPtrList<const Field<Type>>& fieldPtrs
226  );
227 
228  //- Templated write operation
229  template<class Type>
230  fileName writeTemplate
231  (
232  const word& fieldName,
233  const Field<Type>& vals
234  );
235 
236  //- Templated write operation
237  template<class Type>
238  fileName writeTemplate
239  (
240  const word& fieldName,
241  const List<Field<Type>>& fieldValues
242  );
243 
244 
245 public:
246 
247  //- Runtime type information (no debug)
248  TypeNameNoDebug("gltf");
249 
250 
251  // Constructors
252 
253  //- Default construct
254  gltfWriter();
255 
256  //- Default construct with specified options
257  explicit gltfWriter(const dictionary& options);
258 
259  //- Construct from components
260  gltfWriter
261  (
262  const coordSet& coords,
263  const fileName& outputPath,
264  const dictionary& options = dictionary()
265  );
266 
267  //- Construct from components
268  gltfWriter
269  (
270  const UPtrList<coordSet>& tracks,
271  const fileName& outputPath,
272  const dictionary& options = dictionary()
273  );
274 
275 
276  //- Destructor. Calls close()
277  virtual ~gltfWriter();
278 
279 
280  // Member Functions
281 
282  //- Expected (characteristic) output file name - information only
283  virtual fileName path() const; // override
284 
285  //- Close and reset, clears backend.
286  virtual void close(bool force = false); // override
287 
288  //- Begin time step. Clears existing backend.
289  virtual void beginTime(const Time& t); // override
290 
291  //- Begin time step. Clears existing backend.
292  virtual void beginTime(const instant& inst); // override
293 
294  //- End time step. Clears existing backend.
295  virtual void endTime(); // override
296 
297 
298  // Write
299 
306 };
307 
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 } // End namespace coordSetWriters
312 } // End namespace Foam
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 
316 #endif
317 
318 // ************************************************************************* //
Base class for generating a colour table from node points.
Definition: colourTable.H:75
dictionary dict
A class for handling file names.
Definition: fileName.H:72
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static const Enum< fieldOption > fieldOptionNames_
Strings corresponding to the field options.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
virtual void endTime()
End time step. Clears existing backend.
virtual ~gltfWriter()
Destructor. Calls close()
fieldOption
Field option used for colours.
Holds list of sampling positions.
Definition: coordSet.H:49
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
Base class for writing coordSet(s) and tracks with fields.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
virtual void beginTime(const Time &t)
Begin time step. Clears existing backend.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
virtual fileName path() const
Expected (characteristic) output file name - information only.
TypeNameNoDebug("gltf")
Runtime type information (no debug)
virtual void close(bool force=false)
Close and reset, clears backend.
Forward declarations for exposed glTF interfaces.
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
A coordSet(s) writer in glTF v2 format, which is particularly useful for writing track data...
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Tensor of scalars, i.e. Tensor<scalar>.
Namespace for OpenFOAM.