Conscience Core
CscGlTFParser.h
Go to the documentation of this file.
1 #ifndef CscGlTFParser_h
2 #define CscGlTFParser_h
3 
4 #include "CscCommon.h"
5 
6 #include "Util/json.h"
7 
8 #include <Eigen/Dense>
9 #include <cmath>
10 #include <fstream>
11 #include <iostream>
12 #include <limits>
13 #include <set>
14 #include <tuple>
15 #include <unordered_set>
16 #include <Axiomes/Csc3dTypes.h>
17 
18 using std::numeric_limits, std::unordered_set, std::set;
19 using namespace conscience_core::axiomes;
20 using namespace conscience_utils::JSON;
21 
27 
29 public:
31  const optional<fs::path> binFilePath;
32 
33  map<unsigned, unsigned> nodeParents;
34 
38  set<unsigned> sceneRootNodeIndices;
39 
40  map<size_t, vector<unsigned char> *> bufferDataCache;
41  map<size_t, CscJsonObjectReader *> nodesJsonByIndex;
42  map<size_t, CscJsonObjectReader *> accessorsJsonByIndex;
43  map<size_t, CscJsonObjectReader *> buffersJsonByIndex;
44  map<size_t, CscJsonObjectReader *> bufferViewsJsonByIndex;
45 
49  set<size_t> sceneNodeIndices;
50 
51  GltfParseContext(CscJsonObjectReader *gltfJsonRootPtr, optional<fs::path> binFilePath);
53 private:
54  CscJsonObjectReader *gltfJsonRootPtr = nullptr;
55 };
56 
57 struct BoundingBox {
60 
61  BoundingBox();
62 
63  void update(const Vec3 &point);
64  void update(double x, double y, double z);
65  string toString() const;
66 };
67 
74 
75 public:
76  CscGlTFParser();
77  GltfParseContext *load(const fs::path &filePath);
78 
84  void computeBoundingBoxAndVerticesAndPolygons(GltfParseContext &context, BoundingBox &globalBBox, bool collectVerticesAndPolygons, vector<const CscPoint3d *> *vertices, vector<const CscPolygon *> *polygons, unsigned nodeIndex, CscJsonObjectReader &node);
85 
89  vector<pair<unsigned, CscJsonObjectReader *>> findNodesStartingBy(GltfParseContext &context, const vector<string> &searchNamePrefixes);
90 
91  void sortByName(vector<pair<unsigned, CscJsonObjectReader *>> &nodes);
92 
93  inline CscJsonObjectReader *getNode(GltfParseContext &context, size_t nodeIndex) {
94  if (context.nodesJsonByIndex.find(nodeIndex) != context.nodesJsonByIndex.end()) {
95  return context.nodesJsonByIndex.at(nodeIndex);
96  }
97 
98  auto nodeJson = context.gltfJsonRoot.getRequiredArrayItemValue<CscJsonObjectReader *>("nodes", nodeIndex);
99  context.nodesJsonByIndex[nodeIndex] = nodeJson;
100  return nodeJson;
101  }
102 
103 private:
104  unique_ptr<CscLogger> logger = CscLogger::getForCategory("CscGlTFParser");
105 
109  void processMesh(GltfParseContext &context, unsigned meshIndex, CscJsonObjectReader &mesh, BoundingBox &globalBBox, bool collectVerticesAndPolygons, vector<const CscPoint3d *> *vertices, vector<const CscPolygon *> *polygons, const double transform[4][4]);
110 
114  void processNode(GltfParseContext &context, unsigned nodeIndex, CscJsonObjectReader &node, BoundingBox &globalBBox, bool collectVerticesAndPolygons, vector<const CscPoint3d *> *vertices, vector<const CscPolygon *> *polygons, const double parentTransform[4][4], int currentDepth, int maxDepth);
115 
116  void fillSceneNodeIndices(GltfParseContext &context, CscJsonObjectReader *currentNode = nullptr, unsigned nodeIndex = -1);
117 };
118 
119 }
120 
121 #endif
conscience_core::core_objects::exchange::gltf::BoundingBox
Definition: CscGlTFParser.h:57
conscience_core::axiomes
Definition: Csc2dTypes.cpp:9
conscience_core::core_objects::exchange::gltf::BoundingBox::min
Vec3 min
Definition: CscGlTFParser.h:58
conscience_core::core_objects::exchange::gltf::GltfParseContext
Definition: CscGlTFParser.h:28
conscience_core::core_objects::exchange
Definition: CscEntityModelData.cpp:9
conscience_core::core_objects::exchange::gltf::GltfParseContext::accessorsJsonByIndex
map< size_t, CscJsonObjectReader * > accessorsJsonByIndex
Definition: CscGlTFParser.h:42
logger
static std::unique_ptr< CscLogger > logger
Definition: gltfHelpers.cpp:6
conscience_core::core_objects::exchange::gltf::CscGlTFParser
Definition: CscGlTFParser.h:73
conscience_core::core_objects::exchange::gltf::BoundingBox::max
Vec3 max
Definition: CscGlTFParser.h:59
conscience_core::core_objects::exchange::gltf::GltfParseContext::gltfJsonRoot
CscJsonObjectReader & gltfJsonRoot
Definition: CscGlTFParser.h:30
conscience_core::core_objects::exchange::gltf::GltfParseContext::bufferViewsJsonByIndex
map< size_t, CscJsonObjectReader * > bufferViewsJsonByIndex
Definition: CscGlTFParser.h:44
conscience_core::core_objects::exchange::gltf
Definition: CscEntityModelGlTFLoader.cpp:32
conscience_core::core_objects::exchange::gltf::GltfParseContext::sceneNodeIndices
set< size_t > sceneNodeIndices
Definition: CscGlTFParser.h:49
conscience_utils::JSON
Definition: CscCommandMetadataBuilder.h:20
conscience_core::core_objects::exchange::gltf::GltfParseContext::nodesJsonByIndex
map< size_t, CscJsonObjectReader * > nodesJsonByIndex
Definition: CscGlTFParser.h:41
conscience_core::core_objects::exchange::gltf::GltfParseContext::buffersJsonByIndex
map< size_t, CscJsonObjectReader * > buffersJsonByIndex
Definition: CscGlTFParser.h:43
CscCommon.h
conscience_core::core_objects::exchange::gltf::CscGlTFParser::getNode
CscJsonObjectReader * getNode(GltfParseContext &context, size_t nodeIndex)
Definition: CscGlTFParser.h:93
Csc3dTypes.h
conscience_core::core_objects::exchange::gltf::GltfParseContext::binFilePath
const optional< fs::path > binFilePath
Definition: CscGlTFParser.h:31
conscience_core::core_objects::exchange::gltf::GltfParseContext::sceneRootNodeIndices
set< unsigned > sceneRootNodeIndices
Definition: CscGlTFParser.h:38
conscience_core::core_objects::exchange::gltf::GltfParseContext::nodeParents
map< unsigned, unsigned > nodeParents
Definition: CscGlTFParser.h:33
conscience_utils::JSON::CscJsonObjectReader::getRequiredArrayItemValue
T getRequiredArrayItemValue(const string &arrayKey, size_t index)
Definition: json.h:279
json.h
conscience_utils::JSON::CscJsonObjectReader
Definition: json.h:100
conscience_core::axiomes::Vec3
Definition: Csc3dTypes.h:367
conscience_core::core_objects::exchange::gltf::GltfParseContext::bufferDataCache
map< size_t, vector< unsigned char > * > bufferDataCache
Definition: CscGlTFParser.h:40