File indexing completed on 2026-09-28 09:19:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _BRepGraph_ParentExplorer_HeaderFile
0015 #define _BRepGraph_ParentExplorer_HeaderFile
0016
0017 #include <BRepGraph.hxx>
0018 #include <BRepGraphInc_Definition.hxx>
0019 #include <BRepGraphInc_Instance.hxx>
0020 #include <BRepGraphInc_Reference.hxx>
0021 #include <NCollection_BaseAllocator.hxx>
0022 #include <NCollection_ForwardRange.hxx>
0023 #include <NCollection_LocalArray.hxx>
0024 #include <TopAbs_Orientation.hxx>
0025 #include <TopLoc_Location.hxx>
0026
0027 #include <optional>
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 class BRepGraph_ParentExplorer
0053 {
0054 public:
0055 DEFINE_STANDARD_ALLOC
0056
0057
0058 enum class LinkKind
0059 {
0060 None,
0061 Reference,
0062 Structural,
0063 };
0064
0065
0066 enum class TraversalMode
0067 {
0068 Recursive,
0069 DirectParents,
0070 };
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 struct Config
0084 {
0085 TraversalMode Mode = TraversalMode::Recursive;
0086 std::optional<BRepGraph_NodeId::Kind>
0087 TargetKind;
0088 std::optional<BRepGraph_NodeId::Kind> AvoidKind;
0089 bool EmitAvoidKind = false;
0090 };
0091
0092
0093
0094
0095
0096 Standard_EXPORT BRepGraph_ParentExplorer(const BRepGraph& theGraph,
0097 const BRepGraph_NodeId theNode,
0098 const Config& theConfig);
0099
0100
0101
0102
0103 Standard_EXPORT BRepGraph_ParentExplorer(const BRepGraph& theGraph,
0104 const BRepGraph_NodeId theNode);
0105
0106
0107
0108
0109
0110 Standard_EXPORT BRepGraph_ParentExplorer(const BRepGraph& theGraph,
0111 const BRepGraph_NodeId theNode,
0112 TraversalMode theMode);
0113
0114
0115
0116
0117
0118
0119
0120 Standard_EXPORT BRepGraph_ParentExplorer(
0121 const BRepGraph& theGraph,
0122 const BRepGraph_NodeId theNode,
0123 const std::optional<BRepGraph_NodeId::Kind>& theAvoidKind,
0124 bool theEmitAvoidKind,
0125 TraversalMode theMode = TraversalMode::Recursive);
0126
0127
0128
0129
0130
0131 Standard_EXPORT BRepGraph_ParentExplorer(const BRepGraph& theGraph,
0132 const BRepGraph_NodeId theNode,
0133 BRepGraph_NodeId::Kind theTargetKind);
0134
0135
0136
0137
0138
0139
0140 Standard_EXPORT BRepGraph_ParentExplorer(const BRepGraph& theGraph,
0141 const BRepGraph_NodeId theNode,
0142 BRepGraph_NodeId::Kind theTargetKind,
0143 TraversalMode theMode);
0144
0145
0146
0147
0148
0149
0150
0151
0152 Standard_EXPORT BRepGraph_ParentExplorer(
0153 const BRepGraph& theGraph,
0154 const BRepGraph_NodeId theNode,
0155 BRepGraph_NodeId::Kind theTargetKind,
0156 const std::optional<BRepGraph_NodeId::Kind>& theAvoidKind,
0157 bool theEmitAvoidKind,
0158 TraversalMode theMode = TraversalMode::Recursive);
0159
0160
0161
0162 [[nodiscard]] const Config& GetConfig() const { return myConfig; }
0163
0164
0165 [[nodiscard]] bool More() const { return myHasMore; }
0166
0167
0168 Standard_EXPORT void Next();
0169
0170
0171 [[nodiscard]] BRepGraphInc::NodeInstance Current() const
0172 {
0173 if (myHasMore)
0174 {
0175 return {myCurrent, myLocation, myOrientation};
0176 }
0177 return {};
0178 }
0179
0180
0181
0182 [[nodiscard]] Standard_EXPORT BRepGraph_NodeId CurrentChild() const;
0183
0184
0185 [[nodiscard]] Standard_EXPORT LinkKind CurrentLinkKind() const;
0186
0187
0188
0189
0190
0191
0192
0193
0194 [[nodiscard]] Standard_EXPORT BRepGraph_RefId CurrentRef() const;
0195
0196
0197 [[nodiscard]] Standard_EXPORT const TopLoc_Location& LeafLocation() const;
0198
0199
0200 [[nodiscard]] Standard_EXPORT TopAbs_Orientation LeafOrientation() const;
0201
0202
0203 [[nodiscard]] Standard_EXPORT bool IsCurrentBranchRoot() const;
0204
0205
0206 NCollection_ForwardRangeIterator<BRepGraph_ParentExplorer> begin()
0207 {
0208 return NCollection_ForwardRangeIterator<BRepGraph_ParentExplorer>(this);
0209 }
0210
0211
0212 NCollection_ForwardRangeSentinel end() const { return NCollection_ForwardRangeSentinel{}; }
0213
0214 private:
0215 struct StackFrame
0216 {
0217 BRepGraph_NodeId Node;
0218 uint32_t NextParentIdx = 0;
0219 int StepToChild = -1;
0220 BRepGraph_RefId RefToChild;
0221 TopLoc_Location AccLocation;
0222 TopAbs_Orientation AccOrientation = TopAbs_FORWARD;
0223 };
0224
0225 Standard_EXPORT void startTraversal();
0226 Standard_EXPORT void advance();
0227 Standard_EXPORT bool emitNextFromCurrentBranch();
0228 Standard_EXPORT void backtrackAfterBranchEmission();
0229 Standard_EXPORT bool nextParentFrame(StackFrame& theChild, StackFrame& theParent) const;
0230 Standard_EXPORT void prepareCurrentBranch();
0231 Standard_EXPORT void applyTransition(const BRepGraph_NodeId theParent,
0232 const BRepGraph_NodeId theChild,
0233 const int theStepToChild,
0234 const BRepGraph_RefId theRefToChild,
0235 TopLoc_Location& theLocation,
0236 TopAbs_Orientation& theOrientation) const;
0237
0238 [[nodiscard]] Standard_EXPORT int branchRootFrame() const;
0239
0240 Standard_EXPORT bool findNthOccurrenceWrapper(const BRepGraph_NodeId theNode,
0241 const uint32_t theOrdinal,
0242 BRepGraph_OccurrenceId& theOccurrence,
0243 BRepGraph_OccurrenceRefId& theOccurrenceRef) const;
0244
0245 Standard_EXPORT int findOccurrenceStep(
0246 const BRepGraph_ProductId theParentProduct,
0247 const BRepGraph_OccurrenceId theOccurrence,
0248 BRepGraph_OccurrenceRefId* theOccurrenceRef = nullptr) const;
0249 Standard_EXPORT int findCompoundChildStep(const BRepGraph_CompoundId theParent,
0250 const BRepGraph_NodeId theChild) const;
0251 Standard_EXPORT int findCompSolidSolidStep(const BRepGraph_CompSolidId theParent,
0252 const BRepGraph_SolidId theChild) const;
0253 Standard_EXPORT int findSolidChildStep(const BRepGraph_SolidId theParent,
0254 const BRepGraph_NodeId theChild) const;
0255 Standard_EXPORT int findShellChildStep(const BRepGraph_ShellId theParent,
0256 const BRepGraph_NodeId theChild) const;
0257 Standard_EXPORT int findFaceChildStep(const BRepGraph_FaceId theParent,
0258 const BRepGraph_NodeId theChild) const;
0259 Standard_EXPORT int findWireCoEdgeStep(const BRepGraph_WireId theParent,
0260 const BRepGraph_CoEdgeId theChild) const;
0261 Standard_EXPORT int findEdgeVertexStep(const BRepGraph_EdgeId theParent,
0262 const BRepGraph_VertexId theChild) const;
0263
0264
0265
0266
0267 Standard_EXPORT bool nextCompoundOrOccurrenceParent(BRepGraph_NodeId theNode,
0268 uint32_t theRemainingIdx,
0269 StackFrame& theParent) const;
0270
0271 static std::optional<BRepGraph_NodeId::Kind> normalizeAvoidKind(
0272 const BRepGraph_NodeId theNode,
0273 const std::optional<BRepGraph_NodeId::Kind>& theTargetKind,
0274 const std::optional<BRepGraph_NodeId::Kind>& theAvoidKind);
0275
0276 static bool canContainTarget(BRepGraph_NodeId::Kind theParentKind,
0277 BRepGraph_NodeId::Kind theTargetKind);
0278
0279 Standard_EXPORT void pushFrame(const StackFrame& theFrame);
0280 Standard_EXPORT void popFrame();
0281
0282 [[nodiscard]] bool matchesAvoid(const BRepGraph_NodeId theNode) const
0283 {
0284 return myConfig.AvoidKind.has_value() && theNode.NodeKind == *myConfig.AvoidKind;
0285 }
0286
0287 [[nodiscard]] bool shouldEmit(const BRepGraph_NodeId theNode) const
0288 {
0289 const bool isAvoid = matchesAvoid(theNode);
0290 const bool isFind =
0291 !myConfig.TargetKind.has_value() || theNode.NodeKind == *myConfig.TargetKind;
0292 return myConfig.EmitAvoidKind ? (isFind || isAvoid) : (isFind && !isAvoid);
0293 }
0294
0295 StackFrame& topFrame() { return myStack[myStackTop]; }
0296
0297 const StackFrame& topFrame() const { return myStack[myStackTop]; }
0298
0299 Standard_EXPORT TopLoc_Location stepLocation(const BRepGraph_NodeId theParent,
0300 const int theRefIdx) const;
0301 Standard_EXPORT TopAbs_Orientation stepOrientation(const BRepGraph_NodeId theParent,
0302 const int theRefIdx) const;
0303
0304 private:
0305 static constexpr int THE_INLINE_STACK_SIZE = 16;
0306
0307 const BRepGraph* myGraph = nullptr;
0308 BRepGraph_NodeId myNode;
0309 Config myConfig;
0310
0311 NCollection_LocalArray<StackFrame, THE_INLINE_STACK_SIZE> myStack;
0312 int myStackTop = -1;
0313 int myEmitIndex = -1;
0314 int myCurrentFrame = -1;
0315
0316 BRepGraph_NodeId myCurrent;
0317 TopLoc_Location myLocation;
0318 TopAbs_Orientation myOrientation = TopAbs_FORWARD;
0319 bool myHasMore = false;
0320 };
0321
0322 #endif