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;
177 Usd_CollectionMembershipQueryBase::PathExpansionRuleMap
const &ruleMap);
189template <
class ExprEval>
190class Usd_CollectionMembershipQuery :
public Usd_CollectionMembershipQueryBase
193 using ExpressionEvaluator = ExprEval;
195 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
225 IsPathIncluded(
const SdfPath &path,
226 TfToken *expansionRule=
nullptr)
const {
228 if (UsesPathExpansionRuleMap()) {
230 _IsPathIncludedByRuleMap(path, expansionRule));
233 res = GetExpressionEvaluator().Match(path);
235 *expansionRule = res ?
236 UsdCollectionMembershipQueryTokens->
237 IncludedByMembershipExpression :
238 UsdCollectionMembershipQueryTokens->
239 ExcludedByMembershipExpression;
263 IsPathIncluded(
const SdfPath &path,
264 const TfToken &parentExpansionRule,
265 TfToken *expansionRule=
nullptr)
const {
267 if (UsesPathExpansionRuleMap()) {
269 _IsPathIncludedByRuleMap(
270 path, parentExpansionRule, expansionRule));
273 res = GetExpressionEvaluator().Match(path);
275 *expansionRule = res ?
276 UsdCollectionMembershipQueryTokens->
277 IncludedByMembershipExpression :
278 UsdCollectionMembershipQueryTokens->
279 ExcludedByMembershipExpression;
287 bool UsesPathExpansionRuleMap()
const {
288 return !_HasEmptyRuleMap();
292 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
293 _exprEval = std::move(exprEval);
297 SetExpressionEvaluator(ExpressionEvaluator
const &exprEval) {
298 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
303 ExpressionEvaluator
const &
304 GetExpressionEvaluator()
const {
310 bool HasExpression()
const {
311 return !_exprEval.IsEmpty();
315 bool operator==(Usd_CollectionMembershipQuery
const& rhs)
const {
319 return _topExpansionRule == rhs._topExpansionRule &&
320 _hasExcludes == rhs._hasExcludes &&
321 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
322 _includedCollections == rhs._includedCollections &&
323 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
328 bool operator!=(Usd_CollectionMembershipQuery
const& rhs)
const {
329 return !(*
this == rhs);
334 size_t operator()(Usd_CollectionMembershipQuery
const& query)
const {
340 inline size_t GetHash()
const {
341 return Hash()(*this);
345 ExpressionEvaluator _exprEval;
355 return stage->GetObjectAtPath(path);
357 UsdStageWeakPtr stage;
362 using IncrementalSearcher =
363 typename PathExprEval::IncrementalSearcher<PathToObj>;
385 return !_stage || _evaluator.IsEmpty();
390 UsdStageWeakPtr
const &
GetStage()
const {
return _stage; }
413 UsdStageWeakPtr _stage;
418 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
428 const UsdStageWeakPtr &stage,
439 const UsdStageWeakPtr &stage,
442PXR_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...
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.