2
0

OpenGEXParser.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2014, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_OPEMGEX_IMPORTER
  35. #include "OpenGEXParser.h"
  36. #include "OpenGEXStructs.h"
  37. #include "ParsingUtils.h"
  38. #include "fast_atof.h"
  39. #include <vector>
  40. namespace Assimp {
  41. namespace OpenGEX {
  42. //------------------------------------------------------------------------------------------------
  43. static const std::string Metric = "Metric";
  44. static const std::string GeometryNode = "GeometryNode";
  45. static const std::string GeometryObject = "GeometryObject";
  46. static const std::string Material = "Material";
  47. static const size_t NumObjects = 4;
  48. static const std::string RootNodes[ NumObjects ] = {
  49. Metric,
  50. GeometryNode,
  51. GeometryObject,
  52. Material
  53. };
  54. static bool containsNode( const char *bufferPtr, size_t size, const std::string *nodes, size_t numNodes,
  55. const std::string *tokenFound ) {
  56. tokenFound = NULL;
  57. if( 0 == numNodes ) {
  58. return false;
  59. }
  60. bool found( false );
  61. for( size_t i = 0; i < numNodes; ++i ) {
  62. if( TokenMatch( bufferPtr, nodes[ i ].c_str(), nodes[ i ].size() ) ) {
  63. tokenFound = &nodes[ i ];
  64. found = true;
  65. break;
  66. }
  67. }
  68. return found;
  69. }
  70. //------------------------------------------------------------------------------------------------
  71. OpenGEXParser::OpenGEXParser( const std::vector<char> &buffer )
  72. : m_buffer( buffer )
  73. , m_index( 0 )
  74. , m_buffersize( buffer.size() ) {
  75. }
  76. //------------------------------------------------------------------------------------------------
  77. OpenGEXParser::~OpenGEXParser() {
  78. }
  79. //------------------------------------------------------------------------------------------------
  80. void OpenGEXParser::parse() {
  81. while( parseNextNode() ) {
  82. }
  83. }
  84. //------------------------------------------------------------------------------------------------
  85. std::string OpenGEXParser::getNextToken() {
  86. std::string token;
  87. while( m_index < m_buffersize && IsSpace( m_buffer[ m_index ] ) ) {
  88. ++m_index;
  89. }
  90. while( m_index < m_buffersize && !IsSpace( m_buffer[ m_index ] ) ) {
  91. token += m_buffer[ m_index ];
  92. m_index++;
  93. }
  94. if( token == "//" ) {
  95. skipComments();
  96. token = getNextToken();
  97. }
  98. return token;
  99. }
  100. //------------------------------------------------------------------------------------------------
  101. bool OpenGEXParser::skipComments() {
  102. bool skipped( false );
  103. if( strncmp( &m_buffer[ m_index ], "//", 2 ) == 0) {
  104. while( !IsLineEnd( m_buffer[ m_index ] ) ) {
  105. ++m_index;
  106. }
  107. skipped = true;
  108. }
  109. return skipped;
  110. }
  111. //------------------------------------------------------------------------------------------------
  112. bool OpenGEXParser::parseNextNode() {
  113. std::string token( getNextToken() );
  114. std::string rootNodeName;
  115. if( containsNode( token.c_str(), token.size(), RootNodes, NumObjects, &rootNodeName ) ) {
  116. if( !getNodeHeader( rootNodeName ) ) {
  117. return false;
  118. }
  119. if( !getNodeData() ) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. //------------------------------------------------------------------------------------------------
  126. bool OpenGEXParser::getNodeHeader( const std::string &name ) {
  127. if( name == Metric ) {
  128. std::string token( getNextToken() );
  129. }
  130. return false;
  131. }
  132. //------------------------------------------------------------------------------------------------
  133. bool OpenGEXParser::getBracketOpen() {
  134. const std::string token( getNextToken() );
  135. if( "{" == token ) {
  136. return true;
  137. }
  138. return false;
  139. }
  140. //------------------------------------------------------------------------------------------------
  141. bool OpenGEXParser::getBracketClose() {
  142. const std::string token( getNextToken() );
  143. if( "}" == token ) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. //------------------------------------------------------------------------------------------------
  149. bool OpenGEXParser::getStringData( std::string &data ) {
  150. if( !getBracketOpen() ) {
  151. return false;
  152. }
  153. if( !getBracketClose() ) {
  154. return false;
  155. }
  156. return false;
  157. }
  158. //------------------------------------------------------------------------------------------------
  159. bool OpenGEXParser::getFloatData( size_t num, float *data ) {
  160. ai_assert( nullptr != data );
  161. if( !getBracketOpen() ) {
  162. return false;
  163. }
  164. bool ok( true );
  165. size_t dataIdx( 0 );
  166. for( unsigned int i = 0; i < num; ++i ) {
  167. data[ dataIdx ] = fast_atof( &m_buffer[ m_index ] );
  168. ++dataIdx;
  169. std::string tk = getNextToken();
  170. if( tk == "," ) {
  171. if( i >= ( num - 1 ) ) {
  172. ok = false;
  173. break;
  174. }
  175. }
  176. }
  177. if( !getBracketClose() ) {
  178. return false;
  179. }
  180. return ok;
  181. }
  182. //------------------------------------------------------------------------------------------------
  183. bool OpenGEXParser::getNodeData() {
  184. if( !getBracketOpen() ) {
  185. return false;
  186. }
  187. if( !onMetricNode() ) {
  188. return false;
  189. }
  190. if( !getBracketClose() ) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. //------------------------------------------------------------------------------------------------
  196. bool OpenGEXParser::getMetricAttribute( std::string &attribName ) {
  197. return false;
  198. }
  199. //------------------------------------------------------------------------------------------------
  200. bool OpenGEXParser::onMetricNode() {
  201. std::string attribName;
  202. if( !getMetricAttribute( attribName ) ) {
  203. return false;
  204. }
  205. if( "distance" == attribName ) {
  206. float distance( 0.0f );
  207. getFloatData( 1, &distance );
  208. } else if( "angle" == attribName ) {
  209. float angle( 0.0f );
  210. getFloatData( 1, &angle );
  211. } else if( "time" == attribName ) {
  212. float time( 0.0f );
  213. getFloatData( 1, &time );
  214. } else if( "up" == attribName ) {
  215. std::string up;
  216. getStringData( up );
  217. } else {
  218. return false;
  219. }
  220. return true;
  221. }
  222. //------------------------------------------------------------------------------------------------
  223. } // Namespace openGEX
  224. } // Namespace Assimp
  225. #endif ASSIMP_BUILD_NO_OPEMGEX_IMPORTER