Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:35

0001 // Copyright (c) 2026 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 _BRepGraph_Iterator_HeaderFile
0015 #define _BRepGraph_Iterator_HeaderFile
0016 
0017 #include <BRepGraph.hxx>
0018 #include <BRepGraph_TopoView.hxx>
0019 #include <NCollection_ForwardRange.hxx>
0020 
0021 #include <utility>
0022 
0023 //! @brief Type-safe, allocation-free iterator over BRepGraph definition nodes.
0024 //! @see BRepGraph class comment "Iterator guide" for choosing between iterator types.
0025 //!
0026 //! Provides sequential read-only access to definitions stored in BRepGraph.
0027 //! By default nodes with IsRemoved flag are skipped; set TheFullTraverse
0028 //! to true to include them (types without IsRemoved are unaffected).
0029 //!
0030 //! ## Usage
0031 //! @code
0032 //!   for (BRepGraph_Iterator<BRepGraphInc::FaceDef> anIt(aGraph);
0033 //!        anIt.More(); anIt.Next())
0034 //!   {
0035 //!     const BRepGraphInc::FaceDef& aFace = anIt.Current();
0036 //!   }
0037 //! @endcode
0038 namespace BRepGraph_IteratorDetail
0039 {
0040 
0041 //! Compile-time traits mapping from definition type to typed NodeId,
0042 //! count accessor, and definition accessor.
0043 template <typename T>
0044 struct NodeTraits;
0045 
0046 template <>
0047 struct NodeTraits<BRepGraphInc::SolidDef>
0048 {
0049   using TypedId = BRepGraph_SolidId;
0050 
0051   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Solids().Nb(); }
0052 
0053   static const BRepGraphInc::SolidDef& Get(const BRepGraph& theGraph, const TypedId theId)
0054   {
0055     return theGraph.Topo().Solids().Definition(theId);
0056   }
0057 };
0058 
0059 template <>
0060 struct NodeTraits<BRepGraphInc::ShellDef>
0061 {
0062   using TypedId = BRepGraph_ShellId;
0063 
0064   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Shells().Nb(); }
0065 
0066   static const BRepGraphInc::ShellDef& Get(const BRepGraph& theGraph, const TypedId theId)
0067   {
0068     return theGraph.Topo().Shells().Definition(theId);
0069   }
0070 };
0071 
0072 template <>
0073 struct NodeTraits<BRepGraphInc::FaceDef>
0074 {
0075   using TypedId = BRepGraph_FaceId;
0076 
0077   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Faces().Nb(); }
0078 
0079   static const BRepGraphInc::FaceDef& Get(const BRepGraph& theGraph, const TypedId theId)
0080   {
0081     return theGraph.Topo().Faces().Definition(theId);
0082   }
0083 };
0084 
0085 template <>
0086 struct NodeTraits<BRepGraphInc::WireDef>
0087 {
0088   using TypedId = BRepGraph_WireId;
0089 
0090   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Wires().Nb(); }
0091 
0092   static const BRepGraphInc::WireDef& Get(const BRepGraph& theGraph, const TypedId theId)
0093   {
0094     return theGraph.Topo().Wires().Definition(theId);
0095   }
0096 };
0097 
0098 template <>
0099 struct NodeTraits<BRepGraphInc::EdgeDef>
0100 {
0101   using TypedId = BRepGraph_EdgeId;
0102 
0103   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Edges().Nb(); }
0104 
0105   static const BRepGraphInc::EdgeDef& Get(const BRepGraph& theGraph, const TypedId theId)
0106   {
0107     return theGraph.Topo().Edges().Definition(theId);
0108   }
0109 };
0110 
0111 template <>
0112 struct NodeTraits<BRepGraphInc::VertexDef>
0113 {
0114   using TypedId = BRepGraph_VertexId;
0115 
0116   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Vertices().Nb(); }
0117 
0118   static const BRepGraphInc::VertexDef& Get(const BRepGraph& theGraph, const TypedId theId)
0119   {
0120     return theGraph.Topo().Vertices().Definition(theId);
0121   }
0122 };
0123 
0124 template <>
0125 struct NodeTraits<BRepGraphInc::ProductDef>
0126 {
0127   using TypedId = BRepGraph_ProductId;
0128 
0129   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Products().Nb(); }
0130 
0131   static const BRepGraphInc::ProductDef& Get(const BRepGraph& theGraph, const TypedId theId)
0132   {
0133     return theGraph.Topo().Products().Definition(theId);
0134   }
0135 };
0136 
0137 template <>
0138 struct NodeTraits<BRepGraphInc::OccurrenceDef>
0139 {
0140   using TypedId = BRepGraph_OccurrenceId;
0141 
0142   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Occurrences().Nb(); }
0143 
0144   static const BRepGraphInc::OccurrenceDef& Get(const BRepGraph& theGraph, const TypedId theId)
0145   {
0146     return theGraph.Topo().Occurrences().Definition(theId);
0147   }
0148 };
0149 
0150 template <>
0151 struct NodeTraits<BRepGraphInc::CoEdgeDef>
0152 {
0153   using TypedId = BRepGraph_CoEdgeId;
0154 
0155   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().CoEdges().Nb(); }
0156 
0157   static const BRepGraphInc::CoEdgeDef& Get(const BRepGraph& theGraph, const TypedId theId)
0158   {
0159     return theGraph.Topo().CoEdges().Definition(theId);
0160   }
0161 };
0162 
0163 template <>
0164 struct NodeTraits<BRepGraphInc::CompoundDef>
0165 {
0166   using TypedId = BRepGraph_CompoundId;
0167 
0168   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().Compounds().Nb(); }
0169 
0170   static const BRepGraphInc::CompoundDef& Get(const BRepGraph& theGraph, const TypedId theId)
0171   {
0172     return theGraph.Topo().Compounds().Definition(theId);
0173   }
0174 };
0175 
0176 template <>
0177 struct NodeTraits<BRepGraphInc::CompSolidDef>
0178 {
0179   using TypedId = BRepGraph_CompSolidId;
0180 
0181   static uint32_t Count(const BRepGraph& theGraph) { return theGraph.Topo().CompSolids().Nb(); }
0182 
0183   static const BRepGraphInc::CompSolidDef& Get(const BRepGraph& theGraph, const TypedId theId)
0184   {
0185     return theGraph.Topo().CompSolids().Definition(theId);
0186   }
0187 };
0188 } // namespace BRepGraph_IteratorDetail
0189 
0190 //! @brief Type-safe, allocation-free iterator over BRepGraph definition nodes.
0191 //!
0192 //! @tparam NodeType        Definition struct type (e.g. BRepGraphInc::FaceDef).
0193 //! @tparam TheFullTraverse When true, removed nodes are NOT skipped (for special cases).
0194 template <typename NodeType, bool TheFullTraverse = false>
0195 class BRepGraph_Iterator
0196 {
0197 public:
0198   using Traits  = BRepGraph_IteratorDetail::NodeTraits<NodeType>;
0199   using TypedId = typename Traits::TypedId;
0200 
0201   BRepGraph_Iterator(const BRepGraph& theGraph)
0202       : myGraph(theGraph),
0203         myLength(TypedId(Traits::Count(theGraph)))
0204   {
0205     skipRemoved();
0206   }
0207 
0208   BRepGraph_Iterator(const BRepGraph& theGraph, const TypedId theStartId)
0209       : myGraph(theGraph),
0210         myCurrent(theStartId),
0211         myLength(TypedId(Traits::Count(theGraph)))
0212   {
0213     if (myCurrent < TypedId::Start())
0214     {
0215       myCurrent = TypedId::Start();
0216     }
0217     skipRemoved();
0218   }
0219 
0220   [[nodiscard]] bool More() const { return myCurrent < myLength; }
0221 
0222   void Next()
0223   {
0224     ++myCurrent;
0225     skipRemoved();
0226   }
0227 
0228   [[nodiscard]] const NodeType& Current() const { return Traits::Get(myGraph, myCurrent); }
0229 
0230   //! Current definition index as a typed NodeId.
0231   [[nodiscard]] TypedId CurrentId() const { return myCurrent; }
0232 
0233   //! Returns an STL-compatible iterator for range-based for loops.
0234   NCollection_ForwardRangeIterator<BRepGraph_Iterator> begin()
0235   {
0236     return NCollection_ForwardRangeIterator<BRepGraph_Iterator>(this);
0237   }
0238 
0239   //! Returns a sentinel marking the end of iteration.
0240   NCollection_ForwardRangeSentinel end() const { return NCollection_ForwardRangeSentinel{}; }
0241 
0242 private:
0243   //! Advance past any nodes marked as removed.
0244   void skipRemoved()
0245   {
0246     if constexpr (!TheFullTraverse)
0247     {
0248       while (myCurrent < myLength && myCurrent.IsRemoved(myGraph))
0249       {
0250         ++myCurrent;
0251       }
0252     }
0253   }
0254 
0255   const BRepGraph& myGraph;
0256   TypedId          myCurrent = TypedId::Start();
0257   TypedId          myLength;
0258 };
0259 
0260 // ---------------------------------------------------------------------------
0261 // Convenience type aliases (skip removed nodes  default)
0262 // ---------------------------------------------------------------------------
0263 
0264 using BRepGraph_SolidIterator      = BRepGraph_Iterator<BRepGraphInc::SolidDef>;
0265 using BRepGraph_ShellIterator      = BRepGraph_Iterator<BRepGraphInc::ShellDef>;
0266 using BRepGraph_FaceIterator       = BRepGraph_Iterator<BRepGraphInc::FaceDef>;
0267 using BRepGraph_WireIterator       = BRepGraph_Iterator<BRepGraphInc::WireDef>;
0268 using BRepGraph_EdgeIterator       = BRepGraph_Iterator<BRepGraphInc::EdgeDef>;
0269 using BRepGraph_VertexIterator     = BRepGraph_Iterator<BRepGraphInc::VertexDef>;
0270 using BRepGraph_CoEdgeIterator     = BRepGraph_Iterator<BRepGraphInc::CoEdgeDef>;
0271 using BRepGraph_CompoundIterator   = BRepGraph_Iterator<BRepGraphInc::CompoundDef>;
0272 using BRepGraph_CompSolidIterator  = BRepGraph_Iterator<BRepGraphInc::CompSolidDef>;
0273 using BRepGraph_ProductIterator    = BRepGraph_Iterator<BRepGraphInc::ProductDef>;
0274 using BRepGraph_OccurrenceIterator = BRepGraph_Iterator<BRepGraphInc::OccurrenceDef>;
0275 
0276 // ---------------------------------------------------------------------------
0277 // Full-traverse aliases (include removed/invalidated nodes  use only
0278 // in special cases such as compaction, validation, or debugging)
0279 // ---------------------------------------------------------------------------
0280 
0281 using BRepGraph_FullSolidIterator      = BRepGraph_Iterator<BRepGraphInc::SolidDef, true>;
0282 using BRepGraph_FullShellIterator      = BRepGraph_Iterator<BRepGraphInc::ShellDef, true>;
0283 using BRepGraph_FullFaceIterator       = BRepGraph_Iterator<BRepGraphInc::FaceDef, true>;
0284 using BRepGraph_FullWireIterator       = BRepGraph_Iterator<BRepGraphInc::WireDef, true>;
0285 using BRepGraph_FullEdgeIterator       = BRepGraph_Iterator<BRepGraphInc::EdgeDef, true>;
0286 using BRepGraph_FullVertexIterator     = BRepGraph_Iterator<BRepGraphInc::VertexDef, true>;
0287 using BRepGraph_FullCoEdgeIterator     = BRepGraph_Iterator<BRepGraphInc::CoEdgeDef, true>;
0288 using BRepGraph_FullCompoundIterator   = BRepGraph_Iterator<BRepGraphInc::CompoundDef, true>;
0289 using BRepGraph_FullCompSolidIterator  = BRepGraph_Iterator<BRepGraphInc::CompSolidDef, true>;
0290 using BRepGraph_FullProductIterator    = BRepGraph_Iterator<BRepGraphInc::ProductDef, true>;
0291 using BRepGraph_FullOccurrenceIterator = BRepGraph_Iterator<BRepGraphInc::OccurrenceDef, true>;
0292 
0293 //! @brief Allocation-free iterator over root product identifiers.
0294 //!
0295 //! Iterates directly over BRepGraph::RootProductIds() - products not
0296 //! referenced by any active occurrence.
0297 class BRepGraph_RootProductIterator
0298 {
0299 public:
0300   explicit BRepGraph_RootProductIterator(const BRepGraph& theGraph)
0301       : myRoots(theGraph.RootProductIds())
0302   {
0303   }
0304 
0305   [[nodiscard]] bool More() const { return myIndex < myRoots.Size(); }
0306 
0307   void Next() { ++myIndex; }
0308 
0309   [[nodiscard]] const BRepGraph_ProductId& Current() const { return myRoots.Value(myIndex); }
0310 
0311   NCollection_ForwardRangeIterator<BRepGraph_RootProductIterator> begin()
0312   {
0313     return NCollection_ForwardRangeIterator<BRepGraph_RootProductIterator>(this);
0314   }
0315 
0316   NCollection_ForwardRangeSentinel end() const { return NCollection_ForwardRangeSentinel{}; }
0317 
0318 private:
0319   const NCollection_LinearVector<BRepGraph_ProductId>& myRoots;
0320   size_t                                               myIndex = 0;
0321 };
0322 
0323 #endif // _BRepGraph_Iterator_HeaderFile