GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.7% 251 / 0 / 293
Functions: 92.9% 26 / 0 / 28
Branches: 58.7% 175 / 0 / 298

src/gltf/GltfForwardTranslator.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "GltfForwardTranslator.hpp"
7 #include "GltfUtils.hpp"
8 #include "GltfMetaData.hpp"
9 #include "GltfUserData.hpp"
10 #include "GltfModelObjectMetaData.hpp"
11 #include "GltfBoundingBox.hpp"
12 #include "GltfMaterialData.hpp"
13
14 #include "../model/Model.hpp"
15
16 #include "../model/RenderingColor.hpp"
17 #include "../model/ConstructionBase.hpp"
18 #include "../model/ConstructionBase_Impl.hpp"
19 #include "../model/ConstructionAirBoundary.hpp"
20 #include "../model/ConstructionAirBoundary_Impl.hpp"
21 #include "../model/AirLoopHVAC.hpp"
22 #include "../model/AirLoopHVAC_Impl.hpp"
23 #include "../model/ThermalZone.hpp"
24 #include "../model/ThermalZone_Impl.hpp"
25 #include "../model/SpaceType.hpp"
26 #include "../model/SpaceType_Impl.hpp"
27 #include "../model/Building.hpp"
28 #include "../model/Building_Impl.hpp"
29 #include "../model/BuildingStory.hpp"
30 #include "../model/BuildingStory_Impl.hpp"
31 #include "../model/BuildingUnit.hpp"
32 #include "../model/BuildingUnit_Impl.hpp"
33 #include "../model/Surface.hpp"
34 #include "../model/Surface_Impl.hpp"
35 #include "../model/SubSurface.hpp"
36 #include "../model/SubSurface_Impl.hpp"
37 #include "../model/ShadingSurface.hpp"
38 #include "../model/ShadingSurface_Impl.hpp"
39 #include "../model/InteriorPartitionSurface.hpp"
40 #include "../model/InteriorPartitionSurface_Impl.hpp"
41 #include "../model/PlanarSurfaceGroup.hpp"
42 #include "../model/PlanarSurfaceGroup_Impl.hpp"
43 #include "../model/PlanarSurface.hpp"
44 #include "../model/PlanarSurface_Impl.hpp"
45 #include "../model/Space.hpp"
46 #include "../model/Space_Impl.hpp"
47 #include "../model/DefaultConstructionSet.hpp"
48 #include "../model/DefaultConstructionSet_Impl.hpp"
49 #include "../model/ShadingSurfaceGroup.hpp"
50 #include "../model/InteriorPartitionSurfaceGroup.hpp"
51
52 #include "../utilities/core/Assert.hpp"
53 #include "../utilities/core/Compare.hpp"
54 #include "../utilities/geometry/Point3d.hpp"
55 #include "../utilities/geometry/Plane.hpp"
56 #include "../utilities/geometry/BoundingBox.hpp"
57 #include "../utilities/geometry/Transformation.hpp"
58 #include "../utilities/geometry/Geometry.hpp"
59 #include "../utilities/geometry/Polygon3d.hpp"
60
61 #define TINYGLTF_IMPLEMENTATION
62 #define STB_IMAGE_IMPLEMENTATION
63 #define STB_IMAGE_WRITE_IMPLEMENTATION
64
65 #include <tiny_gltf.h>
66
67 #include <algorithm>
68 #include <iterator>
69 #include <numeric>
70 #include <type_traits>
71
72 namespace openstudio {
73 namespace gltf {
74
75
1/1
✓ Branch 2 taken 27 times.
27 GltfForwardTranslator::GltfForwardTranslator() {
76
1/1
✓ Branch 1 taken 27 times.
27 m_logSink.setLogLevel(Warn);
77
1/1
✓ Branch 2 taken 27 times.
27 m_logSink.setThreadId(std::this_thread::get_id());
78 27 }
79
80 ✗ std::vector<LogMessage> GltfForwardTranslator::warnings() const {
81 ✗ std::vector<LogMessage> result;
82 ✗ for (const auto& logMessage : m_logSink.logMessages()) {
83 ✗ if (logMessage.logLevel() == Warn) {
84 ✗ result.push_back(logMessage);
85 }
86 ✗ }
87 ✗ return result;
88 ✗ }
89
90 ✗ std::vector<LogMessage> GltfForwardTranslator::errors() const {
91 ✗ std::vector<LogMessage> result;
92 ✗ for (const auto& logMessage : m_logSink.logMessages()) {
93 ✗ if (logMessage.logLevel() > Warn) {
94 ✗ result.push_back(logMessage);
95 }
96 ✗ }
97 ✗ return result;
98 ✗ }
99
100 // Initializes our two main Buffer Views
101 // one for the indices & other for Coordinates and Normals
102 21 void initBufferViews(tinygltf::BufferView& indicesBv, tinygltf::BufferView& coordinatesBv) {
103 21 indicesBv.buffer = 0;
104 // defining bytestride is not required in this case
105 21 indicesBv.target = TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER;
106
107 // The vertices take up 36 bytes (3 vertices * 3 floating points * 4 bytes)
108 // at position 8 in the buffer and are of type ARRAY_BUFFER
109 21 coordinatesBv.buffer = 0;
110 21 coordinatesBv.target = TINYGLTF_TARGET_ARRAY_BUFFER;
111 21 coordinatesBv.byteStride = 12;
112 21 }
113
114 // Finds the vertex Index from all Vertices of a planar Surface
115 // param : point3d
116 // param : allPoints
117 // param : tol
118 // returns : index of the Vertex
119 97278 size_t getOrCreateVertexIndexT(const Point3d& point3d, std::vector<Point3d>& allPoints, double tol = 0.001) {
120 97278 size_t n = allPoints.size();
121
2/2
✓ Branch 0 taken 594158 times.
✓ Branch 1 taken 53124 times.
647282 for (size_t i = 0; i < n; ++i) {
122
2/2
✓ Branch 2 taken 44154 times.
✓ Branch 3 taken 550004 times.
594158 if (openstudio::getDistance(point3d, allPoints[i]) < tol) {
123 44154 return i;
124 }
125 }
126 53124 allPoints.push_back(point3d);
127 53124 return (allPoints.size() - 1);
128 }
129
130 template <typename T>
131 168 std::vector<T> getObjectsAndSort(const model::Model& model) {
132 168 std::vector<T> objects;
133 if constexpr (std::is_same_v<T, model::PlanarSurface> || std::is_same_v<T, model::PlanarSurfaceGroup>) {
134
2/2
std::vector<openstudio::model::PlanarSurface, std::allocator<openstudio::model::PlanarSurface> > openstudio::gltf::getObjectsAndSort<openstudio::model::PlanarSurface>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::PlanarSurfaceGroup, std::allocator<openstudio::model::PlanarSurfaceGroup> > openstudio::gltf::getObjectsAndSort<openstudio::model::PlanarSurfaceGroup>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
42 objects = model.getModelObjects<T>();
135 } else {
136
6/6
std::vector<openstudio::model::AirLoopHVAC, std::allocator<openstudio::model::AirLoopHVAC> > openstudio::gltf::getObjectsAndSort<openstudio::model::AirLoopHVAC>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::BuildingStory, std::allocator<openstudio::model::BuildingStory> > openstudio::gltf::getObjectsAndSort<openstudio::model::BuildingStory>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::BuildingUnit, std::allocator<openstudio::model::BuildingUnit> > openstudio::gltf::getObjectsAndSort<openstudio::model::BuildingUnit>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::DefaultConstructionSet, std::allocator<openstudio::model::DefaultConstructionSet> > openstudio::gltf::getObjectsAndSort<openstudio::model::DefaultConstructionSet>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::SpaceType, std::allocator<openstudio::model::SpaceType> > openstudio::gltf::getObjectsAndSort<openstudio::model::SpaceType>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
std::vector<openstudio::model::ThermalZone, std::allocator<openstudio::model::ThermalZone> > openstudio::gltf::getObjectsAndSort<openstudio::model::ThermalZone>(openstudio::model::Model const&):
✓ Branch 1 taken 21 times.
126 objects = model.getConcreteModelObjects<T>();
137 }
138
8/8
std::vector<openstudio::model::AirLoopHVAC, std::allocator<openstudio::model::AirLoopHVAC> > openstudio::gltf::getObjectsAndSort<openstudio::model::AirLoopHVAC>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::BuildingStory, std::allocator<openstudio::model::BuildingStory> > openstudio::gltf::getObjectsAndSort<openstudio::model::BuildingStory>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::BuildingUnit, std::allocator<openstudio::model::BuildingUnit> > openstudio::gltf::getObjectsAndSort<openstudio::model::BuildingUnit>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::DefaultConstructionSet, std::allocator<openstudio::model::DefaultConstructionSet> > openstudio::gltf::getObjectsAndSort<openstudio::model::DefaultConstructionSet>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::PlanarSurface, std::allocator<openstudio::model::PlanarSurface> > openstudio::gltf::getObjectsAndSort<openstudio::model::PlanarSurface>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::PlanarSurfaceGroup, std::allocator<openstudio::model::PlanarSurfaceGroup> > openstudio::gltf::getObjectsAndSort<openstudio::model::PlanarSurfaceGroup>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::SpaceType, std::allocator<openstudio::model::SpaceType> > openstudio::gltf::getObjectsAndSort<openstudio::model::SpaceType>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
std::vector<openstudio::model::ThermalZone, std::allocator<openstudio::model::ThermalZone> > openstudio::gltf::getObjectsAndSort<openstudio::model::ThermalZone>(openstudio::model::Model const&):
✓ Branch 3 taken 21 times.
168 std::sort(objects.begin(), objects.end(), WorkspaceObjectNameLess());
139 168 return objects;
140 ✗ }
141
142 21 boost::optional<tinygltf::Model> GltfForwardTranslator::toGltfModel(const model::Model& model, std::function<void(double)> updatePercentage) {
143 // MAIN PIPELINE TO TRANSLATE OPENSTUDIO MODEL -> GLTF MODEL
144
145 21 bool triangulateSurfaces = true; //we're always triangulating the surfaces to get the best possible output.
146
147 // TODO: cleanup
148
1/1
✓ Branch 1 taken 21 times.
21 tinygltf::Model gltfModel;
149 21 std::string err;
150 21 std::string warning;
151 21 path p;
152
153 21 std::vector<tinygltf::Accessor>& accessors = gltfModel.accessors;
154 21 std::vector<tinygltf::Material>& materials = gltfModel.materials;
155 21 std::vector<tinygltf::Mesh>& meshes = gltfModel.meshes;
156 21 std::vector<tinygltf::Node>& nodes = gltfModel.nodes;
157
158
1/1
✓ Branch 1 taken 21 times.
21 tinygltf::Scene& scene = gltfModel.scenes.emplace_back();
159
1/1
✓ Branch 1 taken 21 times.
21 tinygltf::Buffer& buffer = gltfModel.buffers.emplace_back();
160
161 // Note: Can't emplace_back twice and store refs (before a reserve at least),
162 // because the first ref would be invalidated by the second emplace_back, so just resize and get refs after that
163
1/1
✓ Branch 1 taken 21 times.
21 gltfModel.bufferViews.resize(2);
164 21 tinygltf::BufferView& indicesBv = gltfModel.bufferViews.front();
165 21 tinygltf::BufferView& coordinatesBv = gltfModel.bufferViews.back();
166 21 std::vector<unsigned char> indicesBuffer;
167 21 std::vector<unsigned char> coordinatesBuffer;
168
169 // Start Region INIT
170
171 // Define the asset. The version is required
172
1/1
✓ Branch 1 taken 21 times.
21 gltfModel.asset.version = "2.0";
173
1/1
✓ Branch 1 taken 21 times.
21 gltfModel.asset.generator = "OpenStudio";
174
175 // Init the scene
176 // aware of all other nodes, meshes, materials, BufferViews, Accessors tightly
177 // tied up in a Hierarchy using indices pointing one to other.
178
1/1
✓ Branch 1 taken 21 times.
21 scene.nodes = {0};
179
180 // I'm going to be emplacing back elements in nodes a bunch later, which is going to invalidate the references, so limit scope
181 {
182
1/1
✓ Branch 1 taken 21 times.
21 tinygltf::Node& topNode = nodes.emplace_back();
183
1/1
✓ Branch 1 taken 21 times.
21 topNode.name = "Z_UP";
184
1/1
✓ Branch 1 taken 21 times.
21 topNode.matrix = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
185 }
186
187
1/1
✓ Branch 1 taken 21 times.
21 initBufferViews(indicesBv, coordinatesBv);
188
189 // End Region INIT
190
191 /*
192
193 This comprises of the other nodes that GLTF provides
194 for further addition of complex details, textures, lights, etc.
195
196 std::vector<tinygltf::Scene> scenes;
197 std::vector<tinygltf::Buffer> buffers;
198 std::vector<tinygltf::Animation> animations;
199 std::vector<tinygltf::Texture> textures;
200 std::vector<tinygltf::Image> images;
201 std::vector<tinygltf::Skin> skins;
202 std::vector<tinygltf::Sampler> samplers;
203 std::vector<tinygltf::Camera> cameras;
204 std::vector<tinygltf::Light> lights;
205
206 */
207
208 // get number of things to translate
209 // make it deterministic by sorting!
210
1/1
✓ Branch 1 taken 21 times.
21 auto planarSurfaces = getObjectsAndSort<model::PlanarSurface>(model);
211
1/1
✓ Branch 1 taken 21 times.
21 auto planarSurfaceGroups = getObjectsAndSort<model::PlanarSurfaceGroup>(model);
212
1/1
✓ Branch 1 taken 21 times.
21 auto buildingStories = getObjectsAndSort<model::BuildingStory>(model);
213
1/1
✓ Branch 1 taken 21 times.
21 auto buildingUnits = getObjectsAndSort<model::BuildingUnit>(model);
214
1/1
✓ Branch 1 taken 21 times.
21 auto thermalZones = getObjectsAndSort<model::ThermalZone>(model);
215
1/1
✓ Branch 1 taken 21 times.
21 auto airLoopHVACs = getObjectsAndSort<model::AirLoopHVAC>(model);
216
1/1
✓ Branch 1 taken 21 times.
21 auto spaceTypes = getObjectsAndSort<model::SpaceType>(model);
217
1/1
✓ Branch 1 taken 21 times.
21 auto defaultConstructionSets = getObjectsAndSort<model::DefaultConstructionSet>(model);
218 21 double n = 0;
219
220 21 size_t N = planarSurfaces.size() + planarSurfaceGroups.size() + buildingStories.size() + buildingUnits.size() + thermalZones.size()
221 21 + airLoopHVACs.size() + spaceTypes.size() + defaultConstructionSets.size() + 1;
222
223
1/1
✓ Branch 1 taken 21 times.
21 updatePercentage(0.0);
224
225 // Start Region CREATE MATERIALS
226 // add model specific materials
227 // End Region CREATE MATERIALS
228
229
3/5
✓ Branch 1 taken 21 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 21 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
63 TransformationVector transformStack{Transformation{}};
230
231
1/1
✓ Branch 1 taken 21 times.
42 std::vector<double> matrixDefaultTransformation{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0};
232
233 // We prepare a vector of Materials
234
1/1
✓ Branch 1 taken 21 times.
21 std::vector<GltfMaterialData> allMaterials = GltfMaterialData::buildMaterials(model);
235
236 // TODO: make it deterministic by sorting!
237 // std::sort(objects.begin(), objects.end(), WorkspaceObjectNameLess());
238
239
2/2
✓ Branch 5 taken 11518 times.
✓ Branch 6 taken 21 times.
11539 for (const auto& planarSurface : planarSurfaces) {
240 // Start Region MAIN LOOP
241 //
242 // TODO: MOVE THAT ENTIRE LOGIC TO THE GltfUserData file? (and rename to GltfPlanarSurfaceData and make it export a Node directly)
243
1/1
✓ Branch 1 taken 11518 times.
11518 std::string planarSurfaceName = planarSurface.nameString();
244 // Construct in place
245
1/1
✓ Branch 1 taken 11518 times.
11518 tinygltf::Node& node = nodes.emplace_back();
246
1/1
✓ Branch 1 taken 11518 times.
11518 node.name = planarSurfaceName;
247
248 // Now the geometry
249
1/1
✓ Branch 1 taken 11518 times.
11518 Transformation buildingTransformation;
250
3/3
✓ Branch 1 taken 11518 times.
✓ Branch 3 taken 11434 times.
✓ Branch 4 taken 84 times.
23036 if (boost::optional<model::PlanarSurfaceGroup> planarSurfaceGroup_ = planarSurface.planarSurfaceGroup()) {
251
2/2
✓ Branch 1 taken 11434 times.
✓ Branch 4 taken 11434 times.
11434 buildingTransformation = planarSurfaceGroup_->buildingTransformation();
252 11518 }
253 // get the vertices
254
1/1
✓ Branch 1 taken 11518 times.
11518 Point3dVector vertices = planarSurface.vertices();
255
2/2
✓ Branch 1 taken 11518 times.
✓ Branch 4 taken 11518 times.
11518 Transformation& t = transformStack.emplace_back(Transformation::alignFace(vertices));
256
1/1
✓ Branch 1 taken 11518 times.
11518 Transformation tInv = t.inverse();
257
2/2
✓ Branch 1 taken 11518 times.
✓ Branch 4 taken 11518 times.
11518 Point3dVector faceVertices = reverse(tInv * vertices);
258
2/2
✓ Branch 1 taken 11518 times.
✓ Branch 4 taken 11518 times.
11518 std::vector<double> matrix = openstudio::toStandardVector(buildingTransformation.vector());
259
260 // Adding a check to avoid warning "NODE_MATRIX_DEFAULT" <Do not specify default transform matrix>.
261 // This is the identity_matrix<4>
262
3/3
✓ Branch 1 taken 11518 times.
✓ Branch 3 taken 7733 times.
✓ Branch 4 taken 3785 times.
11518 if (matrixDefaultTransformation != matrix) {
263
1/1
✓ Branch 1 taken 7733 times.
7733 node.matrix = matrix;
264 } else {
265
1/1
✓ Branch 1 taken 3785 times.
3785 node.matrix = {};
266 }
267 11518 node.mesh = meshes.size();
268
269 // EXTRAS
270
271 // TODO: Based on flag add UserData to nodes..
272 // Initializes all userdata attributes as per the planar Surface
273
1/1
✓ Branch 1 taken 11518 times.
11518 GltfUserData glTFUserData(planarSurface);
274
275
1/1
✓ Branch 1 taken 11518 times.
11518 tinygltf::Mesh& targetMesh = meshes.emplace_back();
276
1/1
✓ Branch 1 taken 11518 times.
11518 targetMesh.name = planarSurfaceName;
277
278
1/1
✓ Branch 1 taken 11518 times.
11518 auto matName = glTFUserData.surfaceTypeMaterialName();
279 11518 size_t materialIndex = 0;
280
1/1
✓ Branch 4 taken 11518 times.
43957 auto it = std::find_if(materials.cbegin(), materials.cend(), [&matName](const auto& mat) { return mat.name == matName; });
281
2/2
✓ Branch 2 taken 11404 times.
✓ Branch 3 taken 114 times.
11518 if (it != materials.cend()) {
282 22808 materialIndex = std::distance(materials.cbegin(), it);
283 } else {
284
2/2
✓ Branch 1 taken 1512 times.
✓ Branch 8 taken 114 times.
1626 auto it2 = std::find_if(allMaterials.cbegin(), allMaterials.cend(), [&matName](const auto& mat) { return mat.materialName() == matName; });
285
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 114 times.
114 if (it2 == allMaterials.cend()) {
286 ✗ it2 = allMaterials.cbegin(); // This is always the "Undefined" material
287 }
288 114 materialIndex = materials.size();
289 // add to gltfModel
290
2/2
✓ Branch 2 taken 114 times.
✓ Branch 5 taken 114 times.
114 materials.emplace_back(it2->toGltf());
291 }
292
293 // get vertices of all sub surfaces
294 11518 Point3dVectorVector faceSubVertices;
295
3/3
✓ Branch 1 taken 11518 times.
✓ Branch 3 taken 10148 times.
✓ Branch 4 taken 1370 times.
23036 if (auto surface_ = planarSurface.optionalCast<model::Surface>()) {
296
4/4
✓ Branch 1 taken 10148 times.
✓ Branch 4 taken 10148 times.
✓ Branch 10 taken 1341 times.
✓ Branch 11 taken 10148 times.
11489 for (const auto& subSurface : surface_->subSurfaces()) {
297
4/4
✓ Branch 1 taken 1341 times.
✓ Branch 4 taken 1341 times.
✓ Branch 7 taken 1341 times.
✓ Branch 10 taken 1341 times.
1341 faceSubVertices.push_back(reverse(tInv * subSurface.vertices()));
298 10148 }
299 11518 }
300
301 11518 Point3dVectorVector finalFaceVertices;
302
1/2
✓ Branch 0 taken 11518 times.
✗ Branch 1 not taken.
11518 if (triangulateSurfaces) {
303
1/1
✓ Branch 1 taken 11518 times.
11518 finalFaceVertices = computeTriangulation(faceVertices, faceSubVertices);
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11518 times.
11518 if (finalFaceVertices.empty()) {
305 ✗ LOG_FREE(Error, "modelToGLTF",
306 "Failed to triangulate surface " << planarSurfaceName << " with " << faceSubVertices.size() << " sub surfaces");
307 }
308 } else {
309 ✗ finalFaceVertices.push_back(faceVertices);
310 }
311
312 11518 Point3dVector allVertices;
313 11518 Point3dVector triangleVertices;
314 11518 std::vector<size_t> faceIndices;
315
2/2
✓ Branch 5 taken 32426 times.
✓ Branch 6 taken 11518 times.
43944 for (const auto& finalFaceVerts : finalFaceVertices) {
316
1/1
✓ Branch 1 taken 32426 times.
32426 Point3dVector finalVerts = t * finalFaceVerts;
317
1/1
✓ Branch 5 taken 32426 times.
32426 triangleVertices.insert(triangleVertices.end(), finalVerts.begin(), finalVerts.end());
318 32426 auto it = finalVerts.rbegin();
319 32426 auto itend = finalVerts.rend();
320
2/2
✓ Branch 1 taken 97278 times.
✓ Branch 2 taken 32426 times.
129704 for (; it != itend; ++it) {
321
2/2
✓ Branch 2 taken 97278 times.
✓ Branch 5 taken 97278 times.
97278 faceIndices.push_back(getOrCreateVertexIndexT(*it, allVertices));
322 }
323 32426 }
324
325
1/1
✓ Branch 1 taken 11518 times.
11518 Vector3d outwardNormal = planarSurface.outwardNormal();
326
1/1
✓ Branch 2 taken 11518 times.
11518 Vector3dVector normalVectors(allVertices.size(), outwardNormal);
327
328
1/1
✓ Branch 1 taken 11518 times.
11518 detail::ShapeComponentIds shapeComponentIds(faceIndices, allVertices, normalVectors, indicesBuffer, coordinatesBuffer, accessors);
329
330
1/1
✓ Branch 1 taken 11518 times.
11518 tinygltf::Primitive& thisPrimitive = targetMesh.primitives.emplace_back();
331
2/2
✓ Branch 1 taken 11518 times.
✓ Branch 4 taken 11518 times.
23036 thisPrimitive.attributes["NORMAL"] = shapeComponentIds.normalsAccessorId;
332
2/2
✓ Branch 1 taken 11518 times.
✓ Branch 4 taken 11518 times.
23036 thisPrimitive.attributes["POSITION"] = shapeComponentIds.verticesAccessorId;
333 11518 thisPrimitive.indices = shapeComponentIds.indicesAccessorId;
334 11518 thisPrimitive.material = static_cast<int>(materialIndex);
335 11518 thisPrimitive.mode = TINYGLTF_MODE_TRIANGLES;
336
337 // TODO: Based on a flag override UserData attribute
338 // Addition of UserData as Extras to the node
339
1/1
✓ Branch 1 taken 11518 times.
11518 node.extras = tinygltf::Value(glTFUserData.toExtras());
340
341 11518 n += 1;
342
1/1
✓ Branch 1 taken 11518 times.
11518 updatePercentage(100.0 * n / N);
343 // End Region MAIN
344 11518 }
345 // Start Region BUILD SCENE | ELEMENT
346
347
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (coordinatesBuffer.empty()) {
348 ✗ return boost::none;
349 }
350
351 // Write the buffer data
352 // TODO: why use a single buffer for two buffersView instead of one buffer for each?
353 // no *.bin file is involved | everything is integrated in the mail output gltf file only.
354 // Having a separate input file for the GLTF is old now everything resides in the main GLTF file only... as a binary buffer data.
355 21 auto padding = indicesBuffer.size() % 4;
356
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 21 times.
61 for (unsigned int i = 0; i < padding; i++) {
357
1/1
✓ Branch 1 taken 40 times.
40 indicesBuffer.push_back(0x00); // padding bytes
358 }
359
360
1/1
✓ Branch 1 taken 21 times.
21 std::vector<unsigned char> allBuffer = indicesBuffer; //std::move(_indicesBuffer);
361
1/1
✓ Branch 5 taken 21 times.
21 allBuffer.insert(allBuffer.end(), coordinatesBuffer.begin(), coordinatesBuffer.end());
362
363
1/1
✓ Branch 1 taken 21 times.
21 buffer.data = allBuffer;
364
365 21 indicesBv.byteLength = indicesBuffer.size();
366 21 indicesBv.byteOffset = 0;
367
368 21 coordinatesBv.byteLength = coordinatesBuffer.size();
369 21 coordinatesBv.byteOffset = indicesBuffer.size();
370 // End Region BUILD SCENE | ELEMENT
371
372 // Other tie ups
373
374 // Declare all nodes as a child of topNode
375 21 auto& topNodeChildren = nodes.front().children;
376
1/1
✓ Branch 2 taken 21 times.
21 topNodeChildren.resize(nodes.size() - 1);
377 21 std::iota(topNodeChildren.begin(), topNodeChildren.end(), 1);
378
379 // SCENE EXTRAS | this will be having all the metadata
380
2/2
✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
21 scene.extras = tinygltf::Value(GltfMetaData(model).toExtras());
381
382
1/1
✓ Branch 1 taken 21 times.
21 return gltfModel;
383 42 }
384
385 5 std::string GltfForwardTranslator::modelToGLTFString(const model::Model& model) {
386
1/1
✓ Branch 2 taken 5 times.
5 boost::optional<tinygltf::Model> gltfModel_ = toGltfModel(model, [](double percentage) {});
387
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!gltfModel_) {
388 ✗ LOG(Error, "Failed to prepare GLTF model");
389 ✗ return "";
390 }
391
392
2/2
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
5 auto gltfModel = gltfModel_.get();
393
394 // Save it to a file
395 // glTF Parser/Serialier context
396 5 tinygltf::TinyGLTF ctx;
397 // Uncomment this to use extras_as_string [std::string] instead of extras [tinygltf::Value]
398 5 ctx.SetStoreOriginalJSONForExtrasAndExtensions(true);
399
400
1/1
✓ Branch 1 taken 5 times.
5 std::stringstream ss;
401
1/1
✓ Branch 1 taken 5 times.
5 bool ret = ctx.WriteGltfSceneToStream(&gltfModel, ss,
402 true, // pretty print
403 false); // write binary
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!ret) {
405 ✗ LOG(Error, "Writing GLTF failed");
406 }
407
1/1
✓ Branch 1 taken 5 times.
5 return ss.str();
408 5 }
409
410 // Exports a gltf against a Model
411 // returns : exports a GLTF file against a Model
412 16 bool GltfForwardTranslator::modelToGLTF(const model::Model& model, const path& outputPath) {
413
1/1
✓ Branch 2 taken 16 times.
16 return modelToGLTF(model, [](double percentage) {}, outputPath);
414 }
415
416 16 bool GltfForwardTranslator::modelToGLTF(const model::Model& model, std::function<void(double)> updatePercentage, const path& outputPath) {
417
418
1/1
✓ Branch 2 taken 16 times.
16 boost::optional<tinygltf::Model> gltfModel_ = toGltfModel(model, [](double percentage) {});
419
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!gltfModel_) {
420 ✗ LOG(Error, "Failed to prepare GLTF model");
421 ✗ return "";
422 }
423
424 // Save it to a file
425
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
16 auto gltfModel = gltfModel_.get();
426
427 // glTF Parser/Serialier context
428 16 tinygltf::TinyGLTF ctx;
429 // Uncomment this to use extras_as_string [std::string] instead of extras [tinygltf::Value]
430 16 ctx.SetStoreOriginalJSONForExtrasAndExtensions(true);
431
2/2
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
16 bool ret = ctx.WriteGltfSceneToFile(&gltfModel, toString(outputPath),
432 true, // embedImages
433 true, // embedBuffers
434 true, // pretty print
435 false); // write binary
436
437
1/1
✓ Branch 1 taken 16 times.
16 updatePercentage(100.0);
438
439 16 return ret;
440 16 }
441
442 // TODO: either rename, or properly populate the model...
443 // To populate a GLTF Model from an existing GLTF file.
444 // also exports a gltf file with a .bin file (non embeded version).
445 1 bool GltfForwardTranslator::loadGLTF(const path& inputPath, const path& inputNonEmbededPath) {
446 1 tinygltf::TinyGLTF loader;
447
1/1
✓ Branch 1 taken 1 time.
1 tinygltf::Model gltf_Model;
448 1 std::string err;
449 1 std::string warning;
450
1/1
✓ Branch 1 taken 1 time.
1 std::string fileName = toString(inputPath);
451
1/1
✓ Branch 1 taken 1 time.
1 bool ret = loader.LoadASCIIFromFile(&gltf_Model, &err, &warning, fileName);
452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!err.empty()) {
453 ✗ LOG(Error, "Error loading GLTF " << err);
454 //ret = false;
455 }
456
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!warning.empty()) {
457 ✗ LOG(Warn, "Error loading GLTF " << warning);
458 }
459
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!ret) {
461 ✗ LOG(Error, "Failed to parse glTF");
462 } else {
463
1/1
✓ Branch 1 taken 1 time.
1 std::string output_filename = toString(inputNonEmbededPath);
464
2/2
✓ Branch 2 taken 1 time.
✓ Branch 5 taken 1 time.
1 std::string embedded_filename = output_filename.substr(0, output_filename.size() - 5) + "-Embedded.gltf";
465
1/1
✓ Branch 1 taken 1 time.
1 bool a = loader.WriteGltfSceneToFile(&gltf_Model, output_filename);
466
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
1 OS_ASSERT(a);
467 // Embedd buffers and (images if present)
468 #ifndef TINYGLTF_NO_STB_IMAGE_WRITE
469
1/1
✓ Branch 1 taken 1 time.
1 bool b = loader.WriteGltfSceneToFile(&gltf_Model, embedded_filename, true, true);
470
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
1 OS_ASSERT(b);
471 #endif
472 1 }
473 1 return ret;
474 1 }
475
476 1 bool GltfForwardTranslator::loadGLTF(const path& inputPath) {
477 1 tinygltf::TinyGLTF loader;
478
1/1
✓ Branch 1 taken 1 time.
1 tinygltf::Model gltf_Model;
479 1 std::string err;
480 1 std::string warning;
481
1/1
✓ Branch 1 taken 1 time.
1 std::string fileName = toString(inputPath);
482
1/1
✓ Branch 1 taken 1 time.
1 bool ret = loader.LoadASCIIFromFile(&gltf_Model, &err, &warning, fileName);
483
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!err.empty()) {
484 ✗ LOG(Error, "Error loading GLTF: " << err);
485 //ret = false;
486 }
487
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (!warning.empty()) {
488 ✗ LOG(Warn, "Warning loading GLTF: " << warning);
489 }
490
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!ret) {
492 ✗ LOG(Error, "Failed to parse glTF");
493 } else {
494 // TODO: get the materials too?
495
496 // USERDATA INITIALIZATION FROM EACH NODE's EXTRAS
497 1 m_userDataCollection.clear();
498 1 bool isTopNode = true;
499
2/2
✓ Branch 5 taken 31 times.
✓ Branch 6 taken 1 time.
32 for (const auto& node : gltf_Model.nodes) {
500
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
31 if (isTopNode) {
501 1 isTopNode = false;
502 1 continue;
503 }
504
2/2
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 30 times.
30 m_userDataCollection.emplace_back(GltfUserData(node.extras));
505 }
506
507 // METADATA INITIALIZATION FROM SCENE's EXTRAS
508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 if (gltf_Model.scenes.size() > 1) {
509 ✗ LOG(Warn, "Found more than one scene object, using the first found, occurred in GLTF File '" << toString(inputPath) << "'");
510
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1 } else if (gltf_Model.scenes.empty()) {
511 ✗ LOG_AND_THROW("Could not find a scene object in the GTLF file '" << toString(inputPath) << "'");
512 }
513
1/1
✓ Branch 2 taken 1 time.
1 m_gltfMetaData = GltfMetaData(gltf_Model.scenes[0].extras);
514 }
515 1 return ret;
516 1 }
517
518 1 std::vector<GltfUserData> GltfForwardTranslator::getUserDataCollection() const {
519 1 return m_userDataCollection;
520 }
521
522 1 boost::optional<GltfUserData> GltfForwardTranslator::getUserDataBySurfaceName(const std::string& surfaceName) const {
523 1 boost::optional<GltfUserData> result;
524
525 auto it =
526
2/2
✓ Branch 1 taken 7 times.
✓ Branch 8 taken 1 time.
8 std::find_if(m_userDataCollection.begin(), m_userDataCollection.end(), [&surfaceName](const auto& obj) { return obj.name() == surfaceName; });
527
1/2
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 if (it != m_userDataCollection.end()) {
528
1/1
✓ Branch 2 taken 1 time.
1 result = *it;
529 }
530 2 return result;
531 ✗ }
532
533 1 GltfMetaData GltfForwardTranslator::getMetaData() const {
534 1 return m_gltfMetaData;
535 }
536
537 } // namespace gltf
538 } // namespace openstudio
539