0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / RWGltf / RWGltf_CafWriter.hxx
CommitLineData
01b2f506 1// Copyright (c) 2017-2019 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#ifndef _RWGltf_CafWriter_HeaderFiler
15#define _RWGltf_CafWriter_HeaderFiler
16
17#include <TColStd_IndexedDataMapOfStringString.hxx>
18#include <TColStd_MapOfAsciiString.hxx>
19#include <TDF_LabelSequence.hxx>
20#include <TopTools_ShapeMapHasher.hxx>
21#include <RWGltf_GltfBufferView.hxx>
22#include <RWGltf_GltfFace.hxx>
23#include <RWGltf_WriterTrsfFormat.hxx>
24#include <RWMesh_CoordinateSystemConverter.hxx>
25#include <XCAFPrs_Style.hxx>
26
27#include <memory>
28
29class Message_ProgressIndicator;
30class RWMesh_FaceIterator;
31class RWGltf_GltfOStreamWriter;
32class RWGltf_GltfMaterialMap;
33class RWGltf_GltfSceneNodeMap;
34class TDocStd_Document;
35
36//! glTF writer context from XCAF document.
37class RWGltf_CafWriter : public Standard_Transient
38{
39 DEFINE_STANDARD_RTTIEXT(RWGltf_CafWriter, Standard_Transient)
40public:
41
42 //! Main constructor.
43 //! @param theFile [in] path to output glTF file
44 //! @param theIsBinary [in] flag to write into binary glTF format (.glb)
45 Standard_EXPORT RWGltf_CafWriter (const TCollection_AsciiString& theFile,
46 Standard_Boolean theIsBinary);
47
48 //! Destructor.
49 Standard_EXPORT virtual ~RWGltf_CafWriter();
50
51 //! Return transformation from OCCT to glTF coordinate system.
52 const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCSTrsf; }
53
54 //! Return transformation from OCCT to glTF coordinate system.
55 RWMesh_CoordinateSystemConverter& ChangeCoordinateSystemConverter() { return myCSTrsf; }
56
57 //! Set transformation from OCCT to glTF coordinate system.
58 void SetCoordinateSystemConverter (const RWMesh_CoordinateSystemConverter& theConverter) { myCSTrsf = theConverter; }
59
60 //! Return flag to write into binary glTF format (.glb), specified within class constructor.
61 bool IsBinary() const { return myIsBinary; }
62
63 //! Return preferred transformation format for writing into glTF file; RWGltf_WriterTrsfFormat_Compact by default.
64 RWGltf_WriterTrsfFormat TransformationFormat() const { return myTrsfFormat; }
65
66 //! Set preferred transformation format for writing into glTF file.
67 void SetTransformationFormat (RWGltf_WriterTrsfFormat theFormat) { myTrsfFormat = theFormat; }
68
69 //! Return TRUE to export UV coordinates even if there are no mapped texture; FALSE by default.
70 bool IsForcedUVExport() const { return myIsForcedUVExport; }
71
72 //! Set flag to export UV coordinates even if there are no mapped texture; FALSE by default.
73 void SetForcedUVExport (bool theToForce) { myIsForcedUVExport = theToForce; }
74
75 //! Return default material definition to be used for nodes with only color defined.
76 const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
77
78 //! Set default material definition to be used for nodes with only color defined.
79 void SetDefaultStyle (const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
80
81 //! Write glTF file and associated binary file.
82 //! Triangulation data should be precomputed within shapes!
83 //! @param theDocument [in] input document
84 //! @param theRootLabels [in] list of root shapes to export
85 //! @param theLabelFilter [in] optional filter with document nodes to export,
86 //! with keys defined by XCAFPrs_DocumentExplorer::DefineChildId() and filled recursively
87 //! (leaves and parent assembly nodes at all levels);
88 //! when not NULL, all nodes not included into the map will be ignored
89 //! @param theFileInfo [in] map with file metadata to put into glTF header section
90 //! @param theProgress [in] optional progress indicator
91 //! @return FALSE on file writing failure
92 Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
93 const TDF_LabelSequence& theRootLabels,
94 const TColStd_MapOfAsciiString* theLabelFilter,
95 const TColStd_IndexedDataMapOfStringString& theFileInfo,
96 const Handle(Message_ProgressIndicator)& theProgress);
97
98 //! Write glTF file and associated binary file.
99 //! Triangulation data should be precomputed within shapes!
100 //! @param theDocument [in] input document
101 //! @param theFileInfo [in] map with file metadata to put into glTF header section
102 //! @param theProgress [in] optional progress indicator
103 //! @return FALSE on file writing failure
104 Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
105 const TColStd_IndexedDataMapOfStringString& theFileInfo,
106 const Handle(Message_ProgressIndicator)& theProgress);
107
108protected:
109
110 //! Write binary data file with triangulation data.
111 //! Triangulation data should be precomputed within shapes!
112 //! @param theDocument [in] input document
113 //! @param theRootLabels [in] list of root shapes to export
114 //! @param theLabelFilter [in] optional filter with document nodes to export
115 //! @param theProgress [in] optional progress indicator
116 //! @return FALSE on file writing failure
117 Standard_EXPORT virtual bool writeBinData (const Handle(TDocStd_Document)& theDocument,
118 const TDF_LabelSequence& theRootLabels,
119 const TColStd_MapOfAsciiString* theLabelFilter,
120 const Handle(Message_ProgressIndicator)& theProgress);
121
122 //! Write JSON file with glTF structure (should be called after writeBinData()).
123 //! @param theDocument [in] input document
124 //! @param theRootLabels [in] list of root shapes to export
125 //! @param theLabelFilter [in] optional filter with document nodes to export
126 //! @param theFileInfo [in] map with file metadata to put into glTF header section
127 //! @param theProgress [in] optional progress indicator
128 //! @return FALSE on file writing failure
129 Standard_EXPORT virtual bool writeJson (const Handle(TDocStd_Document)& theDocument,
130 const TDF_LabelSequence& theRootLabels,
131 const TColStd_MapOfAsciiString* theLabelFilter,
132 const TColStd_IndexedDataMapOfStringString& theFileInfo,
133 const Handle(Message_ProgressIndicator)& theProgress);
134
135protected:
136
137 //! Return TRUE if face mesh should be skipped (e.g. because it is invalid or empty).
138 Standard_EXPORT virtual Standard_Boolean toSkipFaceMesh (const RWMesh_FaceIterator& theFaceIter);
139
140 //! Write mesh nodes into binary file.
141 //! @param theGltfFace [out] glTF face definition
142 //! @param theBinFile [out] output file to write into
143 //! @param theFaceIter [in] current face to write
144 //! @param theAccessorNb [in] [out] last accessor index
145 Standard_EXPORT virtual void saveNodes (RWGltf_GltfFace& theGltfFace,
146 std::ostream& theBinFile,
147 const RWMesh_FaceIterator& theFaceIter,
148 Standard_Integer& theAccessorNb) const;
149
150 //! Write mesh normals into binary file.
151 //! @param theGltfFace [out] glTF face definition
152 //! @param theBinFile [out] output file to write into
153 //! @param theFaceIter [in] current face to write
154 //! @param theAccessorNb [in] [out] last accessor index
155 Standard_EXPORT virtual void saveNormals (RWGltf_GltfFace& theGltfFace,
156 std::ostream& theBinFile,
157 RWMesh_FaceIterator& theFaceIter,
158 Standard_Integer& theAccessorNb) const;
159
160 //! Write mesh texture UV coordinates into binary file.
161 //! @param theGltfFace [out] glTF face definition
162 //! @param theBinFile [out] output file to write into
163 //! @param theFaceIter [in] current face to write
164 //! @param theAccessorNb [in] [out] last accessor index
165 Standard_EXPORT virtual void saveTextCoords (RWGltf_GltfFace& theGltfFace,
166 std::ostream& theBinFile,
167 const RWMesh_FaceIterator& theFaceIter,
168 Standard_Integer& theAccessorNb) const;
169
170 //! Write mesh indexes into binary file.
171 //! @param theGltfFace [out] glTF face definition
172 //! @param theBinFile [out] output file to write into
173 //! @param theFaceIter [in] current face to write
174 //! @param theAccessorNb [in] [out] last accessor index
175 Standard_EXPORT virtual void saveIndices (RWGltf_GltfFace& theGltfFace,
176 std::ostream& theBinFile,
177 const RWMesh_FaceIterator& theFaceIter,
178 Standard_Integer& theAccessorNb);
179
180protected:
181
182 //! Write bufferView for vertex positions within RWGltf_GltfRootElement_Accessors section
183 //! @param theGltfFace [in] face definition to write
184 Standard_EXPORT virtual void writePositions (const RWGltf_GltfFace& theGltfFace);
185
186 //! Write bufferView for vertex normals within RWGltf_GltfRootElement_Accessors section
187 //! @param theGltfFace [in] face definition to write
188 Standard_EXPORT virtual void writeNormals (const RWGltf_GltfFace& theGltfFace);
189
190 //! Write bufferView for vertex texture coordinates within RWGltf_GltfRootElement_Accessors section
191 //! @param theGltfFace [in] face definition to write
192 Standard_EXPORT virtual void writeTextCoords (const RWGltf_GltfFace& theGltfFace);
193
194 //! Write bufferView for triangle indexes within RWGltf_GltfRootElement_Accessors section.
195 //! @param theGltfFace [in] face definition to write
196 Standard_EXPORT virtual void writeIndices (const RWGltf_GltfFace& theGltfFace);
197
198protected:
199
200 //! Write RWGltf_GltfRootElement_Accessors section.
201 //! @param theSceneNodeMap [in] ordered map of scene nodes
202 Standard_EXPORT virtual void writeAccessors (const RWGltf_GltfSceneNodeMap& theSceneNodeMap);
203
204 //! Write RWGltf_GltfRootElement_Animations section (reserved).
205 Standard_EXPORT virtual void writeAnimations();
206
207 //! Write RWGltf_GltfRootElement_Asset section.
208 //! @param theFileInfo [in] optional metadata to write into file header
209 Standard_EXPORT virtual void writeAsset (const TColStd_IndexedDataMapOfStringString& theFileInfo);
210
211 //! Write RWGltf_GltfRootElement_BufferViews section.
212 //! @param theBinDataBufferId [in] index of binary buffer with vertex data
213 Standard_EXPORT virtual void writeBufferViews (const Standard_Integer theBinDataBufferId);
214
215 //! Write RWGltf_GltfRootElement_Buffers section.
216 Standard_EXPORT virtual void writeBuffers();
217
218 //! Write RWGltf_GltfRootElement_ExtensionsUsed/RWGltf_GltfRootElement_ExtensionsRequired sections (reserved).
219 Standard_EXPORT virtual void writeExtensions();
220
221 //! Write RWGltf_GltfRootElement_Images section.
222 //! @param theSceneNodeMap [in] ordered map of scene nodes
223 //! @param theMaterialMap [out] map of materials, filled with image files used by textures
224 Standard_EXPORT virtual void writeImages (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
225 RWGltf_GltfMaterialMap& theMaterialMap);
226
227 //! Write RWGltf_GltfRootElement_Materials section.
228 //! @param theSceneNodeMap [in] ordered map of scene nodes
229 //! @param theMaterialMap [out] map of materials, filled with materials
230 Standard_EXPORT virtual void writeMaterials (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
231 RWGltf_GltfMaterialMap& theMaterialMap);
232
233 //! Write RWGltf_GltfRootElement_Meshes section.
234 //! @param theSceneNodeMap [in] ordered map of scene nodes
235 //! @param theMaterialMap [in] map of materials
236 Standard_EXPORT virtual void writeMeshes (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
237 const RWGltf_GltfMaterialMap& theMaterialMap);
238
239 //! Write RWGltf_GltfRootElement_Nodes section.
240 //! @param theDocument [in] input document
241 //! @param theRootLabels [in] list of root shapes to export
242 //! @param theLabelFilter [in] optional filter with document nodes to export
243 //! @param theSceneNodeMap [in] ordered map of scene nodes
244 //! @param theSceneRootNodeInds [out] sequence of scene nodes pointing to root shapes (to be used for writeScenes())
245 Standard_EXPORT virtual void writeNodes (const Handle(TDocStd_Document)& theDocument,
246 const TDF_LabelSequence& theRootLabels,
247 const TColStd_MapOfAsciiString* theLabelFilter,
248 const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
249 NCollection_Sequence<Standard_Integer>& theSceneRootNodeInds);
250
251 //! Write RWGltf_GltfRootElement_Samplers section.
252 Standard_EXPORT virtual void writeSamplers (const RWGltf_GltfMaterialMap& theMaterialMap);
253
254 //! Write RWGltf_GltfRootElement_Scene section.
255 //! @param theDefSceneId [in] index of default scene (0)
256 Standard_EXPORT virtual void writeScene (const Standard_Integer theDefSceneId);
257
258 //! Write RWGltf_GltfRootElement_Scenes section.
259 //! @param theSceneRootNodeInds [in] sequence of scene nodes pointing to root shapes
260 Standard_EXPORT virtual void writeScenes (const NCollection_Sequence<Standard_Integer>& theSceneRootNodeInds);
261
262 //! Write RWGltf_GltfRootElement_Skins section (reserved).
263 Standard_EXPORT virtual void writeSkins();
264
265 //! Write RWGltf_GltfRootElement_Textures section.
266 //! @param theSceneNodeMap [in] ordered map of scene nodes
267 //! @param theMaterialMap [out] map of materials, filled with textures
268 Standard_EXPORT virtual void writeTextures (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
269 RWGltf_GltfMaterialMap& theMaterialMap);
270
271protected:
272
273 TCollection_AsciiString myFile; //!< output glTF file
274 TCollection_AsciiString myBinFileNameFull; //!< output file with binary data (full path)
275 TCollection_AsciiString myBinFileNameShort; //!< output file with binary data (short path)
276 RWGltf_WriterTrsfFormat myTrsfFormat; //!< transformation format to write into glTF file
277 Standard_Boolean myIsBinary; //!< flag to write into binary glTF format (.glb)
278 Standard_Boolean myIsForcedUVExport; //!< export UV coordinates even if there are no mapped texture
279 RWMesh_CoordinateSystemConverter myCSTrsf; //!< transformation from OCCT to glTF coordinate system
280 XCAFPrs_Style myDefaultStyle; //!< default material definition to be used for nodes with only color defined
281
282 opencascade::std::shared_ptr<RWGltf_GltfOStreamWriter>
283 myWriter; //!< JSON writer
284 RWGltf_GltfBufferView myBuffViewPos; //!< current buffer view with nodes positions
285 RWGltf_GltfBufferView myBuffViewNorm; //!< current buffer view with nodes normals
286 RWGltf_GltfBufferView myBuffViewTextCoord; //!< current buffer view with nodes UV coordinates
287 RWGltf_GltfBufferView myBuffViewInd; //!< current buffer view with triangulation indexes
288 NCollection_DataMap<TopoDS_Shape, RWGltf_GltfFace,
289 TopTools_ShapeMapHasher> myBinDataMap; //!< map for TopoDS_Face to glTF face (merging duplicates)
290 int64_t myBinDataLen64; //!< length of binary file
291
292};
293
294#endif // _RWGltf_CafWriter_HeaderFiler