foamGltfScene.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) 2021 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 "foamGltfScene.H"
29 #include "fileName.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 :
35  objects_(),
36  meshes_(),
37  bufferViews_(),
38  accessors_(),
39  animations_(),
40  bytes_(0)
41 {}
42 
43 
44 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
45 
47 (
48  const vectorField& fld,
49  const word& name,
50  const label meshi,
51  const scalarField& alpha
52 )
53 {
54  if (meshi > meshes_.size() - 1)
55  {
57  << "Mesh " << meshi << " out of range "
58  << (meshes_.size() - 1)
59  << abort(FatalError);
60  }
61 
62  auto& bv = bufferViews_.create(name);
63  bv.byteOffset() = bytes_;
64  bv.byteLength() = fld.size()*3*sizeof(float); // 3 components
65  bv.target() = key(targetTypes::ARRAY_BUFFER);
66  bytes_ += bv.byteLength();
67 
68  auto& acc = accessors_.create(name);
69  acc.bufferViewId() = bv.id();
70  acc.set(fld, false); // no min-max
71 
72  auto& obj = objects_.create(name);
73 
74  if (alpha.size())
75  {
76  bv.byteLength() += fld.size()*sizeof(float);
77  bytes_ += fld.size()*sizeof(float);
78 
79  acc.type() = "VEC4";
80 
81  obj.addData(fld, alpha);
82  }
83  else
84  {
85  obj.addData(fld);
86  }
87 
88  meshes_[meshi].addColour(acc.id());
89 
90  return acc.id();
91 }
92 
93 
95 {
96  animations_.create(name);
97  return animations_.size() - 1;
98 }
99 
100 
102 (
103  const label animationi,
104  const label inputId,
105  const label outputId,
106  const label meshId,
107  const string& interpolation
108 )
109 {
110  if (animationi > animations_.size() - 1)
111  {
113  << "Animation " << animationi << " out of range "
114  << (animations_.size() - 1)
115  << abort(FatalError);
116  }
117 
118  const label nodeId = meshId + 1; // offset by 1 for parent node
119 
120  // Note
121  // using 1 mesh per node +1 parent node => meshes_.size() nodes in total
122  if (nodeId > meshes_.size())
123  {
125  << "Node " << nodeId << " out of range " << meshes_.size()
126  << abort(FatalError);
127  }
128 
129  animations_[animationi].addTranslation
130  (
131  inputId,
132  outputId,
133  nodeId,
135  );
136 }
137 
138 
140 {
141  const fileName base(os.name().lessExt());
142  const fileName binFile
143  (
144  fileName::concat(base.path(), fileName::name(base) + ".bin")
145  );
146 
147  // Write binary file
148  // Note: using stdStream
149  OFstream bin(binFile, IOstream::BINARY);
150  auto& osbin = bin.stdStream();
151 
152  label totalBytes = 0;
153  for (const auto& object : objects_.data())
154  {
155  for (const auto& data : object.data())
156  {
157  osbin.write
158  (
159  reinterpret_cast<const char*>(&data),
160  sizeof(float)
161  );
162 
163  totalBytes += sizeof(float);
164  }
165  }
166 
167  // Write json file
168  os << "{" << nl << incrIndent;
169 
170  os << indent << "\"asset\" : {" << nl << incrIndent
171  << indent << "\"generator\" : \"OpenFOAM - www.openfoam.com\"," << nl
172  << indent << "\"version\" : \"2.0\"" << nl << decrIndent
173  << indent << "}," << nl;
174 
175  os << indent << "\"extras\" : {" << nl << incrIndent
176  /* << content */
177  << decrIndent
178  << indent << "}," << nl;
179 
180  os << indent << "\"scene\": 0," << nl;
181 
182  os << indent << "\"scenes\": [{" << nl << incrIndent
183  << indent << "\"nodes\" : [0]" << nl << decrIndent
184  << indent << "}]," << nl;
185 
186  os << indent << "\"buffers\" : [{" << nl << incrIndent
187  << indent << "\"uri\" : " << string(fileName::name(binFile))
188  << "," << nl
189  << indent << "\"byteLength\" : " << totalBytes << nl << decrIndent
190  << indent << "}]," << nl;
191 
192  os << indent << "\"nodes\" : [" << nl << incrIndent
193  << indent << "{" << nl << incrIndent
194  << indent << "\"children\" : [" << nl << incrIndent;
195 
196  // List of child node indices
197  os << indent;
198  forAll(meshes_, meshi)
199  {
200  const label nodeId = meshi + 1;
201 
202  os << nodeId;
203 
204  if (meshi != meshes_.size() - 1) os << ", ";
205 
206  if ((meshi+1) % 10 == 0) os << nl << indent;
207  }
208 
209  os << decrIndent << nl << indent << "]," << nl
210  << indent << "\"name\" : \"parent\"" << nl << decrIndent
211  << indent << "}," << nl;
212 
213  // List of child meshes
214  forAll(meshes_, meshi)
215  {
216  os << indent << "{" << nl << incrIndent
217  << indent << "\"mesh\" : " << meshi << nl << decrIndent
218  << indent << "}";
219 
220  if (meshi != meshes_.size() - 1) os << ",";
221 
222  os << nl;
223  }
224 
225  os << decrIndent << indent << "]";
226 
227  meshes_.write(os, "meshes");
228 
229  bufferViews_.write(os, "bufferViews");
230 
231  accessors_.write(os, "accessors");
232 
233  animations_.write(os, "animations");
234 
235  os << nl;
236 
237  os << decrIndent << "}" << endl;
238 }
239 
240 
241 // ************************************************************************* //
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::glTF::scene::scene
scene()
Default construct.
Definition: foamGltfScene.C:33
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:212
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
foamGltfScene.H
Foam::glTF::scene::addToAnimation
void addToAnimation(const label animationi, const label inputId, const label outputId, const label meshId, const string &interpolation="LINEAR")
Add to existing animation.
Definition: foamGltfScene.C:102
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:346
Foam::fileName::name
word name() const
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:212
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::glTF::targetTypes::ARRAY_BUFFER
vertex attributes
Foam::glTF::base
Base class for glTF entities.
Definition: foamGltfBase.H:121
Foam::Field< vector >
fileName.H
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::interpolation
Abstract base class for interpolation.
Definition: mappedPatchFieldBase.H:96
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:353
Foam::glTF::scene::write
void write(Ostream &os)
Write to stream (JSON and binary data)
Definition: foamGltfScene.C:139
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::OFstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OFstream.C:102
Foam::glTF::scene::addColourToMesh
label addColourToMesh(const vectorField &fld, const word &name, const label meshi, const scalarField &alpha=scalarField())
Returns accessor index.
Definition: foamGltfScene.C:47
Foam::glTF::scene::createAnimation
label createAnimation(const word &name)
Returns index of last animation.
Definition: foamGltfScene.C:94
Foam::fileName::concat
static fileName concat(const std::string &s1, const std::string &s2, const char delim='/')
Join two strings with a path separator ('/' by default).
Definition: fileName.C:218
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55