24#ifndef PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
25#define PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
38#include "pxr/usd/sdf/path.h"
39#include "pxr/usd/sdf/pathExpression.h"
40#include "pxr/usd/sdf/pathExpressionEval.h"
42#include <unordered_map>
44PXR_NAMESPACE_OPEN_SCOPE
46#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
47 (IncludedByMembershipExpression) \
48 (ExcludedByMembershipExpression)
51 USD_API, USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS);
54class Usd_CollectionMembershipQueryBase
66 using PathExpansionRuleMap = std::unordered_map<
SdfPath,
71 Usd_CollectionMembershipQueryBase() =
default;
76 Usd_CollectionMembershipQueryBase(
77 const PathExpansionRuleMap& pathExpansionRuleMap,
78 const SdfPathSet& includedCollections);
81 Usd_CollectionMembershipQueryBase(
82 PathExpansionRuleMap&& pathExpansionRuleMap,
83 SdfPathSet&& includedCollections);
87 Usd_CollectionMembershipQueryBase(
88 const PathExpansionRuleMap& pathExpansionRuleMap,
89 const SdfPathSet& includedCollections,
90 const TfToken &topExpansionRule);
94 Usd_CollectionMembershipQueryBase(
95 PathExpansionRuleMap&& pathExpansionRuleMap,
96 SdfPathSet&& includedCollections,
97 TfToken const &topExpansionRule);
102 bool HasExcludes()
const {
110 const PathExpansionRuleMap& GetAsPathExpansionRuleMap()
const {
111 return _pathExpansionRuleMap;
120 const SdfPathSet& GetIncludedCollections()
const {
121 return _includedCollections;
129 TfToken GetTopExpansionRule()
const {
130 return _topExpansionRule;
138 size_t operator()(Usd_CollectionMembershipQueryBase
const& query)
const;
142 inline size_t _GetHash()
const {
143 return _Hash()(*this);
147 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
148 TfToken *expansionRule=
nullptr)
const;
151 bool _IsPathIncludedByRuleMap(
const SdfPath &path,
152 const TfToken &parentExpansionRule,
153 TfToken *expansionRule=
nullptr)
const;
158 bool _HasEmptyRuleMap()
const;
162 PathExpansionRuleMap _pathExpansionRuleMap;
164 SdfPathSet _includedCollections;
168 bool _hasExcludes=
false;
182template <
class ExprEval>
183class Usd_CollectionMembershipQuery :
public Usd_CollectionMembershipQueryBase
186 using ExpressionEvaluator = ExprEval;
188 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
218 IsPathIncluded(
const SdfPath &path,
219 TfToken *expansionRule=
nullptr)
const {
221 if (UsesPathExpansionRuleMap()) {
223 _IsPathIncludedByRuleMap(path, expansionRule));
226 res = GetExpressionEvaluator().Match(path);
228 *expansionRule = res ?
229 UsdCollectionMembershipQueryTokens->
230 IncludedByMembershipExpression :
231 UsdCollectionMembershipQueryTokens->
232 ExcludedByMembershipExpression;
256 IsPathIncluded(
const SdfPath &path,
257 const TfToken &parentExpansionRule,
258 TfToken *expansionRule=
nullptr)
const {
260 if (UsesPathExpansionRuleMap()) {
262 _IsPathIncludedByRuleMap(
263 path, parentExpansionRule, expansionRule));
266 res = GetExpressionEvaluator().Match(path);
268 *expansionRule = res ?
269 UsdCollectionMembershipQueryTokens->
270 IncludedByMembershipExpression :
271 UsdCollectionMembershipQueryTokens->
272 ExcludedByMembershipExpression;
280 bool UsesPathExpansionRuleMap()
const {
281 return !_HasEmptyRuleMap();
285 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
286 _exprEval = std::move(exprEval);
290 SetExpressionEvaluator(ExpressionEvaluator
const &exprEval) {
291 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
296 ExpressionEvaluator
const &
297 GetExpressionEvaluator()
const {
303 bool HasExpression()
const {
304 return !_exprEval.IsEmpty();
308 bool operator==(Usd_CollectionMembershipQuery
const& rhs)
const {
312 return _topExpansionRule == rhs._topExpansionRule &&
313 _hasExcludes == rhs._hasExcludes &&
314 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
315 _includedCollections == rhs._includedCollections &&
316 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
321 bool operator!=(Usd_CollectionMembershipQuery
const& rhs)
const {
322 return !(*
this == rhs);
327 size_t operator()(Usd_CollectionMembershipQuery
const& query)
const {
333 inline size_t GetHash()
const {
334 return Hash()(*this);
338 ExpressionEvaluator _exprEval;
348 return stage->GetObjectAtPath(path);
350 UsdStageWeakPtr stage;
355 using IncrementalSearcher =
356 typename PathExprEval::IncrementalSearcher<PathToObj>;
378 return !_stage || _evaluator.IsEmpty();
383 UsdStageWeakPtr
const &
GetStage()
const {
return _stage; }
406 UsdStageWeakPtr _stage;
411 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
421 const UsdStageWeakPtr &stage,
432 const UsdStageWeakPtr &stage,
435PXR_NAMESPACE_CLOSE_SCOPE
Objects of this class represent a logical expression syntax tree consisting of SdfPath matching patte...
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...
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.