vrshoot
diff libs/assimp/FBXParser.cpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/FBXParser.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,1212 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file FBXParser.cpp 1.45 + * @brief Implementation of the FBX parser and the rudimentary DOM that we use 1.46 + */ 1.47 +#include "AssimpPCH.h" 1.48 + 1.49 +#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER 1.50 + 1.51 + 1.52 +#ifdef ASSIMP_BUILD_NO_OWN_ZLIB 1.53 +# include <zlib.h> 1.54 +#else 1.55 +# include "../contrib/zlib/zlib.h" 1.56 +#endif 1.57 + 1.58 + 1.59 +#include "FBXTokenizer.h" 1.60 +#include "FBXParser.h" 1.61 +#include "FBXUtil.h" 1.62 + 1.63 +#include "ParsingUtils.h" 1.64 +#include "fast_atof.h" 1.65 + 1.66 +using namespace Assimp; 1.67 +using namespace Assimp::FBX; 1.68 + 1.69 +namespace { 1.70 + 1.71 + 1.72 + // ------------------------------------------------------------------------------------------------ 1.73 + // signal parse error, this is always unrecoverable. Throws DeadlyImportError. 1.74 + void ParseError(const std::string& message, const Token& token) 1.75 + { 1.76 + throw DeadlyImportError(Util::AddTokenText("FBX-Parser",message,&token)); 1.77 + } 1.78 + 1.79 + // ------------------------------------------------------------------------------------------------ 1.80 + void ParseError(const std::string& message, const Element* element = NULL) 1.81 + { 1.82 + if(element) { 1.83 + ParseError(message,element->KeyToken()); 1.84 + } 1.85 + throw DeadlyImportError("FBX-Parser " + message); 1.86 + } 1.87 + 1.88 + 1.89 + // ------------------------------------------------------------------------------------------------ 1.90 + // print warning, do return 1.91 + void ParseWarning(const std::string& message, const Token& token) 1.92 + { 1.93 + if(DefaultLogger::get()) { 1.94 + DefaultLogger::get()->warn(Util::AddTokenText("FBX-Parser",message,&token)); 1.95 + } 1.96 + } 1.97 + 1.98 + // ------------------------------------------------------------------------------------------------ 1.99 + void ParseWarning(const std::string& message, const Element* element = NULL) 1.100 + { 1.101 + if(element) { 1.102 + ParseWarning(message,element->KeyToken()); 1.103 + return; 1.104 + } 1.105 + if(DefaultLogger::get()) { 1.106 + DefaultLogger::get()->warn("FBX-Parser: " + message); 1.107 + } 1.108 + } 1.109 + 1.110 + // ------------------------------------------------------------------------------------------------ 1.111 + void ParseError(const std::string& message, TokenPtr token) 1.112 + { 1.113 + if(token) { 1.114 + ParseError(message, *token); 1.115 + } 1.116 + ParseError(message); 1.117 + } 1.118 + 1.119 +} 1.120 + 1.121 +namespace Assimp { 1.122 +namespace FBX { 1.123 + 1.124 +// ------------------------------------------------------------------------------------------------ 1.125 +Element::Element(const Token& key_token, Parser& parser) 1.126 +: key_token(key_token) 1.127 +{ 1.128 + TokenPtr n = NULL; 1.129 + do { 1.130 + n = parser.AdvanceToNextToken(); 1.131 + if(!n) { 1.132 + ParseError("unexpected end of file, expected closing bracket",parser.LastToken()); 1.133 + } 1.134 + 1.135 + if (n->Type() == TokenType_DATA) { 1.136 + tokens.push_back(n); 1.137 + 1.138 + n = parser.AdvanceToNextToken(); 1.139 + if(!n) { 1.140 + ParseError("unexpected end of file, expected bracket, comma or key",parser.LastToken()); 1.141 + } 1.142 + 1.143 + const TokenType ty = n->Type(); 1.144 + if (ty != TokenType_OPEN_BRACKET && ty != TokenType_CLOSE_BRACKET && ty != TokenType_COMMA && ty != TokenType_KEY) { 1.145 + ParseError("unexpected token; expected bracket, comma or key",n); 1.146 + } 1.147 + } 1.148 + 1.149 + if (n->Type() == TokenType_OPEN_BRACKET) { 1.150 + compound.reset(new Scope(parser)); 1.151 + 1.152 + // current token should be a TOK_CLOSE_BRACKET 1.153 + n = parser.CurrentToken(); 1.154 + ai_assert(n); 1.155 + 1.156 + if (n->Type() != TokenType_CLOSE_BRACKET) { 1.157 + ParseError("expected closing bracket",n); 1.158 + } 1.159 + 1.160 + parser.AdvanceToNextToken(); 1.161 + return; 1.162 + } 1.163 + } 1.164 + while(n->Type() != TokenType_KEY && n->Type() != TokenType_CLOSE_BRACKET); 1.165 +} 1.166 + 1.167 +// ------------------------------------------------------------------------------------------------ 1.168 +Element::~Element() 1.169 +{ 1.170 + // no need to delete tokens, they are owned by the parser 1.171 +} 1.172 + 1.173 +// ------------------------------------------------------------------------------------------------ 1.174 +Scope::Scope(Parser& parser,bool topLevel) 1.175 +{ 1.176 + if(!topLevel) { 1.177 + TokenPtr t = parser.CurrentToken(); 1.178 + if (t->Type() != TokenType_OPEN_BRACKET) { 1.179 + ParseError("expected open bracket",t); 1.180 + } 1.181 + } 1.182 + 1.183 + TokenPtr n = parser.AdvanceToNextToken(); 1.184 + if(n == NULL) { 1.185 + ParseError("unexpected end of file"); 1.186 + } 1.187 + 1.188 + // note: empty scopes are allowed 1.189 + while(n->Type() != TokenType_CLOSE_BRACKET) { 1.190 + if (n->Type() != TokenType_KEY) { 1.191 + ParseError("unexpected token, expected TOK_KEY",n); 1.192 + } 1.193 + 1.194 + const std::string& str = n->StringContents(); 1.195 + elements.insert(ElementMap::value_type(str,new_Element(*n,parser))); 1.196 + 1.197 + // Element() should stop at the next Key token (or right after a Close token) 1.198 + n = parser.CurrentToken(); 1.199 + if(n == NULL) { 1.200 + if (topLevel) { 1.201 + return; 1.202 + } 1.203 + ParseError("unexpected end of file",parser.LastToken()); 1.204 + } 1.205 + } 1.206 +} 1.207 + 1.208 +// ------------------------------------------------------------------------------------------------ 1.209 +Scope::~Scope() 1.210 +{ 1.211 + BOOST_FOREACH(ElementMap::value_type& v, elements) { 1.212 + delete v.second; 1.213 + } 1.214 +} 1.215 + 1.216 + 1.217 +// ------------------------------------------------------------------------------------------------ 1.218 +Parser::Parser (const TokenList& tokens, bool is_binary) 1.219 +: tokens(tokens) 1.220 +, last() 1.221 +, current() 1.222 +, cursor(tokens.begin()) 1.223 +, is_binary(is_binary) 1.224 +{ 1.225 + root.reset(new Scope(*this,true)); 1.226 +} 1.227 + 1.228 + 1.229 +// ------------------------------------------------------------------------------------------------ 1.230 +Parser::~Parser() 1.231 +{ 1.232 +} 1.233 + 1.234 + 1.235 +// ------------------------------------------------------------------------------------------------ 1.236 +TokenPtr Parser::AdvanceToNextToken() 1.237 +{ 1.238 + last = current; 1.239 + if (cursor == tokens.end()) { 1.240 + current = NULL; 1.241 + } 1.242 + else { 1.243 + current = *cursor++; 1.244 + } 1.245 + return current; 1.246 +} 1.247 + 1.248 + 1.249 +// ------------------------------------------------------------------------------------------------ 1.250 +TokenPtr Parser::CurrentToken() const 1.251 +{ 1.252 + return current; 1.253 +} 1.254 + 1.255 + 1.256 +// ------------------------------------------------------------------------------------------------ 1.257 +TokenPtr Parser::LastToken() const 1.258 +{ 1.259 + return last; 1.260 +} 1.261 + 1.262 + 1.263 +// ------------------------------------------------------------------------------------------------ 1.264 +uint64_t ParseTokenAsID(const Token& t, const char*& err_out) 1.265 +{ 1.266 + err_out = NULL; 1.267 + 1.268 + if (t.Type() != TokenType_DATA) { 1.269 + err_out = "expected TOK_DATA token"; 1.270 + return 0L; 1.271 + } 1.272 + 1.273 + if(t.IsBinary()) 1.274 + { 1.275 + const char* data = t.begin(); 1.276 + if (data[0] != 'L') { 1.277 + err_out = "failed to parse ID, unexpected data type, expected L(ong) (binary)"; 1.278 + return 0L; 1.279 + } 1.280 + 1.281 + ai_assert(t.end() - data == 9); 1.282 + 1.283 + BE_NCONST uint64_t id = *reinterpret_cast<const uint64_t*>(data+1); 1.284 + AI_SWAP8(id); 1.285 + return id; 1.286 + } 1.287 + 1.288 + // XXX: should use size_t here 1.289 + unsigned int length = static_cast<unsigned int>(t.end() - t.begin()); 1.290 + ai_assert(length > 0); 1.291 + 1.292 + const char* out; 1.293 + const uint64_t id = strtoul10_64(t.begin(),&out,&length); 1.294 + if (out > t.end()) { 1.295 + err_out = "failed to parse ID (text)"; 1.296 + return 0L; 1.297 + } 1.298 + 1.299 + return id; 1.300 +} 1.301 + 1.302 + 1.303 +// ------------------------------------------------------------------------------------------------ 1.304 +size_t ParseTokenAsDim(const Token& t, const char*& err_out) 1.305 +{ 1.306 + // same as ID parsing, except there is a trailing asterisk 1.307 + err_out = NULL; 1.308 + 1.309 + if (t.Type() != TokenType_DATA) { 1.310 + err_out = "expected TOK_DATA token"; 1.311 + return 0; 1.312 + } 1.313 + 1.314 + if(t.IsBinary()) 1.315 + { 1.316 + const char* data = t.begin(); 1.317 + if (data[0] != 'L') { 1.318 + err_out = "failed to parse ID, unexpected data type, expected L(ong) (binary)"; 1.319 + return 0; 1.320 + } 1.321 + 1.322 + ai_assert(t.end() - data == 9); 1.323 + BE_NCONST uint64_t id = *reinterpret_cast<const uint64_t*>(data+1); 1.324 + AI_SWAP8(id); 1.325 + return static_cast<size_t>(id); 1.326 + } 1.327 + 1.328 + if(*t.begin() != '*') { 1.329 + err_out = "expected asterisk before array dimension"; 1.330 + return 0; 1.331 + } 1.332 + 1.333 + // XXX: should use size_t here 1.334 + unsigned int length = static_cast<unsigned int>(t.end() - t.begin()); 1.335 + if(length == 0) { 1.336 + err_out = "expected valid integer number after asterisk"; 1.337 + return 0; 1.338 + } 1.339 + 1.340 + const char* out; 1.341 + const size_t id = static_cast<size_t>(strtoul10_64(t.begin() + 1,&out,&length)); 1.342 + if (out > t.end()) { 1.343 + err_out = "failed to parse ID"; 1.344 + return 0; 1.345 + } 1.346 + 1.347 + return id; 1.348 +} 1.349 + 1.350 + 1.351 +// ------------------------------------------------------------------------------------------------ 1.352 +float ParseTokenAsFloat(const Token& t, const char*& err_out) 1.353 +{ 1.354 + err_out = NULL; 1.355 + 1.356 + if (t.Type() != TokenType_DATA) { 1.357 + err_out = "expected TOK_DATA token"; 1.358 + return 0.0f; 1.359 + } 1.360 + 1.361 + if(t.IsBinary()) 1.362 + { 1.363 + const char* data = t.begin(); 1.364 + if (data[0] != 'F' && data[0] != 'D') { 1.365 + err_out = "failed to parse F(loat) or D(ouble), unexpected data type (binary)"; 1.366 + return 0.0f; 1.367 + } 1.368 + 1.369 + if (data[0] == 'F') { 1.370 + ai_assert(t.end() - data == 5); 1.371 + // no byte swapping needed for ieee floats 1.372 + float res; 1.373 + memcpy(&res, data + 1, sizeof res); 1.374 + return res; 1.375 + } 1.376 + else { 1.377 + ai_assert(t.end() - data == 9); 1.378 + // no byte swapping needed for ieee floats 1.379 + double res; 1.380 + memcpy(&res, data + 1, sizeof res); 1.381 + return (float)res; 1.382 + } 1.383 + } 1.384 + 1.385 + // need to copy the input string to a temporary buffer 1.386 + // first - next in the fbx token stream comes ',', 1.387 + // which fast_atof could interpret as decimal point. 1.388 +#define MAX_FLOAT_LENGTH 31 1.389 + char temp[MAX_FLOAT_LENGTH + 1]; 1.390 + const size_t length = static_cast<size_t>(t.end()-t.begin()); 1.391 + std::copy(t.begin(),t.end(),temp); 1.392 + temp[std::min(static_cast<size_t>(MAX_FLOAT_LENGTH),length)] = '\0'; 1.393 + 1.394 + return fast_atof(temp); 1.395 +} 1.396 + 1.397 + 1.398 +// ------------------------------------------------------------------------------------------------ 1.399 +int ParseTokenAsInt(const Token& t, const char*& err_out) 1.400 +{ 1.401 + err_out = NULL; 1.402 + 1.403 + if (t.Type() != TokenType_DATA) { 1.404 + err_out = "expected TOK_DATA token"; 1.405 + return 0; 1.406 + } 1.407 + 1.408 + if(t.IsBinary()) 1.409 + { 1.410 + const char* data = t.begin(); 1.411 + if (data[0] != 'I') { 1.412 + err_out = "failed to parse I(nt), unexpected data type (binary)"; 1.413 + return 0; 1.414 + } 1.415 + 1.416 + ai_assert(t.end() - data == 5); 1.417 + BE_NCONST int32_t ival = *reinterpret_cast<const int32_t*>(data+1); 1.418 + AI_SWAP4(ival); 1.419 + return static_cast<int>(ival); 1.420 + } 1.421 + 1.422 + ai_assert(static_cast<size_t>(t.end() - t.begin()) > 0); 1.423 + 1.424 + const char* out; 1.425 + const int intval = strtol10(t.begin(),&out); 1.426 + if (out != t.end()) { 1.427 + err_out = "failed to parse ID"; 1.428 + return 0; 1.429 + } 1.430 + 1.431 + return intval; 1.432 +} 1.433 + 1.434 + 1.435 +// ------------------------------------------------------------------------------------------------ 1.436 +std::string ParseTokenAsString(const Token& t, const char*& err_out) 1.437 +{ 1.438 + err_out = NULL; 1.439 + 1.440 + if (t.Type() != TokenType_DATA) { 1.441 + err_out = "expected TOK_DATA token"; 1.442 + return ""; 1.443 + } 1.444 + 1.445 + if(t.IsBinary()) 1.446 + { 1.447 + const char* data = t.begin(); 1.448 + if (data[0] != 'S') { 1.449 + err_out = "failed to parse S(tring), unexpected data type (binary)"; 1.450 + return ""; 1.451 + } 1.452 + 1.453 + ai_assert(t.end() - data >= 5); 1.454 + 1.455 + // read string length 1.456 + BE_NCONST int32_t len = *reinterpret_cast<const int32_t*>(data+1); 1.457 + AI_SWAP4(len); 1.458 + 1.459 + ai_assert(t.end() - data == 5 + len); 1.460 + return std::string(data + 5, len); 1.461 + } 1.462 + 1.463 + const size_t length = static_cast<size_t>(t.end() - t.begin()); 1.464 + if(length < 2) { 1.465 + err_out = "token is too short to hold a string"; 1.466 + return ""; 1.467 + } 1.468 + 1.469 + const char* s = t.begin(), *e = t.end() - 1; 1.470 + if (*s != '\"' || *e != '\"') { 1.471 + err_out = "expected double quoted string"; 1.472 + return ""; 1.473 + } 1.474 + 1.475 + return std::string(s+1,length-2); 1.476 +} 1.477 + 1.478 + 1.479 +namespace { 1.480 + 1.481 +// ------------------------------------------------------------------------------------------------ 1.482 +// read the type code and element count of a binary data array and stop there 1.483 +void ReadBinaryDataArrayHead(const char*& data, const char* end, char& type, uint32_t& count, 1.484 + const Element& el) 1.485 +{ 1.486 + if (static_cast<size_t>(end-data) < 5) { 1.487 + ParseError("binary data array is too short, need five (5) bytes for type signature and element count",&el); 1.488 + } 1.489 + 1.490 + // data type 1.491 + type = *data; 1.492 + 1.493 + // read number of elements 1.494 + BE_NCONST uint32_t len = *reinterpret_cast<const uint32_t*>(data+1); 1.495 + AI_SWAP4(len); 1.496 + 1.497 + count = len; 1.498 + data += 5; 1.499 +} 1.500 + 1.501 + 1.502 +// ------------------------------------------------------------------------------------------------ 1.503 +// read binary data array, assume cursor points to the 'compression mode' field (i.e. behind the header) 1.504 +void ReadBinaryDataArray(char type, uint32_t count, const char*& data, const char* end, 1.505 + std::vector<char>& buff, 1.506 + const Element& el) 1.507 +{ 1.508 + ai_assert(static_cast<size_t>(end-data) >= 4); // runtime check for this happens at tokenization stage 1.509 + 1.510 + BE_NCONST uint32_t encmode = *reinterpret_cast<const uint32_t*>(data); 1.511 + AI_SWAP4(encmode); 1.512 + data += 4; 1.513 + 1.514 + // next comes the compressed length 1.515 + BE_NCONST uint32_t comp_len = *reinterpret_cast<const uint32_t*>(data); 1.516 + AI_SWAP4(comp_len); 1.517 + data += 4; 1.518 + 1.519 + ai_assert(data + comp_len == end); 1.520 + 1.521 + // determine the length of the uncompressed data by looking at the type signature 1.522 + uint32_t stride; 1.523 + switch(type) 1.524 + { 1.525 + case 'f': 1.526 + case 'i': 1.527 + stride = 4; 1.528 + break; 1.529 + 1.530 + case 'd': 1.531 + case 'l': 1.532 + stride = 8; 1.533 + break; 1.534 + 1.535 + default: 1.536 + ai_assert(false); 1.537 + }; 1.538 + 1.539 + const uint32_t full_length = stride * count; 1.540 + buff.resize(full_length); 1.541 + 1.542 + if(encmode == 0) { 1.543 + ai_assert(full_length == comp_len); 1.544 + 1.545 + // plain data, no compression 1.546 + std::copy(data, end, buff.begin()); 1.547 + } 1.548 + else if(encmode == 1) { 1.549 + // zlib/deflate, next comes ZIP head (0x78 0x01) 1.550 + // see http://www.ietf.org/rfc/rfc1950.txt 1.551 + 1.552 + z_stream zstream; 1.553 + zstream.opaque = Z_NULL; 1.554 + zstream.zalloc = Z_NULL; 1.555 + zstream.zfree = Z_NULL; 1.556 + zstream.data_type = Z_BINARY; 1.557 + 1.558 + // http://hewgill.com/journal/entries/349-how-to-decompress-gzip-stream-with-zlib 1.559 + inflateInit(&zstream); 1.560 + 1.561 + zstream.next_in = reinterpret_cast<Bytef*>( const_cast<char*>(data) ); 1.562 + zstream.avail_in = comp_len; 1.563 + 1.564 + zstream.avail_out = buff.size(); 1.565 + zstream.next_out = reinterpret_cast<Bytef*>(&*buff.begin()); 1.566 + const int ret = inflate(&zstream, Z_FINISH); 1.567 + 1.568 + if (ret != Z_STREAM_END && ret != Z_OK) { 1.569 + ParseError("failure decompressing compressed data section"); 1.570 + } 1.571 + 1.572 + // terminate zlib 1.573 + inflateEnd(&zstream); 1.574 + } 1.575 +#ifdef _DEBUG 1.576 + else { 1.577 + // runtime check for this happens at tokenization stage 1.578 + ai_assert(false); 1.579 + } 1.580 +#endif 1.581 + 1.582 + data += comp_len; 1.583 + ai_assert(data == end); 1.584 +} 1.585 + 1.586 +} // !anon 1.587 + 1.588 + 1.589 +// ------------------------------------------------------------------------------------------------ 1.590 +// read an array of float3 tuples 1.591 +void ParseVectorDataArray(std::vector<aiVector3D>& out, const Element& el) 1.592 +{ 1.593 + out.clear(); 1.594 + 1.595 + const TokenList& tok = el.Tokens(); 1.596 + if(tok.empty()) { 1.597 + ParseError("unexpected empty element",&el); 1.598 + } 1.599 + 1.600 + if(tok[0]->IsBinary()) { 1.601 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.602 + 1.603 + char type; 1.604 + uint32_t count; 1.605 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.606 + 1.607 + if(count % 3 != 0) { 1.608 + ParseError("number of floats is not a multiple of three (3) (binary)",&el); 1.609 + } 1.610 + 1.611 + if(!count) { 1.612 + return; 1.613 + } 1.614 + 1.615 + if (type != 'd' && type != 'f') { 1.616 + ParseError("expected float or double array (binary)",&el); 1.617 + } 1.618 + 1.619 + std::vector<char> buff; 1.620 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.621 + 1.622 + ai_assert(data == end); 1.623 + ai_assert(buff.size() == count * (type == 'd' ? 8 : 4)); 1.624 + 1.625 + const uint32_t count3 = count / 3; 1.626 + out.reserve(count3); 1.627 + 1.628 + if (type == 'd') { 1.629 + const double* d = reinterpret_cast<const double*>(&buff[0]); 1.630 + for (unsigned int i = 0; i < count3; ++i, d += 3) { 1.631 + out.push_back(aiVector3D(static_cast<float>(d[0]), 1.632 + static_cast<float>(d[1]), 1.633 + static_cast<float>(d[2]))); 1.634 + } 1.635 + } 1.636 + else if (type == 'f') { 1.637 + const float* f = reinterpret_cast<const float*>(&buff[0]); 1.638 + for (unsigned int i = 0; i < count3; ++i, f += 3) { 1.639 + out.push_back(aiVector3D(f[0],f[1],f[2])); 1.640 + } 1.641 + } 1.642 + 1.643 + return; 1.644 + } 1.645 + 1.646 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.647 + 1.648 + // may throw bad_alloc if the input is rubbish, but this need 1.649 + // not to be prevented - importing would fail but we wouldn't 1.650 + // crash since assimp handles this case properly. 1.651 + out.reserve(dim); 1.652 + 1.653 + const Scope& scope = GetRequiredScope(el); 1.654 + const Element& a = GetRequiredElement(scope,"a",&el); 1.655 + 1.656 + if (a.Tokens().size() % 3 != 0) { 1.657 + ParseError("number of floats is not a multiple of three (3)",&el); 1.658 + } 1.659 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.660 + aiVector3D v; 1.661 + v.x = ParseTokenAsFloat(**it++); 1.662 + v.y = ParseTokenAsFloat(**it++); 1.663 + v.z = ParseTokenAsFloat(**it++); 1.664 + 1.665 + out.push_back(v); 1.666 + } 1.667 +} 1.668 + 1.669 + 1.670 +// ------------------------------------------------------------------------------------------------ 1.671 +// read an array of color4 tuples 1.672 +void ParseVectorDataArray(std::vector<aiColor4D>& out, const Element& el) 1.673 +{ 1.674 + out.clear(); 1.675 + const TokenList& tok = el.Tokens(); 1.676 + if(tok.empty()) { 1.677 + ParseError("unexpected empty element",&el); 1.678 + } 1.679 + 1.680 + if(tok[0]->IsBinary()) { 1.681 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.682 + 1.683 + char type; 1.684 + uint32_t count; 1.685 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.686 + 1.687 + if(count % 4 != 0) { 1.688 + ParseError("number of floats is not a multiple of four (4) (binary)",&el); 1.689 + } 1.690 + 1.691 + if(!count) { 1.692 + return; 1.693 + } 1.694 + 1.695 + if (type != 'd' && type != 'f') { 1.696 + ParseError("expected float or double array (binary)",&el); 1.697 + } 1.698 + 1.699 + std::vector<char> buff; 1.700 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.701 + 1.702 + ai_assert(data == end); 1.703 + ai_assert(buff.size() == count * (type == 'd' ? 8 : 4)); 1.704 + 1.705 + const uint32_t count4 = count / 4; 1.706 + out.reserve(count4); 1.707 + 1.708 + if (type == 'd') { 1.709 + const double* d = reinterpret_cast<const double*>(&buff[0]); 1.710 + for (unsigned int i = 0; i < count4; ++i, d += 4) { 1.711 + out.push_back(aiColor4D(static_cast<float>(d[0]), 1.712 + static_cast<float>(d[1]), 1.713 + static_cast<float>(d[2]), 1.714 + static_cast<float>(d[3]))); 1.715 + } 1.716 + } 1.717 + else if (type == 'f') { 1.718 + const float* f = reinterpret_cast<const float*>(&buff[0]); 1.719 + for (unsigned int i = 0; i < count4; ++i, f += 4) { 1.720 + out.push_back(aiColor4D(f[0],f[1],f[2],f[3])); 1.721 + } 1.722 + } 1.723 + return; 1.724 + } 1.725 + 1.726 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.727 + 1.728 + // see notes in ParseVectorDataArray() above 1.729 + out.reserve(dim); 1.730 + 1.731 + const Scope& scope = GetRequiredScope(el); 1.732 + const Element& a = GetRequiredElement(scope,"a",&el); 1.733 + 1.734 + if (a.Tokens().size() % 4 != 0) { 1.735 + ParseError("number of floats is not a multiple of four (4)",&el); 1.736 + } 1.737 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.738 + aiColor4D v; 1.739 + v.r = ParseTokenAsFloat(**it++); 1.740 + v.g = ParseTokenAsFloat(**it++); 1.741 + v.b = ParseTokenAsFloat(**it++); 1.742 + v.a = ParseTokenAsFloat(**it++); 1.743 + 1.744 + out.push_back(v); 1.745 + } 1.746 +} 1.747 + 1.748 + 1.749 +// ------------------------------------------------------------------------------------------------ 1.750 +// read an array of float2 tuples 1.751 +void ParseVectorDataArray(std::vector<aiVector2D>& out, const Element& el) 1.752 +{ 1.753 + out.clear(); 1.754 + const TokenList& tok = el.Tokens(); 1.755 + if(tok.empty()) { 1.756 + ParseError("unexpected empty element",&el); 1.757 + } 1.758 + 1.759 + if(tok[0]->IsBinary()) { 1.760 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.761 + 1.762 + char type; 1.763 + uint32_t count; 1.764 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.765 + 1.766 + if(count % 2 != 0) { 1.767 + ParseError("number of floats is not a multiple of two (2) (binary)",&el); 1.768 + } 1.769 + 1.770 + if(!count) { 1.771 + return; 1.772 + } 1.773 + 1.774 + if (type != 'd' && type != 'f') { 1.775 + ParseError("expected float or double array (binary)",&el); 1.776 + } 1.777 + 1.778 + std::vector<char> buff; 1.779 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.780 + 1.781 + ai_assert(data == end); 1.782 + ai_assert(buff.size() == count * (type == 'd' ? 8 : 4)); 1.783 + 1.784 + const uint32_t count2 = count / 2; 1.785 + out.reserve(count2); 1.786 + 1.787 + if (type == 'd') { 1.788 + const double* d = reinterpret_cast<const double*>(&buff[0]); 1.789 + for (unsigned int i = 0; i < count2; ++i, d += 2) { 1.790 + out.push_back(aiVector2D(static_cast<float>(d[0]), 1.791 + static_cast<float>(d[1]))); 1.792 + } 1.793 + } 1.794 + else if (type == 'f') { 1.795 + const float* f = reinterpret_cast<const float*>(&buff[0]); 1.796 + for (unsigned int i = 0; i < count2; ++i, f += 2) { 1.797 + out.push_back(aiVector2D(f[0],f[1])); 1.798 + } 1.799 + } 1.800 + 1.801 + return; 1.802 + } 1.803 + 1.804 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.805 + 1.806 + // see notes in ParseVectorDataArray() above 1.807 + out.reserve(dim); 1.808 + 1.809 + const Scope& scope = GetRequiredScope(el); 1.810 + const Element& a = GetRequiredElement(scope,"a",&el); 1.811 + 1.812 + if (a.Tokens().size() % 2 != 0) { 1.813 + ParseError("number of floats is not a multiple of two (2)",&el); 1.814 + } 1.815 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.816 + aiVector2D v; 1.817 + v.x = ParseTokenAsFloat(**it++); 1.818 + v.y = ParseTokenAsFloat(**it++); 1.819 + 1.820 + out.push_back(v); 1.821 + } 1.822 +} 1.823 + 1.824 + 1.825 +// ------------------------------------------------------------------------------------------------ 1.826 +// read an array of ints 1.827 +void ParseVectorDataArray(std::vector<int>& out, const Element& el) 1.828 +{ 1.829 + out.clear(); 1.830 + const TokenList& tok = el.Tokens(); 1.831 + if(tok.empty()) { 1.832 + ParseError("unexpected empty element",&el); 1.833 + } 1.834 + 1.835 + if(tok[0]->IsBinary()) { 1.836 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.837 + 1.838 + char type; 1.839 + uint32_t count; 1.840 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.841 + 1.842 + if(!count) { 1.843 + return; 1.844 + } 1.845 + 1.846 + if (type != 'i') { 1.847 + ParseError("expected int array (binary)",&el); 1.848 + } 1.849 + 1.850 + std::vector<char> buff; 1.851 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.852 + 1.853 + ai_assert(data == end); 1.854 + ai_assert(buff.size() == count * 4); 1.855 + 1.856 + out.reserve(count); 1.857 + 1.858 + const int32_t* ip = reinterpret_cast<const int32_t*>(&buff[0]); 1.859 + for (unsigned int i = 0; i < count; ++i, ++ip) { 1.860 + BE_NCONST int32_t val = *ip; 1.861 + AI_SWAP4(val); 1.862 + out.push_back(val); 1.863 + } 1.864 + 1.865 + return; 1.866 + } 1.867 + 1.868 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.869 + 1.870 + // see notes in ParseVectorDataArray() 1.871 + out.reserve(dim); 1.872 + 1.873 + const Scope& scope = GetRequiredScope(el); 1.874 + const Element& a = GetRequiredElement(scope,"a",&el); 1.875 + 1.876 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.877 + const int ival = ParseTokenAsInt(**it++); 1.878 + out.push_back(ival); 1.879 + } 1.880 +} 1.881 + 1.882 + 1.883 +// ------------------------------------------------------------------------------------------------ 1.884 +// read an array of floats 1.885 +void ParseVectorDataArray(std::vector<float>& out, const Element& el) 1.886 +{ 1.887 + out.clear(); 1.888 + const TokenList& tok = el.Tokens(); 1.889 + if(tok.empty()) { 1.890 + ParseError("unexpected empty element",&el); 1.891 + } 1.892 + 1.893 + if(tok[0]->IsBinary()) { 1.894 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.895 + 1.896 + char type; 1.897 + uint32_t count; 1.898 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.899 + 1.900 + if(!count) { 1.901 + return; 1.902 + } 1.903 + 1.904 + if (type != 'd' && type != 'f') { 1.905 + ParseError("expected float or double array (binary)",&el); 1.906 + } 1.907 + 1.908 + std::vector<char> buff; 1.909 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.910 + 1.911 + ai_assert(data == end); 1.912 + ai_assert(buff.size() == count * (type == 'd' ? 8 : 4)); 1.913 + 1.914 + if (type == 'd') { 1.915 + const double* d = reinterpret_cast<const double*>(&buff[0]); 1.916 + for (unsigned int i = 0; i < count; ++i, ++d) { 1.917 + out.push_back(static_cast<float>(*d)); 1.918 + } 1.919 + } 1.920 + else if (type == 'f') { 1.921 + const float* f = reinterpret_cast<const float*>(&buff[0]); 1.922 + for (unsigned int i = 0; i < count; ++i, ++f) { 1.923 + out.push_back(*f); 1.924 + } 1.925 + } 1.926 + 1.927 + return; 1.928 + } 1.929 + 1.930 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.931 + 1.932 + // see notes in ParseVectorDataArray() 1.933 + out.reserve(dim); 1.934 + 1.935 + const Scope& scope = GetRequiredScope(el); 1.936 + const Element& a = GetRequiredElement(scope,"a",&el); 1.937 + 1.938 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.939 + const float ival = ParseTokenAsFloat(**it++); 1.940 + out.push_back(ival); 1.941 + } 1.942 +} 1.943 + 1.944 + 1.945 +// ------------------------------------------------------------------------------------------------ 1.946 +// read an array of uints 1.947 +void ParseVectorDataArray(std::vector<unsigned int>& out, const Element& el) 1.948 +{ 1.949 + out.clear(); 1.950 + const TokenList& tok = el.Tokens(); 1.951 + if(tok.empty()) { 1.952 + ParseError("unexpected empty element",&el); 1.953 + } 1.954 + 1.955 + if(tok[0]->IsBinary()) { 1.956 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.957 + 1.958 + char type; 1.959 + uint32_t count; 1.960 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.961 + 1.962 + if(!count) { 1.963 + return; 1.964 + } 1.965 + 1.966 + if (type != 'i') { 1.967 + ParseError("expected (u)int array (binary)",&el); 1.968 + } 1.969 + 1.970 + std::vector<char> buff; 1.971 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.972 + 1.973 + ai_assert(data == end); 1.974 + ai_assert(buff.size() == count * 4); 1.975 + 1.976 + out.reserve(count); 1.977 + 1.978 + const int32_t* ip = reinterpret_cast<const int32_t*>(&buff[0]); 1.979 + for (unsigned int i = 0; i < count; ++i, ++ip) { 1.980 + BE_NCONST int32_t val = *ip; 1.981 + if(val < 0) { 1.982 + ParseError("encountered negative integer index (binary)"); 1.983 + } 1.984 + 1.985 + AI_SWAP4(val); 1.986 + out.push_back(val); 1.987 + } 1.988 + 1.989 + return; 1.990 + } 1.991 + 1.992 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.993 + 1.994 + // see notes in ParseVectorDataArray() 1.995 + out.reserve(dim); 1.996 + 1.997 + const Scope& scope = GetRequiredScope(el); 1.998 + const Element& a = GetRequiredElement(scope,"a",&el); 1.999 + 1.1000 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.1001 + const int ival = ParseTokenAsInt(**it++); 1.1002 + if(ival < 0) { 1.1003 + ParseError("encountered negative integer index"); 1.1004 + } 1.1005 + out.push_back(static_cast<unsigned int>(ival)); 1.1006 + } 1.1007 +} 1.1008 + 1.1009 + 1.1010 +// ------------------------------------------------------------------------------------------------ 1.1011 +// read an array of uint64_ts 1.1012 +void ParseVectorDataArray(std::vector<uint64_t>& out, const Element& el) 1.1013 +{ 1.1014 + out.clear(); 1.1015 + const TokenList& tok = el.Tokens(); 1.1016 + if(tok.empty()) { 1.1017 + ParseError("unexpected empty element",&el); 1.1018 + } 1.1019 + 1.1020 + if(tok[0]->IsBinary()) { 1.1021 + const char* data = tok[0]->begin(), *end = tok[0]->end(); 1.1022 + 1.1023 + char type; 1.1024 + uint32_t count; 1.1025 + ReadBinaryDataArrayHead(data, end, type, count, el); 1.1026 + 1.1027 + if(!count) { 1.1028 + return; 1.1029 + } 1.1030 + 1.1031 + if (type != 'l') { 1.1032 + ParseError("expected long array (binary)",&el); 1.1033 + } 1.1034 + 1.1035 + std::vector<char> buff; 1.1036 + ReadBinaryDataArray(type, count, data, end, buff, el); 1.1037 + 1.1038 + ai_assert(data == end); 1.1039 + ai_assert(buff.size() == count * 8); 1.1040 + 1.1041 + out.reserve(count); 1.1042 + 1.1043 + const uint64_t* ip = reinterpret_cast<const uint64_t*>(&buff[0]); 1.1044 + for (unsigned int i = 0; i < count; ++i, ++ip) { 1.1045 + BE_NCONST uint64_t val = *ip; 1.1046 + AI_SWAP8(val); 1.1047 + out.push_back(val); 1.1048 + } 1.1049 + 1.1050 + return; 1.1051 + } 1.1052 + 1.1053 + const size_t dim = ParseTokenAsDim(*tok[0]); 1.1054 + 1.1055 + // see notes in ParseVectorDataArray() 1.1056 + out.reserve(dim); 1.1057 + 1.1058 + const Scope& scope = GetRequiredScope(el); 1.1059 + const Element& a = GetRequiredElement(scope,"a",&el); 1.1060 + 1.1061 + for (TokenList::const_iterator it = a.Tokens().begin(), end = a.Tokens().end(); it != end; ) { 1.1062 + const uint64_t ival = ParseTokenAsID(**it++); 1.1063 + 1.1064 + out.push_back(ival); 1.1065 + } 1.1066 +} 1.1067 + 1.1068 + 1.1069 +// ------------------------------------------------------------------------------------------------ 1.1070 +aiMatrix4x4 ReadMatrix(const Element& element) 1.1071 +{ 1.1072 + std::vector<float> values; 1.1073 + ParseVectorDataArray(values,element); 1.1074 + 1.1075 + if(values.size() != 16) { 1.1076 + ParseError("expected 16 matrix elements"); 1.1077 + } 1.1078 + 1.1079 + aiMatrix4x4 result; 1.1080 + 1.1081 + 1.1082 + result.a1 = values[0]; 1.1083 + result.a2 = values[1]; 1.1084 + result.a3 = values[2]; 1.1085 + result.a4 = values[3]; 1.1086 + 1.1087 + result.b1 = values[4]; 1.1088 + result.b2 = values[5]; 1.1089 + result.b3 = values[6]; 1.1090 + result.b4 = values[7]; 1.1091 + 1.1092 + result.c1 = values[8]; 1.1093 + result.c2 = values[9]; 1.1094 + result.c3 = values[10]; 1.1095 + result.c4 = values[11]; 1.1096 + 1.1097 + result.d1 = values[12]; 1.1098 + result.d2 = values[13]; 1.1099 + result.d3 = values[14]; 1.1100 + result.d4 = values[15]; 1.1101 + 1.1102 + result.Transpose(); 1.1103 + return result; 1.1104 +} 1.1105 + 1.1106 + 1.1107 +// ------------------------------------------------------------------------------------------------ 1.1108 +// wrapper around ParseTokenAsString() with ParseError handling 1.1109 +std::string ParseTokenAsString(const Token& t) 1.1110 +{ 1.1111 + const char* err; 1.1112 + const std::string& i = ParseTokenAsString(t,err); 1.1113 + if(err) { 1.1114 + ParseError(err,t); 1.1115 + } 1.1116 + return i; 1.1117 +} 1.1118 + 1.1119 + 1.1120 +// ------------------------------------------------------------------------------------------------ 1.1121 +// extract a required element from a scope, abort if the element cannot be found 1.1122 +const Element& GetRequiredElement(const Scope& sc, const std::string& index, const Element* element /*= NULL*/) 1.1123 +{ 1.1124 + const Element* el = sc[index]; 1.1125 + if(!el) { 1.1126 + ParseError("did not find required element \"" + index + "\"",element); 1.1127 + } 1.1128 + return *el; 1.1129 +} 1.1130 + 1.1131 + 1.1132 +// ------------------------------------------------------------------------------------------------ 1.1133 +// extract required compound scope 1.1134 +const Scope& GetRequiredScope(const Element& el) 1.1135 +{ 1.1136 + const Scope* const s = el.Compound(); 1.1137 + if(!s) { 1.1138 + ParseError("expected compound scope",&el); 1.1139 + } 1.1140 + 1.1141 + return *s; 1.1142 +} 1.1143 + 1.1144 + 1.1145 +// ------------------------------------------------------------------------------------------------ 1.1146 +// get token at a particular index 1.1147 +const Token& GetRequiredToken(const Element& el, unsigned int index) 1.1148 +{ 1.1149 + const TokenList& t = el.Tokens(); 1.1150 + if(index >= t.size()) { 1.1151 + ParseError(Formatter::format( "missing token at index " ) << index,&el); 1.1152 + } 1.1153 + 1.1154 + return *t[index]; 1.1155 +} 1.1156 + 1.1157 + 1.1158 +// ------------------------------------------------------------------------------------------------ 1.1159 +// wrapper around ParseTokenAsID() with ParseError handling 1.1160 +uint64_t ParseTokenAsID(const Token& t) 1.1161 +{ 1.1162 + const char* err; 1.1163 + const uint64_t i = ParseTokenAsID(t,err); 1.1164 + if(err) { 1.1165 + ParseError(err,t); 1.1166 + } 1.1167 + return i; 1.1168 +} 1.1169 + 1.1170 + 1.1171 +// ------------------------------------------------------------------------------------------------ 1.1172 +// wrapper around ParseTokenAsDim() with ParseError handling 1.1173 +size_t ParseTokenAsDim(const Token& t) 1.1174 +{ 1.1175 + const char* err; 1.1176 + const size_t i = ParseTokenAsDim(t,err); 1.1177 + if(err) { 1.1178 + ParseError(err,t); 1.1179 + } 1.1180 + return i; 1.1181 +} 1.1182 + 1.1183 + 1.1184 +// ------------------------------------------------------------------------------------------------ 1.1185 +// wrapper around ParseTokenAsFloat() with ParseError handling 1.1186 +float ParseTokenAsFloat(const Token& t) 1.1187 +{ 1.1188 + const char* err; 1.1189 + const float i = ParseTokenAsFloat(t,err); 1.1190 + if(err) { 1.1191 + ParseError(err,t); 1.1192 + } 1.1193 + return i; 1.1194 +} 1.1195 + 1.1196 + 1.1197 +// ------------------------------------------------------------------------------------------------ 1.1198 +// wrapper around ParseTokenAsInt() with ParseError handling 1.1199 +int ParseTokenAsInt(const Token& t) 1.1200 +{ 1.1201 + const char* err; 1.1202 + const int i = ParseTokenAsInt(t,err); 1.1203 + if(err) { 1.1204 + ParseError(err,t); 1.1205 + } 1.1206 + return i; 1.1207 +} 1.1208 + 1.1209 + 1.1210 + 1.1211 +} // !FBX 1.1212 +} // !Assimp 1.1213 + 1.1214 +#endif 1.1215 +