Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_LayerHistory_HeaderFile
0015 #define _BRepGraph_LayerHistory_HeaderFile
0016 
0017 #include <BRepGraph_Layer.hxx>
0018 #include <BRepGraph_NodeId.hxx>
0019 #include <BRepGraph_ItemUID.hxx>
0020 #include <BRepGraph_UID.hxx>
0021 #include <NCollection_Array1.hxx>
0022 #include <NCollection_DataMap.hxx>
0023 #include <NCollection_DynamicArray.hxx>
0024 #include <NCollection_FlatMap.hxx>
0025 #include <NCollection_LinearVector.hxx>
0026 #include <TCollection_AsciiString.hxx>
0027 #include <TopoDS_Shape.hxx>
0028 #include <TopTools_ShapeMapHasher.hxx>
0029 
0030 #include <cstdint>
0031 
0032 class BRepGraph;
0033 class BRepTools_History;
0034 
0035 //! History layer for BRepGraph.
0036 //!
0037 //! BRepGraph_LayerHistory maintains an append-only log of modification events
0038 //! and per-kind lookup maps for efficient queries.  Four event kinds are
0039 //! tracked (see #BRepGraph_LayerHistory::Kind):
0040 //!   - **Modified**: input -> { modified images } (default).
0041 //!   - **Generated**: input -> { generated images } (new entities born
0042 //!     from the input but not sharing its identity).
0043 //!   - **Deleted**: input has been consumed and has no image in the
0044 //!     result.
0045 //!   - **Replaced**: input was structurally detached and replaced by another
0046 //!     node; this maps as Modified and also marks the input as deleted.
0047 //!
0048 //! Recording can be toggled on/off at runtime.  Graph-owned history is registered
0049 //! as a layer and accessed through #Ensure / #Find; algorithms wrapping OCCT's
0050 //! `BRepTools_History` can import results through #Absorb.
0051 class BRepGraph_LayerHistory : public BRepGraph_Layer
0052 {
0053 public:
0054   //! Classification of a history event.
0055   enum class Kind : std::uint8_t
0056   {
0057     Modified  = 0, //!< Default; input persists into the result(s).
0058     Generated = 1, //!< Output entity is freshly produced from the input.
0059     Deleted   = 2, //!< Input has no image in the result.
0060     Replaced  = 3  //!< Input was detached and continued by replacement(s).
0061   };
0062 
0063   //! One atomic modification event recorded in the graph's history log.
0064   struct Event
0065   {
0066     Event() = default;
0067 
0068     TCollection_AsciiString OperationName;
0069     size_t                  SequenceNumber = 0;
0070     Kind                    RecordKind     = Kind::Modified;
0071 
0072     //! Key: original node id before the operation.
0073     //! Value: sequence of replacement node ids after the operation.
0074     NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_NodeId>> Mapping;
0075 
0076     //! UID-keyed mapping for cross-graph history records.
0077     NCollection_DataMap<BRepGraph_UID, NCollection_LinearVector<BRepGraph_UID>> UidMapping;
0078 
0079     //! ItemUID-keyed mapping for durable all-domain history records.
0080     NCollection_DataMap<BRepGraph_ItemUID, NCollection_LinearVector<BRepGraph_ItemUID>>
0081       ItemUidMapping;
0082 
0083     //! Optional diagnostic representation.
0084     TCollection_AsciiString ExtraInfo;
0085   };
0086 
0087   //! Default constructor.
0088   Standard_EXPORT BRepGraph_LayerHistory();
0089 
0090   //! Stable layer GUID.
0091   Standard_EXPORT static const Standard_GUID& GetID();
0092 
0093   //! Layer type identity.
0094   [[nodiscard]] Standard_EXPORT const Standard_GUID& ID() const override;
0095 
0096   //! Layer display name.
0097   [[nodiscard]] Standard_EXPORT const TCollection_AsciiString& Name() const override;
0098 
0099   //! Record a modification: theOriginal was replaced by theReplacements.
0100   //!
0101   //! @note When @p theReplacements is empty the record is auto-downgraded to
0102   //!       Kind::Deleted and @p theOriginal is added to the deleted set,
0103   //!       regardless of @p theKind.  Use #RecordDeleted directly for the
0104   //!       deletion case to avoid relying on this implicit conversion.
0105   //! @param[in] theOpLabel      human-readable operation name
0106   //! @param[in] theOriginal     node id before the operation
0107   //! @param[in] theReplacements node ids after the operation
0108   //! @param[in] theKind         classification of this record (default Modified)
0109   Standard_EXPORT void Record(
0110     const TCollection_AsciiString&              theOpLabel,
0111     const BRepGraph_NodeId                      theOriginal,
0112     const NCollection_Array1<BRepGraph_NodeId>& theReplacements,
0113     const BRepGraph_LayerHistory::Kind          theKind = BRepGraph_LayerHistory::Kind::Modified);
0114 
0115   //! Record a batch of 1-to-1 modifications in a single history event.
0116   //! Each original is paired with the replacement at the same logical position.
0117   //! More efficient than calling Record() in a loop: creates one HistoryRecord
0118   //! and updates the per-kind maps with minimal overhead.
0119   //! @param[in] theOpLabel      human-readable operation name
0120   //! @param[in] theOriginals    node ids before the operation
0121   //! @param[in] theReplacements node ids after the operation (same length)
0122   //! @param[in] theExtraInfo    optional diagnostic info stored on the record
0123   //! @param[in] theKind         classification of this record (default Modified)
0124   Standard_EXPORT void RecordBatch(
0125     const TCollection_AsciiString&              theOpLabel,
0126     const NCollection_Array1<BRepGraph_NodeId>& theOriginals,
0127     const NCollection_Array1<BRepGraph_NodeId>& theReplacements,
0128     const TCollection_AsciiString&              theExtraInfo = TCollection_AsciiString(),
0129     const BRepGraph_LayerHistory::Kind          theKind = BRepGraph_LayerHistory::Kind::Modified);
0130 
0131   //! Record that a collection of inputs has been consumed by the operation
0132   //! and has no image in the result.  Each input is appended to the
0133   //! deleted set and emits a single audit record with empty replacements.
0134   //! @param[in] theOpLabel human-readable operation name
0135   //! @param[in] theDeleted node ids that have been removed
0136   Standard_EXPORT void RecordDeleted(const TCollection_AsciiString&              theOpLabel,
0137                                      const NCollection_Array1<BRepGraph_NodeId>& theDeleted);
0138 
0139   //! Record replacements: each original is logically removed/detached and
0140   //! continued by the corresponding replacement.  Replaced records participate
0141   //! in modified-image queries and also mark originals as deleted.
0142   Standard_EXPORT void RecordReplaced(const TCollection_AsciiString& theOpLabel,
0143                                       const BRepGraph_NodeId         theOriginal,
0144                                       const BRepGraph_NodeId         theReplacement);
0145 
0146   //! Record a batch of 1-to-1 replacements in a single history event.
0147   Standard_EXPORT void RecordReplacedBatch(
0148     const TCollection_AsciiString&              theOpLabel,
0149     const NCollection_Array1<BRepGraph_NodeId>& theOriginals,
0150     const NCollection_Array1<BRepGraph_NodeId>& theReplacements,
0151     const TCollection_AsciiString&              theExtraInfo = TCollection_AsciiString());
0152 
0153   //! Record a UID-keyed modification/generation event.
0154   //!
0155   //! This is the durable-history path for operations whose source and result
0156   //! identities may live in different BRepGraph instances.  Existing NodeId
0157   //! records remain available for in-graph algorithms; UID records are queried
0158   //! directly by cross-graph consumers.
0159   Standard_EXPORT void RecordUid(
0160     const TCollection_AsciiString&           theOpLabel,
0161     const BRepGraph_UID&                     theOriginal,
0162     const NCollection_Array1<BRepGraph_UID>& theReplacements,
0163     const BRepGraph_LayerHistory::Kind       theKind = BRepGraph_LayerHistory::Kind::Modified);
0164 
0165   //! Record UID-keyed deletions.
0166   Standard_EXPORT void RecordDeletedUid(const TCollection_AsciiString&           theOpLabel,
0167                                         const NCollection_Array1<BRepGraph_UID>& theDeleted);
0168 
0169   //! Record an all-domain ItemUID-keyed modification/generation event.
0170   Standard_EXPORT void RecordItemUid(
0171     const TCollection_AsciiString&               theOpLabel,
0172     const BRepGraph_ItemUID&                     theOriginal,
0173     const NCollection_Array1<BRepGraph_ItemUID>& theReplacements,
0174     const BRepGraph_LayerHistory::Kind           theKind = BRepGraph_LayerHistory::Kind::Modified);
0175 
0176   //! Record ItemUID-keyed deletions.
0177   Standard_EXPORT void RecordDeletedItemUid(
0178     const TCollection_AsciiString&               theOpLabel,
0179     const NCollection_Array1<BRepGraph_ItemUID>& theDeleted);
0180 
0181   //! Import a BRepTools_History into this graph-native history log.
0182   //!
0183   //! Iterates @p theInputs, queries @p theSource for Modified / Generated /
0184   //! IsRemoved, translates each TopoDS_Shape image to a NodeId via
0185   //! @p theOutputs, and emits the corresponding records.
0186   //!
0187   //! Semantics:
0188   //!   - For every input shape whose Modified() list is non-empty:
0189   //!     emit a Modified record.
0190   //!   - For every input shape whose Generated() list is non-empty:
0191   //!     emit a Generated record.
0192   //!   - For every input shape with IsRemoved() == true: accumulate into
0193   //!     a single Deleted record (IsRemoved takes precedence over
0194   //!     Modified/Generated to handle a known OCCT bug where a shape can
0195   //!     appear in both the removed set and the generated map).
0196   //!
0197   //! Output TopoDS_Shapes that do not appear in @p theOutputs are silently
0198   //! dropped (expected for subshapes merged into a parent compound whose
0199   //! identity is preserved at a higher level).
0200   //!
0201   //! @param[in] theInputs  TopoDS_Shape -> NodeId for every input subshape
0202   //!                        that should be tracked
0203   //! @param[in] theOutputs TopoDS_Shape -> NodeId for every subshape added
0204   //!                        to the graph by this operation (typically from
0205   //!                        BRepGraph::ShapesView::Add with TrackAddedNodes)
0206   //! @param[in] theSource  BRepTools_History from the OCCT algorithm.
0207   //!                        Null is accepted (no-op).
0208   //! @param[in] theOpLabel record label written into every emitted record
0209   Standard_EXPORT void Absorb(
0210     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theInputs,
0211     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theOutputs,
0212     const occ::handle<BRepTools_History>&                                               theSource,
0213     const TCollection_AsciiString&                                                      theOpLabel);
0214 
0215   //! Import a BRepTools_History using persistent UIDs from source/result graphs.
0216   //!
0217   //! This overload is the canonical bridge for cross-graph algorithms: input
0218   //! shapes are resolved in @p theInputGraph, output shapes are resolved in
0219   //! @p theOutputGraph, and the resulting history is stored by UID.
0220   Standard_EXPORT void Absorb(
0221     const BRepGraph& theInputGraph,
0222     const BRepGraph& theOutputGraph,
0223     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theInputs,
0224     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theOutputs,
0225     const occ::handle<BRepTools_History>&                                               theSource,
0226     const TCollection_AsciiString&                                                      theOpLabel);
0227 
0228   //! Walk backwards from a modified node to its original.
0229   //! Follows the reverse map recursively until a root is reached.
0230   //! @param[in] theModified node id to trace back
0231   //! @return the root original node id, or theModified itself if not found
0232   [[nodiscard]] Standard_EXPORT BRepGraph_NodeId
0233     FindOriginal(const BRepGraph_NodeId theModified) const;
0234 
0235   //! Walk forwards from an original node to all derived nodes, including
0236   //! both Modified and Generated descendants.  Follows the forward maps
0237   //! recursively, collecting every transitively-reachable descendant
0238   //! (intermediate nodes and leaves alike, but not @p theOriginal itself).
0239   //! @param[in] theOriginal node id to trace forward
0240   //! @return all transitively derived node ids in breadth-first order
0241   [[nodiscard]] Standard_EXPORT NCollection_LinearVector<BRepGraph_NodeId> FindDerived(
0242     const BRepGraph_NodeId theOriginal) const;
0243 
0244   //! Direct lookup of the Modified images of @p theOriginal, non-recursive.
0245   //! @param[in] theOriginal node id to query
0246   //! @return pointer to the stored vector, or nullptr if @p theOriginal has
0247   //!         no Modified record (note: nullptr does not imply IsDeleted).
0248   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_NodeId>* FindModified(
0249     const BRepGraph_NodeId theOriginal) const;
0250 
0251   //! Direct lookup of the Generated images of @p theOriginal, non-recursive.
0252   //! @param[in] theOriginal node id to query
0253   //! @return pointer to the stored vector, or nullptr if @p theOriginal has
0254   //!         no Generated record.
0255   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_NodeId>* FindGenerated(
0256     const BRepGraph_NodeId theOriginal) const;
0257 
0258   //! Test whether @p theOriginal was deleted by some recorded operation.
0259   //! @param[in] theOriginal node id to query
0260   //! @return true if @p theOriginal is in the deleted set
0261   [[nodiscard]] Standard_EXPORT bool IsDeleted(const BRepGraph_NodeId theOriginal) const;
0262 
0263   //! Borrowed access to the full deleted set.
0264   //! @return reference to the deleted-node set
0265   [[nodiscard]] Standard_EXPORT const NCollection_FlatMap<BRepGraph_NodeId>& DeletedNodes() const;
0266 
0267   //! UID-keyed Modified images stored directly in this history.
0268   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_UID>* FindModified(
0269     const BRepGraph_UID& theUID) const;
0270 
0271   //! Direct lookup of all immediate node origins of @p theDerived.
0272   //! A derived entity can have more than one parent in reconstructive algorithms.
0273   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_NodeId>* FindOriginals(
0274     const BRepGraph_NodeId theDerived) const;
0275 
0276   //! UID-keyed Generated images stored directly in this history.
0277   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_UID>* FindGenerated(
0278     const BRepGraph_UID& theUID) const;
0279 
0280   //! UID-keyed deletion test stored directly in this history.
0281   [[nodiscard]] Standard_EXPORT bool IsDeleted(const BRepGraph_UID& theUID) const;
0282 
0283   //! UID-keyed deleted set stored directly in this history.
0284   [[nodiscard]] Standard_EXPORT const NCollection_FlatMap<BRepGraph_UID>& DeletedUids() const;
0285 
0286   //! Test whether @p theUID was registered as an operation input.
0287   [[nodiscard]] Standard_EXPORT bool HasKnownInput(const BRepGraph_UID& theUID) const;
0288 
0289   //! ItemUID-keyed Modified images stored directly in this history.
0290   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_ItemUID>* FindModified(
0291     const BRepGraph_ItemUID& theUID) const;
0292 
0293   //! ItemUID-keyed Generated images stored directly in this history.
0294   [[nodiscard]] Standard_EXPORT const NCollection_LinearVector<BRepGraph_ItemUID>* FindGenerated(
0295     const BRepGraph_ItemUID& theUID) const;
0296 
0297   //! ItemUID-keyed deletion test stored directly in this history.
0298   [[nodiscard]] Standard_EXPORT bool IsDeleted(const BRepGraph_ItemUID& theUID) const;
0299 
0300   //! ItemUID-keyed deleted set stored directly in this history.
0301   [[nodiscard]] Standard_EXPORT const NCollection_FlatMap<BRepGraph_ItemUID>& DeletedItemUids()
0302     const;
0303 
0304   //! Test whether @p theUID was registered as an operation input.
0305   [[nodiscard]] Standard_EXPORT bool HasKnownInput(const BRepGraph_ItemUID& theUID) const;
0306 
0307   //! UID-keyed convenience: Modified images of the input identified by
0308   //! @p theUID, resolved against @p theGraph.  Returns an empty vector if
0309   //! the UID cannot be resolved or has no Modified record.
0310   //! @param[in] theGraph graph used to translate UID <-> NodeId
0311   //! @param[in] theUID   UID of the input entity
0312   //! @return UIDs of the modified images (in record-insertion order)
0313   [[nodiscard]] Standard_EXPORT NCollection_LinearVector<BRepGraph_UID> FindModified(
0314     const BRepGraph&     theGraph,
0315     const BRepGraph_UID& theUID) const;
0316 
0317   //! UID-keyed convenience: Generated images.  See #FindModified for the
0318   //! resolution contract.
0319   //! @param[in] theGraph graph used to translate UID <-> NodeId
0320   //! @param[in] theUID   UID of the input entity
0321   //! @return UIDs of the generated images (in record-insertion order)
0322   [[nodiscard]] Standard_EXPORT NCollection_LinearVector<BRepGraph_UID> FindGenerated(
0323     const BRepGraph&     theGraph,
0324     const BRepGraph_UID& theUID) const;
0325 
0326   //! UID-keyed convenience: deletion test.
0327   //! @param[in] theGraph graph used to resolve the UID
0328   //! @param[in] theUID   UID of the input entity
0329   //! @return true if the resolved NodeId is in the deleted set
0330   [[nodiscard]] Standard_EXPORT bool IsDeleted(const BRepGraph&     theGraph,
0331                                                const BRepGraph_UID& theUID) const;
0332 
0333   //! UID-keyed convenience: dump the full deleted set as UIDs.
0334   //! @param[in] theGraph graph used to translate NodeId -> UID
0335   //! @return UIDs of all deleted entities (insertion order is not stable)
0336   [[nodiscard]] Standard_EXPORT NCollection_LinearVector<BRepGraph_UID> DeletedUids(
0337     const BRepGraph& theGraph) const;
0338 
0339   //! Number of recorded history events.
0340   //! @return record count
0341   [[nodiscard]] Standard_EXPORT size_t NbRecords() const;
0342 
0343   //! Access a record by index (0-based).
0344   //! @param[in] theRecordIdx zero-based index into the records vector
0345   //! @return the history record at the given index
0346   [[nodiscard]] Standard_EXPORT const Event& Record(const size_t theRecordIdx) const;
0347 
0348   //! Enable or disable history recording.
0349   //! @param[in] theVal true to enable, false to disable
0350   Standard_EXPORT void SetEnabled(const bool theVal);
0351 
0352   //! Query whether history recording is enabled.
0353   //! @return true if recording is active
0354   [[nodiscard]] Standard_EXPORT bool IsEnabled() const;
0355 
0356   //! Clear all records and lookup maps.
0357   Standard_EXPORT void Clear() noexcept override;
0358 
0359   //! Layer removal callback. Records pure graph deletions when enabled.
0360   Standard_EXPORT void OnNodeRemoved(const BRepGraph_NodeId theNode) noexcept override;
0361 
0362   //! Copy history records whose source items have copied target items.
0363   Standard_EXPORT void CopyTo(const BRepGraph_CopyRemap& theCopy) const override;
0364 
0365   //! Clear derived caches by dropping collected history.
0366   Standard_EXPORT void InvalidateAll() noexcept override;
0367 
0368   DEFINE_STANDARD_RTTIEXT(BRepGraph_LayerHistory, BRepGraph_Layer)
0369 
0370 private:
0371   //! Rebuild all lookup caches from myRecords.
0372   void rebuildCaches();
0373 
0374   NCollection_DynamicArray<Event> myRecords;
0375 
0376   //! Full reverse map: derived node -> all immediate original nodes.
0377   NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_NodeId>>
0378     myDerivedToOriginals;
0379 
0380   //! Forward map: original node -> vector of Modified images.
0381   NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_NodeId>>
0382     myOriginalToModified;
0383 
0384   //! Forward map: original node -> vector of Generated images.
0385   NCollection_DataMap<BRepGraph_NodeId, NCollection_LinearVector<BRepGraph_NodeId>>
0386     myOriginalToGenerated;
0387 
0388   //! Flat set of inputs that have been consumed (no image in the result).
0389   NCollection_FlatMap<BRepGraph_NodeId> myDeleted;
0390 
0391   //! UID-keyed forward map: original UID -> Modified image UIDs.
0392   NCollection_DataMap<BRepGraph_UID, NCollection_LinearVector<BRepGraph_UID>>
0393     myUidOriginalToModified;
0394 
0395   //! UID-keyed forward map: original UID -> Generated image UIDs.
0396   NCollection_DataMap<BRepGraph_UID, NCollection_LinearVector<BRepGraph_UID>>
0397     myUidOriginalToGenerated;
0398 
0399   //! UID-keyed operation inputs, including inputs with no images.
0400   NCollection_FlatMap<BRepGraph_UID> myUidKnownInputs;
0401 
0402   //! UID-keyed consumed inputs.
0403   NCollection_FlatMap<BRepGraph_UID> myUidDeleted;
0404 
0405   //! ItemUID-keyed forward map: original UID -> Modified image UIDs.
0406   NCollection_DataMap<BRepGraph_ItemUID, NCollection_LinearVector<BRepGraph_ItemUID>>
0407     myItemUidOriginalToModified;
0408 
0409   //! ItemUID-keyed forward map: original UID -> Generated image UIDs.
0410   NCollection_DataMap<BRepGraph_ItemUID, NCollection_LinearVector<BRepGraph_ItemUID>>
0411     myItemUidOriginalToGenerated;
0412 
0413   //! ItemUID-keyed operation inputs, including inputs with no images.
0414   NCollection_FlatMap<BRepGraph_ItemUID> myItemUidKnownInputs;
0415 
0416   //! ItemUID-keyed consumed inputs.
0417   NCollection_FlatMap<BRepGraph_ItemUID> myItemUidDeleted;
0418 
0419   bool myEnabled = true;
0420 };
0421 
0422 #endif // _BRepGraph_LayerHistory_HeaderFile