FBXParser.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*************************************************************************/
  2. /* FBXParser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. /*
  31. Open Asset Import Library (assimp)
  32. ----------------------------------------------------------------------
  33. Copyright (c) 2006-2019, assimp team
  34. All rights reserved.
  35. Redistribution and use of this software in source and binary forms,
  36. with or without modification, are permitted provided that the
  37. following conditions are met:
  38. * Redistributions of source code must retain the above
  39. copyright notice, this list of conditions and the
  40. following disclaimer.
  41. * Redistributions in binary form must reproduce the above
  42. copyright notice, this list of conditions and the
  43. following disclaimer in the documentation and/or other
  44. materials provided with the distribution.
  45. * Neither the name of the assimp team, nor the names of its
  46. contributors may be used to endorse or promote products
  47. derived from this software without specific prior
  48. written permission of the assimp team.
  49. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. ----------------------------------------------------------------------
  61. */
  62. /** @file FBXParser.h
  63. * @brief FBX parsing code
  64. */
  65. #ifndef FBX_PARSER_H
  66. #define FBX_PARSER_H
  67. #include <stdint.h>
  68. #include <map>
  69. #include <memory>
  70. #include "core/math/color.h"
  71. #include "core/math/transform.h"
  72. #include "core/math/vector2.h"
  73. #include "core/math/vector3.h"
  74. #include "FBXTokenizer.h"
  75. namespace FBXDocParser {
  76. class Scope;
  77. class Parser;
  78. class Element;
  79. typedef Element *ElementPtr;
  80. typedef Scope *ScopePtr;
  81. typedef std::vector<ScopePtr> ScopeList;
  82. typedef std::multimap<std::string, ElementPtr> ElementMap;
  83. typedef std::pair<ElementMap::const_iterator, ElementMap::const_iterator> ElementCollection;
  84. #define new_Scope new Scope
  85. #define new_Element new Element
  86. /** FBX data entity that consists of a key:value tuple.
  87. *
  88. * Example:
  89. * @verbatim
  90. * AnimationCurve: 23, "AnimCurve::", "" {
  91. * [..]
  92. * }
  93. * @endverbatim
  94. *
  95. * As can be seen in this sample, elements can contain nested #Scope
  96. * as their trailing member. **/
  97. class Element {
  98. public:
  99. Element(TokenPtr key_token, Parser &parser);
  100. ~Element();
  101. ScopePtr Compound() const {
  102. return compound;
  103. }
  104. TokenPtr KeyToken() const {
  105. return key_token;
  106. }
  107. const TokenList &Tokens() const {
  108. return tokens;
  109. }
  110. private:
  111. TokenList tokens;
  112. ScopePtr compound = nullptr;
  113. std::vector<ScopePtr> compound_scope;
  114. TokenPtr key_token = nullptr;
  115. };
  116. /** FBX data entity that consists of a 'scope', a collection
  117. * of not necessarily unique #Element instances.
  118. *
  119. * Example:
  120. * @verbatim
  121. * GlobalSettings: {
  122. * Version: 1000
  123. * Properties70:
  124. * [...]
  125. * }
  126. * @endverbatim */
  127. class Scope {
  128. public:
  129. Scope(Parser &parser, bool topLevel = false);
  130. ~Scope();
  131. ElementPtr GetElement(const std::string &index) const {
  132. ElementMap::const_iterator it = elements.find(index);
  133. return it == elements.end() ? nullptr : (*it).second;
  134. }
  135. ElementPtr FindElementCaseInsensitive(const std::string &elementName) const {
  136. for (auto element = elements.begin(); element != elements.end(); ++element) {
  137. if (element->first.compare(elementName)) {
  138. return element->second;
  139. }
  140. }
  141. // nothing to reference / expired.
  142. return nullptr;
  143. }
  144. ElementCollection GetCollection(const std::string &index) const {
  145. return elements.equal_range(index);
  146. }
  147. const ElementMap &Elements() const {
  148. return elements;
  149. }
  150. private:
  151. ElementMap elements;
  152. };
  153. /** FBX parsing class, takes a list of input tokens and generates a hierarchy
  154. * of nested #Scope instances, representing the fbx DOM.*/
  155. class Parser {
  156. public:
  157. /** Parse given a token list. Does not take ownership of the tokens -
  158. * the objects must persist during the entire parser lifetime */
  159. Parser(const TokenList &tokens, bool is_binary);
  160. ~Parser();
  161. ScopePtr GetRootScope() const {
  162. return root;
  163. }
  164. bool IsBinary() const {
  165. return is_binary;
  166. }
  167. private:
  168. friend class Scope;
  169. friend class Element;
  170. TokenPtr AdvanceToNextToken();
  171. TokenPtr LastToken() const;
  172. TokenPtr CurrentToken() const;
  173. private:
  174. ScopeList scopes;
  175. const TokenList &tokens;
  176. TokenPtr last = nullptr, current = nullptr;
  177. TokenList::const_iterator cursor;
  178. ScopePtr root = nullptr;
  179. const bool is_binary;
  180. };
  181. /* token parsing - this happens when building the DOM out of the parse-tree*/
  182. uint64_t ParseTokenAsID(const TokenPtr t, const char *&err_out);
  183. size_t ParseTokenAsDim(const TokenPtr t, const char *&err_out);
  184. float ParseTokenAsFloat(const TokenPtr t, const char *&err_out);
  185. int ParseTokenAsInt(const TokenPtr t, const char *&err_out);
  186. int64_t ParseTokenAsInt64(const TokenPtr t, const char *&err_out);
  187. std::string ParseTokenAsString(const TokenPtr t, const char *&err_out);
  188. /* wrapper around ParseTokenAsXXX() with DOMError handling */
  189. uint64_t ParseTokenAsID(const TokenPtr t);
  190. size_t ParseTokenAsDim(const TokenPtr t);
  191. float ParseTokenAsFloat(const TokenPtr t);
  192. int ParseTokenAsInt(const TokenPtr t);
  193. int64_t ParseTokenAsInt64(const TokenPtr t);
  194. std::string ParseTokenAsString(const TokenPtr t);
  195. /* read data arrays */
  196. void ParseVectorDataArray(std::vector<Vector3> &out, const ElementPtr el);
  197. void ParseVectorDataArray(std::vector<Color> &out, const ElementPtr el);
  198. void ParseVectorDataArray(std::vector<Vector2> &out, const ElementPtr el);
  199. void ParseVectorDataArray(std::vector<int> &out, const ElementPtr el);
  200. void ParseVectorDataArray(std::vector<float> &out, const ElementPtr el);
  201. void ParseVectorDataArray(std::vector<float> &out, const ElementPtr el);
  202. void ParseVectorDataArray(std::vector<unsigned int> &out, const ElementPtr el);
  203. void ParseVectorDataArray(std::vector<uint64_t> &out, const ElementPtr ep);
  204. void ParseVectorDataArray(std::vector<int64_t> &out, const ElementPtr el);
  205. bool HasElement(const ScopePtr sc, const std::string &index);
  206. // extract a required element from a scope, abort if the element cannot be found
  207. ElementPtr GetRequiredElement(const ScopePtr sc, const std::string &index, const ElementPtr element = nullptr);
  208. ScopePtr GetRequiredScope(const ElementPtr el); // New in 2020. (less likely to destroy application)
  209. ElementPtr GetOptionalElement(const ScopePtr sc, const std::string &index, const ElementPtr element = nullptr);
  210. // extract required compound scope
  211. ScopePtr GetRequiredScope(const ElementPtr el);
  212. // get token at a particular index
  213. TokenPtr GetRequiredToken(const ElementPtr el, unsigned int index);
  214. // ------------------------------------------------------------------------------------------------
  215. // read a 4x4 matrix from an array of 16 floats
  216. Transform ReadMatrix(const ElementPtr element);
  217. } // namespace FBXDocParser
  218. #endif // FBX_PARSER_H