From 1a5695ff485ec28f50db62f6376bab1fdf5d584e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 2 Nov 2014 19:01:04 +0100 Subject: [PATCH] update: add parsing of metric nodes. Signed-off-by: Kim Kulling --- code/OpenGEXParser.cpp | 84 ++++++++++++++++++++++++++++++------------ code/OpenGEXParser.h | 26 +++++++++++-- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/code/OpenGEXParser.cpp b/code/OpenGEXParser.cpp index efb1ff7b0..0acf209a6 100644 --- a/code/OpenGEXParser.cpp +++ b/code/OpenGEXParser.cpp @@ -118,6 +118,16 @@ static OpenGEXParser::TokenType getTokenTypeByName( const char *in ) { return type; } +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; +} + //------------------------------------------------------------------------------------------------ OpenGEXParser::OpenGEXParser( const std::vector &buffer ) : m_buffer( buffer ) @@ -182,14 +192,14 @@ bool OpenGEXParser::skipComments() { //------------------------------------------------------------------------------------------------ bool OpenGEXParser::parseNextNode() { std::string token( getNextToken() ); - std::string rootNodeName; + std::string rootNodeName, nodeType; if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, rootNodeName ) ) { m_nodeTypeStack.push_back( getTokenTypeByName( rootNodeName.c_str() ) ); - if( !getNodeHeader( rootNodeName ) ) { + if( !getNodeHeader( nodeType ) ) { return false; } - if( !getNodeData() ) { + if( !getNodeData( nodeType ) ) { return false; } @@ -200,11 +210,10 @@ bool OpenGEXParser::parseNextNode() { } //------------------------------------------------------------------------------------------------ -bool OpenGEXParser::getNodeHeader( const std::string &name ) { +bool OpenGEXParser::getNodeHeader( std::string &name ) { bool success( false ); if( m_nodeTypeStack.back() == MetricNode ) { - std::string attribName, value; - if( getMetricAttributeKey( attribName, value ) ) { + if( getMetricAttributeKey( name ) ) { success = true; } } @@ -248,6 +257,9 @@ bool OpenGEXParser::getStringData( std::string &data ) { bool OpenGEXParser::getFloatData( size_t num, float *data ) { ai_assert( NULL != data ); + std::string tk; + tk = getNextToken(); + if( !getBracketOpen() ) { return false; } @@ -257,7 +269,7 @@ bool OpenGEXParser::getFloatData( size_t num, float *data ) { for( unsigned int i = 0; i < num; ++i ) { data[ dataIdx ] = fast_atof( &m_buffer[ m_index ] ); ++dataIdx; - std::string tk = getNextToken(); + tk = getNextToken(); if( tk == "," ) { if( i >= ( num - 1 ) ) { ok = false; @@ -274,24 +286,41 @@ bool OpenGEXParser::getFloatData( size_t num, float *data ) { } //------------------------------------------------------------------------------------------------ -bool OpenGEXParser::getNodeData() { - return true; +bool OpenGEXParser::getNodeData( const std::string &nodeType ) { + bool success( false ); + + if( !getBracketOpen() ) { + return false; + } + + TokenType type( m_nodeTypeStack.back() ); + if( type == MetricNode ) { + success = onMetricNode( nodeType ); + } + + if( !getBracketClose() ) { + return false; + } + + return success; } //------------------------------------------------------------------------------------------------ -bool OpenGEXParser::getMetricAttributeKey( std::string &attribName, std::string &value ) { +bool OpenGEXParser::getMetricAttributeKey( std::string &attribName ) { bool ok( false ); attribName = ""; std::string token( getNextToken() ); if( token[ 0 ] == '(' ) { // get attribute - attribName = getNextToken(); - std::string equal = getNextToken(); - value = getNextToken(); - token = getNextToken(); - if( token[ 0 ] == ')' ) { - ok = true; + if( "key" == token ) { + std::string equal = getNextToken(); + attribName = getNextToken(); + token = getNextToken(); + if( token[ 0 ] == ')' ) { + ok = true; + removeQuotes( attribName ); + } } } @@ -300,28 +329,37 @@ bool OpenGEXParser::getMetricAttributeKey( std::string &attribName, std::string //------------------------------------------------------------------------------------------------ bool OpenGEXParser::onMetricNode( const std::string &attribName ) { + bool success( true ); if( "distance" == attribName ) { float distance( 0.0f ); - getFloatData( 1, &distance ); + if( getFloatData( 1, &distance ) ) { + m_model.m_metrics.m_distance = distance; + } } else if( "angle" == attribName ) { float angle( 0.0f ); - getFloatData( 1, &angle ); + if( getFloatData( 1, &angle ) ) { + m_model.m_metrics.m_angle = angle; + } } else if( "time" == attribName ) { float time( 0.0f ); - getFloatData( 1, &time ); + if( getFloatData( 1, &time ) ) { + m_model.m_metrics.m_time = time; + } } else if( "up" == attribName ) { std::string up; - getStringData( up ); + if( getStringData( up ) ) { + m_model.m_metrics.m_up = up; + } } else { - return false; + success = false; } - return true; + return success; } //------------------------------------------------------------------------------------------------ -} // Namespace openGEX +} // Namespace OpenGEX } // Namespace Assimp #endif ASSIMP_BUILD_NO_OPEMGEX_IMPORTER diff --git a/code/OpenGEXParser.h b/code/OpenGEXParser.h index 1733366f7..f6ef2df14 100644 --- a/code/OpenGEXParser.h +++ b/code/OpenGEXParser.h @@ -41,11 +41,30 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define ASSIMP_OPENGEX_OPENGEXPARSER_H_INC #ifndef ASSIMP_BUILD_NO_OPEMGEX_IMPORTER + #include +#include namespace Assimp { namespace OpenGEX { +struct OpenGEXModel { + struct Metrics { + float m_distance; + float m_angle; + float m_time; + std::string m_up; + + Metrics() + : m_distance( 0.0f ) + , m_angle( 0.0f ) + , m_time( 0.0f ) + , m_up() { + // empty + } + } m_metrics; +}; + class OpenGEXParser { public: enum TokenType { @@ -69,13 +88,13 @@ protected: std::string getNextToken(); bool skipComments(); bool parseNextNode(); - bool getNodeHeader( const std::string &name ); + bool getNodeHeader( std::string &name ); bool getBracketOpen(); bool getBracketClose(); bool getStringData( std::string &data ); bool getFloatData( size_t num, float *data ); - bool getNodeData(); - bool getMetricAttributeKey( std::string &attribName, std::string &value ); + bool getNodeData( const std::string &nodeType ); + bool getMetricAttributeKey( std::string &attribName ); bool onMetricNode( const std::string &attribName ); private: @@ -85,6 +104,7 @@ private: private: const std::vector &m_buffer; std::vector m_nodeTypeStack; + OpenGEXModel m_model; size_t m_index; size_t m_buffersize; };