X3DImporter.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, 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 X3DImporter.hpp
  34. /// \brief X3D-format files importer for Assimp.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. // Thanks to acorn89 for support.
  38. #ifndef INCLUDED_AI_X3D_IMPORTER_H
  39. #define INCLUDED_AI_X3D_IMPORTER_H
  40. // Header files, Assimp.
  41. #include <assimp/BaseImporter.h>
  42. #include <assimp/XmlParser.h>
  43. #include <assimp/importerdesc.h>
  44. #include <assimp/scene.h>
  45. #include <assimp/types.h>
  46. #include <assimp/DefaultLogger.hpp>
  47. #include <assimp/ProgressHandler.hpp>
  48. #include <list>
  49. namespace Assimp {
  50. inline void Throw_ArgOutOfRange(const std::string &argument) {
  51. throw DeadlyImportError("Argument value is out of range for: \"" + argument + "\".");
  52. }
  53. inline void Throw_CloseNotFound(const std::string &node) {
  54. throw DeadlyImportError("Close tag for node <" + node + "> not found. Seems file is corrupt.");
  55. }
  56. inline void Throw_ConvertFail_Str2ArrF(const std::string &nodeName, const std::string &pAttrValue) {
  57. throw DeadlyImportError("In <" + nodeName + "> failed to convert attribute value \"" + pAttrValue +
  58. "\" from string to array of floats.");
  59. }
  60. inline void Throw_DEF_And_USE(const std::string &nodeName) {
  61. throw DeadlyImportError("\"DEF\" and \"USE\" can not be defined both in <" + nodeName + ">.");
  62. }
  63. inline void Throw_IncorrectAttr(const std::string &nodeName, const std::string &pAttrName) {
  64. throw DeadlyImportError("Node <" + nodeName + "> has incorrect attribute \"" + pAttrName + "\".");
  65. }
  66. inline void Throw_IncorrectAttrValue(const std::string &nodeName, const std::string &pAttrName) {
  67. throw DeadlyImportError("Attribute \"" + pAttrName + "\" in node <" + nodeName + "> has incorrect value.");
  68. }
  69. inline void Throw_MoreThanOnceDefined(const std::string &nodeName, const std::string &pNodeType, const std::string &pDescription) {
  70. throw DeadlyImportError("\"" + pNodeType + "\" node can be used only once in " + nodeName + ". Description: " + pDescription);
  71. }
  72. inline void Throw_TagCountIncorrect(const std::string &pNode) {
  73. throw DeadlyImportError("Count of open and close tags for node <" + pNode + "> are not equivalent. Seems file is corrupt.");
  74. }
  75. inline void Throw_USE_NotFound(const std::string &nodeName, const std::string &pAttrValue) {
  76. throw DeadlyImportError("Not found node with name \"" + pAttrValue + "\" in <" + nodeName + ">.");
  77. }
  78. inline void LogInfo(const std::string &message) {
  79. DefaultLogger::get()->info(message);
  80. }
  81. /// \class X3DImporter
  82. /// Class that holding scene graph which include: groups, geometry, metadata etc.
  83. ///
  84. /// Limitations.
  85. ///
  86. /// Pay attention that X3D is format for interactive graphic and simulations for web browsers.
  87. /// So not all features can be imported using Assimp.
  88. ///
  89. /// Unsupported nodes:
  90. /// CAD geometry component:
  91. /// "CADAssembly", "CADFace", "CADLayer", "CADPart", "IndexedQuadSet", "QuadSet"
  92. /// Core component:
  93. /// "ROUTE", "ExternProtoDeclare", "ProtoDeclare", "ProtoInstance", "ProtoInterface", "WorldInfo"
  94. /// Distributed interactive simulation (DIS) component:
  95. /// "DISEntityManager", "DISEntityTypeMapping", "EspduTransform", "ReceiverPdu", "SignalPdu", "TransmitterPdu"
  96. /// Cube map environmental texturing component:
  97. /// "ComposedCubeMapTexture", "GeneratedCubeMapTexture", "ImageCubeMapTexture"
  98. /// Environmental effects component:
  99. /// "Background", "Fog", "FogCoordinate", "LocalFog", "TextureBackground"
  100. /// Environmental sensor component:
  101. /// "ProximitySensor", "TransformSensor", "VisibilitySensor"
  102. /// Followers component:
  103. /// "ColorChaser", "ColorDamper", "CoordinateChaser", "CoordinateDamper", "OrientationChaser", "OrientationDamper", "PositionChaser",
  104. /// "PositionChaser2D", "PositionDamper", "PositionDamper2D", "ScalarChaser", "ScalarDamper", "TexCoordChaser2D", "TexCoordDamper2D"
  105. /// Geospatial component:
  106. /// "GeoCoordinate", "GeoElevationGrid", "GeoLocation", "GeoLOD", "GeoMetadata", "GeoOrigin", "GeoPositionInterpolator", "GeoProximitySensor",
  107. /// "GeoTouchSensor", "GeoTransform", "GeoViewpoint"
  108. /// Humanoid Animation (H-Anim) component:
  109. /// "HAnimDisplacer", "HAnimHumanoid", "HAnimJoint", "HAnimSegment", "HAnimSite"
  110. /// Interpolation component:
  111. /// "ColorInterpolator", "CoordinateInterpolator", "CoordinateInterpolator2D", "EaseInEaseOut", "NormalInterpolator", "OrientationInterpolator",
  112. /// "PositionInterpolator", "PositionInterpolator2D", "ScalarInterpolator", "SplinePositionInterpolator", "SplinePositionInterpolator2D",
  113. /// "SplineScalarInterpolator", "SquadOrientationInterpolator",
  114. /// Key device sensor component:
  115. /// "KeySensor", "StringSensor"
  116. /// Layering component:
  117. /// "Layer", "LayerSet", "Viewport"
  118. /// Layout component:
  119. /// "Layout", "LayoutGroup", "LayoutLayer", "ScreenFontStyle", "ScreenGroup"
  120. /// Navigation component:
  121. /// "Billboard", "Collision", "LOD", "NavigationInfo", "OrthoViewpoint", "Viewpoint", "ViewpointGroup"
  122. /// Networking component:
  123. /// "EXPORT", "IMPORT", "Anchor", "LoadSensor"
  124. /// NURBS component:
  125. /// "Contour2D", "ContourPolyline2D", "CoordinateDouble", "NurbsCurve", "NurbsCurve2D", "NurbsOrientationInterpolator", "NurbsPatchSurface",
  126. /// "NurbsPositionInterpolator", "NurbsSet", "NurbsSurfaceInterpolator", "NurbsSweptSurface", "NurbsSwungSurface", "NurbsTextureCoordinate",
  127. /// "NurbsTrimmedSurface"
  128. /// Particle systems component:
  129. /// "BoundedPhysicsModel", "ConeEmitter", "ExplosionEmitter", "ForcePhysicsModel", "ParticleSystem", "PointEmitter", "PolylineEmitter",
  130. /// "SurfaceEmitter", "VolumeEmitter", "WindPhysicsModel"
  131. /// Picking component:
  132. /// "LinePickSensor", "PickableGroup", "PointPickSensor", "PrimitivePickSensor", "VolumePickSensor"
  133. /// Pointing device sensor component:
  134. /// "CylinderSensor", "PlaneSensor", "SphereSensor", "TouchSensor"
  135. /// Rendering component:
  136. /// "ClipPlane"
  137. /// Rigid body physics:
  138. /// "BallJoint", "CollidableOffset", "CollidableShape", "CollisionCollection", "CollisionSensor", "CollisionSpace", "Contact", "DoubleAxisHingeJoint",
  139. /// "MotorJoint", "RigidBody", "RigidBodyCollection", "SingleAxisHingeJoint", "SliderJoint", "UniversalJoint"
  140. /// Scripting component:
  141. /// "Script"
  142. /// Programmable shaders component:
  143. /// "ComposedShader", "FloatVertexAttribute", "Matrix3VertexAttribute", "Matrix4VertexAttribute", "PackagedShader", "ProgramShader", "ShaderPart",
  144. /// "ShaderProgram",
  145. /// Shape component:
  146. /// "FillProperties", "LineProperties", "TwoSidedMaterial"
  147. /// Sound component:
  148. /// "AudioClip", "Sound"
  149. /// Text component:
  150. /// "FontStyle", "Text"
  151. /// Texturing3D Component:
  152. /// "ComposedTexture3D", "ImageTexture3D", "PixelTexture3D", "TextureCoordinate3D", "TextureCoordinate4D", "TextureTransformMatrix3D",
  153. /// "TextureTransform3D"
  154. /// Texturing component:
  155. /// "MovieTexture", "MultiTexture", "MultiTextureCoordinate", "MultiTextureTransform", "PixelTexture", "TextureCoordinateGenerator",
  156. /// "TextureProperties",
  157. /// Time component:
  158. /// "TimeSensor"
  159. /// Event Utilities component:
  160. /// "BooleanFilter", "BooleanSequencer", "BooleanToggle", "BooleanTrigger", "IntegerSequencer", "IntegerTrigger", "TimeTrigger",
  161. /// Volume rendering component:
  162. /// "BlendedVolumeStyle", "BoundaryEnhancementVolumeStyle", "CartoonVolumeStyle", "ComposedVolumeStyle", "EdgeEnhancementVolumeStyle",
  163. /// "IsoSurfaceVolumeData", "OpacityMapVolumeStyle", "ProjectionVolumeStyle", "SegmentedVolumeData", "ShadedVolumeStyle",
  164. /// "SilhouetteEnhancementVolumeStyle", "ToneMappedVolumeStyle", "VolumeData"
  165. ///
  166. /// Supported nodes:
  167. /// Core component:
  168. /// "MetadataBoolean", "MetadataDouble", "MetadataFloat", "MetadataInteger", "MetadataSet", "MetadataString"
  169. /// Geometry2D component:
  170. /// "Arc2D", "ArcClose2D", "Circle2D", "Disk2D", "Polyline2D", "Polypoint2D", "Rectangle2D", "TriangleSet2D"
  171. /// Geometry3D component:
  172. /// "Box", "Cone", "Cylinder", "ElevationGrid", "Extrusion", "IndexedFaceSet", "Sphere"
  173. /// Grouping component:
  174. /// "Group", "StaticGroup", "Switch", "Transform"
  175. /// Lighting component:
  176. /// "DirectionalLight", "PointLight", "SpotLight"
  177. /// Networking component:
  178. /// "Inline"
  179. /// Rendering component:
  180. /// "Color", "ColorRGBA", "Coordinate", "IndexedLineSet", "IndexedTriangleFanSet", "IndexedTriangleSet", "IndexedTriangleStripSet", "LineSet",
  181. /// "PointSet", "TriangleFanSet", "TriangleSet", "TriangleStripSet", "Normal"
  182. /// Shape component:
  183. /// "Shape", "Appearance", "Material"
  184. /// Texturing component:
  185. /// "ImageTexture", "TextureCoordinate", "TextureTransform"
  186. ///
  187. /// Limitations of attribute "USE".
  188. /// If "USE" is set then node must be empty, like that:
  189. /// <Node USE='name'/>
  190. /// not the
  191. /// <Node USE='name'><!-- something --> </Node>
  192. ///
  193. /// Ignored attributes: "creaseAngle", "convex", "solid".
  194. ///
  195. /// Texture coordinates generating: only for Sphere, Cone, Cylinder. In all other case used PLANE mapping.
  196. /// It's better that Assimp main code has powerful texture coordinates generator. Then is not needed to
  197. /// duplicate this code in every importer.
  198. ///
  199. /// Lighting limitations.
  200. /// If light source placed in some group with "DEF" set. And after that some node is use it group with "USE" attribute then
  201. /// you will get error about duplicate light sources. That's happening because Assimp require names for lights but do not like
  202. /// duplicates of it )).
  203. ///
  204. /// Color for faces.
  205. /// That's happening when attribute "colorPerVertex" is set to "false". But Assimp do not hold how many colors has mesh and require
  206. /// equal length for mVertices and mColors. You will see the colors but vertices will use call which last used in "colorIdx".
  207. ///
  208. /// That's all for now. Enjoy
  209. ///
  210. enum class X3DElemType {
  211. ENET_Group, ///< Element has type "Group".
  212. ENET_MetaBoolean, ///< Element has type "Metadata boolean".
  213. ENET_MetaDouble, ///< Element has type "Metadata double".
  214. ENET_MetaFloat, ///< Element has type "Metadata float".
  215. ENET_MetaInteger, ///< Element has type "Metadata integer".
  216. ENET_MetaSet, ///< Element has type "Metadata set".
  217. ENET_MetaString, ///< Element has type "Metadata string".
  218. ENET_Arc2D, ///< Element has type "Arc2D".
  219. ENET_ArcClose2D, ///< Element has type "ArcClose2D".
  220. ENET_Circle2D, ///< Element has type "Circle2D".
  221. ENET_Disk2D, ///< Element has type "Disk2D".
  222. ENET_Polyline2D, ///< Element has type "Polyline2D".
  223. ENET_Polypoint2D, ///< Element has type "Polypoint2D".
  224. ENET_Rectangle2D, ///< Element has type "Rectangle2D".
  225. ENET_TriangleSet2D, ///< Element has type "TriangleSet2D".
  226. ENET_Box, ///< Element has type "Box".
  227. ENET_Cone, ///< Element has type "Cone".
  228. ENET_Cylinder, ///< Element has type "Cylinder".
  229. ENET_Sphere, ///< Element has type "Sphere".
  230. ENET_ElevationGrid, ///< Element has type "ElevationGrid".
  231. ENET_Extrusion, ///< Element has type "Extrusion".
  232. ENET_Coordinate, ///< Element has type "Coordinate".
  233. ENET_Normal, ///< Element has type "Normal".
  234. ENET_TextureCoordinate, ///< Element has type "TextureCoordinate".
  235. ENET_IndexedFaceSet, ///< Element has type "IndexedFaceSet".
  236. ENET_IndexedLineSet, ///< Element has type "IndexedLineSet".
  237. ENET_IndexedTriangleSet, ///< Element has type "IndexedTriangleSet".
  238. ENET_IndexedTriangleFanSet, ///< Element has type "IndexedTriangleFanSet".
  239. ENET_IndexedTriangleStripSet, ///< Element has type "IndexedTriangleStripSet".
  240. ENET_LineSet, ///< Element has type "LineSet".
  241. ENET_PointSet, ///< Element has type "PointSet".
  242. ENET_TriangleSet, ///< Element has type "TriangleSet".
  243. ENET_TriangleFanSet, ///< Element has type "TriangleFanSet".
  244. ENET_TriangleStripSet, ///< Element has type "TriangleStripSet".
  245. ENET_Color, ///< Element has type "Color".
  246. ENET_ColorRGBA, ///< Element has type "ColorRGBA".
  247. ENET_Shape, ///< Element has type "Shape".
  248. ENET_Appearance, ///< Element has type "Appearance".
  249. ENET_Material, ///< Element has type "Material".
  250. ENET_ImageTexture, ///< Element has type "ImageTexture".
  251. ENET_TextureTransform, ///< Element has type "TextureTransform".
  252. ENET_DirectionalLight, ///< Element has type "DirectionalLight".
  253. ENET_PointLight, ///< Element has type "PointLight".
  254. ENET_SpotLight, ///< Element has type "SpotLight".
  255. ENET_Invalid ///< Element has invalid type and possible contain invalid data.
  256. };
  257. struct X3DNodeElementBase {
  258. X3DNodeElementBase *Parent;
  259. std::string ID;
  260. std::list<X3DNodeElementBase *> Child;
  261. X3DElemType Type;
  262. };
  263. class X3DImporter : public BaseImporter {
  264. public:
  265. std::list<X3DNodeElementBase *> NodeElement_List; ///< All elements of scene graph.
  266. public:
  267. /// Default constructor.
  268. X3DImporter();
  269. /// Default destructor.
  270. ~X3DImporter();
  271. /***********************************************/
  272. /******** Functions: parse set, public *********/
  273. /***********************************************/
  274. /// Parse X3D file and fill scene graph. The function has no return value. Result can be found by analyzing the generated graph.
  275. /// Also exception can be thrown if trouble will found.
  276. /// \param [in] pFile - name of file to be parsed.
  277. /// \param [in] pIOHandler - pointer to IO helper object.
  278. void ParseFile(const std::string &pFile, IOSystem *pIOHandler);
  279. bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool pCheckSig) const;
  280. void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler);
  281. const aiImporterDesc *GetInfo() const;
  282. void Clear();
  283. private:
  284. static const aiImporterDesc Description;
  285. X3DNodeElementBase *mNodeElementCur; ///< Current element.
  286. }; // class X3DImporter
  287. } // namespace Assimp
  288. #endif // INCLUDED_AI_X3D_IMPORTER_H