Back to home page

EIC code displayed by LXR

 
 

    


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

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 _BRepGraphInc_Storage_HeaderFile
0015 #define _BRepGraphInc_Storage_HeaderFile
0016 
0017 #include <BRepGraph_ItemId.hxx>
0018 #include <BRepGraph_NodeId.hxx>
0019 #include <BRepGraph_RefUID.hxx>
0020 #include <BRepGraph_UID.hxx>
0021 #include <BRepGraphInc_BitFlags.hxx>
0022 #include <BRepGraphInc_Definition.hxx>
0023 #include <BRepGraphInc_Load.hxx>
0024 #include <BRepGraphInc_Reference.hxx>
0025 #include <BRepGraphInc_Relations.hxx>
0026 #include <BRepGraphInc_Representation.hxx>
0027 #include <NCollection_Array1.hxx>
0028 #include <NCollection_IncAllocator.hxx>
0029 #include <NCollection_DataMap.hxx>
0030 #include <NCollection_FlatDataMap.hxx>
0031 #include <NCollection_DynamicArray.hxx>
0032 #include <NCollection_LinearVector.hxx>
0033 #include <Standard_GUID.hxx>
0034 #include <Standard_Assert.hxx>
0035 #include <Standard_DefineAlloc.hxx>
0036 #include <TopoDS_Shape.hxx>
0037 #include <TopoDS_TShape.hxx>
0038 #include <TopTools_ShapeMapHasher.hxx>
0039 
0040 #include <atomic>
0041 #include <shared_mutex>
0042 #include <utility>
0043 
0044 //! @brief Central backend storage container for the incidence-table topology model.
0045 //!
0046 //! Holds all entity vectors (Vertex through Occurrence), representation
0047 //! vectors (Surface, Curve3D, Curve2D, Triangulation, Polygon), relation
0048 //! tables for connectivity navigation, TShape deduplication maps, original
0049 //! shape bindings, and per-kind UID vectors. Provides typed accessors
0050 //! enforcing compile-time safety for backend code. External callers should
0051 //! normally use the BRepGraph facade rather than reaching into this storage
0052 //! directly. BRepGraphInc_Populate has friend access for efficient bulk writes
0053 //! during graph population.
0054 class BRepGraphInc_Storage
0055 {
0056 public:
0057   DEFINE_STANDARD_ALLOC
0058 
0059   //! Gen-validated shape cache entry.
0060   struct CachedShape
0061   {
0062     //! Reconstructed shape cached for a node id.
0063     TopoDS_Shape Shape;
0064 
0065     //! Subtree generation captured when the cached shape was built.
0066     uint32_t StoredSubtreeGen = 0;
0067   };
0068 
0069   //! Construct an empty storage with no entities or representations.
0070   Standard_EXPORT BRepGraphInc_Storage();
0071 
0072   //! Clear allocator-backed containers before member destructors walk them.
0073   Standard_EXPORT ~BRepGraphInc_Storage();
0074 
0075   //! Return the allocator used for backend storage.
0076   [[nodiscard]] const occ::handle<NCollection_BaseAllocator>& Allocator() const
0077   {
0078     return myAllocator;
0079   }
0080 
0081   //! Return products not referenced by any active occurrence.
0082   [[nodiscard]] const NCollection_LinearVector<BRepGraph_ProductId>& RootProductIds() const
0083   {
0084     return myRootProductIds;
0085   }
0086 
0087   //! Return products not referenced by any active occurrence.
0088   NCollection_LinearVector<BRepGraph_ProductId>& ChangeRootProductIds() { return myRootProductIds; }
0089 
0090   //! Return nodes accumulated during deferred invalidation.
0091   [[nodiscard]] const NCollection_LinearVector<BRepGraph_NodeId>& DeferredModified() const
0092   {
0093     return myDeferredModified;
0094   }
0095 
0096   //! Return nodes accumulated during deferred invalidation.
0097   NCollection_LinearVector<BRepGraph_NodeId>& ChangeDeferredModified()
0098   {
0099     return myDeferredModified;
0100   }
0101 
0102   //! Return refs accumulated during deferred invalidation.
0103   [[nodiscard]] const NCollection_LinearVector<BRepGraph_RefId>& DeferredRefModified() const
0104   {
0105     return myDeferredRefModified;
0106   }
0107 
0108   //! Return refs accumulated during deferred invalidation.
0109   NCollection_LinearVector<BRepGraph_RefId>& ChangeDeferredRefModified()
0110   {
0111     return myDeferredRefModified;
0112   }
0113 
0114   //! Return true when the graph contains no topology definitions.
0115   //! Checks whether any node kind (Vertex, Edge, Wire, Face, Shell, Solid,
0116   //! Compound, CompSolid, Product, Occurrence) has been allocated.
0117   [[nodiscard]] Standard_EXPORT bool IsEmpty() const;
0118 
0119   //! Return the next UID counter for a given node kind.
0120   [[nodiscard]] Standard_EXPORT uint32_t NextNodeUIDCounter(BRepGraph_NodeId::Kind theKind) const;
0121 
0122   //! Override the next UID counter for a given node kind.
0123   Standard_EXPORT void SetNextNodeUIDCounter(BRepGraph_NodeId::Kind theKind, uint32_t theCounter);
0124 
0125   //! Return the next UID counter for a given reference kind.
0126   [[nodiscard]] Standard_EXPORT uint32_t NextRefUIDCounter(BRepGraph_RefId::Kind theKind) const;
0127 
0128   //! Override the next UID counter for a given reference kind.
0129   Standard_EXPORT void SetNextRefUIDCounter(BRepGraph_RefId::Kind theKind, uint32_t theCounter);
0130 
0131   //! Allocate a node UID: write counter into the entity, bind reverse map, advance counter.
0132   Standard_EXPORT BRepGraph_UID AllocateNodeUID(BRepGraph_NodeId theNodeId);
0133 
0134   //! Allocate a reference UID: write counter into the ref, bind reverse map, advance counter.
0135   Standard_EXPORT BRepGraph_RefUID AllocateRefUID(BRepGraph_RefId theRefId);
0136 
0137   //! Return the current graph generation used by VersionStamp staleness checks.
0138   [[nodiscard]] uint32_t Generation() const { return myGeneration.load(std::memory_order_relaxed); }
0139 
0140   //! Override the current graph generation.
0141   void SetGeneration(const uint32_t theGeneration)
0142   {
0143     myGeneration.store(theGeneration, std::memory_order_relaxed);
0144   }
0145 
0146   //! Increment the graph generation after a structural mutation batch.
0147   void IncrementGeneration() { myGeneration.fetch_add(1, std::memory_order_relaxed); }
0148 
0149   //! Return the stable graph instance GUID.
0150   [[nodiscard]] const Standard_GUID& GraphGUID() const { return myGraphGUID; }
0151 
0152   //! Override the stable graph instance GUID.
0153   void SetGraphGUID(const Standard_GUID& theGuid) { myGraphGUID = theGuid; }
0154 
0155   //! Return whether invalidation is currently deferred.
0156   [[nodiscard]] bool DeferredMode() const { return myDeferredMode.load(std::memory_order_relaxed); }
0157 
0158   //! Enable or disable deferred invalidation mode.
0159   void SetDeferredMode(const bool theEnabled)
0160   {
0161     myDeferredMode.store(theEnabled, std::memory_order_relaxed);
0162   }
0163 
0164   //! Return the current propagation wave id used to avoid revisiting parents.
0165   [[nodiscard]] uint32_t PropagationWave() const
0166   {
0167     return myPropagationWave.load(std::memory_order_relaxed);
0168   }
0169 
0170   //! Increment the propagation wave and return the new value.
0171   [[nodiscard]] uint32_t AdvancePropagationWave()
0172   {
0173     return myPropagationWave.fetch_add(1, std::memory_order_relaxed) + 1;
0174   }
0175 
0176   //! Increment the propagation wave without reading it back.
0177   void IncrementPropagationWave() { myPropagationWave.fetch_add(1, std::memory_order_relaxed); }
0178 
0179   //! Return the recursion depth of the active RemoveSubgraph cascade.
0180   [[nodiscard]] uint32_t RemoveSubgraphDepth() const { return myRemoveSubgraphDepth; }
0181 
0182   //! Enter one nested RemoveSubgraph scope.
0183   void IncrementRemoveSubgraphDepth() { ++myRemoveSubgraphDepth; }
0184 
0185   //! Leave one nested RemoveSubgraph scope.
0186   void DecrementRemoveSubgraphDepth()
0187   {
0188     Standard_ASSERT_VOID(myRemoveSubgraphDepth > 0, "RemoveSubgraphDepth underflow");
0189     if (myRemoveSubgraphDepth > 0)
0190     {
0191       --myRemoveSubgraphDepth;
0192     }
0193   }
0194 
0195   //! Returns the total number of vertex entities (including removed).
0196   [[nodiscard]] uint32_t NbVertices() const { return myVertices.Nb(); }
0197 
0198   //! Returns the total number of edge entities (including removed).
0199   [[nodiscard]] uint32_t NbEdges() const { return myEdges.Nb(); }
0200 
0201   //! Returns the total number of coedge entities (including removed).
0202   [[nodiscard]] uint32_t NbCoEdges() const { return myCoEdges.Nb(); }
0203 
0204   //! Returns the total number of wire entities (including removed).
0205   [[nodiscard]] uint32_t NbWires() const { return myWires.Nb(); }
0206 
0207   //! Returns the total number of face entities (including removed).
0208   [[nodiscard]] uint32_t NbFaces() const { return myFaces.Nb(); }
0209 
0210   //! Returns the total number of shell entities (including removed).
0211   [[nodiscard]] uint32_t NbShells() const { return myShells.Nb(); }
0212 
0213   //! Returns the total number of solid entities (including removed).
0214   [[nodiscard]] uint32_t NbSolids() const { return mySolids.Nb(); }
0215 
0216   //! Returns the total number of compound entities (including removed).
0217   [[nodiscard]] uint32_t NbCompounds() const { return myCompounds.Nb(); }
0218 
0219   //! Returns the total number of compsolid entities (including removed).
0220   [[nodiscard]] uint32_t NbCompSolids() const { return myCompSolids.Nb(); }
0221 
0222   //! Returns the total number of product entities (including removed).
0223   [[nodiscard]] uint32_t NbProducts() const { return myProducts.Nb(); }
0224 
0225   //! Returns the total number of occurrence entities (including removed).
0226   [[nodiscard]] uint32_t NbOccurrences() const { return myOccurrences.Nb(); }
0227 
0228   //! Returns the total number of shell reference entries (including removed).
0229   [[nodiscard]] uint32_t NbShellRefs() const { return myShellRefs.Nb(); }
0230 
0231   //! Returns the total number of face reference entries (including removed).
0232   [[nodiscard]] uint32_t NbFaceRefs() const { return myFaceRefs.Nb(); }
0233 
0234   //! Returns the total number of wire reference entries (including removed).
0235   [[nodiscard]] uint32_t NbWireRefs() const { return myWireRefs.Nb(); }
0236 
0237   //! Returns the total number of vertex reference entries (including removed).
0238   [[nodiscard]] uint32_t NbVertexRefs() const { return myVertexRefs.Nb(); }
0239 
0240   //! Returns the total number of solid reference entries (including removed).
0241   [[nodiscard]] uint32_t NbSolidRefs() const { return mySolidRefs.Nb(); }
0242 
0243   //! Returns the total number of child reference entries (including removed).
0244   [[nodiscard]] uint32_t NbChildRefs() const { return myChildRefs.Nb(); }
0245 
0246   //! Returns the total number of occurrence reference entries (including removed).
0247   [[nodiscard]] uint32_t NbOccurrenceRefs() const { return myOccurrenceRefs.Nb(); }
0248 
0249   //! Returns the number of active vertex entities (excluding removed).
0250   [[nodiscard]] uint32_t NbActiveVertices() const { return myVertices.NbActive; }
0251 
0252   //! Returns the number of active edge entities (excluding removed).
0253   [[nodiscard]] uint32_t NbActiveEdges() const { return myEdges.NbActive; }
0254 
0255   //! Returns the number of active coedge entities (excluding removed).
0256   [[nodiscard]] uint32_t NbActiveCoEdges() const { return myCoEdges.NbActive; }
0257 
0258   //! Returns the number of active wire entities (excluding removed).
0259   [[nodiscard]] uint32_t NbActiveWires() const { return myWires.NbActive; }
0260 
0261   //! Returns the number of active face entities (excluding removed).
0262   [[nodiscard]] uint32_t NbActiveFaces() const { return myFaces.NbActive; }
0263 
0264   //! Returns the number of active shell entities (excluding removed).
0265   [[nodiscard]] uint32_t NbActiveShells() const { return myShells.NbActive; }
0266 
0267   //! Returns the number of active solid entities (excluding removed).
0268   [[nodiscard]] uint32_t NbActiveSolids() const { return mySolids.NbActive; }
0269 
0270   //! Returns the number of active compound entities (excluding removed).
0271   [[nodiscard]] uint32_t NbActiveCompounds() const { return myCompounds.NbActive; }
0272 
0273   //! Returns the number of active compsolid entities (excluding removed).
0274   [[nodiscard]] uint32_t NbActiveCompSolids() const { return myCompSolids.NbActive; }
0275 
0276   //! Returns the number of active product entities (excluding removed).
0277   [[nodiscard]] uint32_t NbActiveProducts() const { return myProducts.NbActive; }
0278 
0279   //! Returns the number of active occurrence entities (excluding removed).
0280   [[nodiscard]] uint32_t NbActiveOccurrences() const { return myOccurrences.NbActive; }
0281 
0282   //! Returns the number of active shell reference entries (excluding removed).
0283   [[nodiscard]] uint32_t NbActiveShellRefs() const { return myShellRefs.NbActive; }
0284 
0285   //! Returns the number of active face reference entries (excluding removed).
0286   [[nodiscard]] uint32_t NbActiveFaceRefs() const { return myFaceRefs.NbActive; }
0287 
0288   //! Returns the number of active wire reference entries (excluding removed).
0289   [[nodiscard]] uint32_t NbActiveWireRefs() const { return myWireRefs.NbActive; }
0290 
0291   //! Returns the number of active vertex reference entries (excluding removed).
0292   [[nodiscard]] uint32_t NbActiveVertexRefs() const { return myVertexRefs.NbActive; }
0293 
0294   //! Returns the number of active solid reference entries (excluding removed).
0295   [[nodiscard]] uint32_t NbActiveSolidRefs() const { return mySolidRefs.NbActive; }
0296 
0297   //! Returns the number of active child reference entries (excluding removed).
0298   [[nodiscard]] uint32_t NbActiveChildRefs() const { return myChildRefs.NbActive; }
0299 
0300   //! Returns the number of active occurrence reference entries (excluding removed).
0301   [[nodiscard]] uint32_t NbActiveOccurrenceRefs() const { return myOccurrenceRefs.NbActive; }
0302 
0303   //! Mark an entity node as removed and decrement its active counter once.
0304   //! @param[in] theNodeId typed entity id
0305   //! @return true if the node transitioned from active to removed
0306   Standard_EXPORT bool MarkRemoved(const BRepGraph_NodeId theNodeId);
0307 
0308   //! Mark a reference entry as removed and decrement its active counter once.
0309   //! @param[in] theRefId typed reference id
0310   //! @return true if the ref transitioned from active to removed
0311   Standard_EXPORT bool MarkRemovedRef(const BRepGraph_RefId theRefId);
0312 
0313   //! Returns the number of edge 3D curve use records.
0314   [[nodiscard]] uint32_t NbEdgeCurves3D() const { return myEdgeCurves3D.Nb(); }
0315 
0316   //! Returns the number of edge 3D polygon use records.
0317   [[nodiscard]] uint32_t NbEdgePolygons3D() const { return myEdgePolygons3D.Nb(); }
0318 
0319   //! Returns the number of coedge 2D curve use records.
0320   [[nodiscard]] uint32_t NbCoEdgeCurves2D() const { return myCoEdgeCurves2D.Nb(); }
0321 
0322   //! Returns the number of coedge 2D polygon use records.
0323   [[nodiscard]] uint32_t NbCoEdgePolygons2D() const { return myCoEdgePolygons2D.Nb(); }
0324 
0325   //! Returns the number of coedge polygon-on-triangulation use records.
0326   [[nodiscard]] uint32_t NbCoEdgePolygonsOnTri() const { return myCoEdgePolygonsOnTri.Nb(); }
0327 
0328   //! Returns the number of face surface use records.
0329   [[nodiscard]] uint32_t NbFaceSurfaces() const { return myFaceSurfaces.Nb(); }
0330 
0331   //! Returns the number of face triangulation use records.
0332   [[nodiscard]] uint32_t NbFaceTriangulations() const { return myFaceTriangulations.Nb(); }
0333 
0334   //! Returns the number of active (parent-valid) edge 3D curve use records.
0335   [[nodiscard]] Standard_EXPORT uint32_t NbActiveEdgeCurves3D() const;
0336 
0337   //! Returns the number of active (parent-valid) coedge 2D curve use records.
0338   [[nodiscard]] Standard_EXPORT uint32_t NbActiveCoEdgeCurves2D() const;
0339 
0340   //! Returns the number of active (parent-valid) face surface use records.
0341   [[nodiscard]] Standard_EXPORT uint32_t NbActiveFaceSurfaces() const;
0342 
0343   //! Returns the number of active (parent-valid) face triangulation use records.
0344   [[nodiscard]] Standard_EXPORT uint32_t NbActiveFaceTriangulations() const;
0345 
0346   //! Returns the number of active (parent-valid) edge 3D polygon use records.
0347   [[nodiscard]] Standard_EXPORT uint32_t NbActiveEdgePolygons3D() const;
0348 
0349   //! Returns the number of active (parent-valid) coedge 2D polygon use records.
0350   [[nodiscard]] Standard_EXPORT uint32_t NbActiveCoEdgePolygons2D() const;
0351 
0352   //! Returns the number of active (parent-valid) coedge polygon-on-triangulation use records.
0353   [[nodiscard]] Standard_EXPORT uint32_t NbActiveCoEdgePolygonsOnTri() const;
0354 
0355   //! Returns the edge 3D curve use at the given id.
0356   [[nodiscard]] const BRepGraphInc::EdgeCurve3DRep& EdgeCurve3DRep(
0357     const BRepGraph_EdgeCurve3DRepId theId) const
0358   {
0359     return myEdgeCurves3D.Get(theId);
0360   }
0361 
0362   //! Returns a mutable reference to the edge 3D curve use at the given id.
0363   BRepGraphInc::EdgeCurve3DRep& ChangeEdgeCurve3DRep(const BRepGraph_EdgeCurve3DRepId theId)
0364   {
0365     return myEdgeCurves3D.Change(theId);
0366   }
0367 
0368   //! Returns the edge 3D polygon use at the given id.
0369   [[nodiscard]] const BRepGraphInc::EdgePolygon3DRep& EdgePolygon3DRep(
0370     const BRepGraph_EdgePolygon3DRepId theId) const
0371   {
0372     return myEdgePolygons3D.Get(theId);
0373   }
0374 
0375   //! Returns a mutable reference to the edge 3D polygon use at the given id.
0376   BRepGraphInc::EdgePolygon3DRep& ChangeEdgePolygon3DRep(const BRepGraph_EdgePolygon3DRepId theId)
0377   {
0378     return myEdgePolygons3D.Change(theId);
0379   }
0380 
0381   //! Returns the coedge 2D curve use at the given id.
0382   [[nodiscard]] const BRepGraphInc::CoEdgeCurve2DRep& CoEdgeCurve2DRep(
0383     const BRepGraph_CoEdgeCurve2DRepId theId) const
0384   {
0385     return myCoEdgeCurves2D.Get(theId);
0386   }
0387 
0388   //! Returns a mutable reference to the coedge 2D curve use at the given id.
0389   BRepGraphInc::CoEdgeCurve2DRep& ChangeCoEdgeCurve2DRep(const BRepGraph_CoEdgeCurve2DRepId theId)
0390   {
0391     return myCoEdgeCurves2D.Change(theId);
0392   }
0393 
0394   //! Returns the coedge 2D polygon use at the given id.
0395   [[nodiscard]] const BRepGraphInc::CoEdgePolygon2DRep& CoEdgePolygon2DRep(
0396     const BRepGraph_CoEdgePolygon2DRepId theId) const
0397   {
0398     return myCoEdgePolygons2D.Get(theId);
0399   }
0400 
0401   //! Returns a mutable reference to the coedge 2D polygon use at the given id.
0402   BRepGraphInc::CoEdgePolygon2DRep& ChangeCoEdgePolygon2DRep(
0403     const BRepGraph_CoEdgePolygon2DRepId theId)
0404   {
0405     return myCoEdgePolygons2D.Change(theId);
0406   }
0407 
0408   //! Returns the coedge polygon-on-triangulation use at the given id.
0409   [[nodiscard]] const BRepGraphInc::CoEdgePolygonOnTriRep& CoEdgePolygonOnTriRep(
0410     const BRepGraph_CoEdgePolygonOnTriRepId theId) const
0411   {
0412     return myCoEdgePolygonsOnTri.Get(theId);
0413   }
0414 
0415   //! Returns a mutable reference to the coedge polygon-on-triangulation use at the given id.
0416   BRepGraphInc::CoEdgePolygonOnTriRep& ChangeCoEdgePolygonOnTriRep(
0417     const BRepGraph_CoEdgePolygonOnTriRepId theId)
0418   {
0419     return myCoEdgePolygonsOnTri.Change(theId);
0420   }
0421 
0422   //! Returns the face surface use at the given id.
0423   [[nodiscard]] const BRepGraphInc::FaceSurfaceRep& FaceSurfaceRep(
0424     const BRepGraph_FaceSurfaceRepId theId) const
0425   {
0426     return myFaceSurfaces.Get(theId);
0427   }
0428 
0429   //! Returns a mutable reference to the face surface use at the given id.
0430   BRepGraphInc::FaceSurfaceRep& ChangeFaceSurfaceRep(const BRepGraph_FaceSurfaceRepId theId)
0431   {
0432     return myFaceSurfaces.Change(theId);
0433   }
0434 
0435   //! Returns the face triangulation use at the given id.
0436   [[nodiscard]] const BRepGraphInc::FaceTriangulationRep& FaceTriangulationRep(
0437     const BRepGraph_FaceTriangulationRepId theId) const
0438   {
0439     return myFaceTriangulations.Get(theId);
0440   }
0441 
0442   //! Returns a mutable reference to the face triangulation use at the given id.
0443   BRepGraphInc::FaceTriangulationRep& ChangeFaceTriangulationRep(
0444     const BRepGraph_FaceTriangulationRepId theId)
0445   {
0446     return myFaceTriangulations.Change(theId);
0447   }
0448 
0449   //! Appends a new edge 3D curve use record and returns its id.
0450   BRepGraph_EdgeCurve3DRepId AppendEdgeCurve3DRep() { return myEdgeCurves3D.Append(); }
0451 
0452   //! Appends a new edge 3D polygon use record and returns its id.
0453   BRepGraph_EdgePolygon3DRepId AppendEdgePolygon3DRep() { return myEdgePolygons3D.Append(); }
0454 
0455   //! Appends a new coedge 2D curve use record and returns its id.
0456   BRepGraph_CoEdgeCurve2DRepId AppendCoEdgeCurve2DRep() { return myCoEdgeCurves2D.Append(); }
0457 
0458   //! Appends a new coedge 2D polygon use record and returns its id.
0459   BRepGraph_CoEdgePolygon2DRepId AppendCoEdgePolygon2DRep() { return myCoEdgePolygons2D.Append(); }
0460 
0461   //! Appends a new coedge polygon-on-triangulation use record and returns its id.
0462   BRepGraph_CoEdgePolygonOnTriRepId AppendCoEdgePolygonOnTriRep()
0463   {
0464     return myCoEdgePolygonsOnTri.Append();
0465   }
0466 
0467   //! Appends a new face surface use record and returns its id.
0468   BRepGraph_FaceSurfaceRepId AppendFaceSurfaceRep() { return myFaceSurfaces.Append(); }
0469 
0470   //! Appends a new face triangulation use record and returns its id.
0471   BRepGraph_FaceTriangulationRepId AppendFaceTriangulationRep()
0472   {
0473     return myFaceTriangulations.Append();
0474   }
0475 
0476   //! Mark a representation-use record as removed and decrement its active counter once.
0477   //! @param[in] theRepId typed use id
0478   //! @return true if the use transitioned from active to removed
0479   Standard_EXPORT bool MarkRemoved(const BRepGraph_RepId theRepId);
0480 
0481   //! Set or clear the soft-removal flag for a representation-use record.
0482   //! @param[in] theRepId typed use id
0483   //! @param[in] theVal true to mark removed, false to mark active
0484   Standard_EXPORT void SetRemoved(const BRepGraph_RepId theRepId, const bool theVal);
0485 
0486   //! Returns the vertex entity at the given typed id.
0487   //! @param[in] theVertex typed vertex id
0488   [[nodiscard]] const BRepGraphInc::VertexDef& Vertex(const BRepGraph_VertexId theVertex) const
0489   {
0490     return myVertices.Get(theVertex);
0491   }
0492 
0493   //! Returns the edge entity at the given typed id.
0494   //! @param[in] theEdge typed edge id
0495   [[nodiscard]] const BRepGraphInc::EdgeDef& Edge(const BRepGraph_EdgeId theEdge) const
0496   {
0497     return myEdges.Get(theEdge);
0498   }
0499 
0500   //! Returns the coedge entity at the given typed id.
0501   //! @param[in] theCoEdge typed coedge id
0502   [[nodiscard]] const BRepGraphInc::CoEdgeDef& CoEdge(const BRepGraph_CoEdgeId theCoEdge) const
0503   {
0504     return myCoEdges.Get(theCoEdge);
0505   }
0506 
0507   //! Returns the wire entity at the given typed id.
0508   //! @param[in] theWire typed wire id
0509   [[nodiscard]] const BRepGraphInc::WireDef& Wire(const BRepGraph_WireId theWire) const
0510   {
0511     return myWires.Get(theWire);
0512   }
0513 
0514   //! Returns the face entity at the given typed id.
0515   //! @param[in] theFace typed face id
0516   [[nodiscard]] const BRepGraphInc::FaceDef& Face(const BRepGraph_FaceId theFace) const
0517   {
0518     return myFaces.Get(theFace);
0519   }
0520 
0521   //! Returns the shell entity at the given typed id.
0522   //! @param[in] theShell typed shell id
0523   [[nodiscard]] const BRepGraphInc::ShellDef& Shell(const BRepGraph_ShellId theShell) const
0524   {
0525     return myShells.Get(theShell);
0526   }
0527 
0528   //! Returns the solid entity at the given typed id.
0529   //! @param[in] theSolid typed solid id
0530   [[nodiscard]] const BRepGraphInc::SolidDef& Solid(const BRepGraph_SolidId theSolid) const
0531   {
0532     return mySolids.Get(theSolid);
0533   }
0534 
0535   //! Returns the compound entity at the given typed id.
0536   //! @param[in] theCompound typed compound id
0537   [[nodiscard]] const BRepGraphInc::CompoundDef& Compound(
0538     const BRepGraph_CompoundId theCompound) const
0539   {
0540     return myCompounds.Get(theCompound);
0541   }
0542 
0543   //! Returns the compsolid entity at the given typed id.
0544   //! @param[in] theCompSolid typed comp-solid id
0545   [[nodiscard]] const BRepGraphInc::CompSolidDef& CompSolid(
0546     const BRepGraph_CompSolidId theCompSolid) const
0547   {
0548     return myCompSolids.Get(theCompSolid);
0549   }
0550 
0551   //! Returns the product entity at the given typed id.
0552   //! @param[in] theProduct typed product id
0553   [[nodiscard]] const BRepGraphInc::ProductDef& Product(const BRepGraph_ProductId theProduct) const
0554   {
0555     return myProducts.Get(theProduct);
0556   }
0557 
0558   //! Returns the occurrence entity at the given typed id.
0559   //! @param[in] theOccurrence typed occurrence id
0560   [[nodiscard]] const BRepGraphInc::OccurrenceDef& Occurrence(
0561     const BRepGraph_OccurrenceId theOccurrence) const
0562   {
0563     return myOccurrences.Get(theOccurrence);
0564   }
0565 
0566   //! Returns the shell reference entry at the given typed id.
0567   [[nodiscard]] const BRepGraphInc::ShellRef& ShellRef(const BRepGraph_ShellRefId theRefId) const
0568   {
0569     return myShellRefs.Get(theRefId);
0570   }
0571 
0572   //! Returns the face reference entry at the given typed id.
0573   [[nodiscard]] const BRepGraphInc::FaceRef& FaceRef(const BRepGraph_FaceRefId theRefId) const
0574   {
0575     return myFaceRefs.Get(theRefId);
0576   }
0577 
0578   //! Returns the wire reference entry at the given typed id.
0579   [[nodiscard]] const BRepGraphInc::WireRef& WireRef(const BRepGraph_WireRefId theRefId) const
0580   {
0581     return myWireRefs.Get(theRefId);
0582   }
0583 
0584   //! Returns the vertex reference entry at the given typed id.
0585   [[nodiscard]] const BRepGraphInc::VertexRef& VertexRef(const BRepGraph_VertexRefId theRefId) const
0586   {
0587     return myVertexRefs.Get(theRefId);
0588   }
0589 
0590   //! Returns the solid reference entry at the given typed id.
0591   [[nodiscard]] const BRepGraphInc::SolidRef& SolidRef(const BRepGraph_SolidRefId theRefId) const
0592   {
0593     return mySolidRefs.Get(theRefId);
0594   }
0595 
0596   //! Returns the child reference entry at the given typed id.
0597   [[nodiscard]] const BRepGraphInc::ChildRef& ChildRef(const BRepGraph_ChildRefId theRefId) const
0598   {
0599     return myChildRefs.Get(theRefId);
0600   }
0601 
0602   //! Returns the occurrence reference entry at the given typed id.
0603   [[nodiscard]] const BRepGraphInc::OccurrenceRef& OccurrenceRef(
0604     const BRepGraph_OccurrenceRefId theRefId) const
0605   {
0606     return myOccurrenceRefs.Get(theRefId);
0607   }
0608 
0609   //! Returns a mutable reference to the vertex entity at the given typed id.
0610   //! @param[in] theVertex typed vertex id
0611   BRepGraphInc::VertexDef& ChangeVertex(const BRepGraph_VertexId theVertex)
0612   {
0613     return myVertices.Change(theVertex);
0614   }
0615 
0616   //! Returns a mutable reference to the edge entity at the given typed id.
0617   //! @param[in] theEdge typed edge id
0618   BRepGraphInc::EdgeDef& ChangeEdge(const BRepGraph_EdgeId theEdge)
0619   {
0620     return myEdges.Change(theEdge);
0621   }
0622 
0623   //! Returns a mutable reference to the coedge entity at the given typed id.
0624   //! @param[in] theCoEdge typed coedge id
0625   BRepGraphInc::CoEdgeDef& ChangeCoEdge(const BRepGraph_CoEdgeId theCoEdge)
0626   {
0627     return myCoEdges.Change(theCoEdge);
0628   }
0629 
0630   //! Returns a mutable reference to the wire entity at the given typed id.
0631   //! @param[in] theWire typed wire id
0632   BRepGraphInc::WireDef& ChangeWire(const BRepGraph_WireId theWire)
0633   {
0634     return myWires.Change(theWire);
0635   }
0636 
0637   //! Returns a mutable reference to the face entity at the given typed id.
0638   //! @param[in] theFace typed face id
0639   BRepGraphInc::FaceDef& ChangeFace(const BRepGraph_FaceId theFace)
0640   {
0641     return myFaces.Change(theFace);
0642   }
0643 
0644   //! Returns a mutable reference to the shell entity at the given typed id.
0645   //! @param[in] theShell typed shell id
0646   BRepGraphInc::ShellDef& ChangeShell(const BRepGraph_ShellId theShell)
0647   {
0648     return myShells.Change(theShell);
0649   }
0650 
0651   //! Returns a mutable reference to the solid entity at the given typed id.
0652   //! @param[in] theSolid typed solid id
0653   BRepGraphInc::SolidDef& ChangeSolid(const BRepGraph_SolidId theSolid)
0654   {
0655     return mySolids.Change(theSolid);
0656   }
0657 
0658   //! Returns a mutable reference to the compound entity at the given typed id.
0659   //! @param[in] theCompound typed compound id
0660   BRepGraphInc::CompoundDef& ChangeCompound(const BRepGraph_CompoundId theCompound)
0661   {
0662     return myCompounds.Change(theCompound);
0663   }
0664 
0665   //! Returns a mutable reference to the compsolid entity at the given typed id.
0666   //! @param[in] theCompSolid typed comp-solid id
0667   BRepGraphInc::CompSolidDef& ChangeCompSolid(const BRepGraph_CompSolidId theCompSolid)
0668   {
0669     return myCompSolids.Change(theCompSolid);
0670   }
0671 
0672   //! Returns a mutable reference to the product entity at the given typed id.
0673   //! @param[in] theProduct typed product id
0674   BRepGraphInc::ProductDef& ChangeProduct(const BRepGraph_ProductId theProduct)
0675   {
0676     return myProducts.Change(theProduct);
0677   }
0678 
0679   //! Returns a mutable reference to the occurrence entity at the given typed id.
0680   //! @param[in] theOccurrence typed occurrence id
0681   BRepGraphInc::OccurrenceDef& ChangeOccurrence(const BRepGraph_OccurrenceId theOccurrence)
0682   {
0683     return myOccurrences.Change(theOccurrence);
0684   }
0685 
0686   //! Returns a mutable reference to the shell reference entry at the given typed id.
0687   BRepGraphInc::ShellRef& ChangeShellRef(const BRepGraph_ShellRefId theRefId)
0688   {
0689     return myShellRefs.Change(theRefId);
0690   }
0691 
0692   //! Returns a mutable reference to the face reference entry at the given typed id.
0693   BRepGraphInc::FaceRef& ChangeFaceRef(const BRepGraph_FaceRefId theRefId)
0694   {
0695     return myFaceRefs.Change(theRefId);
0696   }
0697 
0698   //! Returns a mutable reference to the wire reference entry at the given typed id.
0699   BRepGraphInc::WireRef& ChangeWireRef(const BRepGraph_WireRefId theRefId)
0700   {
0701     return myWireRefs.Change(theRefId);
0702   }
0703 
0704   //! Returns a mutable reference to the vertex reference entry at the given typed id.
0705   BRepGraphInc::VertexRef& ChangeVertexRef(const BRepGraph_VertexRefId theRefId)
0706   {
0707     return myVertexRefs.Change(theRefId);
0708   }
0709 
0710   //! Returns a mutable reference to the solid reference entry at the given typed id.
0711   BRepGraphInc::SolidRef& ChangeSolidRef(const BRepGraph_SolidRefId theRefId)
0712   {
0713     return mySolidRefs.Change(theRefId);
0714   }
0715 
0716   //! Returns a mutable reference to the child reference entry at the given typed id.
0717   BRepGraphInc::ChildRef& ChangeChildRef(const BRepGraph_ChildRefId theRefId)
0718   {
0719     return myChildRefs.Change(theRefId);
0720   }
0721 
0722   //! Returns a mutable reference to the occurrence reference entry at the given typed id.
0723   BRepGraphInc::OccurrenceRef& ChangeOccurrenceRef(const BRepGraph_OccurrenceRefId theRefId)
0724   {
0725     return myOccurrenceRefs.Change(theRefId);
0726   }
0727 
0728   //! Return the face relations for a given face identifier.
0729   //! @param[in] theId face identifier
0730   //! @return const reference to the face relation representation
0731   [[nodiscard]] const BRepGraphInc::FaceRelations& FaceRelations(const BRepGraph_FaceId theId) const
0732   {
0733     return myFaceRelations.Value(static_cast<size_t>(theId.Index));
0734   }
0735 
0736   //! Return the wire relations for a given wire identifier.
0737   //! @param[in] theId wire identifier
0738   //! @return const reference to the wire relation representation
0739   [[nodiscard]] const BRepGraphInc::WireRelations& WireRelations(const BRepGraph_WireId theId) const
0740   {
0741     return myWireRelations.Value(static_cast<size_t>(theId.Index));
0742   }
0743 
0744   //! Return the edge relations for a given edge identifier.
0745   //! @param[in] theId edge identifier
0746   //! @return const reference to the edge relation representation
0747   [[nodiscard]] const BRepGraphInc::EdgeRelations& EdgeRelations(const BRepGraph_EdgeId theId) const
0748   {
0749     return myEdgeRelations.Value(static_cast<size_t>(theId.Index));
0750   }
0751 
0752   //! Return the shell relations for a given shell identifier.
0753   //! @param[in] theId shell identifier
0754   //! @return const reference to the shell relation representation
0755   [[nodiscard]] const BRepGraphInc::ShellRelations& ShellRelations(
0756     const BRepGraph_ShellId theId) const
0757   {
0758     return myShellRelations.Value(static_cast<size_t>(theId.Index));
0759   }
0760 
0761   //! Return the solid relations for a given solid identifier.
0762   //! @param[in] theId solid identifier
0763   //! @return const reference to the solid relation representation
0764   [[nodiscard]] const BRepGraphInc::SolidRelations& SolidRelations(
0765     const BRepGraph_SolidId theId) const
0766   {
0767     return mySolidRelations.Value(static_cast<size_t>(theId.Index));
0768   }
0769 
0770   //! Return the compound relations for a given compound identifier.
0771   //! @param[in] theId compound identifier
0772   //! @return const reference to the compound relation representation
0773   [[nodiscard]] const BRepGraphInc::CompoundRelations& CompoundRelations(
0774     const BRepGraph_CompoundId theId) const
0775   {
0776     return myCompoundRelations.Value(static_cast<size_t>(theId.Index));
0777   }
0778 
0779   //! Return the compsolid relations for a given compsolid identifier.
0780   //! @param[in] theId compsolid identifier
0781   //! @return const reference to the compsolid relation representation
0782   [[nodiscard]] const BRepGraphInc::CompSolidRelations& CompSolidRelations(
0783     const BRepGraph_CompSolidId theId) const
0784   {
0785     return myCompSolidRelations.Value(static_cast<size_t>(theId.Index));
0786   }
0787 
0788   //! Return the vertex relations for a given vertex identifier.
0789   //! @param[in] theId vertex identifier
0790   //! @return const reference to the vertex relation representation
0791   [[nodiscard]] const BRepGraphInc::VertexRelations& VertexRelations(
0792     const BRepGraph_VertexId theId) const
0793   {
0794     return myVertexRelations.Value(static_cast<size_t>(theId.Index));
0795   }
0796 
0797   //! Return the product relations for a given product identifier.
0798   //! @param[in] theId product identifier
0799   //! @return const reference to the product relation representation
0800   [[nodiscard]] const BRepGraphInc::ProductRelations& ProductRelations(
0801     const BRepGraph_ProductId theId) const
0802   {
0803     return myProductRelations.Value(static_cast<size_t>(theId.Index));
0804   }
0805 
0806   //! Return the occurrence relations for a given occurrence identifier.
0807   //! @param[in] theId occurrence identifier
0808   //! @return const reference to the occurrence relation representation
0809   [[nodiscard]] const BRepGraphInc::OccurrenceRelations& OccurrenceRelations(
0810     const BRepGraph_OccurrenceId theId) const
0811   {
0812     return myOccurrenceRelations.Value(static_cast<size_t>(theId.Index));
0813   }
0814 
0815   //! Return the compound child reference identifiers that point to a given node.
0816   //! @param[in] theNode node identifier
0817   //! @return const reference to the list of child reference identifiers
0818   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_ChildRefId>&
0819                                       CompoundRefsOfNode(const BRepGraph_NodeId theNode) const;
0820 
0821   //! Return the occurrence reference identifiers that point to a given node.
0822   //! @param[in] theNode node identifier
0823   //! @return const reference to the list of occurrence reference identifiers
0824   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_OccurrenceRefId>&
0825                                       OccurrenceRefsOfNode(const BRepGraph_NodeId theNode) const;
0826 
0827   //! Appends a new vertex entity and returns its typed id.
0828   BRepGraph_VertexId AppendVertex()
0829   {
0830     const BRepGraph_VertexId anId = myVertices.Append();
0831     myVertexRelations.Appended();
0832     return anId;
0833   }
0834 
0835   //! Appends a new edge entity and returns its typed id.
0836   BRepGraph_EdgeId AppendEdge()
0837   {
0838     const BRepGraph_EdgeId anId = myEdges.Append();
0839     myEdgeRelations.Appended();
0840     return anId;
0841   }
0842 
0843   //! Appends a new coedge entity and returns its typed id.
0844   BRepGraph_CoEdgeId AppendCoEdge() { return myCoEdges.Append(); }
0845 
0846   //! Appends a new wire entity and returns its typed id.
0847   BRepGraph_WireId AppendWire()
0848   {
0849     const BRepGraph_WireId anId = myWires.Append();
0850     myWireRelations.Appended();
0851     return anId;
0852   }
0853 
0854   //! Appends a new face entity and returns its typed id.
0855   BRepGraph_FaceId AppendFace()
0856   {
0857     const BRepGraph_FaceId anId = myFaces.Append();
0858     myFaceRelations.Appended();
0859     return anId;
0860   }
0861 
0862   //! Appends a new shell entity and returns its typed id.
0863   BRepGraph_ShellId AppendShell()
0864   {
0865     const BRepGraph_ShellId anId = myShells.Append();
0866     myShellRelations.Appended();
0867     return anId;
0868   }
0869 
0870   //! Appends a new solid entity and returns its typed id.
0871   BRepGraph_SolidId AppendSolid()
0872   {
0873     const BRepGraph_SolidId anId = mySolids.Append();
0874     mySolidRelations.Appended();
0875     return anId;
0876   }
0877 
0878   //! Appends a new compound entity and returns its typed id.
0879   BRepGraph_CompoundId AppendCompound()
0880   {
0881     const BRepGraph_CompoundId anId = myCompounds.Append();
0882     myCompoundRelations.Appended();
0883     return anId;
0884   }
0885 
0886   //! Appends a new compsolid entity and returns its typed id.
0887   BRepGraph_CompSolidId AppendCompSolid()
0888   {
0889     const BRepGraph_CompSolidId anId = myCompSolids.Append();
0890     myCompSolidRelations.Appended();
0891     return anId;
0892   }
0893 
0894   //! Appends a new product entity and returns its typed id.
0895   BRepGraph_ProductId AppendProduct()
0896   {
0897     const BRepGraph_ProductId anId = myProducts.Append();
0898     myProductRelations.Appended();
0899     return anId;
0900   }
0901 
0902   //! Appends a new occurrence entity and returns its typed id.
0903   BRepGraph_OccurrenceId AppendOccurrence()
0904   {
0905     const BRepGraph_OccurrenceId anId = myOccurrences.Append();
0906     myOccurrenceRelations.Appended();
0907     return anId;
0908   }
0909 
0910   //! Appends a new shell reference entry and returns its typed id.
0911   BRepGraph_ShellRefId AppendShellRef() { return myShellRefs.Append(); }
0912 
0913   //! Appends a new face reference entry and returns its typed id.
0914   BRepGraph_FaceRefId AppendFaceRef() { return myFaceRefs.Append(); }
0915 
0916   //! Appends a new wire reference entry and returns its typed id.
0917   BRepGraph_WireRefId AppendWireRef() { return myWireRefs.Append(); }
0918 
0919   //! Appends a new vertex reference entry and returns its typed id.
0920   BRepGraph_VertexRefId AppendVertexRef() { return myVertexRefs.Append(); }
0921 
0922   //! Appends a new solid reference entry and returns its typed id.
0923   BRepGraph_SolidRefId AppendSolidRef() { return mySolidRefs.Append(); }
0924 
0925   //! Appends a new child reference entry and returns its typed id.
0926   BRepGraph_ChildRefId AppendChildRef() { return myChildRefs.Append(); }
0927 
0928   //! Appends a new occurrence reference entry and returns its typed id.
0929   BRepGraph_OccurrenceRefId AppendOccurrenceRef() { return myOccurrenceRefs.Append(); }
0930 
0931   //! Create a coedge use record binding an edge to a wire within a face context.
0932   //! @param[in] theParentWireId owning wire identifier
0933   //! @param[in] theChildEdgeId  referenced edge identifier
0934   //! @param[in] theFaceId       face context identifier
0935   //! @param[in] theOrientation  orientation of the coedge
0936   //! @return the newly created coedge identifier
0937   Standard_EXPORT BRepGraph_CoEdgeId
0938     CreateCoEdgeUse(const BRepGraph_WireId                theParentWireId,
0939                     const BRepGraph_EdgeId                theChildEdgeId,
0940                     const BRepGraph_FaceId                theFaceId,
0941                     const BRepGraphInc::ParityOrientation theOrientation);
0942 
0943   //! Attach an edge to a vertex by creating a vertex reference.
0944   //! @param[in] theEdgeId   edge identifier
0945   //! @param[in] theVertexId vertex identifier
0946   Standard_EXPORT void AttachEdgeToVertex(const BRepGraph_EdgeId   theEdgeId,
0947                                           const BRepGraph_VertexId theVertexId);
0948 
0949   //! Attach a wire to a face by creating a wire reference.
0950   //! @param[in] theParentFaceId parent face identifier
0951   //! @param[in] theChildWireId  child wire identifier
0952   //! @param[in] theOrientation  orientation within parent
0953   //! @return the newly created wire reference identifier
0954   Standard_EXPORT BRepGraph_WireRefId
0955     AttachWireToFace(const BRepGraph_FaceId                theParentFaceId,
0956                      const BRepGraph_WireId                theChildWireId,
0957                      const BRepGraphInc::ParityOrientation theOrientation = TopAbs_FORWARD);
0958 
0959   //! Attach a face to a shell by creating a face reference.
0960   //! @param[in] theParentShellId parent shell identifier
0961   //! @param[in] theChildFaceId   child face identifier
0962   //! @param[in] theOrientation   orientation within parent
0963   //! @return the newly created face reference identifier
0964   Standard_EXPORT BRepGraph_FaceRefId
0965     AttachFaceToShell(const BRepGraph_ShellId               theParentShellId,
0966                       const BRepGraph_FaceId                theChildFaceId,
0967                       const BRepGraphInc::ParityOrientation theOrientation = TopAbs_FORWARD);
0968 
0969   //! Attach a shell to a solid by creating a shell reference.
0970   //! @param[in] theParentSolidId parent solid identifier
0971   //! @param[in] theChildShellId  child shell identifier
0972   //! @param[in] theOrientation   orientation within parent
0973   //! @return the newly created shell reference identifier
0974   Standard_EXPORT BRepGraph_ShellRefId
0975     AttachShellToSolid(const BRepGraph_SolidId               theParentSolidId,
0976                        const BRepGraph_ShellId               theChildShellId,
0977                        const BRepGraphInc::ParityOrientation theOrientation = TopAbs_FORWARD);
0978 
0979   //! Attach a solid to a compsolid by creating a solid reference.
0980   //! @param[in] theParentCompSolidId parent compsolid identifier
0981   //! @param[in] theChildSolidId      child solid identifier
0982   //! @param[in] theOrientation       orientation within parent
0983   //! @return the newly created solid reference identifier
0984   Standard_EXPORT BRepGraph_SolidRefId
0985     AttachSolidToCompSolid(const BRepGraph_CompSolidId           theParentCompSolidId,
0986                            const BRepGraph_SolidId               theChildSolidId,
0987                            const BRepGraphInc::ParityOrientation theOrientation = TopAbs_FORWARD);
0988 
0989   //! Attach a child node to a compound by creating a child reference.
0990   //! @param[in] theParentCompoundId parent compound identifier
0991   //! @param[in] theChildNodeId      child node identifier
0992   //! @param[in] theLocation         optional location transformation
0993   //! @param[in] theOrientation      orientation within parent
0994   //! @return the newly created child reference identifier
0995   Standard_EXPORT BRepGraph_ChildRefId
0996     AttachChildToCompound(const BRepGraph_CompoundId            theParentCompoundId,
0997                           const BRepGraph_NodeId                theChildNodeId,
0998                           const TopLoc_Location&                theLocation    = TopLoc_Location(),
0999                           const BRepGraphInc::ParityOrientation theOrientation = TopAbs_FORWARD);
1000 
1001   //! Attach an occurrence to a product by creating an occurrence reference.
1002   //! @param[in] theParentProductId     parent product identifier
1003   //! @param[in] theChildOccurrenceId   child occurrence identifier
1004   //! @param[in] theLocation            optional location transformation
1005   //! @return the newly created occurrence reference identifier
1006   Standard_EXPORT BRepGraph_OccurrenceRefId
1007     AttachOccurrenceToProduct(const BRepGraph_ProductId    theParentProductId,
1008                               const BRepGraph_OccurrenceId theChildOccurrenceId,
1009                               const TopLoc_Location&       theLocation = TopLoc_Location());
1010 
1011   //! Detach a coedge use from its parent wire.
1012   //! @param[in] theParentWireId owning wire identifier
1013   //! @param[in] theCoEdgeId     coedge identifier to detach
1014   //! @return true if the coedge was found and removed
1015   Standard_EXPORT bool DetachCoEdgeUse(const BRepGraph_WireId   theParentWireId,
1016                                        const BRepGraph_CoEdgeId theCoEdgeId);
1017 
1018   //! Replace a single coedge with a pair of new coedges in a wire.
1019   //! @param[in] theParentWireId    owning wire identifier
1020   //! @param[in] theOldCoEdgeId     coedge to replace
1021   //! @param[in] theNewFirstCoEdgeId  first replacement coedge
1022   //! @param[in] theNewSecondCoEdgeId second replacement coedge
1023   //! @return true if the replacement succeeded
1024   Standard_EXPORT bool ReplaceCoEdgeUseWithPair(const BRepGraph_WireId   theParentWireId,
1025                                                 const BRepGraph_CoEdgeId theOldCoEdgeId,
1026                                                 const BRepGraph_CoEdgeId theNewFirstCoEdgeId,
1027                                                 const BRepGraph_CoEdgeId theNewSecondCoEdgeId);
1028 
1029   //! Detach a wire reference from its parent face.
1030   //! @param[in] theParentFaceId parent face identifier
1031   //! @param[in] theRefId        wire reference identifier to detach
1032   //! @return true if the reference was found and removed
1033   Standard_EXPORT bool DetachWireFromFace(const BRepGraph_FaceId    theParentFaceId,
1034                                           const BRepGraph_WireRefId theRefId);
1035 
1036   //! Detach a face reference from its parent shell.
1037   //! @param[in] theParentShellId parent shell identifier
1038   //! @param[in] theRefId         face reference identifier to detach
1039   //! @return true if the reference was found and removed
1040   Standard_EXPORT bool DetachFaceFromShell(const BRepGraph_ShellId   theParentShellId,
1041                                            const BRepGraph_FaceRefId theRefId);
1042 
1043   //! Detach a shell reference from its parent solid.
1044   //! @param[in] theParentSolidId parent solid identifier
1045   //! @param[in] theRefId         shell reference identifier to detach
1046   //! @return true if the reference was found and removed
1047   Standard_EXPORT bool DetachShellFromSolid(const BRepGraph_SolidId    theParentSolidId,
1048                                             const BRepGraph_ShellRefId theRefId);
1049 
1050   //! Detach a solid reference from its parent compsolid.
1051   //! @param[in] theParentCompSolidId parent compsolid identifier
1052   //! @param[in] theRefId             solid reference identifier to detach
1053   //! @return true if the reference was found and removed
1054   Standard_EXPORT bool DetachSolidFromCompSolid(const BRepGraph_CompSolidId theParentCompSolidId,
1055                                                 const BRepGraph_SolidRefId  theRefId);
1056 
1057   //! Detach a child reference from its parent compound.
1058   //! @param[in] theParentCompoundId parent compound identifier
1059   //! @param[in] theRefId            child reference identifier to detach
1060   //! @return true if the reference was found and removed
1061   Standard_EXPORT bool DetachChildFromCompound(const BRepGraph_CompoundId theParentCompoundId,
1062                                                const BRepGraph_ChildRefId theRefId);
1063 
1064   //! Detach an occurrence reference from its parent product.
1065   //! @param[in] theParentProductId parent product identifier
1066   //! @param[in] theRefId           occurrence reference identifier to detach
1067   //! @return true if the reference was found and removed
1068   Standard_EXPORT bool DetachOccurrenceFromProduct(const BRepGraph_ProductId theParentProductId,
1069                                                    const BRepGraph_OccurrenceRefId theRefId);
1070 
1071   //! Rebind the child node of an occurrence to a new node.
1072   //! @param[in] theOccurrence occurrence identifier
1073   //! @param[in] theOldChild   old child node identifier
1074   //! @param[in] theNewChild   new child node identifier
1075   Standard_EXPORT void RebindOccurrenceChild(const BRepGraph_OccurrenceId theOccurrence,
1076                                              const BRepGraph_NodeId       theOldChild,
1077                                              const BRepGraph_NodeId       theNewChild);
1078 
1079   //! Rebind vertex edge references from one vertex to another, excluding a specific ref.
1080   //! @param[in] theOldVertex   old vertex identifier
1081   //! @param[in] theNewVertex   new vertex identifier
1082   //! @param[in] theEdge        edge identifier
1083   //! @param[in] theExcludingRef reference identifier to exclude from rebinding
1084   Standard_EXPORT void RebindVertexEdge(const BRepGraph_VertexId    theOldVertex,
1085                                         const BRepGraph_VertexId    theNewVertex,
1086                                         const BRepGraph_EdgeId      theEdge,
1087                                         const BRepGraph_VertexRefId theExcludingRef);
1088 
1089   //! Rebind a vertex reference to point to a new vertex.
1090   //! @param[in] theRefId     vertex reference identifier
1091   //! @param[in] theOldVertex old vertex identifier
1092   //! @param[in] theNewVertex new vertex identifier
1093   Standard_EXPORT void RebindVertexRef(const BRepGraph_VertexRefId theRefId,
1094                                        const BRepGraph_VertexId    theOldVertex,
1095                                        const BRepGraph_VertexId    theNewVertex);
1096 
1097   //! Rebind a coedge to reference a different edge.
1098   //! @param[in] theCoEdge  coedge identifier
1099   //! @param[in] theOldEdge old edge identifier
1100   //! @param[in] theNewEdge new edge identifier
1101   Standard_EXPORT void RebindCoEdgeEdge(const BRepGraph_CoEdgeId theCoEdge,
1102                                         const BRepGraph_EdgeId   theOldEdge,
1103                                         const BRepGraph_EdgeId   theNewEdge);
1104 
1105   //! Rebind a wire reference to point to a new wire.
1106   //! @param[in] theRefId   wire reference identifier
1107   //! @param[in] theOldWire old wire identifier
1108   //! @param[in] theNewWire new wire identifier
1109   Standard_EXPORT void RebindWireRef(const BRepGraph_WireRefId theRefId,
1110                                      const BRepGraph_WireId    theOldWire,
1111                                      const BRepGraph_WireId    theNewWire);
1112 
1113   //! Rebind a face reference to point to a new face.
1114   //! @param[in] theRefId   face reference identifier
1115   //! @param[in] theOldFace old face identifier
1116   //! @param[in] theNewFace new face identifier
1117   Standard_EXPORT void RebindFaceRef(const BRepGraph_FaceRefId theRefId,
1118                                      const BRepGraph_FaceId    theOldFace,
1119                                      const BRepGraph_FaceId    theNewFace);
1120 
1121   //! Rebind a shell reference to point to a new shell.
1122   //! @param[in] theRefId    shell reference identifier
1123   //! @param[in] theOldShell old shell identifier
1124   //! @param[in] theNewShell new shell identifier
1125   Standard_EXPORT void RebindShellRef(const BRepGraph_ShellRefId theRefId,
1126                                       const BRepGraph_ShellId    theOldShell,
1127                                       const BRepGraph_ShellId    theNewShell);
1128 
1129   //! Rebind a solid reference to point to a new solid.
1130   //! @param[in] theRefId    solid reference identifier
1131   //! @param[in] theOldSolid old solid identifier
1132   //! @param[in] theNewSolid new solid identifier
1133   Standard_EXPORT void RebindSolidRef(const BRepGraph_SolidRefId theRefId,
1134                                       const BRepGraph_SolidId    theOldSolid,
1135                                       const BRepGraph_SolidId    theNewSolid);
1136 
1137   //! Rebind a child reference to point to a new child node.
1138   //! @param[in] theRefId   child reference identifier
1139   //! @param[in] theOldChild old child node identifier
1140   //! @param[in] theNewChild new child node identifier
1141   Standard_EXPORT void RebindChildRef(const BRepGraph_ChildRefId theRefId,
1142                                       const BRepGraph_NodeId     theOldChild,
1143                                       const BRepGraph_NodeId     theNewChild);
1144 
1145   //! Rebind an occurrence reference to point to a new occurrence.
1146   //! @param[in] theRefId         occurrence reference identifier
1147   //! @param[in] theOldOccurrence old occurrence identifier
1148   //! @param[in] theNewOccurrence new occurrence identifier
1149   Standard_EXPORT void RebindOccurrenceRef(const BRepGraph_OccurrenceRefId theRefId,
1150                                            const BRepGraph_OccurrenceId    theOldOccurrence,
1151                                            const BRepGraph_OccurrenceId    theNewOccurrence);
1152 
1153   //! Reverse the order of coedges in a wire.
1154   //! @param[in] theWireId wire identifier
1155   Standard_EXPORT void ReverseWireCoEdges(const BRepGraph_WireId theWireId);
1156 
1157   //! Replace the coedge list of a wire with a new set.
1158   //! @param[in] theWireId    wire identifier
1159   //! @param[in] theCoEdgeIds new coedge identifiers
1160   Standard_EXPORT void SetWireCoEdges(const BRepGraph_WireId                        theWireId,
1161                                       const NCollection_Array1<BRepGraph_CoEdgeId>& theCoEdgeIds);
1162 
1163   //! Replace the wire reference list of a face with a new set.
1164   //! @param[in] theFaceId    face identifier
1165   //! @param[in] theWireRefIds new wire reference identifiers
1166   Standard_EXPORT void SetFaceWireRefs(
1167     const BRepGraph_FaceId                         theFaceId,
1168     const NCollection_Array1<BRepGraph_WireRefId>& theWireRefIds);
1169 
1170   //! Replace the face reference list of a shell with a new set.
1171   //! @param[in] theShellId   shell identifier
1172   //! @param[in] theFaceRefIds new face reference identifiers
1173   Standard_EXPORT void SetShellFaceRefs(
1174     const BRepGraph_ShellId                        theShellId,
1175     const NCollection_Array1<BRepGraph_FaceRefId>& theFaceRefIds);
1176 
1177   //! Replace the shell reference list of a solid with a new set.
1178   //! @param[in] theSolidId    solid identifier
1179   //! @param[in] theShellRefIds new shell reference identifiers
1180   Standard_EXPORT void SetSolidShellRefs(
1181     const BRepGraph_SolidId                         theSolidId,
1182     const NCollection_Array1<BRepGraph_ShellRefId>& theShellRefIds);
1183 
1184   //! Replace the solid reference list of a compsolid with a new set.
1185   //! @param[in] theCompSolidId compsolid identifier
1186   //! @param[in] theSolidRefIds new solid reference identifiers
1187   Standard_EXPORT void SetCompSolidSolidRefs(
1188     const BRepGraph_CompSolidId                     theCompSolidId,
1189     const NCollection_Array1<BRepGraph_SolidRefId>& theSolidRefIds);
1190 
1191   //! Replace the child reference list of a compound with a new set.
1192   //! @param[in] theCompoundId compound identifier
1193   //! @param[in] theChildRefIds new child reference identifiers
1194   Standard_EXPORT void SetCompoundChildRefs(
1195     const BRepGraph_CompoundId                      theCompoundId,
1196     const NCollection_Array1<BRepGraph_ChildRefId>& theChildRefIds);
1197 
1198   //! Replace the occurrence reference list of a product with a new set.
1199   //! @param[in] theProductId       product identifier
1200   //! @param[in] theOccurrenceRefIds new occurrence reference identifiers
1201   Standard_EXPORT void SetProductOccurrenceRefs(
1202     const BRepGraph_ProductId                            theProductId,
1203     const NCollection_Array1<BRepGraph_OccurrenceRefId>& theOccurrenceRefIds);
1204 
1205   //! Return the BaseRef portion of any ref entry by generic RefId.
1206   //! @param[in] theRefId generic reference identifier
1207   //! @return const reference to the BaseRef base of the ref entry
1208   [[nodiscard]] Standard_EXPORT const BRepGraphInc::BaseRef& BaseRef(
1209     const BRepGraph_RefId theRefId) const;
1210 
1211   //! Return the mutable BaseRef portion of any ref entry by generic RefId.
1212   //! @param[in] theRefId generic reference identifier
1213   //! @return mutable pointer to the BaseRef base of the ref entry, or nullptr if not found
1214   [[nodiscard]] Standard_EXPORT BRepGraphInc::BaseRef* ChangeBaseRef(
1215     const BRepGraph_RefId theRefId);
1216 
1217   //! Resolve an active node UID through storage reverse maps.
1218   [[nodiscard]] Standard_EXPORT BRepGraph_NodeId FindNodeIdByUID(const BRepGraph_UID& theUID) const;
1219 
1220   //! Resolve an active reference UID through storage reverse maps.
1221   [[nodiscard]] Standard_EXPORT BRepGraph_RefId
1222     FindRefIdByUID(const BRepGraph_RefUID& theUID) const;
1223 
1224   //! Returns the node id bound to the given shape definition key, or invalid if not bound.
1225   [[nodiscard]] Standard_EXPORT BRepGraph_NodeId
1226     FindDefinitionByShape(const TopoDS_Shape& theShape) const;
1227 
1228   //! Returns true if the given shape definition key is bound to a node.
1229   [[nodiscard]] Standard_EXPORT bool HasShapeBinding(const TopoDS_Shape& theShape) const;
1230 
1231   //! Set or update the shape-to-node binding. Uses replacement semantics:
1232   //! binds if absent, updates if already bound.
1233   Standard_EXPORT void SetDefinitionShapeBinding(const TopoDS_Shape&    theShape,
1234                                                  const BRepGraph_NodeId theNodeId);
1235 
1236   //! Remove the shape-to-node binding only if it points to the expected node.
1237   //! Returns true if the binding was removed.
1238   Standard_EXPORT bool RemoveDefinitionShapeBinding(const TopoDS_Shape&    theShape,
1239                                                     const BRepGraph_NodeId theExpectedNodeId);
1240 
1241   //! Back-reference to the construction-time `TopoDS_Shape` key a node was built from.
1242   //! Only populated during graph construction; absent bindings are a valid state.
1243 
1244   //! Returns the original shape for the given node id, or a null shape if not bound.
1245   [[nodiscard]] Standard_EXPORT TopoDS_Shape FindOriginal(const BRepGraph_NodeId theNodeId) const;
1246 
1247   //! Returns true if the given node id has an original shape binding.
1248   [[nodiscard]] Standard_EXPORT bool HasOriginal(const BRepGraph_NodeId theNodeId) const;
1249 
1250   //! Binds the given node id to its construction-time shape key.
1251   Standard_EXPORT void BindOriginal(const BRepGraph_NodeId theNodeId, const TopoDS_Shape& theShape);
1252 
1253   //! Removes the original shape binding for the given node id.
1254   Standard_EXPORT void UnBindOriginal(const BRepGraph_NodeId theNodeId);
1255 
1256   //! Iterate all shape-to-NodeId bindings, invoking theFunc(shape, NodeId) for each entry.
1257   //! Used by Compact to rebuild the map after the rebuild-and-swap.
1258   template <typename FuncT>
1259   void ForEachShapeBinding(FuncT&& theFunc) const
1260   {
1261     std::shared_lock<std::shared_mutex> aLock(myShapeBindingsMutex);
1262     for (NCollection_FlatDataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>::Iterator
1263            anIt(myShapeToNodeId);
1264          anIt.More();
1265          anIt.Next())
1266     {
1267       theFunc(anIt.Key(), anIt.Value());
1268     }
1269   }
1270 
1271   //! Iterate all NodeId-to-OriginalShape bindings, invoking theFunc(NodeId, Shape) for each entry.
1272   //! Used by Compact to rebuild the map after the rebuild-and-swap.
1273   template <typename FuncT>
1274   void ForEachOriginalBinding(FuncT&& theFunc) const
1275   {
1276     std::shared_lock<std::shared_mutex> aLock(myShapeBindingsMutex);
1277     for (NCollection_FlatDataMap<BRepGraph_NodeId, TopoDS_Shape>::Iterator anIt(myOriginalShapes);
1278          anIt.More();
1279          anIt.Next())
1280     {
1281       theFunc(anIt.Key(), anIt.Value());
1282     }
1283   }
1284 
1285   //! Copy shape-to-NodeId and Original shape bindings from another storage.
1286   //! Used by identity copy to preserve shape reconstruction bindings.
1287   Standard_EXPORT void CopyShapeBindingsFrom(const BRepGraphInc_Storage& theSource);
1288 
1289   //! Return the generation-validated node-to-shape reconstruction cache.
1290   [[nodiscard]] const NCollection_FlatDataMap<BRepGraph_NodeId, CachedShape>& CurrentShapes() const
1291   {
1292     return myCurrentShapes;
1293   }
1294 
1295   //! Return the mutable generation-validated node-to-shape reconstruction cache.
1296   NCollection_FlatDataMap<BRepGraph_NodeId, CachedShape>& ChangeCurrentShapes()
1297   {
1298     return myCurrentShapes;
1299   }
1300 
1301   //! Return the mutex protecting the reconstruction cache.
1302   [[nodiscard]] std::shared_mutex& CurrentShapesMutex() const { return myCurrentShapesMutex; }
1303 
1304   //! Clear the generation-validated shape reconstruction cache.
1305   Standard_EXPORT void ClearCurrentShapes();
1306 
1307   //! Remove one entry from the generation-validated shape reconstruction cache.
1308   Standard_EXPORT void UnbindCurrentShape(const BRepGraph_NodeId theNode);
1309 
1310   //! Clear deferred invalidation queues and release their batch allocator.
1311   Standard_EXPORT void ClearDeferredQueues();
1312 
1313   //! Clear all storage.
1314   Standard_EXPORT void Clear();
1315 
1316   //! Prepare fixed-size destination ranges for indexed load.
1317   //!
1318   //! This is an internal backend preparation API intended for persistence read
1319   //! paths that know final section sizes in advance. It clears previous content,
1320   //! pre-sizes defs/refs/reps and UID vectors, and initializes relation tables
1321   //! exactly once. The load path then restores serialized relation lists and
1322   //! calls RebuildDerivedRelations() once to refresh derived incoming maps.
1323   //! @param theCounts final per-section slot counts.
1324   Standard_EXPORT void PrepareForLoad(const BRepGraphInc_Load::Counts& theCounts);
1325 
1326   //! Override active-slot counters after a trusted indexed load path.
1327   //!
1328   //! This is intended for persistence backends that already touched every slot
1329   //! during load and therefore know exact active counts without rescanning
1330   //! storage after relation construction.
1331   //! @param theCounts trusted active per-section counts.
1332   Standard_EXPORT void SetActiveCounts(const BRepGraphInc_Load::Counts& theCounts);
1333 
1334   //! Build a Counts struct from current allocated slot counts.
1335   [[nodiscard]] Standard_EXPORT BRepGraphInc_Load::Counts Counts() const;
1336 
1337   //! Build a Counts struct from current active (non-removed) counts.
1338   [[nodiscard]] Standard_EXPORT BRepGraphInc_Load::Counts ActiveCounts() const;
1339 
1340   //! Recount active-slot counters from current `IsRemoved` flags without rebuilding indexes.
1341   Standard_EXPORT void RecountActiveCounts();
1342 
1343   //! Rebuild centralized relation tables from entity and reference endpoints.
1344   //! This is intended for raw load, compact, and explicit repair paths only;
1345   //! editor mutations maintain relation containers incrementally.
1346   Standard_EXPORT void RebuildDerivedRelations();
1347 
1348   //! Rebuild relation maps after a trusted load already restored active counts.
1349   Standard_EXPORT void RebuildDerivedRelationsPreservingActiveCounts();
1350 
1351   //! Bulk-copy all RemovedFlags bit-planes from theSource.
1352   //! Source must have been loaded with the same entity counts (identity copy path).
1353   Standard_EXPORT void CopyRemovedFlagsFrom(const BRepGraphInc_Storage& theSource);
1354 
1355   //! Debug: verify relation-table consistency against entity/reference endpoints.
1356   //! @return true if all relations are consistent
1357   Standard_EXPORT bool ValidateRelations() const;
1358 
1359   //! Verify coedge ordering consistency for a specific wire.
1360   //! @param[in] theWireId wire identifier
1361   //! @return true if the coedge order is valid
1362   Standard_EXPORT bool ValidateWireCoEdgeOrder(const BRepGraph_WireId theWireId) const;
1363 
1364   //! Verify coedge ordering consistency for all wires.
1365   //! @return true if all wire coedge orders are valid
1366   Standard_EXPORT bool ValidateWireCoEdgeOrders() const;
1367 
1368   //! Result of wire coedge order canonicalization.
1369   enum class WireCoEdgeOrderStatus
1370   {
1371     Connected,        //!< Stored order was already connected.
1372     Reordered,        //!< Stored coedges were reordered into an exactly connected chain.
1373     ToleranceOrdered, //!< Stored coedges were ordered using tolerance-equivalent endpoints.
1374     Partial,          //!< Stored coedges were grouped into best-effort connected runs.
1375     InvalidInput      //!< Wire or coedge ownership/input data is invalid.
1376   };
1377 
1378   //! Canonicalize the coedge ordering of a wire and report the achieved order quality.
1379   //! @param[in] theWireId wire identifier
1380   //! @return canonicalization status
1381   Standard_EXPORT WireCoEdgeOrderStatus
1382     CanonicalizeWireCoEdgeOrderStatus(const BRepGraph_WireId theWireId);
1383 
1384   //! Canonicalize the coedge ordering of a wire to a consistent form.
1385   //! @param[in] theWireId wire identifier
1386   //! @return true if canonicalization succeeded
1387   Standard_EXPORT bool CanonicalizeWireCoEdgeOrder(const BRepGraph_WireId theWireId);
1388 
1389   //! Rebuild UID reverse indexes (UID->NodeId, RefUID->RefId)
1390   //! from the current UID vectors. Clears indexes and resets allocators before rebuilding.
1391   //! Called after Compact, Load, etc. where UID vectors have been modified externally.
1392   Standard_EXPORT void RebuildUIDReverseIndexes();
1393 
1394   //! Mark UID reverse indexes stale after bulk UID-vector replacement.
1395   Standard_EXPORT void MarkUIDReverseIndexesDirty();
1396 
1397   //! Lazily rebuild the node UID reverse index if it is stale.
1398   Standard_EXPORT void EnsureUIDReverseIndex() const;
1399 
1400   //! Lazily rebuild the reference UID reverse index if it is stale.
1401   Standard_EXPORT void EnsureRefUIDReverseIndex() const;
1402 
1403   //! Copy all forward/reverse relation vectors directly from theSource.
1404   //! Used by identity copy to avoid the clear+rebuild cycle.
1405   Standard_EXPORT void CopyDerivedRelationsFrom(const BRepGraphInc_Storage& theSource);
1406 
1407 private:
1408   friend class BRepGraphInc_Populate;
1409   friend class BRepGraph;
1410 
1411   Standard_EXPORT void ClearStorageForReuse();
1412   Standard_EXPORT void ClearUIDIndexes();
1413   Standard_EXPORT void ClearShapeCache();
1414   Standard_EXPORT void ClearRelations();
1415 
1416   BRepGraphInc::FaceRelations& ChangeFaceRelationsInternal(const BRepGraph_FaceId theId)
1417   {
1418     return myFaceRelations.ChangeValue(static_cast<size_t>(theId.Index));
1419   }
1420 
1421   BRepGraphInc::WireRelations& ChangeWireRelationsInternal(const BRepGraph_WireId theId)
1422   {
1423     return myWireRelations.ChangeValue(static_cast<size_t>(theId.Index));
1424   }
1425 
1426   BRepGraphInc::EdgeRelations& ChangeEdgeRelationsInternal(const BRepGraph_EdgeId theId)
1427   {
1428     return myEdgeRelations.ChangeValue(static_cast<size_t>(theId.Index));
1429   }
1430 
1431   BRepGraphInc::ShellRelations& ChangeShellRelationsInternal(const BRepGraph_ShellId theId)
1432   {
1433     return myShellRelations.ChangeValue(static_cast<size_t>(theId.Index));
1434   }
1435 
1436   BRepGraphInc::SolidRelations& ChangeSolidRelationsInternal(const BRepGraph_SolidId theId)
1437   {
1438     return mySolidRelations.ChangeValue(static_cast<size_t>(theId.Index));
1439   }
1440 
1441   BRepGraphInc::CompoundRelations& ChangeCompoundRelationsInternal(const BRepGraph_CompoundId theId)
1442   {
1443     return myCompoundRelations.ChangeValue(static_cast<size_t>(theId.Index));
1444   }
1445 
1446   BRepGraphInc::CompSolidRelations& ChangeCompSolidRelationsInternal(
1447     const BRepGraph_CompSolidId theId)
1448   {
1449     return myCompSolidRelations.ChangeValue(static_cast<size_t>(theId.Index));
1450   }
1451 
1452   BRepGraphInc::VertexRelations& ChangeVertexRelationsInternal(const BRepGraph_VertexId theId)
1453   {
1454     return myVertexRelations.ChangeValue(static_cast<size_t>(theId.Index));
1455   }
1456 
1457   BRepGraphInc::ProductRelations& ChangeProductRelationsInternal(const BRepGraph_ProductId theId)
1458   {
1459     return myProductRelations.ChangeValue(static_cast<size_t>(theId.Index));
1460   }
1461 
1462   BRepGraphInc::OccurrenceRelations& ChangeOccurrenceRelationsInternal(
1463     const BRepGraph_OccurrenceId theId)
1464   {
1465     return myOccurrenceRelations.ChangeValue(static_cast<size_t>(theId.Index));
1466   }
1467 
1468   Standard_EXPORT NCollection_LinearVector<BRepGraph_ChildRefId>& ChangeCompoundRefsOfNodeInternal(
1469     const BRepGraph_NodeId theNode);
1470 
1471   Standard_EXPORT NCollection_LinearVector<BRepGraph_OccurrenceRefId>&
1472                   ChangeOccurrenceRefsOfNodeInternal(const BRepGraph_NodeId theNode);
1473 
1474   Standard_EXPORT void rebuildDerivedRelationsInternal(const bool theRecountActiveCounts);
1475 
1476   //! Return true if the typed entity has at least one parent compound (internal).
1477   template <typename T>
1478   [[nodiscard]] bool HasCompoundParentTyped(const T theId) const;
1479 
1480   //! Set or clear the "has parent compound" flag for a typed entity (internal).
1481   template <typename T>
1482   void SetHasCompoundParentTyped(const T theId, const bool theVal);
1483 
1484   //! Return true if the typed entity has at least one parent occurrence (internal).
1485   template <typename T>
1486   [[nodiscard]] bool HasOccurrenceParentTyped(const T theId) const;
1487 
1488   //! Set or clear the "has parent occurrence" flag for a typed entity (internal).
1489   template <typename T>
1490   void SetHasOccurrenceParentTyped(const T theId, const bool theVal);
1491 
1492   //! Set or clear the "has parent compound" flag for a generic NodeId (internal dispatch).
1493   Standard_EXPORT void SetHasCompoundParent(const BRepGraph_NodeId theNode, bool theVal);
1494 
1495   //! Set or clear the "has parent occurrence" flag for a generic NodeId (internal dispatch).
1496   Standard_EXPORT void SetHasOccurrenceParent(const BRepGraph_NodeId theNode, bool theVal);
1497 
1498   //! Template store for topology entity kinds.
1499   template <typename EntityT>
1500   struct DefStore
1501   {
1502     using TypeId    = typename EntityT::TypeId;
1503     using ValueType = EntityT;
1504 
1505     //! Entity representations stored by typed id index.
1506     NCollection_DynamicArray<EntityT> Entities;
1507 
1508     //! Bit-flag plane for soft-removal status.
1509     BRepGraphInc_BitFlags RemovedFlags;
1510 
1511     //! Bit-flag plane for ownership status.
1512     BRepGraphInc_BitFlags OwnedFlags;
1513 
1514     //! Bit-flag plane for active MutGuard tracking.
1515     BRepGraphInc_BitFlags GuardFlags;
1516 
1517     //! Bit-flag plane: node has at least one parent compound (ChildRef).
1518     BRepGraphInc_BitFlags HasCompoundParentFlags;
1519 
1520     //! Bit-flag plane: node has at least one parent occurrence (OccurrenceRef).
1521     BRepGraphInc_BitFlags HasOccurrenceParentFlags;
1522 
1523     //! Number of non-removed entities currently present in the store.
1524     uint32_t NbActive = 0;
1525 
1526     //! Per-kind monotonic UID counter. Valid UIDs start at 1.
1527     std::atomic<uint32_t> NextUIDCounter{1};
1528 
1529     DefStore() = delete;
1530 
1531     DefStore(const int theBlockSize, const occ::handle<NCollection_BaseAllocator>& theAlloc)
1532         : Entities(theBlockSize, theAlloc)
1533     {
1534     }
1535 
1536     uint32_t Nb() const { return static_cast<uint32_t>(Entities.Size()); }
1537 
1538     const EntityT& Get(const TypeId theId) const
1539     {
1540       return Entities.Value(static_cast<size_t>(theId.Index));
1541     }
1542 
1543     EntityT& Change(const TypeId theId)
1544     {
1545       return Entities.ChangeValue(static_cast<size_t>(theId.Index));
1546     }
1547 
1548     //! Append a default-constructed entity and return its typed slot id.
1549     TypeId Append()
1550     {
1551       const TypeId anId(static_cast<uint32_t>(Entities.Size()));
1552       ++NbActive;
1553       Entities.Appended();
1554       RemovedFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1555       OwnedFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1556       GuardFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1557       HasCompoundParentFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1558       HasOccurrenceParentFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1559       return anId;
1560     }
1561 
1562     void DecrementActive()
1563     {
1564       Standard_ASSERT_VOID(NbActive > 0u, "DefStore::DecrementActive: underflow");
1565       if (NbActive > 0u)
1566       {
1567         --NbActive;
1568       }
1569     }
1570 
1571     bool MarkRemoved(const TypeId theId)
1572     {
1573       if (!theId.IsValid(Nb()))
1574       {
1575         return false;
1576       }
1577 
1578       if (RemovedFlags.Test(theId.Index))
1579       {
1580         return false;
1581       }
1582       RemovedFlags.Set(theId.Index);
1583       DecrementActive();
1584       return true;
1585     }
1586 
1587     void Clear(const bool theReleaseMemory = false)
1588     {
1589       Entities.Clear(theReleaseMemory);
1590       RemovedFlags.ClearAll();
1591       OwnedFlags.ClearAll();
1592       GuardFlags.ClearAll();
1593       HasCompoundParentFlags.ClearAll();
1594       HasOccurrenceParentFlags.ClearAll();
1595       NbActive = 0;
1596       // Note: NextUIDCounter is NOT reset. UIDs stay monotonic across Clear() cycles.
1597       // Generation + GraphGUID protect against stale UID aliasing.
1598     }
1599   };
1600 
1601   //! Template store for transitional reference kinds.
1602   template <typename RefT>
1603   struct RefStore
1604   {
1605     using TypeId    = typename RefT::TypeId;
1606     using ValueType = RefT;
1607 
1608     //! Reference representations stored by typed id index.
1609     NCollection_DynamicArray<RefT> Refs;
1610 
1611     //! Bit-flag plane for soft-removal status.
1612     BRepGraphInc_BitFlags RemovedFlags;
1613 
1614     //! Bit-flag plane for ownership status.
1615     BRepGraphInc_BitFlags OwnedFlags;
1616 
1617     //! Bit-flag plane for active MutGuard tracking.
1618     BRepGraphInc_BitFlags GuardFlags;
1619 
1620     //! Bit-flag plane: ref has at least one parent compound (unused for refs, kept for macro
1621     //! uniformity).
1622     BRepGraphInc_BitFlags HasCompoundParentFlags;
1623 
1624     //! Bit-flag plane: ref has at least one parent occurrence (unused for refs, kept for macro
1625     //! uniformity).
1626     BRepGraphInc_BitFlags HasOccurrenceParentFlags;
1627 
1628     //! Number of non-removed references currently present in the store.
1629     uint32_t NbActive = 0;
1630 
1631     //! Per-kind monotonic UID counter. Valid UIDs start at 1.
1632     std::atomic<uint32_t> NextUIDCounter{1};
1633 
1634     RefStore() = delete;
1635 
1636     RefStore(const int theBlockSize, const occ::handle<NCollection_BaseAllocator>& theAlloc)
1637         : Refs(theBlockSize, theAlloc)
1638     {
1639     }
1640 
1641     uint32_t Nb() const { return static_cast<uint32_t>(Refs.Size()); }
1642 
1643     const RefT& Get(const TypeId theId) const
1644     {
1645       return Refs.Value(static_cast<size_t>(theId.Index));
1646     }
1647 
1648     RefT& Change(const TypeId theId) { return Refs.ChangeValue(static_cast<size_t>(theId.Index)); }
1649 
1650     //! Append a default-constructed ref entry and return its typed slot id.
1651     TypeId Append()
1652     {
1653       const TypeId anId(static_cast<uint32_t>(Refs.Size()));
1654       ++NbActive;
1655       Refs.Appended();
1656       RemovedFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1657       OwnedFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1658       GuardFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1659       HasCompoundParentFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1660       HasOccurrenceParentFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1661       return anId;
1662     }
1663 
1664     void DecrementActive()
1665     {
1666       Standard_ASSERT_VOID(NbActive > 0u, "RefStore::DecrementActive: underflow");
1667       if (NbActive > 0u)
1668       {
1669         --NbActive;
1670       }
1671     }
1672 
1673     bool MarkRemoved(const TypeId theId)
1674     {
1675       if (!theId.IsValid(Nb()))
1676       {
1677         return false;
1678       }
1679 
1680       if (RemovedFlags.Test(theId.Index))
1681       {
1682         return false;
1683       }
1684       RemovedFlags.Set(theId.Index);
1685       DecrementActive();
1686       return true;
1687     }
1688 
1689     void Clear(const bool theReleaseMemory = false)
1690     {
1691       Refs.Clear(theReleaseMemory);
1692       RemovedFlags.ClearAll();
1693       OwnedFlags.ClearAll();
1694       GuardFlags.ClearAll();
1695       HasCompoundParentFlags.ClearAll();
1696       HasOccurrenceParentFlags.ClearAll();
1697       NbActive = 0;
1698       // Note: NextUIDCounter is NOT reset. UIDs stay monotonic across Clear() cycles.
1699     }
1700   };
1701 
1702   //! Primary allocator for backend arrays, stores, and transient backend maps.
1703   occ::handle<NCollection_IncAllocator> myAllocator = new NCollection_IncAllocator;
1704 
1705   //! Backend-owned root products and deferred invalidation queues.
1706   NCollection_LinearVector<BRepGraph_ProductId> myRootProductIds;
1707   NCollection_LinearVector<BRepGraph_NodeId>    myDeferredModified;
1708   NCollection_LinearVector<BRepGraph_RefId>     myDeferredRefModified;
1709 
1710   //! Vertex definition store.
1711   DefStore<BRepGraphInc::VertexDef> myVertices;
1712 
1713   //! Edge definition store.
1714   DefStore<BRepGraphInc::EdgeDef> myEdges;
1715 
1716   //! Coedge definition store.
1717   DefStore<BRepGraphInc::CoEdgeDef> myCoEdges;
1718 
1719   //! Wire definition store.
1720   DefStore<BRepGraphInc::WireDef> myWires;
1721 
1722   //! Face definition store.
1723   DefStore<BRepGraphInc::FaceDef> myFaces;
1724 
1725   //! Shell definition store.
1726   DefStore<BRepGraphInc::ShellDef> myShells;
1727 
1728   //! Solid definition store.
1729   DefStore<BRepGraphInc::SolidDef> mySolids;
1730 
1731   //! Compound definition store.
1732   DefStore<BRepGraphInc::CompoundDef> myCompounds;
1733 
1734   //! CompSolid definition store.
1735   DefStore<BRepGraphInc::CompSolidDef> myCompSolids;
1736 
1737   //! Product definition store.
1738   DefStore<BRepGraphInc::ProductDef> myProducts;
1739 
1740   //! Occurrence definition store.
1741   DefStore<BRepGraphInc::OccurrenceDef> myOccurrences;
1742 
1743   //! Shell reference store.
1744   RefStore<BRepGraphInc::ShellRef> myShellRefs;
1745 
1746   //! Face reference store.
1747   RefStore<BRepGraphInc::FaceRef> myFaceRefs;
1748 
1749   //! Wire reference store.
1750   RefStore<BRepGraphInc::WireRef> myWireRefs;
1751 
1752   //! Vertex reference store.
1753   RefStore<BRepGraphInc::VertexRef> myVertexRefs;
1754 
1755   //! Solid reference store.
1756   RefStore<BRepGraphInc::SolidRef> mySolidRefs;
1757 
1758   //! Child reference store.
1759   RefStore<BRepGraphInc::ChildRef> myChildRefs;
1760 
1761   //! Occurrence reference store.
1762   RefStore<BRepGraphInc::OccurrenceRef> myOccurrenceRefs;
1763 
1764   //! Centralized relation tables parallel to entity stores.
1765   NCollection_DynamicArray<BRepGraphInc::FaceRelations>       myFaceRelations;
1766   NCollection_DynamicArray<BRepGraphInc::WireRelations>       myWireRelations;
1767   NCollection_DynamicArray<BRepGraphInc::EdgeRelations>       myEdgeRelations;
1768   NCollection_DynamicArray<BRepGraphInc::ShellRelations>      myShellRelations;
1769   NCollection_DynamicArray<BRepGraphInc::SolidRelations>      mySolidRelations;
1770   NCollection_DynamicArray<BRepGraphInc::CompoundRelations>   myCompoundRelations;
1771   NCollection_DynamicArray<BRepGraphInc::CompSolidRelations>  myCompSolidRelations;
1772   NCollection_DynamicArray<BRepGraphInc::VertexRelations>     myVertexRelations;
1773   NCollection_DynamicArray<BRepGraphInc::ProductRelations>    myProductRelations;
1774   NCollection_DynamicArray<BRepGraphInc::OccurrenceRelations> myOccurrenceRelations;
1775 
1776   //! Sparse incoming compound child refs keyed by referenced node.
1777   NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_ChildRefId>>
1778     myNodeToCompounds;
1779 
1780   //! Sparse incoming product occurrence refs keyed by occurrence child node.
1781   NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_OccurrenceRefId>>
1782     myNodeToOccurrences;
1783 
1784   //! Representation-use store with removal tracking.
1785   template <typename UseT>
1786   struct RepStore
1787   {
1788     using TypeId = typename UseT::TypeId;
1789 
1790     NCollection_DynamicArray<UseT> Uses;
1791     BRepGraphInc_BitFlags          RemovedFlags;
1792     uint32_t                       NbActive = 0;
1793 
1794     RepStore() = delete;
1795 
1796     RepStore(const int theBlockSize, const occ::handle<NCollection_BaseAllocator>& theAlloc)
1797         : Uses(theBlockSize, theAlloc)
1798     {
1799     }
1800 
1801     uint32_t Nb() const { return static_cast<uint32_t>(Uses.Size()); }
1802 
1803     const UseT& Get(const TypeId theId) const
1804     {
1805       return Uses.Value(static_cast<size_t>(theId.Index));
1806     }
1807 
1808     UseT& Change(const TypeId theId) { return Uses.ChangeValue(static_cast<size_t>(theId.Index)); }
1809 
1810     TypeId Append()
1811     {
1812       const TypeId anId(static_cast<uint32_t>(Uses.Size()));
1813       ++NbActive;
1814       Uses.Appended();
1815       RemovedFlags.Resize(static_cast<size_t>(anId.Index) + 1);
1816       return anId;
1817     }
1818 
1819     void DecrementActive()
1820     {
1821       Standard_ASSERT_VOID(NbActive > 0u, "RepStore::DecrementActive: underflow");
1822       if (NbActive > 0u)
1823       {
1824         --NbActive;
1825       }
1826     }
1827 
1828     bool MarkRemoved(const TypeId theId)
1829     {
1830       if (!theId.IsValid(Nb()))
1831       {
1832         return false;
1833       }
1834       if (RemovedFlags.Test(theId.Index))
1835       {
1836         return false;
1837       }
1838       RemovedFlags.Set(theId.Index);
1839       DecrementActive();
1840       return true;
1841     }
1842 
1843     bool IsRemoved(const TypeId theId) const
1844     {
1845       return theId.IsValid(Nb()) && RemovedFlags.Test(theId.Index);
1846     }
1847 
1848     void Clear(const bool theReleaseMemory = false)
1849     {
1850       Uses.Clear(theReleaseMemory);
1851       RemovedFlags.ClearAll();
1852       NbActive = 0;
1853     }
1854   };
1855 
1856   //! Edge 3D curve use store.
1857   RepStore<BRepGraphInc::EdgeCurve3DRep> myEdgeCurves3D;
1858 
1859   //! Edge 3D polygon use store.
1860   RepStore<BRepGraphInc::EdgePolygon3DRep> myEdgePolygons3D;
1861 
1862   //! CoEdge 2D curve use store.
1863   RepStore<BRepGraphInc::CoEdgeCurve2DRep> myCoEdgeCurves2D;
1864 
1865   //! CoEdge 2D polygon use store.
1866   RepStore<BRepGraphInc::CoEdgePolygon2DRep> myCoEdgePolygons2D;
1867 
1868   //! CoEdge polygon-on-triangulation use store.
1869   RepStore<BRepGraphInc::CoEdgePolygonOnTriRep> myCoEdgePolygonsOnTri;
1870 
1871   //! Face surface use store.
1872   RepStore<BRepGraphInc::FaceSurfaceRep> myFaceSurfaces;
1873 
1874   //! Face triangulation use store.
1875   RepStore<BRepGraphInc::FaceTriangulationRep> myFaceTriangulations;
1876 
1877   //! UID reverse indexes: eagerly maintained on allocate/remove, rebuilt on compact/load.
1878   mutable NCollection_FlatDataMap<BRepGraph_UID, BRepGraph_NodeId>   myUIDToNodeId;
1879   mutable std::shared_mutex                                          myUIDToNodeIdMutex;
1880   mutable NCollection_FlatDataMap<BRepGraph_RefUID, BRepGraph_RefId> myRefUIDToRefId;
1881   mutable std::shared_mutex                                          myRefUIDToRefIdMutex;
1882   mutable std::atomic<bool>                                          myUIDToNodeIdDirty{false};
1883   mutable std::atomic<bool>                                          myRefUIDToRefIdDirty{false};
1884 
1885   //! Bindings from reconstructed / source OCCT shapes back to backend ids.
1886   NCollection_FlatDataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher> myShapeToNodeId;
1887   NCollection_FlatDataMap<BRepGraph_NodeId, TopoDS_Shape>                          myOriginalShapes;
1888   mutable std::shared_mutex myShapeBindingsMutex;
1889 
1890   //! Persistent backend identity state.
1891   std::atomic<uint32_t> myGeneration{0};
1892   Standard_GUID         myGraphGUID;
1893 
1894   //! Transient mutation-control state used by EditorView invalidation paths.
1895   std::atomic<bool>     myDeferredMode{false};
1896   std::atomic<uint32_t> myPropagationWave{0};
1897   uint32_t              myRemoveSubgraphDepth = 0;
1898 
1899   //! Transient generation-validated shape reconstruction cache.
1900   mutable NCollection_FlatDataMap<BRepGraph_NodeId, CachedShape> myCurrentShapes;
1901   mutable std::shared_mutex                                      myCurrentShapesMutex;
1902 
1903 private:
1904   //! Trait mapping a typed identifier to its store's bit-flag planes.
1905   //! One specialization per typed ID type provides O(1) compile-time dispatch.
1906   template <typename T>
1907   struct TypedStorePlanes;
1908 
1909   template <typename T>
1910   friend struct TypedStorePlanes;
1911 
1912   template <typename T>
1913   [[nodiscard]] bool isInRange(const T theId) const;
1914 
1915   template <typename FuncT>
1916   [[nodiscard]] bool dispatchItemId(const BRepGraph_ItemId& theId, FuncT&& theFunc) const;
1917 
1918 public:
1919   //! Return true if the entity identified by the given typed ID is soft-removed.
1920   //! @param[in] theId typed entity identifier
1921   template <typename T>
1922   [[nodiscard]] bool IsRemoved(const T theId) const;
1923 
1924   //! Set or clear the soft-removal flag for the entity identified by the given typed ID.
1925   //! @param[in] theId typed entity identifier
1926   //! @param[in] theVal true to mark removed, false to mark active
1927   template <typename T>
1928   void SetRemoved(const T theId, const bool theVal);
1929 
1930   //! Return true if the entity identified by the given typed ID has a registered owner.
1931   //! @param[in] theId typed entity identifier
1932   template <typename T>
1933   [[nodiscard]] bool IsOwned(const T theId) const;
1934 
1935   //! Set or clear the ownership flag for the entity identified by the given typed ID.
1936   //! @param[in] theId typed entity identifier
1937   //! @param[in] theVal true to mark owned, false to mark unowned
1938   template <typename T>
1939   void SetOwned(const T theId, const bool theVal);
1940 
1941   //! Return true if the entity identified by the given typed ID has an active MutGuard.
1942   //! @param[in] theId typed entity identifier
1943   template <typename T>
1944   [[nodiscard]] bool IsGuarded(const T theId) const;
1945 
1946   //! Register an active MutGuard on the entity identified by the given typed ID.
1947   //! @param[in] theId typed entity identifier
1948   template <typename T>
1949   void SetGuarded(const T theId);
1950 
1951   //! Deregister an active MutGuard from the entity identified by the given typed ID.
1952   //! @param[in] theId typed entity identifier
1953   template <typename T>
1954   void ClearGuarded(const T theId);
1955 
1956   //! Return true if the node identified by the given generic NodeId has a parent compound.
1957   //! Dispatches by node kind to the appropriate per-kind bitset.
1958   //! @param[in] theNode generic node identifier
1959   [[nodiscard]] Standard_EXPORT bool HasCompoundParent(const BRepGraph_NodeId theNode) const;
1960 
1961   //! Return true if the node identified by the given generic NodeId has a parent occurrence.
1962   //! Dispatches by node kind to the appropriate per-kind bitset.
1963   //! @param[in] theNode generic node identifier
1964   [[nodiscard]] Standard_EXPORT bool HasOccurrenceParent(const BRepGraph_NodeId theNode) const;
1965 
1966   //! Return true if the entity identified by the given generic item id has an active MutGuard.
1967   //! @param[in] theId generic item identifier (node, reference, or representation)
1968   [[nodiscard]] bool IsGuarded(const BRepGraph_ItemId& theId) const;
1969 
1970   //! Register an active MutGuard on the entity identified by the given generic item id.
1971   //! @param[in] theId generic item identifier (node, reference, or representation)
1972   void SetGuarded(const BRepGraph_ItemId& theId);
1973 
1974   //! Deregister an active MutGuard from the entity identified by the given generic item id.
1975   //! @param[in] theId generic item identifier (node, reference, or representation)
1976   void ClearGuarded(const BRepGraph_ItemId& theId);
1977 
1978   //! Return true if any entity in any store has an active MutGuard.
1979   //! Used to assert no guards are active before Clear().
1980   [[nodiscard]] Standard_EXPORT bool HasAnyGuard() const;
1981 };
1982 
1983 #include <BRepGraphInc_Storage.lxx>
1984 
1985 #endif // _BRepGraphInc_Storage_HeaderFile