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;
60 Usd_CollectionMembershipQueryBase(
61 const PathExpansionRuleMap& pathExpansionRuleMap,
62 const SdfPathSet& includedCollections);
66 Usd_CollectionMembershipQueryBase(
67 PathExpansionRuleMap&& pathExpansionRuleMap,
68 SdfPathSet&& includedCollections);
73 Usd_CollectionMembershipQueryBase(
74 const PathExpansionRuleMap& pathExpansionRuleMap,
75 const SdfPathSet& includedCollections,
76 const TfToken &topExpansionRule);
81 Usd_CollectionMembershipQueryBase(
82 PathExpansionRuleMap&& pathExpansionRuleMap,
83 SdfPathSet&& includedCollections,
84 TfToken const &topExpansionRule);
89 bool HasExcludes()
const {
96 const PathExpansionRuleMap& GetAsPathExpansionRuleMap()
const {
97 return _pathExpansionRuleMap;
106 const SdfPathSet& GetIncludedCollections()
const {
107 return _includedCollections;
115 TfToken GetTopExpansionRule()
const {
116 return _topExpansionRule;
124 size_t operator()(Usd_CollectionMembershipQueryBase
const& query)
const;
128 inline size_t _GetHash()
const {
129 return _Hash()(*this);
133 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
134 TfToken *expansionRule=
nullptr)
const;
137 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
138 const TfToken &parentExpansionRule,
139 TfToken *expansionRule=
nullptr)
const;
144 bool _HasEmptyRuleMap()
const;
148 PathExpansionRuleMap _pathExpansionRuleMap;
150 SdfPathSet _includedCollections;
154 bool _hasExcludes=
false;
163 Usd_CollectionMembershipQueryBase::PathExpansionRuleMap
const &ruleMap);
175template <
class ExprEval>
176class Usd_CollectionMembershipQuery :
public Usd_CollectionMembershipQueryBase
179 using ExpressionEvaluator = ExprEval;
181 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
211 IsPathIncluded(
const SdfPath &path,
212 TfToken *expansionRule=
nullptr)
const {
214 if (UsesPathExpansionRuleMap()) {
216 _IsPathIncludedByRuleMap(path, expansionRule));
219 res = GetExpressionEvaluator().Match(path);
221 *expansionRule = res ?
222 UsdCollectionMembershipQueryTokens->
223 IncludedByMembershipExpression :
224 UsdCollectionMembershipQueryTokens->
225 ExcludedByMembershipExpression;
249 IsPathIncluded(
const SdfPath &path,
250 const TfToken &parentExpansionRule,
251 TfToken *expansionRule=
nullptr)
const {
253 if (UsesPathExpansionRuleMap()) {
255 _IsPathIncludedByRuleMap(
256 path, parentExpansionRule, expansionRule));
259 res = GetExpressionEvaluator().Match(path);
261 *expansionRule = res ?
262 UsdCollectionMembershipQueryTokens->
263 IncludedByMembershipExpression :
264 UsdCollectionMembershipQueryTokens->
265 ExcludedByMembershipExpression;
273 bool UsesPathExpansionRuleMap()
const {
274 return !_HasEmptyRuleMap();
278 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
279 _exprEval = std::move(exprEval);
283 SetExpressionEvaluator(ExpressionEvaluator
const &exprEval) {
284 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
289 ExpressionEvaluator
const &
290 GetExpressionEvaluator()
const {
296 bool HasExpression()
const {
297 return !_exprEval.IsEmpty();
301 bool operator==(Usd_CollectionMembershipQuery
const& rhs)
const {
305 return _topExpansionRule == rhs._topExpansionRule &&
306 _hasExcludes == rhs._hasExcludes &&
307 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
308 _includedCollections == rhs._includedCollections &&
309 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
314 bool operator!=(Usd_CollectionMembershipQuery
const& rhs)
const {
315 return !(*
this == rhs);
320 size_t operator()(Usd_CollectionMembershipQuery
const& query)
const {
326 inline size_t GetHash()
const {
327 return Hash()(*this);
331 ExpressionEvaluator _exprEval;
341 return stage->GetObjectAtPath(path);
343 UsdStageWeakPtr stage;
348 using IncrementalSearcher =
349 typename PathExprEval::IncrementalSearcher<PathToObj>;
371 return !_stage || _evaluator.IsEmpty();
376 UsdStageWeakPtr
const &
GetStage()
const {
return _stage; }
399 UsdStageWeakPtr _stage;
404 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
414 const UsdStageWeakPtr &stage,
425 const UsdStageWeakPtr &stage,
428PXR_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.