HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
10 /// \file usd/collectionMembershipQuery.h
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/usd/usd/common.h"
15 #include "pxr/usd/usd/object.h"
16 #include "pxr/usd/usd/primFlags.h"
17 
19 #include "pxr/base/tf/hash.h"
21 #include "pxr/usd/sdf/path.h"
24 
25 #include <unordered_map>
26 
28 
29 #define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
30  (IncludedByMembershipExpression) \
31  (ExcludedByMembershipExpression)
32 
33 TF_DECLARE_PUBLIC_TOKENS(UsdCollectionMembershipQueryTokens,
35 
36 
38 {
39 public:
40  /// Holds an unordered map describing membership of paths in this collection
41  /// and the associated expansionRule for how the paths are to be expanded.
42  /// Valid expansionRules are UsdTokens->explicitOnly,
43  /// UsdTokens->expandPrims, and UsdTokens->expandPrimsAndProperties. For
44  /// more information on the expansion rules, please see the expansionRule
45  /// attribute on UsdCollectionAPI.
46  /// If a collection includes another collection, the included collection's
47  /// PathExpansionRuleMap is merged into this one. If a path is excluded,
48  /// its expansion rule is set to UsdTokens->exclude.
49  using PathExpansionRuleMap = std::unordered_map<SdfPath,
51 
52  /// Default Constructor, creates an empty Usd_CollectionMembershipQueryBase
53  /// object
55 
56  /// Constructor that takes a path expansion rule map. The map is scanned
57  /// for 'excludes' when the Usd_CollectionMembershipQueryBase object is
58  /// constructed.
60  const PathExpansionRuleMap& pathExpansionRuleMap,
61  const SdfPathSet& includedCollections);
62 
63  /// Constructor that takes a path expansion rule map as an rvalue reference
65  PathExpansionRuleMap&& pathExpansionRuleMap,
66  SdfPathSet&& includedCollections);
67 
68  /// Constructor that additionally takes an additional expression evaluator
69  /// and a top-level expansion rule.
71  const PathExpansionRuleMap& pathExpansionRuleMap,
72  const SdfPathSet& includedCollections,
73  const TfToken &topExpansionRule);
74 
75  /// Constructor that additionally takes an additional expression evaluator
76  /// as an rvalue reference and a top-level expansion rule.
78  PathExpansionRuleMap&& pathExpansionRuleMap,
79  SdfPathSet&& includedCollections,
80  TfToken const &topExpansionRule);
81 
82  /// Returns true if the collection excludes one or more paths below an
83  /// included path via the excludes relationship (see
84  /// UsdCollectionAPI::GetExcludesRel()).
85  bool HasExcludes() const {
86  return _hasExcludes;
87  }
88 
89 
90  /// Returns a raw map of the paths included or excluded in the
91  /// collection along with the expansion rules for the included
92  /// paths.
94  return _pathExpansionRuleMap;
95  }
96 
97  /// Returns a set of paths for all collections that were included in the
98  /// collection from which this Usd_CollectionMembershipQueryBase object was
99  /// computed. This set is recursive, so collections that were included by
100  /// other collections will be part of this set. The collection from which
101  /// this Usd_CollectionMembershipQueryBase object was computed is *not* part
102  /// of this set.
104  return _includedCollections;
105  }
106 
107  /// Return the top expansion rule for this query object. This rule is the
108  /// expansion rule from the UsdCollectionAPI instance that was used to build
109  /// this query object. The top expansion rule is used when evaluating the
110  /// expression associated with this query, to determine whether it should
111  /// match prims only, or both prims and properties.
113  return _topExpansionRule;
114  }
115 
116 protected:
117 
118  /// Hash functor
119  struct _Hash {
120  USD_API
122  };
123 
124  /// Hash function
125  inline size_t _GetHash() const {
126  return _Hash()(*this);
127  }
128 
129  USD_API
131  TfToken *expansionRule=nullptr) const;
132 
133  USD_API
135  const TfToken &parentExpansionRule,
136  TfToken *expansionRule=nullptr) const;
137 
138  // Return true if the _pathExpansionRuleMap is empty, meaning that we would
139  // use an expression in the derived class, for example.
140  USD_API
141  bool _HasEmptyRuleMap() const;
142 
144 
146 
148 
149  // A cached flag indicating whether _pathExpansionRuleMap contains
150  // any exclude rules.
151  bool _hasExcludes=false;
152 };
153 
154 /// Compute an SdfPathExpression that matches the same paths as \p ruleMap. The
155 /// resulting SdfPathExpression is always complete (see
156 /// SdfPathExpression::IsComplete()) and never contains predicates.
157 USD_API
161 
162 // -------------------------------------------------------------------------- //
163 // UsdCollectionMembershipQuery //
164 // -------------------------------------------------------------------------- //
165 /// \class UsdCollectionMembershipQuery
166 ///
167 /// \brief Represents a flattened view of a collection. For more information
168 /// about collections, please see UsdCollectionAPI as a way to encode and
169 /// retrieve a collection from scene description. A
170 /// UsdCollectionMembershipQuery object can be used to answer queries about
171 /// membership of paths in the collection efficiently.
172 template <class ExprEval>
174 {
175 public:
176  using ExpressionEvaluator = ExprEval;
177 
179 
180  /// \overload
181  /// Returns whether the given path is included in the collection from which
182  /// this Usd_CollectionMembershipQueryBase object was computed. This is the
183  /// API that clients should use for determining if a given object is a
184  /// member of the collection. To enumerate all the members of a collection,
185  /// use \ref UsdComputeIncludedObjectsFromCollection or \ref
186  /// UsdComputeIncludedPathsFromCollection.
187  ///
188  /// If \p expansionRule is not nullptr, it is set to the expansion- rule
189  /// value that caused the path to be included in or excluded from the
190  /// collection. If \p path is not included in the collection, \p
191  /// expansionRule is set to UsdTokens->exclude. If this query is not using
192  /// an expansion rule map and is instead using a pattern-based membership
193  /// expression, then expansionRule is set to one of the special
194  /// UsdCollectionMembershipQueryTokens values,
195  /// IncludedByMembershipExpression or ExcludedByMembershipExpression as
196  /// appropriate.
197  ///
198  /// It is useful to specify this parameter and use this overload of
199  /// IsPathIncluded(), when you're interested in traversing a subtree
200  /// and want to know whether the root of the subtree is included in a
201  /// collection. For evaluating membership of descendants of the root,
202  /// please use the other overload of IsPathIncluded(), that takes both
203  /// a path and the parent expansionRule.
204  ///
205  /// The python version of this method only returns the boolean result. It
206  /// does not return an SdfPredicateFunctionResult or set \p expansionRule.
209  TfToken *expansionRule=nullptr) const {
210  // If we have a rule map, go that way. Otherwise try the expression.
211  if (UsesPathExpansionRuleMap()) {
213  _IsPathIncludedByRuleMap(path, expansionRule));
214  }
216  res = GetExpressionEvaluator().Match(path);
217  if (expansionRule) {
218  *expansionRule = res ?
219  UsdCollectionMembershipQueryTokens->
220  IncludedByMembershipExpression :
221  UsdCollectionMembershipQueryTokens->
222  ExcludedByMembershipExpression;
223  }
224  return res;
225  }
226 
227  /// \overload
228  /// Returns whether the given path, \p path is included in the
229  /// collection from which this Usd_CollectionMembershipQueryBase object was
230  /// computed, given the parent-path's inherited expansion rule,
231  /// \p parentExpansionRule.
232  ///
233  /// If \p expansionRule is not nullptr, it is set to the expansion- rule
234  /// value that caused the path to be included in or excluded from the
235  /// collection. If \p path is not included in the collection, \p
236  /// expansionRule is set to UsdTokens->exclude. If this query is not using
237  /// an expansion rule map and is instead using a pattern-based membership
238  /// expression, then expansionRule is set to one of the special
239  /// UsdCollectionMembershipQueryTokens values,
240  /// IncludedByMembershipExpression or ExcludedByMembershipExpression as
241  /// appropriate.
242  ///
243  /// The python version of this method only returns the boolean result.
244  /// It does not return \p expansionRule.
247  const TfToken &parentExpansionRule,
248  TfToken *expansionRule=nullptr) const {
249  // If we have a rule map, go that way. Otherwise try the expression.
250  if (UsesPathExpansionRuleMap()) {
253  path, parentExpansionRule, expansionRule));
254  }
256  res = GetExpressionEvaluator().Match(path);
257  if (expansionRule) {
258  *expansionRule = res ?
259  UsdCollectionMembershipQueryTokens->
260  IncludedByMembershipExpression :
261  UsdCollectionMembershipQueryTokens->
262  ExcludedByMembershipExpression;
263  }
264  return res;
265  }
266 
267  /// Return true if this query uses the explicit path-expansion rule method
268  /// to determine collection membership. Otherwise, return false if it uses
269  /// the pattern-based membership expression to determine membership.
271  return !_HasEmptyRuleMap();
272  }
273 
274  void
276  _exprEval = std::move(exprEval);
277  }
278 
279  void
282  }
283 
284  /// Return the expression evaluator associated with this query object. This
285  /// may be an empty evaluator. See HasExpression().
286  ExpressionEvaluator const &
288  return _exprEval;
289  }
290 
291  /// Return true if the expression evaluator associated with this query
292  /// object is not empty. See GetExpressionEvaluator().
293  bool HasExpression() const {
294  return !_exprEval.IsEmpty();
295  }
296 
297  /// Equality operator
298  bool operator==(Usd_CollectionMembershipQuery const& rhs) const {
299  // Note that MembershipQuery objects that have non-empty _exprEval never
300  // compare equal to each other. This is because the evaluator objects
301  // run code, and there's no good way to determine equivalence.
302  return _topExpansionRule == rhs._topExpansionRule &&
303  _hasExcludes == rhs._hasExcludes &&
306  _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
307  ;
308  }
309 
310  /// Inequality operator
311  bool operator!=(Usd_CollectionMembershipQuery const& rhs) const {
312  return !(*this == rhs);
313  }
314 
315  /// Hash functor
316  struct Hash {
318  return TfHash::Combine(query._GetHash(), query._exprEval.IsEmpty());
319  }
320  };
321 
322  /// Hash function
323  inline size_t GetHash() const {
324  return Hash()(*this);
325  }
326 
327 private:
328  ExpressionEvaluator _exprEval;
329 };
330 
331 
332 /// \class UsdObjectCollectionExpressionEvaluator
333 ///
334 /// \brief Evaluates SdfPathExpressions with objects from a given UsdStage.
336  struct PathToObj {
337  UsdObject operator()(SdfPath const &path) const {
338  return stage->GetObjectAtPath(path);
339  }
340  UsdStageWeakPtr stage;
341  };
342 
343 public:
345  using IncrementalSearcher =
346  typename PathExprEval::IncrementalSearcher<PathToObj>;
347 
348  /// Construct an empty evaluator.
350 
351  /// Construct an evaluator that evalutates \p expr on objects from \p
352  /// stage. The \p expr must be "complete" (see
353  /// SdfPathExpression::IsComplete()).
354  ///
355  /// Typically these objects are not constructed directly, but instead
356  /// are created by UsdCollectionAPI::ComputeMembershipQuery() for
357  /// Usd_CollectionMembershipQuery's use. However it is possible to
358  /// construct them directly if one wishes. Consider calling
359  /// UsdCollectionAPI::ResolveCompleteMembershipExpression() to produce
360  /// an approprate expression.
361  USD_API
363  SdfPathExpression const &expr);
364 
365  /// Return true if this evaluator has an invalid stage or an empty
366  /// underlying SdfPathExpressionEval object.
367  bool IsEmpty() const {
368  return !_stage || _evaluator.IsEmpty();
369  }
370 
371  /// Return the stage this object was constructed with, or nullptr if it
372  /// was default constructed.
373  UsdStageWeakPtr const &GetStage() const { return _stage; }
374 
375  /// Return the result of evaluating the expression against \p path.
376  USD_API
378  Match(SdfPath const &path) const;
379 
380  /// Return the result of evaluating the expression against \p object.
381  USD_API
383  Match(UsdObject const &object) const;
384 
385  /// Create an incremental searcher from this evaluator. See
386  /// SdfPathExpressionEval::IncrementalSearcher for more info and API.
387  ///
388  /// The returned IncrementalSearcher refers to the evaluator object owned by
389  /// this UsdObjectCollectionExpressionEvaluator object. This means that the
390  /// IncrementalSearcher must not be used after this
391  /// UsdObjectCollectionExpressionEvaluator object's lifetime ends.
392  USD_API
394 
395 private:
396  UsdStageWeakPtr _stage;
397  PathExprEval _evaluator;
398 };
399 
402 
403 /// Returns all the usd objects that satisfy the predicate, \p pred in the
404 /// collection represented by the UsdCollectionMembershipQuery object, \p
405 /// query.
406 ///
407 /// The results depends on the load state of the UsdStage, \p stage.
408 USD_API
409 std::set<UsdObject> UsdComputeIncludedObjectsFromCollection(
411  const UsdStageWeakPtr &stage,
413 
414 /// Returns all the paths that satisfy the predicate, \p pred in the
415 /// collection represented by the UsdCollectionMembershipQuery object, \p
416 /// query.
417 ///
418 /// The result depends on the load state of the UsdStage, \p stage.
419 USD_API
422  const UsdStageWeakPtr &stage,
424 
426 
427 #endif
GLenum query
Definition: glad.h:2772
#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS
const SdfPathSet & GetIncludedCollections() const
typename PathExprEval::IncrementalSearcher< PathToObj > IncrementalSearcher
#define USD_API
Definition: api.h:23
SdfPredicateFunctionResult IsPathIncluded(const SdfPath &path, TfToken *expansionRule=nullptr) const
USD_API std::set< UsdObject > UsdComputeIncludedObjectsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2099
const PathExpansionRuleMap & GetAsPathExpansionRuleMap() const
UsdObjectCollectionExpressionEvaluator()=default
Construct an empty evaluator.
size_t operator()(Usd_CollectionMembershipQuery const &query) const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
USD_API bool _IsPathIncludedByRuleMap(const SdfPath &path, TfToken *expansionRule=nullptr) const
std::unordered_map< SdfPath, TfToken, SdfPath::Hash > PathExpansionRuleMap
Represents a flattened view of a collection. For more information about collections, please see UsdCollectionAPI as a way to encode and retrieve a collection from scene description. A UsdCollectionMembershipQuery object can be used to answer queries about membership of paths in the collection efficiently.
UsdStagePtr UsdStageWeakPtr
Definition: common.h:38
size_t GetHash() const
Hash function.
Definition: token.h:70
TF_DECLARE_PUBLIC_TOKENS(UsdCollectionMembershipQueryTokens, USD_API, USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS)
void SetExpressionEvaluator(ExpressionEvaluator &&exprEval)
SdfPredicateFunctionResult IsPathIncluded(const SdfPath &path, const TfToken &parentExpansionRule, TfToken *expansionRule=nullptr) const
static SdfPredicateFunctionResult MakeVarying(bool value)
Create with value and 'MayVaryOverDescendants'.
Definition: path.h:273
bool operator!=(Usd_CollectionMembershipQuery const &rhs) const
Inequality operator.
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:192
UsdStageWeakPtr const & GetStage() const
USD_API IncrementalSearcher MakeIncrementalSearcher() const
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
USD_API const Usd_PrimFlagsConjunction UsdPrimDefaultPredicate
Evaluates SdfPathExpressions with objects from a given UsdStage.
USD_API SdfPathExpression UsdComputePathExpressionFromCollectionMembershipQueryRuleMap(Usd_CollectionMembershipQueryBase::PathExpansionRuleMap const &ruleMap)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
ExpressionEvaluator const & GetExpressionEvaluator() const
size_t _GetHash() const
Hash function.
USD_API SdfPredicateFunctionResult Match(SdfPath const &path) const
Return the result of evaluating the expression against path.
USD_API SdfPathSet UsdComputeIncludedPathsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
bool operator==(Usd_CollectionMembershipQuery const &rhs) const
Equality operator.
USD_API size_t operator()(Usd_CollectionMembershipQueryBase const &query) const
void SetExpressionEvaluator(ExpressionEvaluator const &exprEval)
SdfPathExpressionEval< UsdObject > PathExprEval
USD_API bool _HasEmptyRuleMap() const