vrshoot
view libs/assimp/FBXParser.h @ 1:e7ca128b8713
looks nice :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 00:35:22 +0200 |
parents | |
children |
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file FBXParser.h
42 * @brief FBX parsing code
43 */
44 #ifndef INCLUDED_AI_FBX_PARSER_H
45 #define INCLUDED_AI_FBX_PARSER_H
47 #include <vector>
48 #include <map>
49 #include <string>
50 #include <utility>
52 #include <boost/shared_ptr.hpp>
54 #include "LogAux.h"
56 #include "FBXCompileConfig.h"
57 #include "FBXTokenizer.h"
59 namespace Assimp {
60 namespace FBX {
62 class Scope;
63 class Parser;
64 class Element;
66 // XXX should use C++11's unique_ptr - but assimp's need to keep working with 03
67 typedef std::vector< Scope* > ScopeList;
68 typedef std::fbx_unordered_multimap< std::string, Element* > ElementMap;
70 typedef std::pair<ElementMap::const_iterator,ElementMap::const_iterator> ElementCollection;
72 # define new_Scope new Scope
73 # define new_Element new Element
76 /** FBX data entity that consists of a key:value tuple.
77 *
78 * Example:
79 * @verbatim
80 * AnimationCurve: 23, "AnimCurve::", "" {
81 * [..]
82 * }
83 * @endverbatim
84 *
85 * As can be seen in this sample, elements can contain nested #Scope
86 * as their trailing member. **/
87 class Element
88 {
89 public:
91 Element(const Token& key_token, Parser& parser);
92 ~Element();
94 public:
96 const Scope* Compound() const {
97 return compound.get();
98 }
100 const Token& KeyToken() const {
101 return key_token;
102 }
104 const TokenList& Tokens() const {
105 return tokens;
106 }
108 private:
110 const Token& key_token;
111 TokenList tokens;
112 boost::scoped_ptr<Scope> compound;
113 };
117 /** FBX data entity that consists of a 'scope', a collection
118 * of not necessarily unique #Element instances.
119 *
120 * Example:
121 * @verbatim
122 * GlobalSettings: {
123 * Version: 1000
124 * Properties70:
125 * [...]
126 * }
127 * @endverbatim */
128 class Scope
129 {
131 public:
133 Scope(Parser& parser, bool topLevel = false);
134 ~Scope();
136 public:
138 const Element* operator[] (const std::string& index) const {
139 ElementMap::const_iterator it = elements.find(index);
140 return it == elements.end() ? NULL : (*it).second;
141 }
143 ElementCollection GetCollection(const std::string& index) const {
144 return elements.equal_range(index);
145 }
147 const ElementMap& Elements() const {
148 return elements;
149 }
151 private:
153 ElementMap elements;
154 };
157 /** FBX parsing class, takes a list of input tokens and generates a hierarchy
158 * of nested #Scope instances, representing the fbx DOM.*/
159 class Parser
160 {
161 public:
163 /** Parse given a token list. Does not take ownership of the tokens -
164 * the objects must persist during the entire parser lifetime */
165 Parser (const TokenList& tokens,bool is_binary);
166 ~Parser();
168 public:
170 const Scope& GetRootScope() const {
171 return *root.get();
172 }
175 bool IsBinary() const {
176 return is_binary;
177 }
179 private:
181 friend class Scope;
182 friend class Element;
184 TokenPtr AdvanceToNextToken();
186 TokenPtr LastToken() const;
187 TokenPtr CurrentToken() const;
191 private:
193 const TokenList& tokens;
195 TokenPtr last, current;
196 TokenList::const_iterator cursor;
197 boost::scoped_ptr<Scope> root;
199 const bool is_binary;
200 };
203 /* token parsing - this happens when building the DOM out of the parse-tree*/
204 uint64_t ParseTokenAsID(const Token& t, const char*& err_out);
205 size_t ParseTokenAsDim(const Token& t, const char*& err_out);
207 float ParseTokenAsFloat(const Token& t, const char*& err_out);
208 int ParseTokenAsInt(const Token& t, const char*& err_out);
209 std::string ParseTokenAsString(const Token& t, const char*& err_out);
212 /* wrapper around ParseTokenAsXXX() with DOMError handling */
213 uint64_t ParseTokenAsID(const Token& t);
214 size_t ParseTokenAsDim(const Token& t);
215 float ParseTokenAsFloat(const Token& t);
216 int ParseTokenAsInt(const Token& t);
217 std::string ParseTokenAsString(const Token& t);
219 /* read data arrays */
220 void ParseVectorDataArray(std::vector<aiVector3D>& out, const Element& el);
221 void ParseVectorDataArray(std::vector<aiColor4D>& out, const Element& el);
222 void ParseVectorDataArray(std::vector<aiVector2D>& out, const Element& el);
223 void ParseVectorDataArray(std::vector<int>& out, const Element& el);
224 void ParseVectorDataArray(std::vector<float>& out, const Element& el);
225 void ParseVectorDataArray(std::vector<unsigned int>& out, const Element& el);
226 void ParseVectorDataArray(std::vector<uint64_t>& out, const Element& e);
230 // extract a required element from a scope, abort if the element cannot be found
231 const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element = NULL);
233 // extract required compound scope
234 const Scope& GetRequiredScope(const Element& el);
235 // get token at a particular index
236 const Token& GetRequiredToken(const Element& el, unsigned int index);
240 // read a 4x4 matrix from an array of 16 floats
241 aiMatrix4x4 ReadMatrix(const Element& element);
243 } // ! FBX
244 } // ! Assimp
246 #endif // ! INCLUDED_AI_FBX_PARSER_H