/src/assimp/code/AssetLib/FBX/FBXParser.h
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2025, assimp team |
6 | | |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use of this software in source and binary forms, |
10 | | with or without modification, are permitted provided that the |
11 | | following conditions are met: |
12 | | |
13 | | * Redistributions of source code must retain the above |
14 | | copyright notice, this list of conditions and the |
15 | | following disclaimer. |
16 | | |
17 | | * Redistributions in binary form must reproduce the above |
18 | | copyright notice, this list of conditions and the |
19 | | following disclaimer in the documentation and/or other |
20 | | materials provided with the distribution. |
21 | | |
22 | | * Neither the name of the assimp team, nor the names of its |
23 | | contributors may be used to endorse or promote products |
24 | | derived from this software without specific prior |
25 | | written permission of the assimp team. |
26 | | |
27 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
30 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
31 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
32 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
33 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
34 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
35 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
36 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
37 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | |
39 | | ---------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /** @file FBXParser.h |
43 | | * @brief FBX parsing code |
44 | | */ |
45 | | #ifndef INCLUDED_AI_FBX_PARSER_H |
46 | | #define INCLUDED_AI_FBX_PARSER_H |
47 | | |
48 | | #include <stdint.h> |
49 | | #include <map> |
50 | | #include <memory> |
51 | | #include <vector> |
52 | | #include <assimp/LogAux.h> |
53 | | #include <assimp/fast_atof.h> |
54 | | |
55 | | #include "Common/StackAllocator.h" |
56 | | #include "FBXCompileConfig.h" |
57 | | #include "FBXTokenizer.h" |
58 | | |
59 | | namespace Assimp { |
60 | | namespace FBX { |
61 | | |
62 | | class Scope; |
63 | | class Parser; |
64 | | class Element; |
65 | | |
66 | | using ScopeList = std::vector<Scope*>; |
67 | | using ElementMap = std::fbx_unordered_multimap< std::string, Element*>; |
68 | | using ElementCollection = std::pair<ElementMap::const_iterator,ElementMap::const_iterator>; |
69 | | |
70 | 100 | #define new_Scope new (allocator.Allocate(sizeof(Scope))) Scope |
71 | 4.76k | #define new_Element new (allocator.Allocate(sizeof(Element))) Element |
72 | 47 | #define delete_Scope(_p) (_p)->~Scope() |
73 | 2.38k | #define delete_Element(_p) (_p)->~Element() |
74 | | |
75 | | /** FBX data entity that consists of a key:value tuple. |
76 | | * |
77 | | * Example: |
78 | | * @verbatim |
79 | | * AnimationCurve: 23, "AnimCurve::", "" { |
80 | | * [..] |
81 | | * } |
82 | | * @endverbatim |
83 | | * |
84 | | * As can be seen in this sample, elements can contain nested #Scope |
85 | | * as their trailing member. |
86 | | **/ |
87 | | class Element { |
88 | | public: |
89 | | Element(const Token& key_token, Parser& parser); |
90 | | ~Element(); |
91 | | |
92 | 0 | const Scope* Compound() const { |
93 | 0 | return compound; |
94 | 0 | } |
95 | | |
96 | 0 | const Token& KeyToken() const { |
97 | 0 | return key_token; |
98 | 0 | } |
99 | | |
100 | 0 | const TokenList& Tokens() const { |
101 | 0 | return tokens; |
102 | 0 | } |
103 | | |
104 | | private: |
105 | | const Token& key_token; |
106 | | TokenList tokens; |
107 | | Scope* compound; |
108 | | }; |
109 | | |
110 | | /** FBX data entity that consists of a 'scope', a collection |
111 | | * of not necessarily unique #Element instances. |
112 | | * |
113 | | * Example: |
114 | | * @verbatim |
115 | | * GlobalSettings: { |
116 | | * Version: 1000 |
117 | | * Properties70: |
118 | | * [...] |
119 | | * } |
120 | | * @endverbatim */ |
121 | | class Scope |
122 | | { |
123 | | public: |
124 | | Scope(Parser& parser, bool topLevel = false); |
125 | | ~Scope(); |
126 | | |
127 | 7 | const Element* operator[] (const std::string& index) const { |
128 | 7 | ElementMap::const_iterator it = elements.find(index); |
129 | 7 | return it == elements.end() ? nullptr : (*it).second; |
130 | 7 | } |
131 | | |
132 | 0 | const Element* FindElementCaseInsensitive(const std::string& elementName) const { |
133 | 0 | const char* elementNameCStr = elementName.c_str(); |
134 | 0 | for (auto element = elements.begin(); element != elements.end(); ++element) |
135 | 0 | { |
136 | 0 | if (!ASSIMP_strincmp(element->first.c_str(), elementNameCStr, AI_MAXLEN)) { |
137 | 0 | return element->second; |
138 | 0 | } |
139 | 0 | } |
140 | 0 | return nullptr; |
141 | 0 | } |
142 | | |
143 | 0 | ElementCollection GetCollection(const std::string& index) const { |
144 | 0 | return elements.equal_range(index); |
145 | 0 | } |
146 | | |
147 | 0 | const ElementMap& Elements() const { |
148 | 0 | return elements; |
149 | 0 | } |
150 | | |
151 | | private: |
152 | | ElementMap elements; |
153 | | }; |
154 | | |
155 | | /** FBX parsing class, takes a list of input tokens and generates a hierarchy |
156 | | * of nested #Scope instances, representing the fbx DOM.*/ |
157 | | class Parser |
158 | | { |
159 | | public: |
160 | | /** Parse given a token list. Does not take ownership of the tokens - |
161 | | * the objects must persist during the entire parser lifetime */ |
162 | | Parser(const TokenList &tokens, StackAllocator &allocator, bool is_binary); |
163 | | ~Parser(); |
164 | | |
165 | 7 | const Scope& GetRootScope() const { |
166 | 7 | return *root; |
167 | 7 | } |
168 | | |
169 | 0 | bool IsBinary() const { |
170 | 0 | return is_binary; |
171 | 0 | } |
172 | | |
173 | 2.43k | StackAllocator &GetAllocator() { |
174 | 2.43k | return allocator; |
175 | 2.43k | } |
176 | | |
177 | | private: |
178 | | friend class Scope; |
179 | | friend class Element; |
180 | | |
181 | | TokenPtr AdvanceToNextToken(); |
182 | | TokenPtr LastToken() const; |
183 | | TokenPtr CurrentToken() const; |
184 | | |
185 | | private: |
186 | | const TokenList& tokens; |
187 | | StackAllocator &allocator; |
188 | | TokenPtr last, current; |
189 | | TokenList::const_iterator cursor; |
190 | | Scope *root; |
191 | | |
192 | | const bool is_binary; |
193 | | }; |
194 | | |
195 | | |
196 | | /* token parsing - this happens when building the DOM out of the parse-tree*/ |
197 | | uint64_t ParseTokenAsID(const Token& t, const char*& err_out); |
198 | | size_t ParseTokenAsDim(const Token& t, const char*& err_out); |
199 | | |
200 | | float ParseTokenAsFloat(const Token& t, const char*& err_out); |
201 | | int ParseTokenAsInt(const Token& t, const char*& err_out); |
202 | | int64_t ParseTokenAsInt64(const Token& t, const char*& err_out); |
203 | | std::string ParseTokenAsString(const Token& t, const char*& err_out); |
204 | | |
205 | | /* wrapper around ParseTokenAsXXX() with DOMError handling */ |
206 | | uint64_t ParseTokenAsID(const Token& t); |
207 | | size_t ParseTokenAsDim(const Token& t); |
208 | | float ParseTokenAsFloat(const Token& t); |
209 | | int ParseTokenAsInt(const Token& t); |
210 | | int64_t ParseTokenAsInt64(const Token& t); |
211 | | std::string ParseTokenAsString(const Token& t); |
212 | | |
213 | | /* read data arrays */ |
214 | | void ParseVectorDataArray(std::vector<aiVector3D>& out, const Element& el); |
215 | | void ParseVectorDataArray(std::vector<aiColor4D>& out, const Element& el); |
216 | | void ParseVectorDataArray(std::vector<aiVector2D>& out, const Element& el); |
217 | | void ParseVectorDataArray(std::vector<int>& out, const Element& el); |
218 | | void ParseVectorDataArray(std::vector<float>& out, const Element& el); |
219 | | void ParseVectorDataArray(std::vector<unsigned int>& out, const Element& el); |
220 | | void ParseVectorDataArray(std::vector<uint64_t>& out, const Element& e); |
221 | | void ParseVectorDataArray(std::vector<int64_t>& out, const Element& el); |
222 | | |
223 | | bool HasElement( const Scope& sc, const std::string& index ); |
224 | | |
225 | | // extract a required element from a scope, abort if the element cannot be found |
226 | | const Element &GetRequiredElement(const Scope &sc, const std::string &index, const Element *element = nullptr); |
227 | | |
228 | | // extract required compound scope |
229 | | const Scope& GetRequiredScope(const Element& el); |
230 | | // get token at a particular index |
231 | | const Token& GetRequiredToken(const Element& el, unsigned int index); |
232 | | |
233 | | // read a 4x4 matrix from an array of 16 floats |
234 | | aiMatrix4x4 ReadMatrix(const Element& element); |
235 | | |
236 | | } // ! FBX |
237 | | } // ! Assimp |
238 | | |
239 | | #endif // ! INCLUDED_AI_FBX_PARSER_H |