Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-11 09:18:58

0001 // Copyright (c) 2017-2019 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _RWMesh_FaceIterator_HeaderFile
0015 #define _RWMesh_FaceIterator_HeaderFile
0016 
0017 #include <RWMesh_ShapeIterator.hxx>
0018 
0019 #include <BRepLProp_SLProps.hxx>
0020 #include <Poly_Triangulation.hxx>
0021 #include <TopoDS_Face.hxx>
0022 
0023 #include <algorithm>
0024 
0025 class TDF_Label;
0026 
0027 //! Auxiliary class to iterate through triangulated faces.
0028 //! Class is designed to provide an interface for iterating over the faces
0029 //! of a shape, specifically focusing on triangulated faces.
0030 //! It inherits from the `RWMesh_ShapeIterator` base class and
0031 //! extends its functionality to handle faces with triangulation data.
0032 class RWMesh_FaceIterator : public RWMesh_ShapeIterator
0033 {
0034 public:
0035   //! Main constructor.
0036   //! @param[in] theLabel Label containing the face data
0037   //! @param[in] theLocation Location of the face
0038   //! @param[in] theToMapColors Flag to indicate if colors should be mapped
0039   //! @param[in] theStyle Style information for the face
0040   Standard_EXPORT RWMesh_FaceIterator(const TDF_Label&       theLabel,
0041                                       const TopLoc_Location& theLocation,
0042                                       const bool             theToMapColors = false,
0043                                       const XCAFPrs_Style&   theStyle       = XCAFPrs_Style());
0044 
0045   //! Auxiliary constructor.
0046   //! @param[in] theShape Shape containing the face data
0047   //! @param[in] theStyle Style information for the face
0048   Standard_EXPORT RWMesh_FaceIterator(const TopoDS_Shape&  theShape,
0049                                       const XCAFPrs_Style& theStyle = XCAFPrs_Style());
0050 
0051   //! Return true if iterator points to the valid triangulation.
0052   bool More() const override { return !myPolyTriang.IsNull(); }
0053 
0054   //! Find next value.
0055   Standard_EXPORT void Next() override;
0056 
0057   //! Return current face.
0058   const TopoDS_Face& Face() const { return myFace; }
0059 
0060   //! Return current face.
0061   const TopoDS_Shape& Shape() const override { return myFace; }
0062 
0063   //! Return current face triangulation.
0064   const occ::handle<Poly_Triangulation>& Triangulation() const { return myPolyTriang; }
0065 
0066   //! Return true if mesh data is defined.
0067   bool IsEmptyMesh() const { return IsEmpty(); }
0068 
0069   //! Return true if mesh data is defined.
0070   bool IsEmpty() const override
0071   {
0072     return myPolyTriang.IsNull()
0073            || (myPolyTriang->NbNodes() < 1 && myPolyTriang->NbTriangles() < 1);
0074   }
0075 
0076 public:
0077   //! Return face material.
0078   const XCAFPrs_Style& FaceStyle() const { return myStyle; }
0079 
0080   //! Return TRUE if face color is set.
0081   bool HasFaceColor() const { return myHasColor; }
0082 
0083   //! Return face color.
0084   const Quantity_ColorRGBA& FaceColor() const { return myColor; }
0085 
0086 public:
0087   //! Return number of elements of specific type for the current face.
0088   int NbTriangles() const { return myPolyTriang->NbTriangles(); }
0089 
0090   //! Lower element index in current triangulation.
0091   int ElemLower() const override { return 1; }
0092 
0093   //! Upper element index in current triangulation.
0094   int ElemUpper() const override { return myPolyTriang->NbTriangles(); }
0095 
0096   //! Return triangle with specified index with applied Face orientation.
0097   Poly_Triangle TriangleOriented(int theElemIndex) const
0098   {
0099     Poly_Triangle aTri = triangle(theElemIndex);
0100     if ((myFace.Orientation() == TopAbs_REVERSED) ^ myIsMirrored)
0101     {
0102       return Poly_Triangle(aTri.Value(1), aTri.Value(3), aTri.Value(2));
0103     }
0104     return aTri;
0105   }
0106 
0107 public:
0108   //! Return true if triangulation has defined normals.
0109   bool HasNormals() const { return myHasNormals; }
0110 
0111   //! Return true if triangulation has defined normals.
0112   bool HasTexCoords() const { return !myPolyTriang.IsNull() && myPolyTriang->HasUVNodes(); }
0113 
0114   //! Return normal at specified node index with face transformation applied and face orientation
0115   //! applied.
0116   gp_Dir NormalTransformed(int theNode) const
0117   {
0118     gp_Dir aNorm = normal(theNode);
0119     if (myTrsf.Form() != gp_Identity)
0120     {
0121       aNorm.Transform(myTrsf);
0122     }
0123     if (myFace.Orientation() == TopAbs_REVERSED)
0124     {
0125       aNorm.Reverse();
0126     }
0127     return aNorm;
0128   }
0129 
0130   //! Return number of nodes for the current face.
0131   int NbNodes() const override { return !myPolyTriang.IsNull() ? myPolyTriang->NbNodes() : 0; }
0132 
0133   //! Lower node index in current triangulation.
0134   int NodeLower() const override { return 1; }
0135 
0136   //! Upper node index in current triangulation.
0137   int NodeUpper() const override { return myPolyTriang->NbNodes(); }
0138 
0139   //! Return texture coordinates for the node.
0140   gp_Pnt2d NodeTexCoord(const int theNode) const
0141   {
0142     return myPolyTriang->HasUVNodes() ? myPolyTriang->UVNode(theNode) : gp_Pnt2d();
0143   }
0144 
0145 public:
0146   //! Return the node with specified index with applied transformation.
0147   gp_Pnt node(const int theNode) const override { return myPolyTriang->Node(theNode); }
0148 
0149   //! Return normal at specified node index without face transformation applied.
0150   Standard_EXPORT gp_Dir normal(int theNode) const;
0151 
0152   //! Return triangle with specified index.
0153   Poly_Triangle triangle(int theElemIndex) const { return myPolyTriang->Triangle(theElemIndex); }
0154 
0155 private:
0156   //! Reset information for current face.
0157   void resetFace()
0158   {
0159     myPolyTriang.Nullify();
0160     myFace.Nullify();
0161     myHasNormals = false;
0162     resetShape();
0163   }
0164 
0165   //! Initialize face properties.
0166   void initFace();
0167 
0168 private:
0169   // clang-format off
0170   TopoDS_Face                myFace;        //!< current face
0171   occ::handle<Poly_Triangulation> myPolyTriang;  //!< triangulation of current face
0172   mutable BRepLProp_SLProps  mySLTool;      //!< auxiliary tool for fetching normals from surface
0173   BRepAdaptor_Surface        myFaceAdaptor; //!< surface adaptor for fetching normals from surface
0174   bool           myHasNormals;  //!< flag indicating that current face has normals
0175   bool           myIsMirrored;  //!< flag indicating that face triangles should be mirrored
0176   // clang-format on
0177 };
0178 
0179 #endif // _RWMesh_FaceIterator_HeaderFile