1 // Copyright (c) 2022 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _RWPly_PlyWriterContext_HeaderFiler
15 #define _RWPly_PlyWriterContext_HeaderFiler
17 #include <Graphic3d_Vec.hxx>
19 #include <TCollection_AsciiString.hxx>
20 #include <TColStd_IndexedDataMapOfStringString.hxx>
24 //! Auxiliary low-level tool writing PLY file.
25 class RWPly_PlyWriterContext
29 //! Empty constructor.
30 Standard_EXPORT RWPly_PlyWriterContext();
32 //! Destructor, will emit error message if file was not closed.
33 Standard_EXPORT ~RWPly_PlyWriterContext();
35 public: //! @name vertex attributes parameters
37 //! Return TRUE if vertex position should be stored with double floating point precision; FALSE by default.
38 bool IsDoublePrecision() const { return myIsDoublePrec; }
40 //! Set if vertex position should be stored with double floating point precision.
41 void SetDoublePrecision (bool theDoublePrec) { myIsDoublePrec = theDoublePrec; }
43 //! Return TRUE if normals should be written as vertex attribute; FALSE by default.
44 bool HasNormals() const { return myHasNormals; }
46 //! Set if normals should be written.
47 void SetNormals (const bool theHasNormals) { myHasNormals = theHasNormals; }
49 //! Return TRUE if UV / texture coordinates should be written as vertex attribute; FALSE by default.
50 bool HasTexCoords() const { return myHasTexCoords; }
52 //! Set if UV / texture coordinates should be written.
53 void SetTexCoords (const bool theHasTexCoords) { myHasTexCoords = theHasTexCoords; }
55 //! Return TRUE if point colors should be written as vertex attribute; FALSE by default.
56 bool HasColors() const { return myHasColors; }
58 //! Set if point colors should be written.
59 void SetColors (bool theToWrite) { myHasColors = theToWrite; }
61 public: //! @name element attributes parameters
63 //! Return TRUE if surface Id should be written as element attribute; FALSE by default.
64 bool HasSurfaceId() const { return myHasSurfId; }
66 //! Set if surface Id should be written as element attribute; FALSE by default.
67 void SetSurfaceId (bool theSurfId) { myHasSurfId = theSurfId; }
69 public: //! @name writing into file
71 //! Return TRUE if file has been opened.
72 bool IsOpened() const { return myStream.get() != nullptr; }
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>());
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);
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);
96 //! Return number of written vertices.
97 Standard_Integer NbWrittenVertices() const { return myNbVerts; }
99 //! Return vertex offset to be applied to element indices; 0 by default.
100 Standard_Integer VertexOffset() const { return myVertOffset; }
102 //! Set vertex offset to be applied to element indices.
103 void SetVertexOffset (Standard_Integer theOffset) { myVertOffset = theOffset; }
105 //! Return surface id to write with element; 0 by default.
106 Standard_Integer SurfaceId() const { return mySurfId; }
108 //! Set surface id to write with element.
109 void SetSurfaceId (Standard_Integer theSurfId) { mySurfId = theSurfId; }
111 //! Writing a triangle.
112 Standard_EXPORT bool WriteTriangle (const Graphic3d_Vec3i& theTri);
115 Standard_EXPORT bool WriteQuad (const Graphic3d_Vec4i& theQuad);
117 //! Return number of written elements.
118 Standard_Integer NbWrittenElements() const { return myNbElems; }
120 //! Correctly close the file.
121 //! @return FALSE in case of writing error
122 Standard_EXPORT bool Close (bool theIsAborted = false);
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;
142 #endif // _RWPly_PlyWriterContext_HeaderFiler