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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
25 #define PXR_USD_USD_COLLECTION_MEMBERSHIP_QUERY_H
26 
27 /// \file usd/collectionMembershipQuery.h
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxr/usd/usd/common.h"
32 #include "pxr/usd/usd/object.h"
33 #include "pxr/usd/usd/primFlags.h"
34 
36 #include "pxr/base/tf/hash.h"
38 #include "pxr/usd/sdf/path.h"
41 
42 #include <unordered_map>
43 
45 
46 #define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
47  (IncludedByMembershipExpression) \
48  (ExcludedByMembershipExpression)
49 
50 TF_DECLARE_PUBLIC_TOKENS(UsdCollectionMembershipQueryTokens,
52 
53 
55 {
56 public:
57  /// Holds an unordered map describing membership of paths in this collection
58  /// and the associated expansionRule for how the paths are to be expanded.
59  /// Valid expansionRules are UsdTokens->explicitOnly,
60  /// UsdTokens->expandPrims, and UsdTokens->expandPrimsAndProperties. For
61  /// more information on the expansion rules, please see the expansionRule
62  /// attribute on UsdCollectionAPI.
63  /// If a collection includes another collection, the included collection's
64  /// PathExpansionRuleMap is merged into this one. If a path is excluded,
65  /// its expansion rule is set to UsdTokens->exclude.
66  using PathExpansionRuleMap = std::unordered_map<SdfPath,
68 
69  /// Default Constructor, creates an empty Usd_CollectionMembershipQueryBase
70  /// object
72 
73  /// Constructor that takes a path expansion rule map. The map is scanned
74  /// for 'excludes' when the Usd_CollectionMembershipQueryBase object is
75  /// constructed.
77  const PathExpansionRuleMap& pathExpansionRuleMap,
78  const SdfPathSet& includedCollections);
79 
80  /// Constructor that takes a path expansion rule map as an rvalue reference
82  PathExpansionRuleMap&& pathExpansionRuleMap,
83  SdfPathSet&& includedCollections);
84 
85  /// Constructor that additionally takes an additional expression evaluator
86  /// and a top-level expansion rule.
88  const PathExpansionRuleMap& pathExpansionRuleMap,
89  const SdfPathSet& includedCollections,
90  const TfToken &topExpansionRule);
91 
92  /// Constructor that additionally takes an additional expression evaluator
93  /// as an rvalue reference and a top-level expansion rule.
95  PathExpansionRuleMap&& pathExpansionRuleMap,
96  SdfPathSet&& includedCollections,
97  TfToken const &topExpansionRule);
98 
99  /// Returns true if the collection excludes one or more paths below an
100  /// included path via the excludes relationship (see
101  /// UsdCollectionAPI::GetExcludesRel()).
102  bool HasExcludes() const {
103  return _hasExcludes;
104  }
105 
106 
107  /// Returns a raw map of the paths included or excluded in the
108  /// collection along with the expansion rules for the included
109  /// paths.
111  return _pathExpansionRuleMap;
112  }
113 
114  /// Returns a set of paths for all collections that were included in the
115  /// collection from which this Usd_CollectionMembershipQueryBase object was
116  /// computed. This set is recursive, so collections that were included
117  /// by other collections will be part of this set. The collection from
118  /// which this Usd_CollectionMembershipQueryBase object was computed is *not*
119  /// part of this set.
121  return _includedCollections;
122  }
123 
124  /// Return the top expansion rule for this query object. This rule is the
125  /// expansion rule from the UsdCollectionAPI instance that was used to build
126  /// this query object. The top expansion rule is used when evaluating the
127  /// expression associated with this query, to determine whether it should
128  /// match prims only, or both prims and properties.
130  return _topExpansionRule;
131  }
132 
133 protected:
134 
135  /// Hash functor
136  struct _Hash {
137  USD_API
139  };
140 
141  /// Hash function
142  inline size_t _GetHash() const {
143  return _Hash()(*this);
144  }
145 
146  USD_API
148  TfToken *expansionRule=nullptr) const;
149 
150  USD_API
152  const TfToken &parentExpansionRule,
153  TfToken *expansionRule=nullptr) const;
154 
155  // Return true if the _pathExpansionRuleMap is empty, meaning that we would
156  // use an expression in the derived class, for example.
157  USD_API
158  bool _HasEmptyRuleMap() const;
159 
161 
163 
165 
166  // A cached flag indicating whether _pathExpansionRuleMap contains
167  // any exclude rules.
168  bool _hasExcludes=false;
169 };
170 
171 
172 // -------------------------------------------------------------------------- //
173 // UsdCollectionMembershipQuery //
174 // -------------------------------------------------------------------------- //
175 /// \class UsdCollectionMembershipQuery
176 ///
177 /// \brief Represents a flattened view of a collection. For more information
178 /// about collections, please see UsdCollectionAPI as a way to encode and
179 /// retrieve a collection from scene description. A
180 /// UsdCollectionMembershipQuery object can be used to answer queries about
181 /// membership of paths in the collection efficiently.
182 template <class ExprEval>
184 {
185 public:
186  using ExpressionEvaluator = ExprEval;
187 
189 
190  /// \overload
191  /// Returns whether the given path is included in the collection from which
192  /// this Usd_CollectionMembershipQueryBase object was computed. This is the
193  /// API that clients should use for determining if a given object is a
194  /// member of the collection. To enumerate all the members of a collection,
195  /// use \ref UsdComputeIncludedObjectsFromCollection or \ref
196  /// UsdComputeIncludedPathsFromCollection.
197  ///
198  /// If \p expansionRule is not nullptr, it is set to the expansion- rule
199  /// value that caused the path to be included in or excluded from the
200  /// collection. If \p path is not included in the collection, \p
201  /// expansionRule is set to UsdTokens->exclude. If this query is not using
202  /// an expansion rule map and is instead using a pattern-based membership
203  /// expression, then expansionRule is set to one of the special
204  /// UsdCollectionMembershipQueryTokens values,
205  /// IncludedByMembershipExpression or ExcludedByMembershipExpression as
206  /// appropriate.
207  ///
208  /// It is useful to specify this parameter and use this overload of
209  /// IsPathIncluded(), when you're interested in traversing a subtree
210  /// and want to know whether the root of the subtree is included in a
211  /// collection. For evaluating membership of descendants of the root,
212  /// please use the other overload of IsPathIncluded(), that takes both
213  /// a path and the parent expansionRule.
214  ///
215  /// The python version of this method only returns the boolean result. It
216  /// does not return an SdfPredicateFunctionResult or set \p expansionRule.
219  TfToken *expansionRule=nullptr) const {
220  // If we have a rule map, go that way. Otherwise try the expression.
221  if (UsesPathExpansionRuleMap()) {
223  _IsPathIncludedByRuleMap(path, expansionRule));
224  }
226  res = GetExpressionEvaluator().Match(path);
227  if (expansionRule) {
228  *expansionRule = res ?
229  UsdCollectionMembershipQueryTokens->
230  IncludedByMembershipExpression :
231  UsdCollectionMembershipQueryTokens->
232  ExcludedByMembershipExpression;
233  }
234  return res;
235  }
236 
237  /// \overload
238  /// Returns whether the given path, \p path is included in the
239  /// collection from which this Usd_CollectionMembershipQueryBase object was
240  /// computed, given the parent-path's inherited expansion rule,
241  /// \p parentExpansionRule.
242  ///
243  /// If \p expansionRule is not nullptr, it is set to the expansion- rule
244  /// value that caused the path to be included in or excluded from the
245  /// collection. If \p path is not included in the collection, \p
246  /// expansionRule is set to UsdTokens->exclude. If this query is not using
247  /// an expansion rule map and is instead using a pattern-based membership
248  /// expression, then expansionRule is set to one of the special
249  /// UsdCollectionMembershipQueryTokens values,
250  /// IncludedByMembershipExpression or ExcludedByMembershipExpression as
251  /// appropriate.
252  ///
253  /// The python version of this method only returns the boolean result.
254  /// It does not return \p expansionRule.
257  const TfToken &parentExpansionRule,
258  TfToken *expansionRule=nullptr) const {
259  // If we have a rule map, go that way. Otherwise try the expression.
260  if (UsesPathExpansionRuleMap()) {
263  path, parentExpansionRule, expansionRule));
264  }
266  res = GetExpressionEvaluator().Match(path);
267  if (expansionRule) {
268  *expansionRule = res ?
269  UsdCollectionMembershipQueryTokens->
270  IncludedByMembershipExpression :
271  UsdCollectionMembershipQueryTokens->
272  ExcludedByMembershipExpression;
273  }
274  return res;
275  }
276 
277  /// Return true if this query uses the explicit path-expansion rule method
278  /// to determine collection membership. Otherwise, return false if it uses
279  /// the pattern-based membership expression to determine membership.
281  return !_HasEmptyRuleMap();
282  }
283 
284  void
286  _exprEval = std::move(exprEval);
287  }
288 
289  void
292  }
293 
294  /// Return the expression evaluator associated with this query object. This
295  /// may be an empty evaluator. See HasExpression().
296  ExpressionEvaluator const &
298  return _exprEval;
299  }
300 
301  /// Return true if the expression evaluator associated with this query
302  /// object is not empty. See GetExpressionEvaluator().
303  bool HasExpression() const {
304  return !_exprEval.IsEmpty();
305  }
306 
307  /// Equality operator
308  bool operator==(Usd_CollectionMembershipQuery const& rhs) const {
309  // Note that MembershipQuery objects that have non-empty _exprEval never
310  // compare equal to each other. This is because the evaluator objects
311  // run code, and there's no good way to determine equivalence.
312  return _topExpansionRule == rhs._topExpansionRule &&
313  _hasExcludes == rhs._hasExcludes &&
316  _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
317  ;
318  }
319 
320  /// Inequality operator
321  bool operator!=(Usd_CollectionMembershipQuery const& rhs) const {
322  return !(*this == rhs);
323  }
324 
325  /// Hash functor
326  struct Hash {
328  return TfHash::Combine(query._GetHash(), query._exprEval.IsEmpty());
329  }
330  };
331 
332  /// Hash function
333  inline size_t GetHash() const {
334  return Hash()(*this);
335  }
336 
337 private:
338  ExpressionEvaluator _exprEval;
339 };
340 
341 
342 /// \class UsdObjectCollectionExpressionEvaluator
343 ///
344 /// \brief Evaluates SdfPathExpressions with objects from a given UsdStage.
346  struct ObjToPath {
347  SdfPath operator()(UsdObject const &obj) const {
348  return obj.GetPath();
349  }
350  };
351 
352  struct PathToObj {
353  UsdObject operator()(SdfPath const &path) const {
354  return stage->GetObjectAtPath(path);
355  }
356  UsdStageWeakPtr stage;
357  };
358 
359 public:
361  using IncrementalSearcher =
362  typename PathExprEval::IncrementalSearcher<ObjToPath, PathToObj>;
363 
364  /// Construct an empty evaluator.
366 
367  /// Construct an evaluator that evalutates \p expr on objects from \p
368  /// stage. The \p expr must be "complete" (see
369  /// SdfPathExpression::IsComplete()).
370  ///
371  /// Typically these objects are not constructed directly, but instead
372  /// are created by UsdCollectionAPI::ComputeMembershipQuery() for
373  /// Usd_CollectionMembershipQuery's use. However it is possible to
374  /// construct them directly if one wishes. Consider calling
375  /// UsdCollectionAPI::ResolveCompleteMembershipExpression() to produce
376  /// an approprate expression.
377  USD_API
379  SdfPathExpression const &expr);
380 
381  /// Return true if this evaluator has an invalid stage or an empty
382  /// underlying SdfPathExpressionEval object.
383  bool IsEmpty() const {
384  return !_stage || _evaluator.IsEmpty();
385  }
386 
387  /// Return the stage this object was constructed with, or nullptr if it
388  /// was default constructed.
389  UsdStageWeakPtr const &GetStage() const { return _stage; }
390 
391  /// Return the result of evaluating the expression against \p path.
392  USD_API
394  Match(SdfPath const &path) const;
395 
396  /// Return the result of evaluating the expression against \p object.
397  USD_API
399  Match(UsdObject const &object) const;
400 
401  /// Create an incremental searcher from this evaluator. See
402  /// SdfPathExpressionEval::IncrementalSearcher for more info and API.
403  ///
404  /// The returned IncrementalSearcher refers to the evaluator object owned by
405  /// this UsdObjectCollectionExpressionEvaluator object. This means that the
406  /// IncrementalSearcher must not be used after this
407  /// UsdObjectCollectionExpressionEvaluator object's lifetime ends.
408  USD_API
410 
411 private:
412  UsdStageWeakPtr _stage;
413  PathExprEval _evaluator;
414 };
415 
418 
419 /// Returns all the usd objects that satisfy the predicate, \p pred in the
420 /// collection represented by the UsdCollectionMembershipQuery object, \p
421 /// query.
422 ///
423 /// The results depends on the load state of the UsdStage, \p stage.
424 USD_API
425 std::set<UsdObject> UsdComputeIncludedObjectsFromCollection(
427  const UsdStageWeakPtr &stage,
429 
430 /// Returns all the paths that satisfy the predicate, \p pred in the
431 /// collection represented by the UsdCollectionMembershipQuery object, \p
432 /// query.
433 ///
434 /// The result depends on the load state of the UsdStage, \p stage.
435 USD_API
438  const UsdStageWeakPtr &stage,
440 
442 
443 #endif
GLenum query
Definition: glad.h:2772
#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS
const SdfPathSet & GetIncludedCollections() const
#define USD_API
Definition: api.h:40
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:2038
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:55
size_t GetHash() const
Hash function.
Definition: token.h:87
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:290
bool operator!=(Usd_CollectionMembershipQuery const &rhs) const
Inequality operator.
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:209
UsdStageWeakPtr const & GetStage() const
SdfPath GetPath() const
Definition: object.h:203
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:492
typename PathExprEval::IncrementalSearcher< ObjToPath, PathToObj > IncrementalSearcher
USD_API const Usd_PrimFlagsConjunction UsdPrimDefaultPredicate
Evaluates SdfPathExpressions with objects from a given UsdStage.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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