Loading...
Searching...
No Matches
collectionMembershipQuery.h
Go to the documentation of this file.
1//
2// Copyright 2019 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
8#define PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/usd/usd/common.h"
15#include "pxr/usd/usd/object.h"
17
19#include "pxr/base/tf/hash.h"
21#include "pxr/usd/sdf/path.h"
22#include "pxr/usd/sdf/pathExpression.h"
23#include "pxr/usd/sdf/pathExpressionEval.h"
24
25#include <unordered_map>
26
27PXR_NAMESPACE_OPEN_SCOPE
28
29#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
30 (IncludedByMembershipExpression) \
31 (ExcludedByMembershipExpression)
32
33TF_DECLARE_PUBLIC_TOKENS(UsdCollectionMembershipQueryTokens,
34 USD_API, USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS);
35
36
37class Usd_CollectionMembershipQueryBase
38{
39public:
49 using PathExpansionRuleMap = std::unordered_map<SdfPath,
50 TfToken, SdfPath::Hash>;
51
54 Usd_CollectionMembershipQueryBase() = default;
55
59 USD_API
60 Usd_CollectionMembershipQueryBase(
61 const PathExpansionRuleMap& pathExpansionRuleMap,
62 const SdfPathSet& includedCollections);
63
65 USD_API
66 Usd_CollectionMembershipQueryBase(
67 PathExpansionRuleMap&& pathExpansionRuleMap,
68 SdfPathSet&& includedCollections);
69
72 USD_API
73 Usd_CollectionMembershipQueryBase(
74 const PathExpansionRuleMap& pathExpansionRuleMap,
75 const SdfPathSet& includedCollections,
76 const TfToken &topExpansionRule);
77
80 USD_API
81 Usd_CollectionMembershipQueryBase(
82 PathExpansionRuleMap&& pathExpansionRuleMap,
83 SdfPathSet&& includedCollections,
84 TfToken const &topExpansionRule);
85
89 bool HasExcludes() const {
90 return _hasExcludes;
91 }
92
96 const PathExpansionRuleMap& GetAsPathExpansionRuleMap() const {
97 return _pathExpansionRuleMap;
98 }
99
106 const SdfPathSet& GetIncludedCollections() const {
107 return _includedCollections;
108 }
109
115 TfToken GetTopExpansionRule() const {
116 return _topExpansionRule;
117 }
118
119protected:
120
122 struct _Hash {
123 USD_API
124 size_t operator()(Usd_CollectionMembershipQueryBase const& query) const;
125 };
126
128 inline size_t _GetHash() const {
129 return _Hash()(*this);
130 }
131
132 USD_API
133 bool _IsPathIncludedByRuleMap(const SdfPath &path,
134 TfToken *expansionRule=nullptr) const;
135
136 USD_API
137 bool _IsPathIncludedByRuleMap(const SdfPath &path,
138 const TfToken &parentExpansionRule,
139 TfToken *expansionRule=nullptr) const;
140
141 // Return true if the _pathExpansionRuleMap is empty, meaning that we would
142 // use an expression in the derived class, for example.
143 USD_API
144 bool _HasEmptyRuleMap() const;
145
146 TfToken _topExpansionRule;
147
148 PathExpansionRuleMap _pathExpansionRuleMap;
149
150 SdfPathSet _includedCollections;
151
152 // A cached flag indicating whether _pathExpansionRuleMap contains
153 // any exclude rules.
154 bool _hasExcludes=false;
155};
156
160USD_API
163 Usd_CollectionMembershipQueryBase::PathExpansionRuleMap const &ruleMap);
164
165// -------------------------------------------------------------------------- //
166// UsdCollectionMembershipQuery //
167// -------------------------------------------------------------------------- //
175template <class ExprEval>
176class Usd_CollectionMembershipQuery : public Usd_CollectionMembershipQueryBase
177{
178public:
179 using ExpressionEvaluator = ExprEval;
180
181 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
182
211 IsPathIncluded(const SdfPath &path,
212 TfToken *expansionRule=nullptr) const {
213 // If we have a rule map, go that way. Otherwise try the expression.
214 if (UsesPathExpansionRuleMap()) {
216 _IsPathIncludedByRuleMap(path, expansionRule));
217 }
219 res = GetExpressionEvaluator().Match(path);
220 if (expansionRule) {
221 *expansionRule = res ?
222 UsdCollectionMembershipQueryTokens->
223 IncludedByMembershipExpression :
224 UsdCollectionMembershipQueryTokens->
225 ExcludedByMembershipExpression;
226 }
227 return res;
228 }
229
249 IsPathIncluded(const SdfPath &path,
250 const TfToken &parentExpansionRule,
251 TfToken *expansionRule=nullptr) const {
252 // If we have a rule map, go that way. Otherwise try the expression.
253 if (UsesPathExpansionRuleMap()) {
255 _IsPathIncludedByRuleMap(
256 path, parentExpansionRule, expansionRule));
257 }
259 res = GetExpressionEvaluator().Match(path);
260 if (expansionRule) {
261 *expansionRule = res ?
262 UsdCollectionMembershipQueryTokens->
263 IncludedByMembershipExpression :
264 UsdCollectionMembershipQueryTokens->
265 ExcludedByMembershipExpression;
266 }
267 return res;
268 }
269
273 bool UsesPathExpansionRuleMap() const {
274 return !_HasEmptyRuleMap();
275 }
276
277 void
278 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
279 _exprEval = std::move(exprEval);
280 }
281
282 void
283 SetExpressionEvaluator(ExpressionEvaluator const &exprEval) {
284 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
285 }
286
289 ExpressionEvaluator const &
290 GetExpressionEvaluator() const {
291 return _exprEval;
292 }
293
296 bool HasExpression() const {
297 return !_exprEval.IsEmpty();
298 }
299
301 bool operator==(Usd_CollectionMembershipQuery const& rhs) const {
302 // Note that MembershipQuery objects that have non-empty _exprEval never
303 // compare equal to each other. This is because the evaluator objects
304 // run code, and there's no good way to determine equivalence.
305 return _topExpansionRule == rhs._topExpansionRule &&
306 _hasExcludes == rhs._hasExcludes &&
307 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
308 _includedCollections == rhs._includedCollections &&
309 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
310 ;
311 }
312
314 bool operator!=(Usd_CollectionMembershipQuery const& rhs) const {
315 return !(*this == rhs);
316 }
317
319 struct Hash {
320 size_t operator()(Usd_CollectionMembershipQuery const& query) const {
321 return TfHash::Combine(query._GetHash(), query._exprEval.IsEmpty());
322 }
323 };
324
326 inline size_t GetHash() const {
327 return Hash()(*this);
328 }
329
330private:
331 ExpressionEvaluator _exprEval;
332};
333
334
339 struct PathToObj {
340 UsdObject operator()(SdfPath const &path) const {
341 return stage->GetObjectAtPath(path);
342 }
343 UsdStageWeakPtr stage;
344 };
345
346public:
348 using IncrementalSearcher =
349 typename PathExprEval::IncrementalSearcher<PathToObj>;
350
353
364 USD_API
365 UsdObjectCollectionExpressionEvaluator(UsdStageWeakPtr const &stage,
366 SdfPathExpression const &expr);
367
370 bool IsEmpty() const {
371 return !_stage || _evaluator.IsEmpty();
372 }
373
376 UsdStageWeakPtr const &GetStage() const { return _stage; }
377
379 USD_API
381 Match(SdfPath const &path) const;
382
384 USD_API
386 Match(UsdObject const &object) const;
387
395 USD_API
396 IncrementalSearcher MakeIncrementalSearcher() const;
397
398private:
399 UsdStageWeakPtr _stage;
400 PathExprEval _evaluator;
401};
402
404 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
405
411USD_API
413 const UsdCollectionMembershipQuery &query,
414 const UsdStageWeakPtr &stage,
415 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
416
422USD_API
424 const UsdCollectionMembershipQuery &query,
425 const UsdStageWeakPtr &stage,
426 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
427
428PXR_NAMESPACE_CLOSE_SCOPE
429
430#endif
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.
Definition path.h:281
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.
Definition hash.h:487
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81
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.
Definition object.h:116
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.