Loading...
Searching...
No Matches
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
28
29#include "pxr/pxr.h"
30
31#include "pxr/usd/usd/common.h"
32#include "pxr/usd/usd/object.h"
34
36#include "pxr/base/tf/hash.h"
38#include "pxr/usd/sdf/path.h"
39#include "pxr/usd/sdf/pathExpression.h"
40#include "pxr/usd/sdf/pathExpressionEval.h"
41
42#include <unordered_map>
43
44PXR_NAMESPACE_OPEN_SCOPE
45
46#define USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS \
47 (IncludedByMembershipExpression) \
48 (ExcludedByMembershipExpression)
49
50TF_DECLARE_PUBLIC_TOKENS(UsdCollectionMembershipQueryTokens,
51 USD_API, USD_COLLECTION_MEMBERSHIP_QUERY_TOKENS);
52
53
54class Usd_CollectionMembershipQueryBase
55{
56public:
66 using PathExpansionRuleMap = std::unordered_map<SdfPath,
67 TfToken, SdfPath::Hash>;
68
71 Usd_CollectionMembershipQueryBase() = default;
72
76 Usd_CollectionMembershipQueryBase(
77 const PathExpansionRuleMap& pathExpansionRuleMap,
78 const SdfPathSet& includedCollections);
79
81 Usd_CollectionMembershipQueryBase(
82 PathExpansionRuleMap&& pathExpansionRuleMap,
83 SdfPathSet&& includedCollections);
84
87 Usd_CollectionMembershipQueryBase(
88 const PathExpansionRuleMap& pathExpansionRuleMap,
89 const SdfPathSet& includedCollections,
90 const TfToken &topExpansionRule);
91
94 Usd_CollectionMembershipQueryBase(
95 PathExpansionRuleMap&& pathExpansionRuleMap,
96 SdfPathSet&& includedCollections,
97 TfToken const &topExpansionRule);
98
102 bool HasExcludes() const {
103 return _hasExcludes;
104 }
105
106
110 const PathExpansionRuleMap& GetAsPathExpansionRuleMap() const {
111 return _pathExpansionRuleMap;
112 }
113
120 const SdfPathSet& GetIncludedCollections() const {
121 return _includedCollections;
122 }
123
129 TfToken GetTopExpansionRule() const {
130 return _topExpansionRule;
131 }
132
133protected:
134
136 struct _Hash {
137 USD_API
138 size_t operator()(Usd_CollectionMembershipQueryBase const& query) const;
139 };
140
142 inline size_t _GetHash() const {
143 return _Hash()(*this);
144 }
145
146 USD_API
147 bool _IsPathIncludedByRuleMap(const SdfPath &path,
148 TfToken *expansionRule=nullptr) const;
149
150 USD_API
151 bool _IsPathIncludedByRuleMap(const SdfPath &path,
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
160 TfToken _topExpansionRule;
161
162 PathExpansionRuleMap _pathExpansionRuleMap;
163
164 SdfPathSet _includedCollections;
165
166 // A cached flag indicating whether _pathExpansionRuleMap contains
167 // any exclude rules.
168 bool _hasExcludes=false;
169};
170
171
172// -------------------------------------------------------------------------- //
173// UsdCollectionMembershipQuery //
174// -------------------------------------------------------------------------- //
182template <class ExprEval>
183class Usd_CollectionMembershipQuery : public Usd_CollectionMembershipQueryBase
184{
185public:
186 using ExpressionEvaluator = ExprEval;
187
188 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
189
218 IsPathIncluded(const SdfPath &path,
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
256 IsPathIncluded(const SdfPath &path,
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()) {
262 _IsPathIncludedByRuleMap(
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
280 bool UsesPathExpansionRuleMap() const {
281 return !_HasEmptyRuleMap();
282 }
283
284 void
285 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
286 _exprEval = std::move(exprEval);
287 }
288
289 void
290 SetExpressionEvaluator(ExpressionEvaluator const &exprEval) {
291 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
292 }
293
296 ExpressionEvaluator const &
297 GetExpressionEvaluator() const {
298 return _exprEval;
299 }
300
303 bool HasExpression() const {
304 return !_exprEval.IsEmpty();
305 }
306
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 &&
314 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
315 _includedCollections == rhs._includedCollections &&
316 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
317 ;
318 }
319
321 bool operator!=(Usd_CollectionMembershipQuery const& rhs) const {
322 return !(*this == rhs);
323 }
324
326 struct Hash {
327 size_t operator()(Usd_CollectionMembershipQuery const& query) const {
328 return TfHash::Combine(query._GetHash(), query._exprEval.IsEmpty());
329 }
330 };
331
333 inline size_t GetHash() const {
334 return Hash()(*this);
335 }
336
337private:
338 ExpressionEvaluator _exprEval;
339};
340
341
346 struct PathToObj {
347 UsdObject operator()(SdfPath const &path) const {
348 return stage->GetObjectAtPath(path);
349 }
350 UsdStageWeakPtr stage;
351 };
352
353public:
355 using IncrementalSearcher =
356 typename PathExprEval::IncrementalSearcher<PathToObj>;
357
360
371 USD_API
372 UsdObjectCollectionExpressionEvaluator(UsdStageWeakPtr const &stage,
373 SdfPathExpression const &expr);
374
377 bool IsEmpty() const {
378 return !_stage || _evaluator.IsEmpty();
379 }
380
383 UsdStageWeakPtr const &GetStage() const { return _stage; }
384
386 USD_API
388 Match(SdfPath const &path) const;
389
391 USD_API
393 Match(UsdObject const &object) const;
394
402 USD_API
403 IncrementalSearcher MakeIncrementalSearcher() const;
404
405private:
406 UsdStageWeakPtr _stage;
407 PathExprEval _evaluator;
408};
409
411 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
412
418USD_API
420 const UsdCollectionMembershipQuery &query,
421 const UsdStageWeakPtr &stage,
422 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
423
429USD_API
431 const UsdCollectionMembershipQuery &query,
432 const UsdStageWeakPtr &stage,
433 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
434
435PXR_NAMESPACE_CLOSE_SCOPE
436
437#endif
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.
Definition: path.h:290
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:492
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
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:132
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.
Definition: staticTokens.h:98