glTFCommon.h 14 KB

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