X3DExporter.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /// \file X3DExporter.hpp
  2. /// \brief X3D-format files exporter for Assimp.
  3. /// \date 2016
  4. /// \author [email protected]
  5. // Thanks to acorn89 for support.
  6. #ifndef INCLUDED_AI_X3D_EXPORTER_H
  7. #define INCLUDED_AI_X3D_EXPORTER_H
  8. // Header files, Assimp.
  9. #include <assimp/material.h>
  10. #include <assimp/scene.h>
  11. #include <assimp/DefaultLogger.hpp>
  12. #include <assimp/Exporter.hpp>
  13. // Header files, stdlib.
  14. #include <list>
  15. #include <string>
  16. namespace Assimp {
  17. /// \class X3DExporter
  18. /// Class which export aiScene to X3D file.
  19. ///
  20. /// Limitations.
  21. ///
  22. /// Pay attention that X3D is format for interactive graphic and simulations for web browsers. aiScene can not contain all features of the X3D format.
  23. /// Also, aiScene contain rasterized-like data. For example, X3D can describe circle all cylinder with one tag, but aiScene contain result of tessellation:
  24. /// vertices, faces etc. Yes, you can use algorithm for detecting figures or shapes, but that's not a good idea at all.
  25. ///
  26. /// Supported nodes:
  27. /// Core component:
  28. /// "MetadataBoolean", "MetadataDouble", "MetadataFloat", "MetadataInteger", "MetadataSet", "MetadataString"
  29. /// Geometry3D component:
  30. /// "IndexedFaceSet"
  31. /// Grouping component:
  32. /// "Group", "Transform"
  33. /// Lighting component:
  34. /// "DirectionalLight", "PointLight", "SpotLight"
  35. /// Rendering component:
  36. /// "ColorRGBA", "Coordinate", "Normal"
  37. /// Shape component:
  38. /// "Shape", "Appearance", "Material"
  39. /// Texturing component:
  40. /// "ImageTexture", "TextureCoordinate", "TextureTransform"
  41. ///
  42. class X3DExporter {
  43. /***********************************************/
  44. /******************** Types ********************/
  45. /***********************************************/
  46. struct SAttribute {
  47. const std::string Name;
  48. const std::string Value;
  49. SAttribute() = default;
  50. SAttribute(const std::string &name, const std::string &value) :
  51. Name(name),
  52. Value(value) {
  53. // empty
  54. }
  55. };
  56. /***********************************************/
  57. /****************** Constants ******************/
  58. /***********************************************/
  59. const aiScene *const mScene;
  60. /***********************************************/
  61. /****************** Variables ******************/
  62. /***********************************************/
  63. IOStream *mOutFile;
  64. std::map<size_t, std::string> mDEF_Map_Mesh;
  65. std::map<size_t, std::string> mDEF_Map_Material;
  66. private:
  67. std::string mIndentationString;
  68. /***********************************************/
  69. /****************** Functions ******************/
  70. /***********************************************/
  71. /// \fn void IndentationStringSet(const size_t pNewLevel)
  72. /// Set value of the indentation string.
  73. /// \param [in] pNewLevel - new level of the indentation.
  74. void IndentationStringSet(const size_t pNewLevel);
  75. /// \fn void XML_Write(const std::string& pData)
  76. /// Write data to XML-file.
  77. /// \param [in] pData - reference to string which must be written.
  78. void XML_Write(const std::string &pData);
  79. /// \fn aiMatrix4x4 Matrix_GlobalToCurrent(const aiNode& pNode) const
  80. /// Calculate transformation matrix for transformation from global coordinate system to pointed aiNode.
  81. /// \param [in] pNode - reference to local node.
  82. /// \return calculated matrix.
  83. aiMatrix4x4 Matrix_GlobalToCurrent(const aiNode &pNode) const;
  84. /// \fn void AttrHelper_CommaToPoint(std::string& pStringWithComma)
  85. /// Convert commas in string to points. That's needed because "std::to_string" result depends on locale (regional settings).
  86. /// \param [in, out] pStringWithComma - reference to string, which must be modified.
  87. void AttrHelper_CommaToPoint(std::string &pStringWithComma) {
  88. for (char &c : pStringWithComma) {
  89. if (c == ',') c = '.';
  90. }
  91. }
  92. /// \fn void AttrHelper_FloatToString(const float pValue, std::string& pTargetString)
  93. /// Converts float to string.
  94. /// \param [in] pValue - value for converting.
  95. /// \param [out] pTargetString - reference to string where result will be placed. Will be cleared before using.
  96. void AttrHelper_FloatToString(const float pValue, std::string &pTargetString);
  97. /// \fn void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  98. /// Converts array of vectors to string.
  99. /// \param [in] pArray - pointer to array of vectors.
  100. /// \param [in] pArray_Size - count of elements in array.
  101. /// \param [out] pTargetString - reference to string where result will be placed. Will be cleared before using.
  102. void AttrHelper_Vec3DArrToString(const aiVector3D *pArray, const size_t pArray_Size, std::string &pTargetString);
  103. /// \fn void AttrHelper_Vec2DArrToString(const aiVector2D* pArray, const size_t pArray_Size, std::string& pTargetString)
  104. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  105. void AttrHelper_Vec2DArrToString(const aiVector2D *pArray, const size_t pArray_Size, std::string &pTargetString);
  106. /// \fn void AttrHelper_Vec3DAsVec2fArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  107. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  108. /// Only x, y is used from aiVector3D.
  109. void AttrHelper_Vec3DAsVec2fArrToString(const aiVector3D *pArray, const size_t pArray_Size, std::string &pTargetString);
  110. /// \fn void AttrHelper_Col4DArrToString(const aiColor4D* pArray, const size_t pArray_Size, std::string& pTargetString)
  111. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  112. /// Converts array of colors to string.
  113. void AttrHelper_Col4DArrToString(const aiColor4D *pArray, const size_t pArray_Size, std::string &pTargetString);
  114. /// \fn void AttrHelper_Col3DArrToString(const aiColor3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  115. /// \overload void AttrHelper_Col4DArrToString(const aiColor4D* pArray, const size_t pArray_Size, std::string& pTargetString)
  116. /// Converts array of colors to string.
  117. void AttrHelper_Col3DArrToString(const aiColor3D *pArray, const size_t pArray_Size, std::string &pTargetString);
  118. /// \fn void AttrHelper_FloatToAttrList(std::list<SAttribute> pList, const std::string& pName, const float pValue, const float pDefaultValue)
  119. /// \overload void AttrHelper_Col3DArrToString(const aiColor3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  120. void AttrHelper_FloatToAttrList(std::list<SAttribute> &pList, const std::string &pName, const float pValue, const float pDefaultValue);
  121. /// \fn void AttrHelper_Color3ToAttrList(std::list<SAttribute> pList, const std::string& pName, const aiColor3D& pValue, const aiColor3D& pDefaultValue)
  122. /// Add attribute to list if value not equal to default.
  123. /// \param [in] pList - target list of the attributes.
  124. /// \param [in] pName - name of new attribute.
  125. /// \param [in] pValue - value of the new attribute.
  126. /// \param [in] pDefaultValue - default value for checking: if pValue is equal to pDefaultValue then attribute will not be added.
  127. void AttrHelper_Color3ToAttrList(std::list<SAttribute> &pList, const std::string &pName, const aiColor3D &pValue, const aiColor3D &pDefaultValue);
  128. /// \fn void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute>& pAttrList)
  129. /// Begin new XML-node element.
  130. /// \param [in] pNodeName - name of the element.
  131. /// \param [in] pTabLevel - indentation level.
  132. /// \param [in] pEmtyElement - if true then empty element will be created.
  133. /// \param [in] pAttrList - list of the attributes for element.
  134. void NodeHelper_OpenNode(const std::string &pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute> &pAttrList);
  135. /// \fn void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement = false)
  136. /// \overload void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute>& pAttrList)
  137. void NodeHelper_OpenNode(const std::string &pNodeName, const size_t pTabLevel, const bool pEmptyElement = false);
  138. /// \fn void NodeHelper_CloseNode(const std::string& pNodeName, const size_t pTabLevel)
  139. /// End XML-node element.
  140. /// \param [in] pNodeName - name of the element.
  141. /// \param [in] pTabLevel - indentation level.
  142. void NodeHelper_CloseNode(const std::string &pNodeName, const size_t pTabLevel);
  143. /// \fn void Export_Node(const aiNode* pNode, const size_t pTabLevel)
  144. /// Export data from scene to XML-file: aiNode.
  145. /// \param [in] pNode - source aiNode.
  146. /// \param [in] pTabLevel - indentation level.
  147. void Export_Node(const aiNode *pNode, const size_t pTabLevel);
  148. /// \fn void Export_Mesh(const size_t pIdxMesh, const size_t pTabLevel)
  149. /// Export data from scene to XML-file: aiMesh.
  150. /// \param [in] pMesh - index of the source aiMesh.
  151. /// \param [in] pTabLevel - indentation level.
  152. void Export_Mesh(const size_t pIdxMesh, const size_t pTabLevel);
  153. /// \fn void Export_Material(const size_t pIdxMaterial, const size_t pTabLevel)
  154. /// Export data from scene to XML-file: aiMaterial.
  155. /// \param [in] pIdxMaterial - index of the source aiMaterial.
  156. /// \param [in] pTabLevel - indentation level.
  157. void Export_Material(const size_t pIdxMaterial, const size_t pTabLevel);
  158. /// \fn void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  159. /// Export data from scene to XML-file: aiMetadata.
  160. /// \param [in] pKey - source data: value of the metadata key.
  161. /// \param [in] pValue - source data: value of the metadata value.
  162. /// \param [in] pTabLevel - indentation level.
  163. void Export_MetadataBoolean(const aiString &pKey, const bool pValue, const size_t pTabLevel);
  164. /// \fn void Export_MetadataDouble(const aiString& pKey, const double pValue, const size_t pTabLevel)
  165. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  166. void Export_MetadataDouble(const aiString &pKey, const double pValue, const size_t pTabLevel);
  167. /// \fn void Export_MetadataFloat(const aiString& pKey, const float pValue, const size_t pTabLevel)
  168. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  169. void Export_MetadataFloat(const aiString &pKey, const float pValue, const size_t pTabLevel);
  170. /// \fn void Export_MetadataInteger(const aiString& pKey, const int32_t pValue, const size_t pTabLevel)
  171. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  172. void Export_MetadataInteger(const aiString &pKey, const int32_t pValue, const size_t pTabLevel);
  173. /// \fn void Export_MetadataString(const aiString& pKey, const aiString& pValue, const size_t pTabLevel)
  174. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  175. void Export_MetadataString(const aiString &pKey, const aiString &pValue, const size_t pTabLevel);
  176. /// \fn bool CheckAndExport_Light(const aiNode& pNode, const size_t pTabLevel)
  177. /// Check if node point to light source. If yes then export light source.
  178. /// \param [in] pNode - reference to node for checking.
  179. /// \param [in] pTabLevel - indentation level.
  180. /// \return true - if node assigned with light and it was exported, else - return false.
  181. bool CheckAndExport_Light(const aiNode &pNode, const size_t pTabLevel);
  182. /***********************************************/
  183. /************** Functions: LOG set *************/
  184. /***********************************************/
  185. /// \fn void LogError(const std::string& pMessage)
  186. /// Short variant for calling \ref DefaultLogger::get()->error()
  187. void LogError(const std::string &pMessage) { DefaultLogger::get()->error(pMessage); }
  188. public:
  189. /// \fn X3DExporter()
  190. /// Default constructor.
  191. X3DExporter(const char *pFileName, IOSystem *pIOSystem, const aiScene *pScene, const ExportProperties *pProperties);
  192. /// \fn ~X3DExporter()
  193. /// Default destructor.
  194. ~X3DExporter() = default;
  195. }; // class X3DExporter
  196. } // namespace Assimp
  197. #endif // INCLUDED_AI_X3D_EXPORTER_H