1 // Copyright (c) 2017-2019 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 _RWMesh_FaceIterator_HeaderFile
15 #define _RWMesh_FaceIterator_HeaderFile
17 #include <BRepLProp_SLProps.hxx>
18 #include <gp_Trsf.hxx>
19 #include <NCollection_DataMap.hxx>
20 #include <Poly_Array1OfTriangle.hxx>
21 #include <Poly_Triangulation.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <TopoDS_Face.hxx>
24 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
25 #include <XCAFPrs_Style.hxx>
31 //! Auxiliary class to iterate through triangulated faces.
32 class RWMesh_FaceIterator
37 Standard_EXPORT RWMesh_FaceIterator (const TDF_Label& theLabel,
38 const TopLoc_Location& theLocation,
39 const Standard_Boolean theToMapColors = false,
40 const XCAFPrs_Style& theStyle = XCAFPrs_Style());
42 //! Return true if iterator points to the valid triangulation.
43 bool More() const { return !myPolyTriang.IsNull(); }
46 Standard_EXPORT void Next();
48 //! Return current face.
49 const TopoDS_Face& Face() const { return myFace; }
51 //! Return current face triangulation.
52 const Handle(Poly_Triangulation)& Triangulation() const { return myPolyTriang; }
54 //! Return true if mesh data is defined.
55 bool IsEmptyMesh() const
57 return myPolyTriang.IsNull()
58 || (myPolyTriang->NbNodes() < 1 && myPolyTriang->NbTriangles() < 1);
63 //! Return face material.
64 const XCAFPrs_Style& FaceStyle() const { return myFaceStyle; }
66 //! Return TRUE if face color is set.
67 bool HasFaceColor() const { return myHasFaceColor; }
69 //! Return face color.
70 const Quantity_ColorRGBA& FaceColor() const { return myFaceColor; }
74 //! Return number of elements of specific type for the current face.
75 Standard_Integer NbTriangles() const { return myPolyTriang->NbTriangles(); }
77 //! Lower element index in current triangulation.
78 Standard_Integer ElemLower() const { return myPolyTriang->Triangles().Lower(); }
80 //! Upper element index in current triangulation.
81 Standard_Integer ElemUpper() const { return myPolyTriang->Triangles().Upper(); }
83 //! Return triangle with specified index with applied Face orientation.
84 Poly_Triangle TriangleOriented (Standard_Integer theElemIndex) const
86 Poly_Triangle aTri = triangle (theElemIndex);
87 if ((myFace.Orientation() == TopAbs_REVERSED) ^ myIsMirrored)
89 return Poly_Triangle (aTri.Value (1), aTri.Value (3), aTri.Value (2));
96 //! Return true if triangulation has defined normals.
97 bool HasNormals() const { return myHasNormals; }
99 //! Return true if triangulation has defined normals.
100 bool HasTexCoords() const { return myNodeUVs != NULL; }
102 //! Return normal at specified node index with face transformation applied and face orientation applied.
103 gp_Dir NormalTransformed (Standard_Integer theNode)
105 gp_Dir aNorm = normal (theNode);
106 if (myTrsf.Form() != gp_Identity)
108 aNorm.Transform (myTrsf);
110 if (myFace.Orientation() == TopAbs_REVERSED)
117 //! Return number of nodes for the current face.
118 Standard_Integer NbNodes() const
120 return !myPolyTriang.IsNull()
121 ? myPolyTriang->Nodes().Length()
125 //! Lower node index in current triangulation.
126 Standard_Integer NodeLower() const { return myPolyTriang->Nodes().Lower(); }
128 //! Upper node index in current triangulation.
129 Standard_Integer NodeUpper() const { return myPolyTriang->Nodes().Upper(); }
131 //! Return the node with specified index with applied transformation.
132 gp_Pnt NodeTransformed (const Standard_Integer theNode) const
134 gp_Pnt aNode = node (theNode);
135 aNode.Transform (myTrsf);
139 //! Return texture coordinates for the node.
140 gp_Pnt2d NodeTexCoord (const Standard_Integer theNode) const
142 return myNodeUVs != NULL ? myNodeUVs->Value (theNode) : gp_Pnt2d();
147 //! Return the node with specified index with applied transformation.
148 gp_Pnt node (const Standard_Integer theNode) const { return myPolyTriang->Nodes().Value (theNode); }
150 //! Return normal at specified node index without face transformation applied.
151 Standard_EXPORT gp_Dir normal (Standard_Integer theNode);
153 //! Return triangle with specified index.
154 Poly_Triangle triangle (Standard_Integer theElemIndex) const { return myPolyTriang->Triangles().Value (theElemIndex); }
158 //! Dispatch face styles.
159 void dispatchStyles (const TDF_Label& theLabel,
160 const TopLoc_Location& theLocation,
161 const XCAFPrs_Style& theStyle);
163 //! Reset information for current face.
166 myPolyTriang.Nullify();
171 myHasNormals = false;
172 myHasFaceColor = false;
173 myFaceColor = Quantity_ColorRGBA();
174 myFaceStyle = XCAFPrs_Style();
177 //! Initialize face properties.
182 NCollection_DataMap<TopoDS_Shape, XCAFPrs_Style, TopTools_ShapeMapHasher>
183 myStyles; //!< Face -> Style map
184 XCAFPrs_Style myDefStyle; //!< default style for faces without dedicated style
185 Standard_Boolean myToMapColors; //!< flag to dispatch styles
187 TopExp_Explorer myFaceIter; //!< face explorer
188 TopoDS_Face myFace; //!< current face
189 Handle(Poly_Triangulation) myPolyTriang; //!< triangulation of current face
190 TopLoc_Location myFaceLocation; //!< current face location
191 BRepLProp_SLProps mySLTool; //!< auxiliary tool for fetching normals from surface
192 BRepAdaptor_Surface myFaceAdaptor; //!< surface adaptor for fetching normals from surface
193 const TColgp_Array1OfPnt* myNodes; //!< node positions of current face
194 const TShort_Array1OfShortReal* myNormals; //!< node normals of current face
195 const TColgp_Array1OfPnt2d* myNodeUVs; //!< node UV coordinates of current face
196 Standard_Boolean myHasNormals; //!< flag indicating that current face has normals
197 gp_Trsf myTrsf; //!< current face transformation
198 Standard_Boolean myIsMirrored; //!< flag indicating that face triangles should be mirrored
199 XCAFPrs_Style myFaceStyle; //!< current face style
200 Quantity_ColorRGBA myFaceColor; //!< current face color
201 Standard_Boolean myHasFaceColor; //!< flag indicating that current face has assigned color
205 #endif // _RWMesh_FaceIterator_HeaderFile