ColladaParser.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /** Implementation of the Collada parser helper*/
  2. /*
  3. ---------------------------------------------------------------------------
  4. Open Asset Import Library (ASSIMP)
  5. ---------------------------------------------------------------------------
  6. Copyright (c) 2006-2008, ASSIMP Development Team
  7. All rights reserved.
  8. Redistribution and use of this software in source and binary forms,
  9. with or without modification, are permitted provided that the following
  10. conditions are met:
  11. * Redistributions of source code must retain the above
  12. copyright notice, this list of conditions and the
  13. following disclaimer.
  14. * Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the
  16. following disclaimer in the documentation and/or other
  17. materials provided with the distribution.
  18. * Neither the name of the ASSIMP team, nor the names of its
  19. contributors may be used to endorse or promote products
  20. derived from this software without specific prior
  21. written permission of the ASSIMP Development Team.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. ---------------------------------------------------------------------------
  34. */
  35. #include "AssimpPCH.h"
  36. #include "ColladaParser.h"
  37. #include "fast_atof.h"
  38. #include "ParsingUtils.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. ColladaParser::ColladaParser( const std::string& pFile)
  43. : mFileName( pFile)
  44. {
  45. mRootNode = NULL;
  46. mUnitSize = 1.0f;
  47. mUpDirection = UP_Z;
  48. // generate a XML reader for it
  49. mReader = irr::io::createIrrXMLReader( pFile.c_str());
  50. if( !mReader)
  51. ThrowException( "Unable to open file.");
  52. // start reading
  53. ReadContents();
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Destructor, private as well
  57. ColladaParser::~ColladaParser()
  58. {
  59. delete mReader;
  60. for( NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it)
  61. delete it->second;
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Reads the contents of the file
  65. void ColladaParser::ReadContents()
  66. {
  67. while( mReader->read())
  68. {
  69. // handle the root element "COLLADA"
  70. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  71. {
  72. if( IsElement( "COLLADA"))
  73. {
  74. ReadStructure();
  75. } else
  76. {
  77. DefaultLogger::get()->debug( boost::str( boost::format( "Ignoring global element \"%s\".") % mReader->getNodeName()));
  78. SkipElement();
  79. }
  80. } else
  81. {
  82. // skip everything else silently
  83. }
  84. }
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. // Reads the structure of the file
  88. void ColladaParser::ReadStructure()
  89. {
  90. while( mReader->read())
  91. {
  92. // beginning of elements
  93. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  94. {
  95. if( IsElement( "asset"))
  96. ReadAssetInfo();
  97. else if( IsElement( "library_geometries"))
  98. ReadGeometryLibrary();
  99. else if( IsElement( "library_visual_scenes"))
  100. ReadSceneLibrary();
  101. else if( IsElement( "scene"))
  102. ReadScene();
  103. else
  104. SkipElement();
  105. }
  106. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  107. {
  108. break;
  109. }
  110. }
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Reads asset informations such as coordinate system informations and legal blah
  114. void ColladaParser::ReadAssetInfo()
  115. {
  116. while( mReader->read())
  117. {
  118. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  119. {
  120. if( IsElement( "unit"))
  121. {
  122. // read unit data from the element's attributes
  123. int attrIndex = GetAttribute( "meter");
  124. mUnitSize = mReader->getAttributeValueAsFloat( attrIndex);
  125. // consume the trailing stuff
  126. if( !mReader->isEmptyElement())
  127. SkipElement();
  128. }
  129. else if( IsElement( "up_axis"))
  130. {
  131. // read content, strip whitespace, compare
  132. const char* content = GetTextContent();
  133. if( strncmp( content, "X_UP", 4) == 0)
  134. mUpDirection = UP_X;
  135. else if( strncmp( content, "Y_UP", 4) == 0)
  136. mUpDirection = UP_Y;
  137. else
  138. mUpDirection = UP_Z;
  139. // check element end
  140. TestClosing( "up_axis");
  141. } else
  142. {
  143. SkipElement();
  144. }
  145. }
  146. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  147. {
  148. break;
  149. }
  150. }
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. // Reads the geometry library contents
  154. void ColladaParser::ReadGeometryLibrary()
  155. {
  156. SkipElement();
  157. }
  158. // ------------------------------------------------------------------------------------------------
  159. // Reads the library of node hierarchies and scene parts
  160. void ColladaParser::ReadSceneLibrary()
  161. {
  162. while( mReader->read())
  163. {
  164. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  165. {
  166. // a visual scene - generate root node under its ID and let ReadNode() do the recursive work
  167. if( IsElement( "visual_scene"))
  168. {
  169. // read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then?
  170. int indexID = GetAttribute( "id");
  171. const char* attrID = mReader->getAttributeValue( indexID);
  172. // read name if given.
  173. int indexName = TestAttribute( "name");
  174. const char* attrName = "unnamed";
  175. if( indexName > -1)
  176. attrName = mReader->getAttributeValue( indexName);
  177. // TODO: (thom) support SIDs
  178. assert( TestAttribute( "sid") == -1);
  179. // create a node and store it in the library under its ID
  180. Node* node = new Node;
  181. node->mID = attrID;
  182. node->mName = attrName;
  183. mNodeLibrary[node->mID] = node;
  184. ReadSceneNode( node);
  185. } else
  186. {
  187. // ignore the rest
  188. SkipElement();
  189. }
  190. }
  191. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  192. {
  193. break;
  194. }
  195. }
  196. }
  197. // ------------------------------------------------------------------------------------------------
  198. // Reads a scene node's contents including children and stores it in the given node
  199. void ColladaParser::ReadSceneNode( Node* pNode)
  200. {
  201. // quit immediately on <bla/> elements
  202. if( mReader->isEmptyElement())
  203. return;
  204. while( mReader->read())
  205. {
  206. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  207. {
  208. if( IsElement( "lookat"))
  209. ReadNodeTransformation( pNode, TF_LOOKAT);
  210. else if( IsElement( "matrix"))
  211. ReadNodeTransformation( pNode, TF_MATRIX);
  212. else if( IsElement( "rotate"))
  213. ReadNodeTransformation( pNode, TF_ROTATE);
  214. else if( IsElement( "scale"))
  215. ReadNodeTransformation( pNode, TF_SCALE);
  216. else if( IsElement( "skew"))
  217. ReadNodeTransformation( pNode, TF_SKEW);
  218. else if( IsElement( "translate"))
  219. ReadNodeTransformation( pNode, TF_TRANSLATE);
  220. else if( IsElement( "node"))
  221. {
  222. Node* child = new Node;
  223. int attrID = TestAttribute( "id");
  224. if( attrID > -1)
  225. child->mID = mReader->getAttributeValue( attrID);
  226. int attrName = TestAttribute( "name");
  227. if( attrName > -1)
  228. child->mName = mReader->getAttributeValue( attrName);
  229. // TODO: (thom) support SIDs
  230. assert( TestAttribute( "sid") == -1);
  231. pNode->mChildren.push_back( child);
  232. child->mParent = pNode;
  233. // read on recursively from there
  234. ReadSceneNode( child);
  235. } else if( IsElement( "instance_node"))
  236. {
  237. // test for it, in case we need to implement it
  238. assert( false);
  239. SkipElement();
  240. } else
  241. {
  242. // skip everything else for the moment
  243. SkipElement();
  244. }
  245. }
  246. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  247. {
  248. break;
  249. }
  250. }
  251. }
  252. // ------------------------------------------------------------------------------------------------
  253. // Reads a node transformation entry of the given type and adds it to the given node's transformation list.
  254. void ColladaParser::ReadNodeTransformation( Node* pNode, TransformType pType)
  255. {
  256. std::string tagName = mReader->getNodeName();
  257. // how many parameters to read per transformation type
  258. static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 };
  259. const char* content = GetTextContent();
  260. // read as many parameters and store in the transformation
  261. Transform tf;
  262. tf.mType = pType;
  263. for( unsigned int a = 0; a < sNumParameters[pType]; a++)
  264. {
  265. // read a number
  266. content = fast_atof_move( content, tf.f[a]);
  267. // skip whitespace after it
  268. SkipSpacesAndLineEnd( &content);
  269. }
  270. // place the transformation at the queue of the node
  271. pNode->mTransforms.push_back( tf);
  272. // and consum the closing tag
  273. TestClosing( tagName.c_str());
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. // Reads the collada scene
  277. void ColladaParser::ReadScene()
  278. {
  279. while( mReader->read())
  280. {
  281. if( mReader->getNodeType() == irr::io::EXN_ELEMENT)
  282. {
  283. if( IsElement( "instance_visual_scene"))
  284. {
  285. // should be the first and only occurence
  286. if( mRootNode)
  287. ThrowException( "Invalid scene containing multiple root nodes");
  288. // read the url of the scene to instance. Should be of format "#some_name"
  289. int urlIndex = GetAttribute( "url");
  290. const char* url = mReader->getAttributeValue( urlIndex);
  291. if( url[0] != '#')
  292. ThrowException( "Unknown reference format");
  293. // find the referred scene, skip the leading #
  294. NodeLibrary::const_iterator sit = mNodeLibrary.find( url+1);
  295. if( sit == mNodeLibrary.end())
  296. ThrowException( boost::str( boost::format( "Unable to resolve visual_scene reference \"%s\".") % url));
  297. mRootNode = sit->second;
  298. } else
  299. {
  300. SkipElement();
  301. }
  302. }
  303. else if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  304. {
  305. break;
  306. }
  307. }
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Aborts the file reading with an exception
  311. void ColladaParser::ThrowException( const std::string& pError) const
  312. {
  313. throw new ImportErrorException( boost::str( boost::format( "%s - %s") % mFileName % pError));
  314. }
  315. // ------------------------------------------------------------------------------------------------
  316. // Skips all data until the end node of the current element
  317. void ColladaParser::SkipElement()
  318. {
  319. // nothing to skip if it's an <element />
  320. if( mReader->isEmptyElement())
  321. return;
  322. // copy the current node's name because it'a pointer to the reader's internal buffer,
  323. // which is going to change with the upcoming parsing
  324. std::string element = mReader->getNodeName();
  325. while( mReader->read())
  326. {
  327. if( mReader->getNodeType() == irr::io::EXN_ELEMENT_END)
  328. if( mReader->getNodeName() == element)
  329. break;
  330. }
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. // Tests for the closing tag of the given element, throws an exception if not found
  334. void ColladaParser::TestClosing( const char* pName)
  335. {
  336. // read closing tag
  337. if( !mReader->read())
  338. ThrowException( boost::str( boost::format( "Unexpected end of file while reading end of \"%s\" element.") % pName));
  339. if( mReader->getNodeType() != irr::io::EXN_ELEMENT_END || strcmp( mReader->getNodeName(), pName) != 0)
  340. ThrowException( boost::str( boost::format( "Expected end of \"%s\" element.") % pName));
  341. }
  342. // ------------------------------------------------------------------------------------------------
  343. // Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes
  344. int ColladaParser::GetAttribute( const char* pAttr) const
  345. {
  346. int index = TestAttribute( pAttr);
  347. if( index != -1)
  348. return index;
  349. // attribute not found -> throw an exception
  350. ThrowException( boost::str( boost::format( "Expected attribute \"%s\" at element \"%s\".") % pAttr % mReader->getNodeName()));
  351. return -1;
  352. }
  353. // ------------------------------------------------------------------------------------------------
  354. // Tests the present element for the presence of one attribute, returns its index or throws an exception if not found
  355. int ColladaParser::TestAttribute( const char* pAttr) const
  356. {
  357. for( int a = 0; a < mReader->getAttributeCount(); a++)
  358. if( strcmp( mReader->getAttributeName( a), pAttr) == 0)
  359. return a;
  360. return -1;
  361. }
  362. // ------------------------------------------------------------------------------------------------
  363. // Reads the text contents of an element, throws an exception if not given. Skips leading whitespace.
  364. const char* ColladaParser::GetTextContent()
  365. {
  366. // present node should be the beginning of an element
  367. if( mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement())
  368. ThrowException( "Expected opening element");
  369. // read contents of the element
  370. if( !mReader->read())
  371. ThrowException( "Unexpected end of file while reading asset up_axis element.");
  372. if( mReader->getNodeType() != irr::io::EXN_TEXT)
  373. ThrowException( "Invalid contents in element \"up_axis\".");
  374. // skip leading whitespace
  375. const char* text = mReader->getNodeData();
  376. SkipSpacesAndLineEnd( &text);
  377. return text;
  378. }
  379. // ------------------------------------------------------------------------------------------------
  380. // Calculates the resulting transformation fromm all the given transform steps
  381. aiMatrix4x4 ColladaParser::CalculateResultTransform( const std::vector<Transform>& pTransforms) const
  382. {
  383. aiMatrix4x4 res;
  384. for( std::vector<Transform>::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it)
  385. {
  386. const Transform& tf = *it;
  387. switch( tf.mType)
  388. {
  389. case TF_LOOKAT:
  390. // TODO: (thom)
  391. assert( false);
  392. break;
  393. case TF_ROTATE:
  394. {
  395. aiMatrix4x4 rot;
  396. aiMatrix4x4::Rotation( tf.f[3], aiVector3D( tf.f[0], tf.f[1], tf.f[2]), rot);
  397. res *= rot;
  398. break;
  399. }
  400. case TF_TRANSLATE:
  401. {
  402. aiMatrix4x4 trans;
  403. aiMatrix4x4::Translation( aiVector3D( tf.f[0], tf.f[1], tf.f[2]), trans);
  404. res *= trans;
  405. break;
  406. }
  407. case TF_SCALE:
  408. {
  409. aiMatrix4x4 scale( tf.f[0], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[1], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[2], 0.0f,
  410. 0.0f, 0.0f, 0.0f, 1.0f);
  411. res *= scale;
  412. break;
  413. }
  414. case TF_SKEW:
  415. // TODO: (thom)
  416. assert( false);
  417. break;
  418. case TF_MATRIX:
  419. {
  420. aiMatrix4x4 mat( tf.f[0], tf.f[1], tf.f[2], tf.f[3], tf.f[4], tf.f[5], tf.f[6], tf.f[7],
  421. tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]);
  422. res *= mat;
  423. break;
  424. }
  425. default:
  426. assert( false);
  427. break;
  428. }
  429. }
  430. return res;
  431. }