Coverage Report

Created: 2026-01-25 07:15

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