FBXParser.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /** @file FBXParser.h
  34. * @brief FBX parsing code
  35. */
  36. #ifndef INCLUDED_AI_FBX_PARSER_H
  37. #define INCLUDED_AI_FBX_PARSER_H
  38. #include <stdint.h>
  39. #include <map>
  40. #include <memory>
  41. #include <vector>
  42. #include <assimp/LogAux.h>
  43. #include <assimp/fast_atof.h>
  44. #include "Common/StackAllocator.h"
  45. #include "FBXCompileConfig.h"
  46. #include "FBXTokenizer.h"
  47. namespace Assimp {
  48. namespace FBX {
  49. class Scope;
  50. class Parser;
  51. class Element;
  52. using ScopeList = std::vector<Scope*>;
  53. using ElementMap = std::fbx_unordered_multimap< std::string, Element*>;
  54. using ElementCollection = std::pair<ElementMap::const_iterator,ElementMap::const_iterator>;
  55. #define new_Scope new (allocator.Allocate(sizeof(Scope))) Scope
  56. #define new_Element new (allocator.Allocate(sizeof(Element))) Element
  57. #define delete_Scope(_p) (_p)->~Scope()
  58. #define delete_Element(_p) (_p)->~Element()
  59. /** FBX data entity that consists of a key:value tuple.
  60. *
  61. * Example:
  62. * @verbatim
  63. * AnimationCurve: 23, "AnimCurve::", "" {
  64. * [..]
  65. * }
  66. * @endverbatim
  67. *
  68. * As can be seen in this sample, elements can contain nested #Scope
  69. * as their trailing member.
  70. **/
  71. class Element {
  72. public:
  73. Element(const Token& key_token, Parser& parser);
  74. ~Element();
  75. const Scope* Compound() const {
  76. return compound;
  77. }
  78. const Token& KeyToken() const {
  79. return key_token;
  80. }
  81. const TokenList& Tokens() const {
  82. return tokens;
  83. }
  84. private:
  85. const Token& key_token;
  86. TokenList tokens;
  87. Scope* compound;
  88. };
  89. /** FBX data entity that consists of a 'scope', a collection
  90. * of not necessarily unique #Element instances.
  91. *
  92. * Example:
  93. * @verbatim
  94. * GlobalSettings: {
  95. * Version: 1000
  96. * Properties70:
  97. * [...]
  98. * }
  99. * @endverbatim */
  100. class Scope
  101. {
  102. public:
  103. Scope(Parser& parser, bool topLevel = false);
  104. ~Scope();
  105. const Element* operator[] (const std::string& index) const {
  106. ElementMap::const_iterator it = elements.find(index);
  107. return it == elements.end() ? nullptr : (*it).second;
  108. }
  109. const Element* FindElementCaseInsensitive(const std::string& elementName) const {
  110. const char* elementNameCStr = elementName.c_str();
  111. for (auto element = elements.begin(); element != elements.end(); ++element)
  112. {
  113. if (!ASSIMP_strincmp(element->first.c_str(), elementNameCStr, AI_MAXLEN)) {
  114. return element->second;
  115. }
  116. }
  117. return nullptr;
  118. }
  119. ElementCollection GetCollection(const std::string& index) const {
  120. return elements.equal_range(index);
  121. }
  122. const ElementMap& Elements() const {
  123. return elements;
  124. }
  125. private:
  126. ElementMap elements;
  127. };
  128. /** FBX parsing class, takes a list of input tokens and generates a hierarchy
  129. * of nested #Scope instances, representing the fbx DOM.*/
  130. class Parser
  131. {
  132. public:
  133. /** Parse given a token list. Does not take ownership of the tokens -
  134. * the objects must persist during the entire parser lifetime */
  135. Parser(const TokenList &tokens, StackAllocator &allocator, bool is_binary);
  136. ~Parser();
  137. const Scope& GetRootScope() const {
  138. return *root;
  139. }
  140. bool IsBinary() const {
  141. return is_binary;
  142. }
  143. StackAllocator &GetAllocator() {
  144. return allocator;
  145. }
  146. private:
  147. friend class Scope;
  148. friend class Element;
  149. TokenPtr AdvanceToNextToken();
  150. TokenPtr LastToken() const;
  151. TokenPtr CurrentToken() const;
  152. private:
  153. const TokenList& tokens;
  154. StackAllocator &allocator;
  155. TokenPtr last, current;
  156. TokenList::const_iterator cursor;
  157. Scope *root;
  158. const bool is_binary;
  159. };
  160. /* token parsing - this happens when building the DOM out of the parse-tree*/
  161. uint64_t ParseTokenAsID(const Token& t, const char*& err_out);
  162. size_t ParseTokenAsDim(const Token& t, const char*& err_out);
  163. float ParseTokenAsFloat(const Token& t, const char*& err_out);
  164. int ParseTokenAsInt(const Token& t, const char*& err_out);
  165. int64_t ParseTokenAsInt64(const Token& t, const char*& err_out);
  166. std::string ParseTokenAsString(const Token& t, const char*& err_out);
  167. /* wrapper around ParseTokenAsXXX() with DOMError handling */
  168. uint64_t ParseTokenAsID(const Token& t);
  169. size_t ParseTokenAsDim(const Token& t);
  170. float ParseTokenAsFloat(const Token& t);
  171. int ParseTokenAsInt(const Token& t);
  172. int64_t ParseTokenAsInt64(const Token& t);
  173. std::string ParseTokenAsString(const Token& t);
  174. /* read data arrays */
  175. void ParseVectorDataArray(std::vector<aiVector3D>& out, const Element& el);
  176. void ParseVectorDataArray(std::vector<aiColor4D>& out, const Element& el);
  177. void ParseVectorDataArray(std::vector<aiVector2D>& out, const Element& el);
  178. void ParseVectorDataArray(std::vector<int>& out, const Element& el);
  179. void ParseVectorDataArray(std::vector<float>& out, const Element& el);
  180. void ParseVectorDataArray(std::vector<unsigned int>& out, const Element& el);
  181. void ParseVectorDataArray(std::vector<uint64_t>& out, const Element& e);
  182. void ParseVectorDataArray(std::vector<int64_t>& out, const Element& el);
  183. bool HasElement( const Scope& sc, const std::string& index );
  184. // extract a required element from a scope, abort if the element cannot be found
  185. const Element &GetRequiredElement(const Scope &sc, const std::string &index, const Element *element = nullptr);
  186. // extract required compound scope
  187. const Scope& GetRequiredScope(const Element& el);
  188. // get token at a particular index
  189. const Token& GetRequiredToken(const Element& el, unsigned int index);
  190. // read a 4x4 matrix from an array of 16 floats
  191. aiMatrix4x4 ReadMatrix(const Element& element);
  192. } // ! FBX
  193. } // ! Assimp
  194. #endif // ! INCLUDED_AI_FBX_PARSER_H