0031332: Data Exchange - RWGltf_CafReader ignores bufferView.byteStride within glTF 2.0
[occt.git] / src / RWGltf / RWGltf_TriangulationReader.hxx
1 // Author: Kirill Gavrilov
2 // Copyright (c) 2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _RWGltf_TriangulationReader_HeaderFile
16 #define _RWGltf_TriangulationReader_HeaderFile
17
18 #include <RWGltf_PrimitiveArrayReader.hxx>
19
20 //! RWGltf_PrimitiveArrayReader implementation creating Poly_Triangulation.
21 class RWGltf_TriangulationReader : public RWGltf_PrimitiveArrayReader
22 {
23   DEFINE_STANDARD_RTTIEXT(RWGltf_TriangulationReader, RWGltf_PrimitiveArrayReader)
24 public:
25
26   //! Empty constructor.
27   Standard_EXPORT RWGltf_TriangulationReader();
28
29 protected:
30
31   //! Create Poly_Triangulation from collected data
32   Standard_EXPORT virtual Handle(Poly_Triangulation) result() Standard_OVERRIDE;
33
34   //! Reset cache before loading primitive array.
35   Standard_EXPORT virtual void reset() Standard_OVERRIDE;
36
37   //! Fill triangulation data and ignore non-triangulation primitives.
38   //! @param theStream   input stream to read from
39   //! @param theName     entity name for logging errors
40   //! @param theAccessor buffer accessor
41   //! @param theType     array type
42   //! @param theMode     primitive mode
43   //! @return FALSE on error
44   Standard_EXPORT virtual bool readBuffer (std::istream& theStream,
45                                            const TCollection_AsciiString& theName,
46                                            const RWGltf_GltfAccessor& theAccessor,
47                                            RWGltf_GltfArrayType theType,
48                                            RWGltf_GltfPrimitiveMode theMode) Standard_OVERRIDE;
49
50 protected: //! @name interface for filling triangulation data
51
52   //! Resize array of position nodes to specified size.
53   virtual bool setNbPositionNodes (Standard_Integer theNbNodes)
54   {
55     if (theNbNodes <= 0)
56     {
57       return false;
58     }
59     myTriangulation->ChangeNodes().Resize (1, theNbNodes, false);
60     return true;
61   }
62
63   //! Set node position.
64   //! @param theIndex node index starting from 1
65   //! @param thePnt   node position
66   virtual void setNodePosition (Standard_Integer theIndex,
67                                 const gp_Pnt& thePnt)
68   {
69     myTriangulation->ChangeNode (theIndex) = thePnt;
70   }
71
72   //! Resize array of UV nodes to specified size.
73   virtual bool setNbUVNodes (Standard_Integer theNbNodes)
74   {
75     if (theNbNodes <= 0
76      || myTriangulation->NbNodes() != theNbNodes)
77     {
78       return false;
79     }
80     myTriangulation->ChangeUVNodes().Resize (1, theNbNodes, false);
81     return true;
82   }
83
84   //! Set node UV texture coordinates.
85   //! @param theIndex node index starting from 1
86   //! @param theUV    node UV coordinates
87   virtual void setNodeUV (Standard_Integer theIndex,
88                           const gp_Pnt2d& theUV)
89   {
90     myTriangulation->ChangeUVNode (theIndex) = theUV;
91   }
92
93   //! Resize array of nodes normals to specified size.
94   virtual bool setNbNormalNodes (Standard_Integer theNbNodes)
95   {
96     if (theNbNodes <= 0
97      || myTriangulation->NbNodes() != theNbNodes)
98     {
99       return false;
100     }
101     myTriangulation->SetNormals (new TShort_HArray1OfShortReal (1, theNbNodes * 3));
102     return true;
103   }
104
105   //! Set node normal.
106   //! @param theIndex  node index starting from 1
107   //! @param theNormal node normal
108   virtual void setNodeNormal (Standard_Integer theIndex,
109                               const gp_Dir& theNormal)
110   {
111     myTriangulation->SetNormal (theIndex, theNormal);
112   }
113
114   //! Resize array of triangles to specified size.
115   virtual bool setNbTriangles (Standard_Integer theNbTris)
116   {
117     if (theNbTris >= 1)
118     {
119       myTriangulation->ChangeTriangles().Resize (1, theNbTris, false);
120       return true;
121     }
122     return false;
123   }
124
125   //! Add triangle element.
126   //! @param theIndex    triangle index starting from 1
127   //! @param theTriangle triangle nodes starting from 1
128   //! @return FALSE if node indexes are out of range
129   virtual bool setTriangle (Standard_Integer theIndex,
130                             const Poly_Triangle& theTriangle)
131   {
132     if (theTriangle.Value (1) < myTriangulation->Nodes().Lower() || theTriangle.Value (1) > myTriangulation->Nodes().Upper()
133      || theTriangle.Value (2) < myTriangulation->Nodes().Lower() || theTriangle.Value (2) > myTriangulation->Nodes().Upper()
134      || theTriangle.Value (3) < myTriangulation->Nodes().Lower() || theTriangle.Value (3) > myTriangulation->Nodes().Upper())
135     {
136       return false;
137     }
138     myTriangulation->ChangeTriangle (theIndex) = theTriangle;
139     return true;
140   }
141
142 protected:
143
144   Handle(Poly_Triangulation) myTriangulation;
145
146 };
147
148 #endif // _RWGltf_TriangulationReader_HeaderFile