ColladaParser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file ColladaParser.h
  34. * @brief Defines the parser helper class for the collada loader
  35. */
  36. #pragma once
  37. #ifndef AI_COLLADAPARSER_H_INC
  38. #define AI_COLLADAPARSER_H_INC
  39. #include "ColladaHelper.h"
  40. #include <assimp/ai_assert.h>
  41. #include <assimp/XmlParser.h>
  42. #include <map>
  43. namespace Assimp {
  44. class ZipArchiveIOSystem;
  45. // ------------------------------------------------------------------------------------------
  46. /** Parser helper class for the Collada loader.
  47. *
  48. * Does all the XML reading and builds internal data structures from it,
  49. * but leaves the resolving of all the references to the loader.
  50. */
  51. class ColladaParser {
  52. friend class ColladaLoader;
  53. public:
  54. /// Map for generic metadata as aiString.
  55. using StringMetaData = std::map<std::string, aiString>;
  56. /// Constructor from XML file.
  57. ColladaParser(IOSystem *pIOHandler, const std::string &pFile);
  58. /// Destructor
  59. ~ColladaParser();
  60. /// Attempts to read the ZAE manifest and returns the DAE to open
  61. static std::string ReadZaeManifest(ZipArchiveIOSystem &zip_archive);
  62. /// Reads the contents of the file
  63. void ReadContents(XmlNode &node);
  64. /// Reads the structure of the file
  65. void ReadStructure(XmlNode &node);
  66. /// Reads asset information such as coordinate system information and legal blah
  67. void ReadAssetInfo(XmlNode &node);
  68. /// Reads contributor information such as author and legal blah
  69. void ReadContributorInfo(XmlNode &node);
  70. /// Reads the animation library
  71. void ReadAnimationLibrary(XmlNode &node);
  72. /// Reads the animation clip library
  73. void ReadAnimationClipLibrary(XmlNode &node);
  74. /// Unwrap controllers dependency hierarchy
  75. void PostProcessControllers();
  76. /// Re-build animations from animation clip library, if present, otherwise combine single-channel animations
  77. void PostProcessRootAnimations();
  78. /// Reads an animation into the given parent structure
  79. void ReadAnimation(XmlNode &node, Collada::Animation *pParent);
  80. /// Reads the skeleton controller library
  81. void ReadControllerLibrary(XmlNode &node);
  82. /// Reads a controller into the given mesh structure
  83. void ReadController(XmlNode &node, Collada::Controller &pController);
  84. /// Reads the image library contents
  85. void ReadImageLibrary(const XmlNode &node);
  86. /// Reads an image entry into the given image
  87. void ReadImage(const XmlNode &node, Collada::Image &pImage) const;
  88. /// Reads the material library
  89. void ReadMaterialLibrary(XmlNode &node);
  90. /// Reads the camera library
  91. void ReadCameraLibrary(XmlNode &node);
  92. /// Reads the light library
  93. void ReadLightLibrary(XmlNode &node);
  94. /// Reads the effect library
  95. void ReadEffectLibrary(XmlNode &node);
  96. /// Reads an effect entry into the given effect
  97. void ReadEffect(XmlNode &node, Collada::Effect &pEffect);
  98. /// Reads an COMMON effect profile
  99. void ReadEffectProfileCommon(XmlNode &node, Collada::Effect &pEffect);
  100. /// Read sampler properties
  101. void ReadSamplerProperties(XmlNode &node, Collada::Sampler &pSampler);
  102. /// Reads an effect entry containing a color or a texture defining that color
  103. void ReadEffectColor(XmlNode &node, aiColor4D &pColor, Collada::Sampler &pSampler);
  104. /// Reads an effect entry containing a float
  105. void ReadEffectFloat(XmlNode &node, ai_real &pFloat);
  106. /// Reads an effect parameter specification of any kind
  107. void ReadEffectParam(XmlNode &node, Collada::EffectParam &pParam);
  108. /// Reads the geometry library contents
  109. void ReadGeometryLibrary(XmlNode &node);
  110. /// Reads a geometry from the geometry library.
  111. void ReadGeometry(XmlNode &node, Collada::Mesh &pMesh);
  112. /// Reads a mesh from the geometry library
  113. void ReadMesh(XmlNode &node, Collada::Mesh &pMesh);
  114. /// Reads a source element - a combination of raw data and an accessor defining
  115. ///things that should not be definable. Yes, that's another rant.
  116. void ReadSource(XmlNode &node);
  117. /// Reads a data array holding a number of elements, and stores it in the global library.
  118. /// Currently supported are array of floats and arrays of strings.
  119. void ReadDataArray(XmlNode &node);
  120. /// Reads an accessor and stores it in the global library under the given ID -
  121. /// accessors use the ID of the parent <source> element
  122. void ReadAccessor(XmlNode &node, const std::string &pID);
  123. /// Reads input declarations of per-vertex mesh data into the given mesh
  124. void ReadVertexData(XmlNode &node, Collada::Mesh &pMesh);
  125. /// Reads input declarations of per-index mesh data into the given mesh
  126. void ReadIndexData(XmlNode &node, Collada::Mesh &pMesh);
  127. /// Reads a single input channel element and stores it in the given array, if valid
  128. void ReadInputChannel(XmlNode &node, std::vector<Collada::InputChannel> &poChannels);
  129. /// Reads a <p> primitive index list and assembles the mesh data into the given mesh
  130. size_t ReadPrimitives(XmlNode &node, Collada::Mesh &pMesh, std::vector<Collada::InputChannel> &pPerIndexChannels,
  131. size_t pNumPrimitives, const std::vector<size_t> &pVCount, Collada::PrimitiveType pPrimType);
  132. /// Copies the data for a single primitive into the mesh, based on the InputChannels
  133. void CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset,
  134. Collada::Mesh &pMesh, std::vector<Collada::InputChannel> &pPerIndexChannels,
  135. size_t currentPrimitive, const std::vector<size_t> &indices);
  136. /// Reads one triangle of a tristrip into the mesh
  137. void ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Collada::Mesh &pMesh,
  138. std::vector<Collada::InputChannel> &pPerIndexChannels, size_t currentPrimitive, const std::vector<size_t> &indices);
  139. /// Extracts a single object from an input channel and stores it in the appropriate mesh data array
  140. void ExtractDataObjectFromChannel(const Collada::InputChannel &pInput, size_t pLocalIndex, Collada::Mesh &pMesh);
  141. /// Reads the library of node hierarchies and scene parts
  142. void ReadSceneLibrary(XmlNode &node);
  143. /// Reads a scene node's contents including children and stores it in the given node
  144. void ReadSceneNode(XmlNode &node, Collada::Node *pNode);
  145. /// Reads a mesh reference in a node and adds it to the node's mesh list
  146. void ReadNodeGeometry(XmlNode &node, Collada::Node *pNode);
  147. /// Reads the collada scene
  148. void ReadScene(XmlNode &node);
  149. /// Processes bind_vertex_input and bind elements
  150. void ReadMaterialVertexInputBinding(XmlNode &node, Collada::SemanticMappingTable &tbl);
  151. /// Reads embedded textures from a ZAE archive
  152. void ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive);
  153. protected:
  154. /// Converts a path read from a collada file to the usual representation
  155. static void UriDecodePath(aiString &ss);
  156. /// Calculates the resulting transformation from all the given transform steps
  157. aiMatrix4x4 CalculateResultTransform(const std::vector<Collada::Transform> &pTransforms) const;
  158. /// Determines the input data type for the given semantic string
  159. Collada::InputType GetTypeForSemantic(const std::string &pSemantic);
  160. /// Finds the item in the given library by its reference, throws if not found
  161. template <typename Type>
  162. const Type &ResolveLibraryReference(const std::map<std::string, Type> &pLibrary, const std::string &pURL) const;
  163. private:
  164. /// Filename, for a verbose error message
  165. std::string mFileName;
  166. /// XML reader, member for everyday use
  167. XmlParser mXmlParser;
  168. /// All data arrays found in the file by ID. Might be referred to by actually
  169. /// everyone. Collada, you are a steaming pile of indirection.
  170. using DataLibrary = std::map<std::string, Collada::Data> ;
  171. DataLibrary mDataLibrary;
  172. /// Same for accessors which define how the data in a data array is accessed.
  173. using AccessorLibrary = std::map<std::string, Collada::Accessor> ;
  174. AccessorLibrary mAccessorLibrary;
  175. /// Mesh library: mesh by ID
  176. using MeshLibrary = std::map<std::string, Collada::Mesh *>;
  177. MeshLibrary mMeshLibrary;
  178. /// node library: root node of the hierarchy part by ID
  179. using NodeLibrary = std::map<std::string, Collada::Node *>;
  180. NodeLibrary mNodeLibrary;
  181. /// Image library: stores texture properties by ID
  182. using ImageLibrary = std::map<std::string, Collada::Image> ;
  183. ImageLibrary mImageLibrary;
  184. /// Effect library: surface attributes by ID
  185. using EffectLibrary = std::map<std::string, Collada::Effect> ;
  186. EffectLibrary mEffectLibrary;
  187. /// Material library: surface material by ID
  188. using MaterialLibrary = std::map<std::string, Collada::Material> ;
  189. MaterialLibrary mMaterialLibrary;
  190. /// Light library: surface light by ID
  191. using LightLibrary = std::map<std::string, Collada::Light> ;
  192. LightLibrary mLightLibrary;
  193. /// Camera library: surface material by ID
  194. using CameraLibrary = std::map<std::string, Collada::Camera> ;
  195. CameraLibrary mCameraLibrary;
  196. /// Controller library: joint controllers by ID
  197. using ControllerLibrary = std::map<std::string, Collada::Controller> ;
  198. ControllerLibrary mControllerLibrary;
  199. /// Animation library: animation references by ID
  200. using AnimationLibrary = std::map<std::string, Collada::Animation *> ;
  201. AnimationLibrary mAnimationLibrary;
  202. /// Animation clip library: clip animation references by ID
  203. using AnimationClipLibrary = std::vector<std::pair<std::string, std::vector<std::string>>> ;
  204. AnimationClipLibrary mAnimationClipLibrary;
  205. /// Pointer to the root node. Don't delete, it just points to one of the nodes in the node library.
  206. Collada::Node *mRootNode;
  207. /// Root animation container
  208. Collada::Animation mAnims;
  209. /// Size unit: how large compared to a meter
  210. ai_real mUnitSize;
  211. /// Which is the up vector
  212. enum { UP_X,
  213. UP_Y,
  214. UP_Z } mUpDirection;
  215. /// Asset metadata (global for scene)
  216. StringMetaData mAssetMetaData;
  217. /// Collada file format version
  218. Collada::FormatVersion mFormat;
  219. };
  220. // ------------------------------------------------------------------------------------------------
  221. // Finds the item in the given library by its reference, throws if not found
  222. template <typename Type>
  223. const Type &ColladaParser::ResolveLibraryReference(const std::map<std::string, Type> &pLibrary, const std::string &pURL) const {
  224. typename std::map<std::string, Type>::const_iterator it = pLibrary.find(pURL);
  225. if (it == pLibrary.end()) {
  226. throw DeadlyImportError("Unable to resolve library reference \"", pURL, "\".");
  227. }
  228. return it->second;
  229. }
  230. } // end of namespace Assimp
  231. #endif // AI_COLLADAPARSER_H_INC