glTFCommon.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef AI_GLFTCOMMON_H_INC
  34. #define AI_GLFTCOMMON_H_INC
  35. #ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER
  36. #include <assimp/Exceptional.h>
  37. #include <assimp/DefaultLogger.hpp>
  38. #include <algorithm>
  39. #include <list>
  40. #include <map>
  41. #include <stdexcept>
  42. #include <string>
  43. #include <vector>
  44. #include <rapidjson/document.h>
  45. #include <rapidjson/error/en.h>
  46. #include <rapidjson/rapidjson.h>
  47. // clang-format off
  48. #ifdef ASSIMP_API
  49. # include <assimp/ByteSwapper.h>
  50. # include <assimp/DefaultIOSystem.h>
  51. # include <memory>
  52. #else
  53. # include <memory>
  54. # define AI_SWAP4(p)
  55. # define ai_assert
  56. #endif
  57. #if _MSC_VER > 1500 || (defined __GNUC___)
  58. # define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
  59. #else
  60. # define gltf_unordered_map map
  61. #endif
  62. #ifdef ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
  63. # include <unordered_map>
  64. # if defined(_MSC_VER) && _MSC_VER <= 1600
  65. # define gltf_unordered_map tr1::unordered_map
  66. # else
  67. # define gltf_unordered_map unordered_map
  68. # endif
  69. #endif
  70. // clang-format on
  71. namespace glTFCommon {
  72. using rapidjson::Document;
  73. using rapidjson::Value;
  74. #ifdef ASSIMP_API
  75. using Assimp::IOStream;
  76. using Assimp::IOSystem;
  77. using std::shared_ptr;
  78. #else
  79. using std::shared_ptr;
  80. typedef std::runtime_error DeadlyImportError;
  81. typedef std::runtime_error DeadlyExportError;
  82. enum aiOrigin {
  83. aiOrigin_SET = 0,
  84. aiOrigin_CUR = 1,
  85. aiOrigin_END = 2
  86. };
  87. class IOSystem;
  88. class IOStream {
  89. public:
  90. IOStream(FILE *file) :
  91. f(file) {}
  92. ~IOStream() {
  93. fclose(f);
  94. }
  95. size_t Read(void *b, size_t sz, size_t n) { return fread(b, sz, n, f); }
  96. size_t Write(const void *b, size_t sz, size_t n) { return fwrite(b, sz, n, f); }
  97. int Seek(size_t off, aiOrigin orig) { return fseek(f, off, int(orig)); }
  98. size_t Tell() const { return ftell(f); }
  99. size_t FileSize() {
  100. long p = Tell(), len = (Seek(0, aiOrigin_END), Tell());
  101. return size_t((Seek(p, aiOrigin_SET), len));
  102. }
  103. private:
  104. FILE *f;
  105. };
  106. #endif
  107. // Vec/matrix types, as raw float arrays
  108. typedef float(vec3)[3];
  109. typedef float(vec4)[4];
  110. typedef float(mat4)[16];
  111. inline void CopyValue(const glTFCommon::vec3 &v, aiColor4D &out) {
  112. out.r = v[0];
  113. out.g = v[1];
  114. out.b = v[2];
  115. out.a = 1.0;
  116. }
  117. inline void CopyValue(const glTFCommon::vec4 &v, aiColor4D &out) {
  118. out.r = v[0];
  119. out.g = v[1];
  120. out.b = v[2];
  121. out.a = v[3];
  122. }
  123. inline void CopyValue(const glTFCommon::vec4 &v, aiColor3D &out) {
  124. out.r = v[0];
  125. out.g = v[1];
  126. out.b = v[2];
  127. }
  128. inline void CopyValue(const glTFCommon::vec3 &v, aiColor3D &out) {
  129. out.r = v[0];
  130. out.g = v[1];
  131. out.b = v[2];
  132. }
  133. inline void CopyValue(const glTFCommon::vec3 &v, aiVector3D &out) {
  134. out.x = v[0];
  135. out.y = v[1];
  136. out.z = v[2];
  137. }
  138. inline void CopyValue(const glTFCommon::vec4 &v, aiQuaternion &out) {
  139. out.x = v[0];
  140. out.y = v[1];
  141. out.z = v[2];
  142. out.w = v[3];
  143. }
  144. inline void CopyValue(const glTFCommon::mat4 &v, aiMatrix4x4 &o) {
  145. o.a1 = v[0];
  146. o.b1 = v[1];
  147. o.c1 = v[2];
  148. o.d1 = v[3];
  149. o.a2 = v[4];
  150. o.b2 = v[5];
  151. o.c2 = v[6];
  152. o.d2 = v[7];
  153. o.a3 = v[8];
  154. o.b3 = v[9];
  155. o.c3 = v[10];
  156. o.d3 = v[11];
  157. o.a4 = v[12];
  158. o.b4 = v[13];
  159. o.c4 = v[14];
  160. o.d4 = v[15];
  161. }
  162. #if _MSC_VER
  163. # pragma warning(push)
  164. # pragma warning(disable : 4310)
  165. #endif // _MSC_VER
  166. inline std::string getCurrentAssetDir(const std::string &pFile) {
  167. int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
  168. if (pos == int(std::string::npos)) {
  169. return std::string();
  170. }
  171. return pFile.substr(0, pos + 1);
  172. }
  173. #if _MSC_VER
  174. # pragma warning(pop)
  175. #endif // _MSC_VER
  176. namespace Util {
  177. void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out);
  178. size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out);
  179. inline size_t DecodeBase64(const char *in, uint8_t *&out) {
  180. return DecodeBase64(in, strlen(in), out);
  181. }
  182. struct DataURI {
  183. const char *mediaType;
  184. const char *charset;
  185. bool base64;
  186. const char *data;
  187. size_t dataLength;
  188. };
  189. //! Check if a uri is a data URI
  190. bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out);
  191. } // namespace Util
  192. #define CHECK_EXT(EXT) \
  193. if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true;
  194. //! Helper struct to represent values that might not be present
  195. template <class T>
  196. struct Nullable {
  197. T value;
  198. bool isPresent;
  199. Nullable() :
  200. isPresent(false) {}
  201. Nullable(T &val) :
  202. value(val),
  203. isPresent(true) {}
  204. };
  205. //! A reference to one top-level object, which is valid
  206. //! until the Asset instance is destroyed
  207. template <class T>
  208. class Ref {
  209. std::vector<T *> *vector;
  210. unsigned int index;
  211. public:
  212. Ref() :
  213. vector(nullptr),
  214. index(0) {}
  215. Ref(std::vector<T *> &vec, unsigned int idx) :
  216. vector(&vec),
  217. index(idx) {}
  218. inline unsigned int GetIndex() const { return index; }
  219. operator bool() const { return vector != nullptr && index < vector->size(); }
  220. T *operator->() { return (*vector)[index]; }
  221. T &operator*() { return *((*vector)[index]); }
  222. };
  223. //
  224. // JSON Value reading helpers
  225. //
  226. template <class T>
  227. struct ReadHelper {
  228. static bool Read(Value &val, T &out) {
  229. return val.IsInt() ? out = static_cast<T>(val.GetInt()), true : false;
  230. }
  231. };
  232. template <>
  233. struct ReadHelper<bool> {
  234. static bool Read(Value &val, bool &out) {
  235. return val.IsBool() ? out = val.GetBool(), true : false;
  236. }
  237. };
  238. template <>
  239. struct ReadHelper<float> {
  240. static bool Read(Value &val, float &out) {
  241. return val.IsNumber() ? out = static_cast<float>(val.GetDouble()), true : false;
  242. }
  243. };
  244. template <unsigned int N>
  245. struct ReadHelper<float[N]> {
  246. static bool Read(Value &val, float (&out)[N]) {
  247. if (!val.IsArray() || val.Size() != N) return false;
  248. for (unsigned int i = 0; i < N; ++i) {
  249. if (val[i].IsNumber())
  250. out[i] = static_cast<float>(val[i].GetDouble());
  251. }
  252. return true;
  253. }
  254. };
  255. template <>
  256. struct ReadHelper<const char *> {
  257. static bool Read(Value &val, const char *&out) {
  258. return val.IsString() ? (out = val.GetString(), true) : false;
  259. }
  260. };
  261. template <>
  262. struct ReadHelper<std::string> {
  263. static bool Read(Value &val, std::string &out) {
  264. return val.IsString() ? (out = std::string(val.GetString(), val.GetStringLength()), true) : false;
  265. }
  266. };
  267. template <class T>
  268. struct ReadHelper<Nullable<T>> {
  269. static bool Read(Value &val, Nullable<T> &out) {
  270. return out.isPresent = ReadHelper<T>::Read(val, out.value);
  271. }
  272. };
  273. template <>
  274. struct ReadHelper<uint64_t> {
  275. static bool Read(Value &val, uint64_t &out) {
  276. return val.IsUint64() ? out = val.GetUint64(), true : false;
  277. }
  278. };
  279. template <>
  280. struct ReadHelper<int64_t> {
  281. static bool Read(Value &val, int64_t &out) {
  282. return val.IsInt64() ? out = val.GetInt64(), true : false;
  283. }
  284. };
  285. template <class T>
  286. inline static bool ReadValue(Value &val, T &out) {
  287. return ReadHelper<T>::Read(val, out);
  288. }
  289. template <class T>
  290. inline static bool ReadMember(Value &obj, const char *id, T &out) {
  291. if (!obj.IsObject()) {
  292. return false;
  293. }
  294. Value::MemberIterator it = obj.FindMember(id);
  295. if (it != obj.MemberEnd()) {
  296. return ReadHelper<T>::Read(it->value, out);
  297. }
  298. return false;
  299. }
  300. template <class T>
  301. inline static T MemberOrDefault(Value &obj, const char *id, T defaultValue) {
  302. T out;
  303. return ReadMember(obj, id, out) ? out : defaultValue;
  304. }
  305. inline Value *FindMember(Value &val, const char *id) {
  306. if (!val.IsObject()) {
  307. return nullptr;
  308. }
  309. Value::MemberIterator it = val.FindMember(id);
  310. return (it != val.MemberEnd()) ? &it->value : nullptr;
  311. }
  312. template <int N>
  313. inline void throwUnexpectedTypeError(const char (&expectedTypeName)[N], const char *memberId, const char *context, const char *extraContext) {
  314. std::string fullContext = context;
  315. if (extraContext && (strlen(extraContext) > 0)) {
  316. fullContext = fullContext + " (" + extraContext + ")";
  317. }
  318. throw DeadlyImportError("Member \"", memberId, "\" was not of type \"", expectedTypeName, "\" when reading ", fullContext);
  319. }
  320. // Look-up functions with type checks. Context and extra context help the user identify the problem if there's an error.
  321. inline Value *FindStringInContext(Value &val, const char *memberId, const char *context, const char *extraContext = nullptr) {
  322. if (!val.IsObject()) {
  323. return nullptr;
  324. }
  325. Value::MemberIterator it = val.FindMember(memberId);
  326. if (it == val.MemberEnd()) {
  327. return nullptr;
  328. }
  329. if (!it->value.IsString()) {
  330. throwUnexpectedTypeError("string", memberId, context, extraContext);
  331. }
  332. return &it->value;
  333. }
  334. inline Value *FindNumberInContext(Value &val, const char *memberId, const char *context, const char *extraContext = nullptr) {
  335. if (!val.IsObject()) {
  336. return nullptr;
  337. }
  338. Value::MemberIterator it = val.FindMember(memberId);
  339. if (it == val.MemberEnd()) {
  340. return nullptr;
  341. }
  342. if (!it->value.IsNumber()) {
  343. throwUnexpectedTypeError("number", memberId, context, extraContext);
  344. }
  345. return &it->value;
  346. }
  347. inline Value *FindUIntInContext(Value &val, const char *memberId, const char *context, const char *extraContext = nullptr) {
  348. if (!val.IsObject()) {
  349. return nullptr;
  350. }
  351. Value::MemberIterator it = val.FindMember(memberId);
  352. if (it == val.MemberEnd()) {
  353. return nullptr;
  354. }
  355. if (!it->value.IsUint()) {
  356. throwUnexpectedTypeError("uint", memberId, context, extraContext);
  357. }
  358. return &it->value;
  359. }
  360. inline Value *FindArrayInContext(Value &val, const char *memberId, const char *context, const char *extraContext = nullptr) {
  361. if (!val.IsObject()) {
  362. return nullptr;
  363. }
  364. Value::MemberIterator it = val.FindMember(memberId);
  365. if (it == val.MemberEnd()) {
  366. return nullptr;
  367. }
  368. if (!it->value.IsArray()) {
  369. throwUnexpectedTypeError("array", memberId, context, extraContext);
  370. }
  371. return &it->value;
  372. }
  373. inline Value *FindObjectInContext(Value &val, const char * memberId, const char *context, const char *extraContext = nullptr) {
  374. if (!val.IsObject()) {
  375. return nullptr;
  376. }
  377. Value::MemberIterator it = val.FindMember(memberId);
  378. if (it == val.MemberEnd()) {
  379. return nullptr;
  380. }
  381. if (!it->value.IsObject()) {
  382. ASSIMP_LOG_ERROR("Member \"", memberId, "\" was not of type \"", context, "\" when reading ", extraContext);
  383. return nullptr;
  384. }
  385. return &it->value;
  386. }
  387. inline Value *FindExtensionInContext(Value &val, const char *extensionId, const char *context, const char *extraContext = nullptr) {
  388. if (Value *extensionList = FindObjectInContext(val, "extensions", context, extraContext)) {
  389. if (Value *extension = FindObjectInContext(*extensionList, extensionId, context, extraContext)) {
  390. return extension;
  391. }
  392. }
  393. return nullptr;
  394. }
  395. // Overloads when the value is the document.
  396. inline Value *FindString(Document &doc, const char *memberId) {
  397. return FindStringInContext(doc, memberId, "the document");
  398. }
  399. inline Value *FindNumber(Document &doc, const char *memberId) {
  400. return FindNumberInContext(doc, memberId, "the document");
  401. }
  402. inline Value *FindUInt(Document &doc, const char *memberId) {
  403. return FindUIntInContext(doc, memberId, "the document");
  404. }
  405. inline Value *FindArray(Document &val, const char *memberId) {
  406. return FindArrayInContext(val, memberId, "the document");
  407. }
  408. inline Value *FindObject(Document &doc, const char *memberId) {
  409. return FindObjectInContext(doc, memberId, "the document");
  410. }
  411. inline Value *FindExtension(Value &val, const char *extensionId) {
  412. return FindExtensionInContext(val, extensionId, "the document");
  413. }
  414. inline Value *FindString(Value &val, const char *id) {
  415. Value::MemberIterator it = val.FindMember(id);
  416. return (it != val.MemberEnd() && it->value.IsString()) ? &it->value : nullptr;
  417. }
  418. inline Value *FindObject(Value &val, const char *id) {
  419. Value::MemberIterator it = val.FindMember(id);
  420. return (it != val.MemberEnd() && it->value.IsObject()) ? &it->value : nullptr;
  421. }
  422. inline Value *FindArray(Value &val, const char *id) {
  423. Value::MemberIterator it = val.FindMember(id);
  424. return (it != val.MemberEnd() && it->value.IsArray()) ? &it->value : nullptr;
  425. }
  426. inline Value *FindNumber(Value &val, const char *id) {
  427. Value::MemberIterator it = val.FindMember(id);
  428. return (it != val.MemberEnd() && it->value.IsNumber()) ? &it->value : nullptr;
  429. }
  430. } // namespace glTFCommon
  431. #endif // ASSIMP_BUILD_NO_GLTF_IMPORTER
  432. #endif // AI_GLFTCOMMON_H_INC