7#ifndef PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
8#define PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
21#include "pxr/usd/sdf/path.h"
22#include "pxr/usd/sdf/pathExpression.h"
23#include "pxr/usd/sdf/pathExpressionEval.h"
25#include <unordered_map>
27PXR_NAMESPACE_OPEN_SCOPE
29#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
30 (IncludedByMembershipExpression) \
31 (ExcludedByMembershipExpression)
34 USD_API, USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS);
37class Usd_CollectionMembershipQueryBase
49 using PathExpansionRuleMap = std::unordered_map<
SdfPath,
54 Usd_CollectionMembershipQueryBase() =
default;
59 Usd_CollectionMembershipQueryBase(
60 const PathExpansionRuleMap& pathExpansionRuleMap,
61 const SdfPathSet& includedCollections);
64 Usd_CollectionMembershipQueryBase(
65 PathExpansionRuleMap&& pathExpansionRuleMap,
66 SdfPathSet&& includedCollections);
70 Usd_CollectionMembershipQueryBase(
71 const PathExpansionRuleMap& pathExpansionRuleMap,
72 const SdfPathSet& includedCollections,
73 const TfToken &topExpansionRule);
77 Usd_CollectionMembershipQueryBase(
78 PathExpansionRuleMap&& pathExpansionRuleMap,
79 SdfPathSet&& includedCollections,
80 TfToken const &topExpansionRule);
85 bool HasExcludes()
const {
93 const PathExpansionRuleMap& GetAsPathExpansionRuleMap()
const {
94 return _pathExpansionRuleMap;
103 const SdfPathSet& GetIncludedCollections()
const {
104 return _includedCollections;
112 TfToken GetTopExpansionRule()
const {
113 return _topExpansionRule;
121 size_t operator()(Usd_CollectionMembershipQueryBase
const& query)
const;
125 inline size_t _GetHash()
const {
126 return _Hash()(*this);
130 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
131 TfToken *expansionRule=
nullptr)
const;
134 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
135 const TfToken &parentExpansionRule,
136 TfToken *expansionRule=
nullptr)
const;
141 bool _HasEmptyRuleMap()
const;
145 PathExpansionRuleMap _pathExpansionRuleMap;
147 SdfPathSet _includedCollections;
151 bool _hasExcludes=
false;
160 Usd_CollectionMembershipQueryBase::PathExpansionRuleMap
const &ruleMap);
172template <
class ExprEval>
173class Usd_CollectionMembershipQuery :
public Usd_CollectionMembershipQueryBase
176 using ExpressionEvaluator = ExprEval;
178 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
208 IsPathIncluded(
const SdfPath &path,
209 TfToken *expansionRule=
nullptr)
const {
211 if (UsesPathExpansionRuleMap()) {
213 _IsPathIncludedByRuleMap(path, expansionRule));
216 res = GetExpressionEvaluator().Match(path);
218 *expansionRule = res ?
219 UsdCollectionMembershipQueryTokens->
220 IncludedByMembershipExpression :
221 UsdCollectionMembershipQueryTokens->
222 ExcludedByMembershipExpression;
246 IsPathIncluded(
const SdfPath &path,
247 const TfToken &parentExpansionRule,
248 TfToken *expansionRule=
nullptr)
const {
250 if (UsesPathExpansionRuleMap()) {
252 _IsPathIncludedByRuleMap(
253 path, parentExpansionRule, expansionRule));
256 res = GetExpressionEvaluator().Match(path);
258 *expansionRule = res ?
259 UsdCollectionMembershipQueryTokens->
260 IncludedByMembershipExpression :
261 UsdCollectionMembershipQueryTokens->
262 ExcludedByMembershipExpression;
270 bool UsesPathExpansionRuleMap()
const {
271 return !_HasEmptyRuleMap();
275 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
276 _exprEval = std::move(exprEval);
280 SetExpressionEvaluator(ExpressionEvaluator
const &exprEval) {
281 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
286 ExpressionEvaluator
const &
287 GetExpressionEvaluator()
const {
293 bool HasExpression()
const {
294 return !_exprEval.IsEmpty();
298 bool operator==(Usd_CollectionMembershipQuery
const& rhs)
const {
302 return _topExpansionRule == rhs._topExpansionRule &&
303 _hasExcludes == rhs._hasExcludes &&
304 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
305 _includedCollections == rhs._includedCollections &&
306 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
311 bool operator!=(Usd_CollectionMembershipQuery
const& rhs)
const {
312 return !(*
this == rhs);
317 size_t operator()(Usd_CollectionMembershipQuery
const& query)
const {
323 inline size_t GetHash()
const {
324 return Hash()(*this);
328 ExpressionEvaluator _exprEval;
338 return stage->GetObjectAtPath(path);
340 UsdStageWeakPtr stage;
345 using IncrementalSearcher =
346 typename PathExprEval::IncrementalSearcher<PathToObj>;
368 return !_stage || _evaluator.IsEmpty();
373 UsdStageWeakPtr
const &
GetStage()
const {
return _stage; }
396 UsdStageWeakPtr _stage;
401 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
411 const UsdStageWeakPtr &stage,
422 const UsdStageWeakPtr &stage,
425PXR_NAMESPACE_CLOSE_SCOPE
Objects of this class represent a logical expression syntax tree consisting of SdfPathPattern s,...
A path value used to locate objects in layers or scenegraphs.
Represents the result of a predicate function: a pair of the boolean result and a Constancy token ind...
static SdfPredicateFunctionResult MakeVarying(bool value)
Create with value and 'MayVaryOverDescendants'.
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Token for efficient comparison, assignment, and hashing of known strings.
Represents a flattened view of a collection.
Evaluates SdfPathExpressions with objects from a given UsdStage.
USD_API IncrementalSearcher MakeIncrementalSearcher() const
Create an incremental searcher from this evaluator.
UsdObjectCollectionExpressionEvaluator()=default
Construct an empty evaluator.
USD_API SdfPredicateFunctionResult Match(UsdObject const &object) const
Return the result of evaluating the expression against object.
bool IsEmpty() const
Return true if this evaluator has an invalid stage or an empty underlying SdfPathExpressionEval objec...
UsdStageWeakPtr const & GetStage() const
Return the stage this object was constructed with, or nullptr if it was default constructed.
USD_API SdfPredicateFunctionResult Match(SdfPath const &path) const
Return the result of evaluating the expression against path.
USD_API UsdObjectCollectionExpressionEvaluator(UsdStageWeakPtr const &stage, SdfPathExpression const &expr)
Construct an evaluator that evalutates expr on objects from stage.
Base class for Usd scenegraph objects, providing common API.
USD_API std::set< UsdObject > UsdComputeIncludedObjectsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
Returns all the usd objects that satisfy the predicate, pred in the collection represented by the Usd...
USD_API SdfPathSet UsdComputeIncludedPathsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
Returns all the paths that satisfy the predicate, pred in the collection represented by the UsdCollec...
USD_API SdfPathExpression UsdComputePathExpressionFromCollectionMembershipQueryRuleMap(Usd_CollectionMembershipQueryBase::PathExpansionRuleMap const &ruleMap)
Compute an SdfPathExpression that matches the same paths as ruleMap.
Standard pointer typedefs.
unspecified UsdPrimDefaultPredicate
The default predicate used for prim traversals in methods like UsdPrim::GetChildren,...
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.