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
174USD_API
177 Usd_CollectionMembershipQueryBase::PathExpansionRuleMap const &ruleMap);
178
179// -------------------------------------------------------------------------- //
180// UsdCollectionMembershipQuery //
181// -------------------------------------------------------------------------- //
189template <class ExprEval>
190class Usd_CollectionMembershipQuery : public Usd_CollectionMembershipQueryBase
191{
192public:
193 using ExpressionEvaluator = ExprEval;
194
195 using Usd_CollectionMembershipQueryBase::Usd_CollectionMembershipQueryBase;
196
225 IsPathIncluded(const SdfPath &path,
226 TfToken *expansionRule=nullptr) const {
227 // If we have a rule map, go that way. Otherwise try the expression.
228 if (UsesPathExpansionRuleMap()) {
230 _IsPathIncludedByRuleMap(path, expansionRule));
231 }
233 res = GetExpressionEvaluator().Match(path);
234 if (expansionRule) {
235 *expansionRule = res ?
236 UsdCollectionMembershipQueryTokens->
237 IncludedByMembershipExpression :
238 UsdCollectionMembershipQueryTokens->
239 ExcludedByMembershipExpression;
240 }
241 return res;
242 }
243
263 IsPathIncluded(const SdfPath &path,
264 const TfToken &parentExpansionRule,
265 TfToken *expansionRule=nullptr) const {
266 // If we have a rule map, go that way. Otherwise try the expression.
267 if (UsesPathExpansionRuleMap()) {
269 _IsPathIncludedByRuleMap(
270 path, parentExpansionRule, expansionRule));
271 }
273 res = GetExpressionEvaluator().Match(path);
274 if (expansionRule) {
275 *expansionRule = res ?
276 UsdCollectionMembershipQueryTokens->
277 IncludedByMembershipExpression :
278 UsdCollectionMembershipQueryTokens->
279 ExcludedByMembershipExpression;
280 }
281 return res;
282 }
283
287 bool UsesPathExpansionRuleMap() const {
288 return !_HasEmptyRuleMap();
289 }
290
291 void
292 SetExpressionEvaluator(ExpressionEvaluator &&exprEval) {
293 _exprEval = std::move(exprEval);
294 }
295
296 void
297 SetExpressionEvaluator(ExpressionEvaluator const &exprEval) {
298 SetExpressionEvaluator(ExpressionEvaluator { exprEval } );
299 }
300
303 ExpressionEvaluator const &
304 GetExpressionEvaluator() const {
305 return _exprEval;
306 }
307
310 bool HasExpression() const {
311 return !_exprEval.IsEmpty();
312 }
313
315 bool operator==(Usd_CollectionMembershipQuery const& rhs) const {
316 // Note that MembershipQuery objects that have non-empty _exprEval never
317 // compare equal to each other. This is because the evaluator objects
318 // run code, and there's no good way to determine equivalence.
319 return _topExpansionRule == rhs._topExpansionRule &&
320 _hasExcludes == rhs._hasExcludes &&
321 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
322 _includedCollections == rhs._includedCollections &&
323 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
324 ;
325 }
326
328 bool operator!=(Usd_CollectionMembershipQuery const& rhs) const {
329 return !(*this == rhs);
330 }
331
333 struct Hash {
334 size_t operator()(Usd_CollectionMembershipQuery const& query) const {
335 return TfHash::Combine(query._GetHash(), query._exprEval.IsEmpty());
336 }
337 };
338
340 inline size_t GetHash() const {
341 return Hash()(*this);
342 }
343
344private:
345 ExpressionEvaluator _exprEval;
346};
347
348
353 struct PathToObj {
354 UsdObject operator()(SdfPath const &path) const {
355 return stage->GetObjectAtPath(path);
356 }
357 UsdStageWeakPtr stage;
358 };
359
360public:
362 using IncrementalSearcher =
363 typename PathExprEval::IncrementalSearcher<PathToObj>;
364
367
378 USD_API
379 UsdObjectCollectionExpressionEvaluator(UsdStageWeakPtr const &stage,
380 SdfPathExpression const &expr);
381
384 bool IsEmpty() const {
385 return !_stage || _evaluator.IsEmpty();
386 }
387
390 UsdStageWeakPtr const &GetStage() const { return _stage; }
391
393 USD_API
395 Match(SdfPath const &path) const;
396
398 USD_API
400 Match(UsdObject const &object) const;
401
409 USD_API
410 IncrementalSearcher MakeIncrementalSearcher() const;
411
412private:
413 UsdStageWeakPtr _stage;
414 PathExprEval _evaluator;
415};
416
418 Usd_CollectionMembershipQuery<UsdObjectCollectionExpressionEvaluator>;
419
425USD_API
427 const UsdCollectionMembershipQuery &query,
428 const UsdStageWeakPtr &stage,
429 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
430
436USD_API
438 const UsdCollectionMembershipQuery &query,
439 const UsdStageWeakPtr &stage,
440 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
441
442PXR_NAMESPACE_CLOSE_SCOPE
443
444#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...
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.
Definition: staticTokens.h:98