glTFCommon.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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. #ifdef ASSIMP_API
  47. #include <assimp/ByteSwapper.h>
  48. #include <assimp/DefaultIOSystem.h>
  49. #include <memory>
  50. #else
  51. #include <memory>
  52. #define AI_SWAP4(p)
  53. #define ai_assert
  54. #endif
  55. #if _MSC_VER > 1500 || (defined __GNUC___)
  56. #define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
  57. #else
  58. #define gltf_unordered_map map
  59. #endif
  60. #ifdef ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
  61. #include <unordered_map>
  62. #if _MSC_VER > 1600
  63. #define gltf_unordered_map unordered_map
  64. #else
  65. #define gltf_unordered_map tr1::unordered_map
  66. #endif
  67. #endif
  68. namespace glTFCommon {
  69. #ifdef ASSIMP_API
  70. using Assimp::IOStream;
  71. using Assimp::IOSystem;
  72. using std::shared_ptr;
  73. #else
  74. using std::shared_ptr;
  75. typedef std::runtime_error DeadlyImportError;
  76. typedef std::runtime_error DeadlyExportError;
  77. enum aiOrigin {
  78. aiOrigin_SET = 0,
  79. aiOrigin_CUR = 1,
  80. aiOrigin_END = 2
  81. };
  82. class IOSystem;
  83. class IOStream {
  84. public:
  85. IOStream(FILE *file) :
  86. f(file) {}
  87. ~IOStream() {
  88. fclose(f);
  89. f = 0;
  90. }
  91. size_t Read(void *b, size_t sz, size_t n) { return fread(b, sz, n, f); }
  92. size_t Write(const void *b, size_t sz, size_t n) { return fwrite(b, sz, n, f); }
  93. int Seek(size_t off, aiOrigin orig) { return fseek(f, off, int(orig)); }
  94. size_t Tell() const { return ftell(f); }
  95. size_t FileSize() {
  96. long p = Tell(), len = (Seek(0, aiOrigin_END), Tell());
  97. return size_t((Seek(p, aiOrigin_SET), len));
  98. }
  99. private:
  100. FILE *f;
  101. };
  102. #endif
  103. // Vec/matrix types, as raw float arrays
  104. typedef float(vec3)[3];
  105. typedef float(vec4)[4];
  106. typedef float(mat4)[16];
  107. inline void CopyValue(const glTFCommon::vec3 &v, aiColor4D &out) {
  108. out.r = v[0];
  109. out.g = v[1];
  110. out.b = v[2];
  111. out.a = 1.0;
  112. }
  113. inline void CopyValue(const glTFCommon::vec4 &v, aiColor4D &out) {
  114. out.r = v[0];
  115. out.g = v[1];
  116. out.b = v[2];
  117. out.a = v[3];
  118. }
  119. inline void CopyValue(const glTFCommon::vec4 &v, aiColor3D &out) {
  120. out.r = v[0];
  121. out.g = v[1];
  122. out.b = v[2];
  123. }
  124. inline void CopyValue(const glTFCommon::vec3 &v, aiColor3D &out) {
  125. out.r = v[0];
  126. out.g = v[1];
  127. out.b = v[2];
  128. }
  129. inline void CopyValue(const glTFCommon::vec3 &v, aiVector3D &out) {
  130. out.x = v[0];
  131. out.y = v[1];
  132. out.z = v[2];
  133. }
  134. inline void CopyValue(const glTFCommon::vec4 &v, aiQuaternion &out) {
  135. out.x = v[0];
  136. out.y = v[1];
  137. out.z = v[2];
  138. out.w = v[3];
  139. }
  140. inline void CopyValue(const glTFCommon::mat4 &v, aiMatrix4x4 &o) {
  141. o.a1 = v[0];
  142. o.b1 = v[1];
  143. o.c1 = v[2];
  144. o.d1 = v[3];
  145. o.a2 = v[4];
  146. o.b2 = v[5];
  147. o.c2 = v[6];
  148. o.d2 = v[7];
  149. o.a3 = v[8];
  150. o.b3 = v[9];
  151. o.c3 = v[10];
  152. o.d3 = v[11];
  153. o.a4 = v[12];
  154. o.b4 = v[13];
  155. o.c4 = v[14];
  156. o.d4 = v[15];
  157. }
  158. #if _MSC_VER
  159. # pragma warning(push)
  160. # pragma warning(disable : 4310)
  161. #endif // _MSC_VER
  162. inline std::string getCurrentAssetDir(const std::string &pFile) {
  163. std::string path = pFile;
  164. int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
  165. if (pos != int(std::string::npos)) {
  166. path = pFile.substr(0, pos + 1);
  167. }
  168. return path;
  169. }
  170. #if _MSC_VER
  171. # pragma warning(pop)
  172. #endif // _MSC_VER
  173. namespace Util {
  174. void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out);
  175. size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out);
  176. inline size_t DecodeBase64(const char *in, uint8_t *&out) {
  177. return DecodeBase64(in, strlen(in), out);
  178. }
  179. struct DataURI {
  180. const char *mediaType;
  181. const char *charset;
  182. bool base64;
  183. const char *data;
  184. size_t dataLength;
  185. };
  186. //! Check if a uri is a data URI
  187. bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out);
  188. template <bool B>
  189. struct DATA {
  190. static const uint8_t tableDecodeBase64[128];
  191. };
  192. template <bool B>
  193. const uint8_t DATA<B>::tableDecodeBase64[128] = {
  194. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  195. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  196. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
  197. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0,
  198. 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  199. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
  200. 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  201. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
  202. };
  203. inline char EncodeCharBase64(uint8_t b) {
  204. return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="[size_t(b)];
  205. }
  206. inline uint8_t DecodeCharBase64(char c) {
  207. if (c & 0x80) {
  208. throw DeadlyImportError("Invalid base64 char value: ", size_t(c));
  209. }
  210. return DATA<true>::tableDecodeBase64[size_t(c & 0x7F)]; // TODO faster with lookup table or ifs?
  211. }
  212. size_t DecodeBase64(const char *in, size_t inLength, uint8_t *&out);
  213. void EncodeBase64(const uint8_t *in, size_t inLength, std::string &out);
  214. } // namespace Util
  215. #define CHECK_EXT(EXT) \
  216. if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true;
  217. } // namespace glTFCommon
  218. #endif // ASSIMP_BUILD_NO_GLTF_IMPORTER
  219. #endif // AI_GLFTCOMMON_H_INC