FBXParser.h 7.2 KB

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