Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_HeaderFile
0015 #define _BRepGraph_HeaderFile
0016 
0017 #include <BRepGraph_NodeId.hxx>
0018 #include <BRepGraph_RefId.hxx>
0019 #include <BRepGraph_RefUID.hxx>
0020 #include <BRepGraphInc_RepId.hxx>
0021 #include <BRepGraph_UID.hxx>
0022 #include <BRepGraph_ItemId.hxx>
0023 #include <BRepGraphInc_Definition.hxx>
0024 #include <BRepGraphInc_Reference.hxx>
0025 #include <BRepGraphInc_Representation.hxx>
0026 #include <Standard_DefineAlloc.hxx>
0027 #include <TopoDS_Shape.hxx>
0028 #include <gp_Trsf.hxx>
0029 #include <Bnd_Box.hxx>
0030 #include <gp_Pnt.hxx>
0031 #include <NCollection_DataMap.hxx>
0032 #include <NCollection_LinearVector.hxx>
0033 
0034 #include <memory>
0035 
0036 template <typename T>
0037 class BRepGraph_MutGuard;
0038 
0039 struct BRepGraph_Data;
0040 class BRepGraphInc_Storage;
0041 class BRepGraph_CacheRegistry;
0042 class BRepGraph_Layer;
0043 class BRepGraph_LayerLock;
0044 class BRepGraph_LayerRegistry;
0045 class BRepGraph_CacheMesh;
0046 class BRepGraph_Validate;
0047 class BRepGraph_Deduplicate;
0048 class BRepGraphODE;
0049 class BRepGraphODE_Storage;
0050 class NCollection_BaseAllocator;
0051 class TCollection_AsciiString;
0052 
0053 //! @brief Topology-geometry graph over TopoDS / BRep.
0054 //!
0055 //! Stores B-Rep topology as flat entity vectors (incidence-table model) with
0056 //! integer cross-references, enabling cache-friendly traversal, relation-table
0057 //! parent navigation, and parallel face-level geometry extraction.
0058 //!
0059 //! Key design concepts:
0060 //! - **NodeId** (Kind + Index): lightweight typed address into per-kind vectors.
0061 //! - **UID** (Kind + Counter): persistent identity surviving compaction/reorder.
0062 //! - **RepId** (Kind + Index): separate geometry/mesh addressing (Surface,
0063 //!   Curve3D, Curve2D, Triangulation, Polygon) decoupled from topology nodes.
0064 //! - **CoEdge**: half-edge entity owning PCurve data for each edge-face binding;
0065 //!   seam edges use paired CoEdges with opposite Orientation (Parasolid convention).
0066 //! - **Lifecycle**: Shapes().Add() populates from TopoDS_Shape;
0067 //!   Editor() is the single mutation entry point for both structural creation/removal
0068 //!   (Add*, Remove*, Append*) and field-level RAII-scoped mutation (Mut*()) with
0069 //!   automatic cache invalidation and upward SubtreeGen propagation.
0070 //!
0071 //! Per-occurrence data (orientation, location) lives on incidence refs.
0072 //! Definition types are aliases to BRepGraphInc entity structs.
0073 //!
0074 //! ## Grouped View API
0075 //! Related methods are grouped behind lightweight view objects.
0076 //! Include the corresponding header (e.g. BRepGraph_TopoView.hxx) to use.
0077 //!
0078 //! ## Thread safety
0079 //! Const query methods are safe for concurrent reads.
0080 //! Concurrent reads during active mutation still require external synchronization.
0081 //! Deferred invalidation (BRepGraph_DeferredScope) batches SubtreeGen propagation;
0082 //! concurrent Editor().Mut*() calls during deferred mode still require external
0083 //! serialization.
0084 //! Shapes().Add() is internally parallel when requested.
0085 //!
0086 //! ## UID persistence
0087 //! UIDs use monotonic counters (not vector indices), persisting across Compact()
0088 //! and node removal. Only BRepGraph::Clear() resets counters (new generation).
0089 //! See BRepGraph_UID.hxx for the serialization contract.
0090 //!
0091 //! ## Extension model
0092 //! Extend via BRepGraph_Layer (persistent metadata / observers) or
0093 //! BRepGraph_CacheRegistry (typed algorithm-computed transient cache services).
0094 //! Direct storage extension is not supported.
0095 //!
0096 //! ## ID systems
0097 //! Four ID types with different stability guarantees:
0098 //! - **NodeId** (Kind + per-kind Index): fast graph-local address. NOT stable across Compact().
0099 //!   Use for in-graph traversal and short-lived algorithm temporaries.
0100 //! - **UID** (Kind + monotonic Counter): persistent identity surviving Compact() and node removal.
0101 //!   Use for cross-session storage, history tracking, and external references.
0102 //! - **RefId** (Kind + per-kind Index): same stability as NodeId, but addresses reference entries
0103 //!   (Shell->Solid binding, Face->Shell binding, CoEdge->Wire binding) rather than defs.
0104 //! - **RepId** (Kind + per-kind Index): addresses owner-scoped geometry/mesh representation slots
0105 //!   (Surface, Curve3D, Curve2D, Triangulation, Polygon).
0106 //!
0107 //! ## Iterator guide
0108 //! Choose the iterator that matches your traversal need:
0109 //! - **BRepGraph_Iterator\<NodeType\>**: flat sequential scan of ALL definitions of one kind
0110 //!   (e.g. every FaceDef, skipping removed). Use for bulk per-kind algorithms.
0111 //! - **BRepGraph_DefsIterator / BRepGraph_RefsIterator**: single-level typed children of one
0112 //!   parent (e.g. active shells of one solid, coedges of one wire). Zero allocation.
0113 //!   Use when you have a specific parent and need its direct children.
0114 //! - **BRepGraph_ChildExplorer**: depth-first downward walk from a root with accumulated
0115 //!   location/orientation per step. Use when visiting descendants across multiple levels or
0116 //!   when the global transform matters. Supports Recursive and DirectChildren modes.
0117 //! - **BRepGraph_ParentExplorer**: upward walk via relation tables from a starting node.
0118 //!   Use when tracing which shells/solids/compounds contain a given face or edge.
0119 //! - **BRepGraph_RelatedIterator**: single-level semantic neighbors (adjacent faces, boundary
0120 //!   edges, incident vertices). No structural descent; no location accumulation.
0121 class BRepGraph
0122 {
0123 public:
0124   DEFINE_STANDARD_ALLOC
0125 
0126   //! Copying is intentionally disabled: BRepGraph is the unique owner of graph data.
0127   BRepGraph(const BRepGraph&) = delete;
0128   //! Copying is intentionally disabled: BRepGraph is the unique owner of graph data.
0129   BRepGraph& operator=(const BRepGraph&) = delete;
0130 
0131   //! Default constructor. Creates an empty graph with default allocator.
0132   Standard_EXPORT BRepGraph();
0133   //! Destructor.
0134   Standard_EXPORT ~BRepGraph();
0135   //! Move constructor.
0136   Standard_EXPORT BRepGraph(BRepGraph&&) noexcept;
0137   //! Move assignment operator.
0138   Standard_EXPORT BRepGraph& operator=(BRepGraph&&) noexcept;
0139 
0140   //! Reset the graph to an empty state. Increments generation and regenerates the graph GUID.
0141   Standard_EXPORT void Clear();
0142 
0143   //! Return true when the graph contains no topology definitions.
0144   [[nodiscard]] Standard_EXPORT bool IsEmpty() const;
0145 
0146   //! Verify relation consistency against entity / reference-entry tables.
0147   //! Intended for debug builds and regression tests of incremental mutation paths.
0148   //! @return true when every stored relation matches its endpoints.
0149   [[nodiscard]] Standard_EXPORT bool ValidateRelations() const;
0150 
0151   //! Return root product identifiers (products not referenced by any active occurrence).
0152   //! Maintained incrementally by Editor/EditorView mutations.
0153   //! Returns empty vector if the graph has not been built.
0154   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_ProductId>&
0155                                       RootProductIds() const;
0156 
0157   //! Return the current allocator.
0158   [[nodiscard]] Standard_EXPORT const occ::handle<NCollection_BaseAllocator>& Allocator() const;
0159 
0160   //! Return true when this wrapper references graph data.
0161   [[nodiscard]] Standard_EXPORT bool IsValid() const noexcept;
0162 
0163   //! Return true when this wrapper does not reference graph data.
0164   [[nodiscard]] bool IsNull() const noexcept { return !IsValid(); }
0165 
0166   class TopoView;
0167   class UIDsView;
0168   class RefsView;
0169   class ShapesView;
0170   class EditorView;
0171   class MeshView;
0172 
0173   //! Access topology definitions, representation access, adjacency queries,
0174   //! raw Product/Occurrence definition storage, and assembly classification.
0175   [[nodiscard]] Standard_EXPORT const TopoView& Topo() const;
0176   //! Access unique identifiers.
0177   [[nodiscard]] Standard_EXPORT const UIDsView& UIDs() const;
0178   //! Access reference entries and their UIDs.
0179   [[nodiscard]] Standard_EXPORT const RefsView& Refs() const;
0180   //! Access cached and fresh shape reconstruction.
0181   [[nodiscard]] Standard_EXPORT ShapesView& Shapes();
0182   //! Access shape ingestion, cached shape reconstruction and fresh shape reconstruction.
0183   [[nodiscard]] Standard_EXPORT const ShapesView& Shapes() const;
0184   //! Access programmatic graph construction and mutation.
0185   [[nodiscard]] Standard_EXPORT EditorView& Editor();
0186   //! Const access to editor-specific state queries.
0187   //! Exposes IsDeferredMode() and ValidateMutationBoundary() on a const graph.
0188   //! All structural mutation methods require the non-const Editor() overload.
0189   [[nodiscard]] Standard_EXPORT const EditorView& Editor() const;
0190   //! Access mesh data with explicit Cache()/Persistent() sub-views and Editor() for cache
0191   //! mutations. Persistent rep creation lives on Editor().Edges(), Editor().CoEdges(),
0192   //! Editor().Faces() (since reps back the topology defs).
0193   //! @return read-only mesh view
0194   [[nodiscard]] Standard_EXPORT const MeshView& Mesh() const;
0195   //! Non-const access to mesh view (required to call Editor() sub-view for cache mutations).
0196   //! @return mutable mesh view
0197   [[nodiscard]] Standard_EXPORT MeshView& Mesh();
0198 
0199   //! Access registered graph layers.
0200   //! @return layer registry for managing attribute layers
0201   [[nodiscard]] Standard_EXPORT BRepGraph_LayerRegistry& LayerRegistry();
0202   //! Access registered graph layers (const).
0203   //! @return layer registry for managing attribute layers
0204   [[nodiscard]] Standard_EXPORT const BRepGraph_LayerRegistry& LayerRegistry() const;
0205 
0206   //! Access registered graph cache services.
0207   //! @return cache registry for managing typed transient cache services
0208   [[nodiscard]] Standard_EXPORT BRepGraph_CacheRegistry& CacheRegistry();
0209   //! Access registered graph cache services (const).
0210   //! @return cache registry for managing typed transient cache services
0211   [[nodiscard]] Standard_EXPORT const BRepGraph_CacheRegistry& CacheRegistry() const;
0212 
0213 private:
0214   friend class BRepGraph_Cache;
0215   friend class BRepGraph_CacheRegistry;
0216   friend class BRepGraph_Compact;
0217   friend class BRepGraph_Copy;
0218   friend class BRepGraph_Deduplicate;
0219   friend class BRepGraph_Layer;
0220   friend class BRepGraph_LayerLock;
0221   friend class BRepGraph_LayerRegistry;
0222   friend class BRepGraph_Tool;
0223   friend class BRepGraph_Transform;
0224   friend class BRepGraph_Validate;
0225   friend class BRepGraphInc_Populate;
0226   friend class BRepGraphInc_Reconstruct;
0227   friend class BRepGraphODE;
0228   friend class BRepGraphODE_Storage;
0229   template <typename>
0230   friend class BRepGraph_MutGuard;
0231 
0232   friend struct BRepGraph_NodeId;
0233   friend struct BRepGraph_RefId;
0234   friend struct BRepGraph_RepId;
0235 
0236   //! Access the underlying storage.
0237   [[nodiscard]] Standard_EXPORT BRepGraphInc_Storage&       incStorage();
0238   [[nodiscard]] Standard_EXPORT const BRepGraphInc_Storage& incStorage() const;
0239 
0240   //! Access the graph data structure.
0241   [[nodiscard]] Standard_EXPORT BRepGraph_Data*       data();
0242   [[nodiscard]] Standard_EXPORT const BRepGraph_Data* data() const;
0243 
0244   //! Bind graph-owned views and registries to this owner.
0245   Standard_EXPORT void initViewsAndRegistries() noexcept;
0246 
0247   //! Access the layer registry.
0248   [[nodiscard]] Standard_EXPORT BRepGraph_LayerRegistry&       layerRegistry();
0249   [[nodiscard]] Standard_EXPORT const BRepGraph_LayerRegistry& layerRegistry() const;
0250 
0251   //! Access the cache registry.
0252   [[nodiscard]] Standard_EXPORT BRepGraph_CacheRegistry&       cacheRegistry();
0253   [[nodiscard]] Standard_EXPORT const BRepGraph_CacheRegistry& cacheRegistry() const;
0254 
0255   //! Generic reference lookup by RefId (const).
0256   //! Returns nullptr if the RefId is invalid or out of range.
0257   Standard_EXPORT const BRepGraphInc::BaseRef* refEntity(const BRepGraph_RefId theId) const;
0258 
0259   //! Invalidate reconstructed shapes and dependent caches below a node.
0260   //! @param[in] theNode root node of the invalidated subgraph
0261   Standard_EXPORT void invalidateSubgraphImpl(const BRepGraph_NodeId theNode);
0262 
0263   //! Allocate and attach a persistent definition UID for a freshly appended node.
0264   //! @param[in] theNodeId node slot receiving the UID
0265   //! @return allocated definition UID
0266   Standard_EXPORT BRepGraph_UID allocateUID(const BRepGraph_NodeId theNodeId);
0267 
0268   //! Allocate and attach a persistent reference UID for a freshly appended reference.
0269   //! @param[in] theRefId reference slot receiving the UID
0270   //! @return allocated reference UID
0271   Standard_EXPORT BRepGraph_RefUID allocateRefUID(const BRepGraph_RefId theRefId);
0272 
0273   //! Mark a topology definition as modified and propagate cache invalidation.
0274   //! @param[in] theNodeId modified topology definition
0275   Standard_EXPORT void markModified(const BRepGraph_NodeId theNodeId) noexcept;
0276 
0277   //! Mark a reference entry as modified and propagate cache invalidation.
0278   //! @param[in] theRefId modified reference entry
0279   Standard_EXPORT void markRefModified(const BRepGraph_RefId theRefId) noexcept;
0280 
0281   //! Optimized overload: skips changeTopoEntity() dispatch
0282   //! when the caller already holds a mutable reference to the target entity.
0283   Standard_EXPORT void markModified(const BRepGraph_NodeId theNodeId,
0284                                     BRepGraphInc::BaseDef& theEntity) noexcept;
0285   //! Increment SubtreeGen on a parent node (NOT OwnGen - parent's own data didn't change).
0286   //! Uses wave guard to prevent exponential blowup on diamond topologies.
0287   //! Mutex-free: no shape cache clear, no dispatch.
0288   Standard_EXPORT void markParentSubtreeGen(const BRepGraph_NodeId theParentId) noexcept;
0289 
0290   //! Propagate SubtreeGen upward through relation tables via markParentSubtreeGen().
0291   Standard_EXPORT void propagateSubtreeGen(const BRepGraph_NodeId theNodeId) noexcept;
0292 
0293   //! Generic topology definition lookup by NodeId (const).
0294   Standard_EXPORT const BRepGraphInc::BaseDef* topoEntity(const BRepGraph_NodeId theId) const;
0295 
0296   //! Generic mutable topology definition lookup by NodeId.
0297   Standard_EXPORT BRepGraphInc::BaseDef* changeTopoEntity(const BRepGraph_NodeId theId);
0298 
0299   // Fields at the bottom (OCCT style)
0300   std::unique_ptr<BRepGraph_Data> myData;
0301 };
0302 
0303 // Included after BRepGraph is complete so the template body sees markModified().
0304 #include <BRepGraph_MutGuard.hxx>
0305 
0306 #endif // _BRepGraph_HeaderFile