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_Triangulation.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopoDS_Face.hxx>
23 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
24 #include <XCAFPrs_Style.hxx>
30 //! Auxiliary class to iterate through triangulated faces.
31 class RWMesh_FaceIterator
36 Standard_EXPORT RWMesh_FaceIterator (const TDF_Label& theLabel,
37 const TopLoc_Location& theLocation,
38 const Standard_Boolean theToMapColors = false,
39 const XCAFPrs_Style& theStyle = XCAFPrs_Style());
41 //! Auxiliary constructor.
42 Standard_EXPORT RWMesh_FaceIterator (const TopoDS_Shape& theShape,
43 const XCAFPrs_Style& theStyle = XCAFPrs_Style());
45 //! Return explored shape.
46 const TopoDS_Shape& ExploredShape() const { return myFaceIter.ExploredShape(); }
48 //! Return true if iterator points to the valid triangulation.
49 bool More() const { return !myPolyTriang.IsNull(); }
52 Standard_EXPORT void Next();
54 //! Return current face.
55 const TopoDS_Face& Face() const { return myFace; }
57 //! Return current face triangulation.
58 const Handle(Poly_Triangulation)& Triangulation() const { return myPolyTriang; }
60 //! Return true if mesh data is defined.
61 bool IsEmptyMesh() const
63 return myPolyTriang.IsNull()
64 || (myPolyTriang->NbNodes() < 1 && myPolyTriang->NbTriangles() < 1);
69 //! Return face material.
70 const XCAFPrs_Style& FaceStyle() const { return myFaceStyle; }
72 //! Return TRUE if face color is set.
73 bool HasFaceColor() const { return myHasFaceColor; }
75 //! Return face color.
76 const Quantity_ColorRGBA& FaceColor() const { return myFaceColor; }
80 //! Return number of elements of specific type for the current face.
81 Standard_Integer NbTriangles() const { return myPolyTriang->NbTriangles(); }
83 //! Lower element index in current triangulation.
84 Standard_Integer ElemLower() const { return 1; }
86 //! Upper element index in current triangulation.
87 Standard_Integer ElemUpper() const { return myPolyTriang->NbTriangles(); }
89 //! Return triangle with specified index with applied Face orientation.
90 Poly_Triangle TriangleOriented (Standard_Integer theElemIndex) const
92 Poly_Triangle aTri = triangle (theElemIndex);
93 if ((myFace.Orientation() == TopAbs_REVERSED) ^ myIsMirrored)
95 return Poly_Triangle (aTri.Value (1), aTri.Value (3), aTri.Value (2));
102 //! Return true if triangulation has defined normals.
103 bool HasNormals() const { return myHasNormals; }
105 //! Return true if triangulation has defined normals.
106 bool HasTexCoords() const { return !myPolyTriang.IsNull() && myPolyTriang->HasUVNodes(); }
108 //! Return normal at specified node index with face transformation applied and face orientation applied.
109 gp_Dir NormalTransformed (Standard_Integer theNode) const
111 gp_Dir aNorm = normal (theNode);
112 if (myTrsf.Form() != gp_Identity)
114 aNorm.Transform (myTrsf);
116 if (myFace.Orientation() == TopAbs_REVERSED)
123 //! Return number of nodes for the current face.
124 Standard_Integer NbNodes() const
126 return !myPolyTriang.IsNull()
127 ? myPolyTriang->NbNodes()
131 //! Lower node index in current triangulation.
132 Standard_Integer NodeLower() const { return 1; }
134 //! Upper node index in current triangulation.
135 Standard_Integer NodeUpper() const { return myPolyTriang->NbNodes(); }
137 //! Return the node with specified index with applied transformation.
138 gp_Pnt NodeTransformed (const Standard_Integer theNode) const
140 gp_Pnt aNode = node (theNode);
141 aNode.Transform (myTrsf);
145 //! Return texture coordinates for the node.
146 gp_Pnt2d NodeTexCoord (const Standard_Integer theNode) const
148 return myPolyTriang->HasUVNodes() ? myPolyTriang->UVNode (theNode) : gp_Pnt2d();
153 //! Return the node with specified index with applied transformation.
154 gp_Pnt node (const Standard_Integer theNode) const { return myPolyTriang->Node (theNode); }
156 //! Return normal at specified node index without face transformation applied.
157 Standard_EXPORT gp_Dir normal (Standard_Integer theNode) const;
159 //! Return triangle with specified index.
160 Poly_Triangle triangle (Standard_Integer theElemIndex) const { return myPolyTriang->Triangle (theElemIndex); }
164 //! Dispatch face styles.
165 void dispatchStyles (const TDF_Label& theLabel,
166 const TopLoc_Location& theLocation,
167 const XCAFPrs_Style& theStyle);
169 //! Reset information for current face.
172 myPolyTriang.Nullify();
174 myHasNormals = false;
175 myHasFaceColor = false;
176 myFaceColor = Quantity_ColorRGBA();
177 myFaceStyle = XCAFPrs_Style();
180 //! Initialize face properties.
185 NCollection_DataMap<TopoDS_Shape, XCAFPrs_Style, TopTools_ShapeMapHasher>
186 myStyles; //!< Face -> Style map
187 XCAFPrs_Style myDefStyle; //!< default style for faces without dedicated style
188 Standard_Boolean myToMapColors; //!< flag to dispatch styles
190 TopExp_Explorer myFaceIter; //!< face explorer
191 TopoDS_Face myFace; //!< current face
192 Handle(Poly_Triangulation) myPolyTriang; //!< triangulation of current face
193 TopLoc_Location myFaceLocation; //!< current face location
194 mutable BRepLProp_SLProps mySLTool; //!< auxiliary tool for fetching normals from surface
195 BRepAdaptor_Surface myFaceAdaptor; //!< surface adaptor for fetching normals from surface
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