2014-09-04 17:41:14 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2014, assimp team
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "AssimpPCH.h"
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OPEMGEX_IMPORTER
|
|
|
|
|
|
|
|
#include "OpenGEXParser.h"
|
2014-09-12 10:00:03 +00:00
|
|
|
#include "OpenGEXStructs.h"
|
|
|
|
#include "ParsingUtils.h"
|
|
|
|
#include "fast_atof.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2014-09-04 17:41:14 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
namespace OpenGEX {
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
static const std::string Metric = "Metric";
|
|
|
|
static const std::string GeometryNode = "GeometryNode";
|
|
|
|
static const std::string GeometryObject = "GeometryObject";
|
|
|
|
static const std::string Material = "Material";
|
2014-10-28 20:45:09 +00:00
|
|
|
static const size_t NumObjects = 4;
|
2014-09-12 10:00:03 +00:00
|
|
|
static const std::string RootNodes[ NumObjects ] = {
|
|
|
|
Metric,
|
|
|
|
GeometryNode,
|
|
|
|
GeometryObject,
|
|
|
|
Material
|
|
|
|
};
|
|
|
|
|
2014-10-28 20:45:09 +00:00
|
|
|
static const size_t NumSeparator = 4;
|
|
|
|
static const std::string Separator[ NumSeparator ] = {
|
|
|
|
"(", ")", "{", "}"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const size_t NumToken = 8;
|
|
|
|
static const std::string Token[ NumToken ] = {
|
|
|
|
RootNodes[ 0 ],
|
|
|
|
RootNodes[ 1 ],
|
|
|
|
RootNodes[ 2 ],
|
|
|
|
RootNodes[ 3 ],
|
|
|
|
Separator[ 0 ],
|
|
|
|
Separator[ 1 ],
|
|
|
|
Separator[ 2 ],
|
|
|
|
Separator[ 3 ]
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool isSeparator( char in ) {
|
|
|
|
return ( in == '(' || in == ')' || in == '{' || in == '}' );
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool containsNode( const char *bufferPtr, size_t size, const std::string *nodes,
|
|
|
|
size_t numNodes, std::string &tokenFound ) {
|
|
|
|
tokenFound = "";
|
2014-09-12 10:00:03 +00:00
|
|
|
if( 0 == numNodes ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found( false );
|
|
|
|
for( size_t i = 0; i < numNodes; ++i ) {
|
|
|
|
if( TokenMatch( bufferPtr, nodes[ i ].c_str(), nodes[ i ].size() ) ) {
|
2014-10-28 20:45:09 +00:00
|
|
|
tokenFound = nodes[ i ];
|
2014-09-12 10:00:03 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2014-10-28 20:45:09 +00:00
|
|
|
static OpenGEXParser::TokenType getTokenTypeByName( const char *in ) {
|
|
|
|
ai_assert( NULL != in );
|
|
|
|
|
|
|
|
OpenGEXParser::TokenType type( OpenGEXParser::None );
|
|
|
|
for( size_t i = 0; i < NumToken; ++i ) {
|
|
|
|
if( TokenMatch( in, Token[ i ].c_str(), Token[ i ].size() ) ) {
|
|
|
|
type = static_cast<OpenGEXParser::TokenType>( i+1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2014-11-02 18:01:04 +00:00
|
|
|
static void removeQuotes( std::string &attribName ) {
|
|
|
|
std::string tmp;
|
|
|
|
for( unsigned int i = 0; i < attribName.size(); ++i ) {
|
|
|
|
if( attribName[ i ] != '\"' ) {
|
|
|
|
tmp += attribName[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
attribName = tmp;
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
OpenGEXParser::OpenGEXParser( const std::vector<char> &buffer )
|
|
|
|
: m_buffer( buffer )
|
|
|
|
, m_index( 0 )
|
2014-10-28 20:45:09 +00:00
|
|
|
, m_buffersize( buffer.size() )
|
|
|
|
, m_nodeTypeStack() {
|
|
|
|
// empty
|
2014-09-04 17:41:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
2014-09-04 17:41:14 +00:00
|
|
|
OpenGEXParser::~OpenGEXParser() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
void OpenGEXParser::parse() {
|
|
|
|
while( parseNextNode() ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
std::string OpenGEXParser::getNextToken() {
|
|
|
|
std::string token;
|
|
|
|
while( m_index < m_buffersize && IsSpace( m_buffer[ m_index ] ) ) {
|
2014-10-28 20:45:09 +00:00
|
|
|
m_index++;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 20:45:09 +00:00
|
|
|
while( m_index < m_buffersize && !IsSpace( m_buffer[ m_index ] ) && !isSeparator( m_buffer[ m_index ] ) ) {
|
2014-09-12 10:00:03 +00:00
|
|
|
token += m_buffer[ m_index ];
|
|
|
|
m_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( token == "//" ) {
|
|
|
|
skipComments();
|
|
|
|
token = getNextToken();
|
|
|
|
}
|
|
|
|
|
2014-10-28 20:45:09 +00:00
|
|
|
if( token.empty() ) {
|
|
|
|
if( isSeparator( m_buffer[ m_index ] ) ) {
|
|
|
|
token += m_buffer[ m_index ];
|
|
|
|
m_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::skipComments() {
|
|
|
|
bool skipped( false );
|
|
|
|
if( strncmp( &m_buffer[ m_index ], "//", 2 ) == 0) {
|
|
|
|
while( !IsLineEnd( m_buffer[ m_index ] ) ) {
|
|
|
|
++m_index;
|
|
|
|
}
|
|
|
|
skipped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return skipped;
|
|
|
|
}
|
|
|
|
|
2014-11-02 19:30:45 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
void OpenGEXParser::readUntilEndOfLine() {
|
|
|
|
while( !IsLineEnd( m_buffer[ m_index ] ) ) {
|
|
|
|
++m_index;
|
|
|
|
}
|
|
|
|
++m_index;
|
|
|
|
if( IsLineEnd( m_buffer[ m_index ] ) ) {
|
|
|
|
++m_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::parseNextNode() {
|
|
|
|
std::string token( getNextToken() );
|
2014-11-02 18:01:04 +00:00
|
|
|
std::string rootNodeName, nodeType;
|
2014-10-28 20:45:09 +00:00
|
|
|
if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, rootNodeName ) ) {
|
|
|
|
m_nodeTypeStack.push_back( getTokenTypeByName( rootNodeName.c_str() ) );
|
2014-11-02 18:01:04 +00:00
|
|
|
if( !getNodeHeader( nodeType ) ) {
|
2014-09-12 10:00:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-02 18:01:04 +00:00
|
|
|
if( !getNodeData( nodeType ) ) {
|
2014-09-12 10:00:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-02 19:30:45 +00:00
|
|
|
|
|
|
|
readUntilEndOfLine();
|
2014-10-28 20:45:09 +00:00
|
|
|
|
|
|
|
m_nodeTypeStack.pop_back();
|
2014-11-02 19:30:45 +00:00
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
2014-11-02 18:01:04 +00:00
|
|
|
bool OpenGEXParser::getNodeHeader( std::string &name ) {
|
2014-10-28 20:45:09 +00:00
|
|
|
bool success( false );
|
2014-11-29 10:33:49 +00:00
|
|
|
TokenType tokenType( m_nodeTypeStack.back() );
|
|
|
|
if( tokenType == MetricNode ) {
|
2014-11-02 18:01:04 +00:00
|
|
|
if( getMetricAttributeKey( name ) ) {
|
2014-10-28 20:45:09 +00:00
|
|
|
success = true;
|
|
|
|
}
|
2014-11-29 10:33:49 +00:00
|
|
|
} else if( tokenType == GeometryNode ) {
|
|
|
|
|
|
|
|
} else if( tokenType == GeometryObject ) {
|
|
|
|
|
|
|
|
} else if( tokenType == Material ) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DefaultLogger::get()->warn( "Unknown token type in file." );
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 20:45:09 +00:00
|
|
|
return success;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::getBracketOpen() {
|
|
|
|
const std::string token( getNextToken() );
|
|
|
|
if( "{" == token ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::getBracketClose() {
|
|
|
|
const std::string token( getNextToken() );
|
|
|
|
if( "}" == token ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::getStringData( std::string &data ) {
|
|
|
|
if( !getBracketOpen() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !getBracketClose() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::getFloatData( size_t num, float *data ) {
|
2014-09-12 16:12:18 +00:00
|
|
|
ai_assert( NULL != data );
|
2014-09-12 10:00:03 +00:00
|
|
|
|
2014-11-02 18:01:04 +00:00
|
|
|
std::string tk;
|
|
|
|
tk = getNextToken();
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
if( !getBracketOpen() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok( true );
|
|
|
|
size_t dataIdx( 0 );
|
|
|
|
for( unsigned int i = 0; i < num; ++i ) {
|
|
|
|
data[ dataIdx ] = fast_atof( &m_buffer[ m_index ] );
|
|
|
|
++dataIdx;
|
2014-11-02 18:01:04 +00:00
|
|
|
tk = getNextToken();
|
2014-09-12 10:00:03 +00:00
|
|
|
if( tk == "," ) {
|
|
|
|
if( i >= ( num - 1 ) ) {
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !getBracketClose() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
2014-11-02 18:01:04 +00:00
|
|
|
bool OpenGEXParser::getNodeData( const std::string &nodeType ) {
|
|
|
|
bool success( false );
|
|
|
|
|
|
|
|
if( !getBracketOpen() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-29 10:33:49 +00:00
|
|
|
const TokenType type( m_nodeTypeStack.back() );
|
2014-11-02 18:01:04 +00:00
|
|
|
if( type == MetricNode ) {
|
|
|
|
success = onMetricNode( nodeType );
|
2014-11-29 10:33:49 +00:00
|
|
|
} else if( type == GeometryNode ) {
|
|
|
|
success = onGeometryNode();
|
|
|
|
} else if( type == GeometryObject ) {
|
|
|
|
success = onGeometryObject();
|
|
|
|
} else if( type == Material ) {
|
|
|
|
success = onMaterial();
|
|
|
|
} else {
|
|
|
|
DefaultLogger::get()->warn( "Unknown token type in file." );
|
2014-11-02 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !getBracketClose() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
2014-11-02 18:01:04 +00:00
|
|
|
bool OpenGEXParser::getMetricAttributeKey( std::string &attribName ) {
|
2014-10-28 20:45:09 +00:00
|
|
|
bool ok( false );
|
|
|
|
attribName = "";
|
|
|
|
std::string token( getNextToken() );
|
|
|
|
if( token[ 0 ] == '(' ) {
|
|
|
|
// get attribute
|
|
|
|
token = getNextToken();
|
2014-11-02 18:01:04 +00:00
|
|
|
if( "key" == token ) {
|
|
|
|
std::string equal = getNextToken();
|
|
|
|
attribName = getNextToken();
|
|
|
|
token = getNextToken();
|
|
|
|
if( token[ 0 ] == ')' ) {
|
|
|
|
ok = true;
|
|
|
|
removeQuotes( attribName );
|
|
|
|
}
|
2014-10-28 20:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
2014-10-28 20:45:09 +00:00
|
|
|
bool OpenGEXParser::onMetricNode( const std::string &attribName ) {
|
2014-11-02 19:30:45 +00:00
|
|
|
float value( 0.0f );
|
2014-11-02 18:01:04 +00:00
|
|
|
bool success( true );
|
2014-09-12 10:00:03 +00:00
|
|
|
if( "distance" == attribName ) {
|
2014-11-02 19:30:45 +00:00
|
|
|
if( getFloatData( 1, &value ) ) {
|
|
|
|
m_model.m_metrics.m_distance = value;
|
2014-11-02 18:01:04 +00:00
|
|
|
}
|
2014-09-12 10:00:03 +00:00
|
|
|
} else if( "angle" == attribName ) {
|
2014-11-02 19:30:45 +00:00
|
|
|
if( getFloatData( 1, &value ) ) {
|
|
|
|
m_model.m_metrics.m_angle = value;
|
2014-11-02 18:01:04 +00:00
|
|
|
}
|
2014-09-12 10:00:03 +00:00
|
|
|
} else if( "time" == attribName ) {
|
2014-11-02 19:30:45 +00:00
|
|
|
if( getFloatData( 1, &value ) ) {
|
|
|
|
m_model.m_metrics.m_time = value;
|
2014-11-02 18:01:04 +00:00
|
|
|
}
|
2014-09-12 10:00:03 +00:00
|
|
|
} else if( "up" == attribName ) {
|
|
|
|
std::string up;
|
2014-11-02 18:01:04 +00:00
|
|
|
if( getStringData( up ) ) {
|
|
|
|
m_model.m_metrics.m_up = up;
|
|
|
|
}
|
2014-09-12 10:00:03 +00:00
|
|
|
} else {
|
2014-11-02 18:01:04 +00:00
|
|
|
success = false;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-02 18:01:04 +00:00
|
|
|
return success;
|
2014-09-12 10:00:03 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:33:49 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::onGeometryNode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::onGeometryObject() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool OpenGEXParser::onMaterial() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:00:03 +00:00
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
|
2014-11-02 18:01:04 +00:00
|
|
|
} // Namespace OpenGEX
|
2014-09-04 17:41:14 +00:00
|
|
|
} // Namespace Assimp
|
|
|
|
|
2014-09-06 07:19:28 +00:00
|
|
|
#endif ASSIMP_BUILD_NO_OPEMGEX_IMPORTER
|