0029325: Modeling Algorithms - add tool BRepLib_PointCloudShape for generation point...
[occt.git] / src / RWPly / RWPly_PlyWriterContext.hxx
1 // Copyright (c) 2022 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 _RWPly_PlyWriterContext_HeaderFiler
15 #define _RWPly_PlyWriterContext_HeaderFiler
16
17 #include <Graphic3d_Vec.hxx>
18 #include <gp_Pnt.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TColStd_IndexedDataMapOfStringString.hxx>
21
22 #include <memory>
23
24 //! Auxiliary low-level tool writing PLY file.
25 class RWPly_PlyWriterContext
26 {
27 public:
28
29   //! Empty constructor.
30   Standard_EXPORT RWPly_PlyWriterContext();
31
32   //! Destructor, will emit error message if file was not closed.
33   Standard_EXPORT ~RWPly_PlyWriterContext();
34
35 public: //! @name vertex attributes parameters
36
37   //! Return TRUE if vertex position should be stored with double floating point precision; FALSE by default.
38   bool IsDoublePrecision() const { return myIsDoublePrec; }
39
40   //! Set if vertex position should be stored with double floating point precision.
41   void SetDoublePrecision (bool theDoublePrec) { myIsDoublePrec = theDoublePrec; }
42
43   //! Return TRUE if normals should be written as vertex attribute; FALSE by default.
44   bool HasNormals() const { return myHasNormals; }
45
46   //! Set if normals should be written.
47   void SetNormals (const bool theHasNormals) { myHasNormals = theHasNormals; }
48
49   //! Return TRUE if UV / texture coordinates should be written as vertex attribute; FALSE by default.
50   bool HasTexCoords() const { return myHasTexCoords; }
51
52   //! Set if UV / texture coordinates should be written.
53   void SetTexCoords (const bool theHasTexCoords) { myHasTexCoords = theHasTexCoords; }
54
55   //! Return TRUE if point colors should be written as vertex attribute; FALSE by default.
56   bool HasColors() const { return myHasColors; }
57
58   //! Set if point colors should be written.
59   void SetColors (bool theToWrite) { myHasColors = theToWrite; }
60
61 public: //! @name element attributes parameters
62
63   //! Return TRUE if surface Id should be written as element attribute; FALSE by default.
64   bool HasSurfaceId() const { return myHasSurfId; }
65
66   //! Set if surface Id should be written as element attribute; FALSE by default.
67   void SetSurfaceId (bool theSurfId) { myHasSurfId = theSurfId; }
68
69 public: //! @name writing into file
70
71   //! Return TRUE if file has been opened.
72   bool IsOpened() const { return myStream.get() != nullptr; }
73
74   //! Open file for writing.
75   Standard_EXPORT bool Open (const TCollection_AsciiString& theName,
76                              const std::shared_ptr<std::ostream>& theStream = std::shared_ptr<std::ostream>());
77
78   //! Write the header.
79   //! @param[in] theNbNodes number of vertex nodes
80   //! @param[in] theNbElems number of mesh elements
81   //! @param[in] theFileInfo optional comments
82   Standard_EXPORT bool WriteHeader (const Standard_Integer theNbNodes,
83                                     const Standard_Integer theNbElems,
84                                     const TColStd_IndexedDataMapOfStringString& theFileInfo);
85
86   //! Write single point with all attributes.
87   //! @param[in] thePoint 3D point coordinates
88   //! @param[in] theNorm  surface normal direction at the point
89   //! @param[in] theUV    surface/texture UV coordinates
90   //! @param[in] theColor RGB color values
91   Standard_EXPORT bool WriteVertex (const gp_Pnt& thePoint,
92                                     const Graphic3d_Vec3& theNorm,
93                                     const Graphic3d_Vec2& theUV,
94                                     const Graphic3d_Vec4ub& theColor);
95
96   //! Return number of written vertices.
97   Standard_Integer NbWrittenVertices() const { return myNbVerts; }
98
99   //! Return vertex offset to be applied to element indices; 0 by default.
100   Standard_Integer VertexOffset() const { return myVertOffset; }
101
102   //! Set vertex offset to be applied to element indices.
103   void SetVertexOffset (Standard_Integer theOffset) { myVertOffset = theOffset; }
104
105   //! Return surface id to write with element; 0 by default.
106   Standard_Integer SurfaceId() const { return mySurfId; }
107
108   //! Set surface id to write with element.
109   void SetSurfaceId (Standard_Integer theSurfId) { mySurfId = theSurfId; }
110
111   //! Writing a triangle.
112   Standard_EXPORT bool WriteTriangle (const Graphic3d_Vec3i& theTri);
113
114   //! Writing a quad.
115   Standard_EXPORT bool WriteQuad (const Graphic3d_Vec4i& theQuad);
116
117   //! Return number of written elements.
118   Standard_Integer NbWrittenElements() const { return myNbElems; }
119
120   //! Correctly close the file.
121   //! @return FALSE in case of writing error
122   Standard_EXPORT bool Close (bool theIsAborted = false);
123
124 private:
125
126   std::shared_ptr<std::ostream> myStream;
127   TCollection_AsciiString myName;
128   Standard_Integer myNbHeaderVerts;
129   Standard_Integer myNbHeaderElems;
130   Standard_Integer myNbVerts;
131   Standard_Integer myNbElems;
132   Standard_Integer mySurfId;
133   Standard_Integer myVertOffset;
134   bool myIsDoublePrec;
135   bool myHasNormals;
136   bool myHasColors;
137   bool myHasTexCoords;
138   bool myHasSurfId;
139
140 };
141
142 #endif // _RWPly_PlyWriterContext_HeaderFiler