update: add parsing of metric nodes.

Signed-off-by: Kim Kulling <kim.kulling@googlemail.com>
pull/502/head
Kim Kulling 2014-11-02 19:01:04 +01:00
parent 6843c42da1
commit 1a5695ff48
2 changed files with 84 additions and 26 deletions

View File

@ -118,6 +118,16 @@ static OpenGEXParser::TokenType getTokenTypeByName( const char *in ) {
return type; 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<char> &buffer ) OpenGEXParser::OpenGEXParser( const std::vector<char> &buffer )
: m_buffer( buffer ) : m_buffer( buffer )
@ -182,14 +192,14 @@ bool OpenGEXParser::skipComments() {
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::parseNextNode() { bool OpenGEXParser::parseNextNode() {
std::string token( getNextToken() ); std::string token( getNextToken() );
std::string rootNodeName; std::string rootNodeName, nodeType;
if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, rootNodeName ) ) { if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, rootNodeName ) ) {
m_nodeTypeStack.push_back( getTokenTypeByName( rootNodeName.c_str() ) ); m_nodeTypeStack.push_back( getTokenTypeByName( rootNodeName.c_str() ) );
if( !getNodeHeader( rootNodeName ) ) { if( !getNodeHeader( nodeType ) ) {
return false; return false;
} }
if( !getNodeData() ) { if( !getNodeData( nodeType ) ) {
return false; 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 ); bool success( false );
if( m_nodeTypeStack.back() == MetricNode ) { if( m_nodeTypeStack.back() == MetricNode ) {
std::string attribName, value; if( getMetricAttributeKey( name ) ) {
if( getMetricAttributeKey( attribName, value ) ) {
success = true; success = true;
} }
} }
@ -248,6 +257,9 @@ bool OpenGEXParser::getStringData( std::string &data ) {
bool OpenGEXParser::getFloatData( size_t num, float *data ) { bool OpenGEXParser::getFloatData( size_t num, float *data ) {
ai_assert( NULL != data ); ai_assert( NULL != data );
std::string tk;
tk = getNextToken();
if( !getBracketOpen() ) { if( !getBracketOpen() ) {
return false; return false;
} }
@ -257,7 +269,7 @@ bool OpenGEXParser::getFloatData( size_t num, float *data ) {
for( unsigned int i = 0; i < num; ++i ) { for( unsigned int i = 0; i < num; ++i ) {
data[ dataIdx ] = fast_atof( &m_buffer[ m_index ] ); data[ dataIdx ] = fast_atof( &m_buffer[ m_index ] );
++dataIdx; ++dataIdx;
std::string tk = getNextToken(); tk = getNextToken();
if( tk == "," ) { if( tk == "," ) {
if( i >= ( num - 1 ) ) { if( i >= ( num - 1 ) ) {
ok = false; ok = false;
@ -274,24 +286,41 @@ bool OpenGEXParser::getFloatData( size_t num, float *data ) {
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool OpenGEXParser::getNodeData() { bool OpenGEXParser::getNodeData( const std::string &nodeType ) {
return true; 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 ); bool ok( false );
attribName = ""; attribName = "";
std::string token( getNextToken() ); std::string token( getNextToken() );
if( token[ 0 ] == '(' ) { if( token[ 0 ] == '(' ) {
// get attribute // get attribute
attribName = getNextToken();
std::string equal = getNextToken();
value = getNextToken();
token = getNextToken(); token = getNextToken();
if( token[ 0 ] == ')' ) { if( "key" == token ) {
ok = true; 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 OpenGEXParser::onMetricNode( const std::string &attribName ) {
bool success( true );
if( "distance" == attribName ) { if( "distance" == attribName ) {
float distance( 0.0f ); float distance( 0.0f );
getFloatData( 1, &distance ); if( getFloatData( 1, &distance ) ) {
m_model.m_metrics.m_distance = distance;
}
} else if( "angle" == attribName ) { } else if( "angle" == attribName ) {
float angle( 0.0f ); float angle( 0.0f );
getFloatData( 1, &angle ); if( getFloatData( 1, &angle ) ) {
m_model.m_metrics.m_angle = angle;
}
} else if( "time" == attribName ) { } else if( "time" == attribName ) {
float time( 0.0f ); float time( 0.0f );
getFloatData( 1, &time ); if( getFloatData( 1, &time ) ) {
m_model.m_metrics.m_time = time;
}
} else if( "up" == attribName ) { } else if( "up" == attribName ) {
std::string up; std::string up;
getStringData( up ); if( getStringData( up ) ) {
m_model.m_metrics.m_up = up;
}
} else { } else {
return false; success = false;
} }
return true; return success;
} }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
} // Namespace openGEX } // Namespace OpenGEX
} // Namespace Assimp } // Namespace Assimp
#endif ASSIMP_BUILD_NO_OPEMGEX_IMPORTER #endif ASSIMP_BUILD_NO_OPEMGEX_IMPORTER

View File

@ -41,11 +41,30 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define ASSIMP_OPENGEX_OPENGEXPARSER_H_INC #define ASSIMP_OPENGEX_OPENGEXPARSER_H_INC
#ifndef ASSIMP_BUILD_NO_OPEMGEX_IMPORTER #ifndef ASSIMP_BUILD_NO_OPEMGEX_IMPORTER
#include <vector> #include <vector>
#include <map>
namespace Assimp { namespace Assimp {
namespace OpenGEX { 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 { class OpenGEXParser {
public: public:
enum TokenType { enum TokenType {
@ -69,13 +88,13 @@ protected:
std::string getNextToken(); std::string getNextToken();
bool skipComments(); bool skipComments();
bool parseNextNode(); bool parseNextNode();
bool getNodeHeader( const std::string &name ); bool getNodeHeader( std::string &name );
bool getBracketOpen(); bool getBracketOpen();
bool getBracketClose(); bool getBracketClose();
bool getStringData( std::string &data ); bool getStringData( std::string &data );
bool getFloatData( size_t num, float *data ); bool getFloatData( size_t num, float *data );
bool getNodeData(); bool getNodeData( const std::string &nodeType );
bool getMetricAttributeKey( std::string &attribName, std::string &value ); bool getMetricAttributeKey( std::string &attribName );
bool onMetricNode( const std::string &attribName ); bool onMetricNode( const std::string &attribName );
private: private:
@ -85,6 +104,7 @@ private:
private: private:
const std::vector<char> &m_buffer; const std::vector<char> &m_buffer;
std::vector<TokenType> m_nodeTypeStack; std::vector<TokenType> m_nodeTypeStack;
OpenGEXModel m_model;
size_t m_index; size_t m_index;
size_t m_buffersize; size_t m_buffersize;
}; };