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
30#include "pxr/pxr.h"
31#include "pxr/usd/sdf/path.h"
32#include "pxr/usd/sdf/pathExpression.h"
33#include "pxr/usd/sdf/pathExpressionEval.h"
34#include "pxr/usd/usd/common.h"
37
38#include <unordered_map>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
42// -------------------------------------------------------------------------- //
43// UsdCollectionMembershipQuery //
44// -------------------------------------------------------------------------- //
53{
54public:
55
60 struct ObjToPath {
61 SdfPath operator()(UsdObject const &obj) const {
62 return obj.GetPath();
63 }
64 };
65
66 struct PathToObj {
67 UsdObject operator()(SdfPath const &path) const {
68 return stage->GetObjectAtPath(path);
69 }
70 UsdStageWeakPtr stage;
71 };
72
73 public:
75 using IncrementalSearcher =
76 typename PathExprEval::IncrementalSearcher<ObjToPath, PathToObj>;
77
80
91 SDF_API
92 ExpressionEvaluator(UsdStageWeakPtr const &stage,
93 SdfPathExpression const &expr);
94
97 bool IsEmpty() const {
98 return !_stage || _evaluator.IsEmpty();
99 }
100
103 UsdStageWeakPtr const &GetStage() const { return _stage; }
104
106 SDF_API
108 Match(UsdObject const &object) const;
109
117 SDF_API
118 IncrementalSearcher MakeIncrementalSearcher() const;
119
120 private:
121 UsdStageWeakPtr _stage;
122 PathExprEval _evaluator;
123 };
124
134 using PathExpansionRuleMap = std::unordered_map<SdfPath,
135 TfToken, SdfPath::Hash>;
136
140
145 const PathExpansionRuleMap& pathExpansionRuleMap,
146 const SdfPathSet& includedCollections);
147
150 PathExpansionRuleMap&& pathExpansionRuleMap,
151 SdfPathSet&& includedCollections);
152
156 const PathExpansionRuleMap& pathExpansionRuleMap,
157 const SdfPathSet& includedCollections,
158 const ExpressionEvaluator &exprEval,
159 const TfToken &topExpansionRule);
160
164 PathExpansionRuleMap&& pathExpansionRuleMap,
165 SdfPathSet&& includedCollections,
166 ExpressionEvaluator &&exprEval,
167 TfToken const &topExpansionRule);
168
191 USD_API
192 bool IsPathIncluded(const SdfPath &path,
193 TfToken *expansionRule=nullptr) const;
194
208 USD_API
209 bool IsPathIncluded(const SdfPath &path,
210 const TfToken &parentExpansionRule,
211 TfToken *expansionRule=nullptr) const;
212
216 bool HasExcludes() const {
217 return _hasExcludes;
218 }
219
222 // Note that MembershipQuery objects that have non-empty _exprEval never
223 // compare equal to each other. This is because the evaluator objects
224 // run code, and there's no good way to determine equivalence.
225 return _topExpansionRule == rhs._topExpansionRule &&
226 _hasExcludes == rhs._hasExcludes &&
227 _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
228 _includedCollections == rhs._includedCollections &&
229 _exprEval.IsEmpty() == rhs._exprEval.IsEmpty();
230 ;
231 }
232
235 return !(*this == rhs);
236 }
237
239 struct Hash {
240 USD_API
241 size_t operator()(UsdCollectionMembershipQuery const& query) const;
242 };
243
245 inline size_t GetHash() const {
246 return Hash()(*this);
247 }
248
253 return _pathExpansionRuleMap;
254 }
255
262 const SdfPathSet& GetIncludedCollections() const {
263 return _includedCollections;
264 }
265
268 ExpressionEvaluator const &
270 return _exprEval;
271 }
272
275 bool HasExpression() const {
276 return !_exprEval.IsEmpty();
277 }
278
285 return _topExpansionRule;
286 }
287
288private:
289 TfToken _topExpansionRule;
290
291 PathExpansionRuleMap _pathExpansionRuleMap;
292
293 SdfPathSet _includedCollections;
294
295 ExpressionEvaluator _exprEval;
296
297 // A cached flag indicating whether _pathExpansionRuleMap contains
298 // any exclude rules.
299 bool _hasExcludes=false;
300};
301
307USD_API
309 const UsdCollectionMembershipQuery &query,
310 const UsdStageWeakPtr &stage,
311 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
312
318USD_API
320 const UsdCollectionMembershipQuery &query,
321 const UsdStageWeakPtr &stage,
322 const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate);
323
324PXR_NAMESPACE_CLOSE_SCOPE
325
326#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:291
Represents the result of a predicate function: a pair of the boolean result and a Constancy token ind...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Evaluates SdfPathExpressions with objects from a given UsdStage.
SDF_API IncrementalSearcher MakeIncrementalSearcher() const
Create an incremental searcher from this evaluator.
SDF_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.
SDF_API ExpressionEvaluator(UsdStageWeakPtr const &stage, SdfPathExpression const &expr)
Construct an evaluator that evalutates expr on objects from statge.
ExpressionEvaluator()=default
Construct an empty evaluator.
Represents a flattened view of a collection.
const SdfPathSet & GetIncludedCollections() const
Returns a set of paths for all collections that were included in the collection from which this UsdCo...
std::unordered_map< SdfPath, TfToken, SdfPath::Hash > PathExpansionRuleMap
Holds an unordered map describing membership of paths in this collection and the associated expansion...
UsdCollectionMembershipQuery()=default
Default Constructor, creates an empty UsdCollectionMembershipQuery object.
UsdCollectionMembershipQuery(PathExpansionRuleMap &&pathExpansionRuleMap, SdfPathSet &&includedCollections)
Constructor that takes a path expansion rule map as an rvalue reference.
const PathExpansionRuleMap & GetAsPathExpansionRuleMap() const
Returns a raw map of the paths included or excluded in the collection along with the expansion rules ...
USD_API bool IsPathIncluded(const SdfPath &path, TfToken *expansionRule=nullptr) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
UsdCollectionMembershipQuery(const PathExpansionRuleMap &pathExpansionRuleMap, const SdfPathSet &includedCollections, const ExpressionEvaluator &exprEval, const TfToken &topExpansionRule)
Constructor that additionally takes an additional expression evaluator and a top-level expansion rule...
bool operator==(UsdCollectionMembershipQuery const &rhs) const
Equality operator.
bool operator!=(UsdCollectionMembershipQuery const &rhs) const
Inequality operator.
size_t GetHash() const
Hash function.
UsdCollectionMembershipQuery(const PathExpansionRuleMap &pathExpansionRuleMap, const SdfPathSet &includedCollections)
Constructor that takes a path expansion rule map.
USD_API bool IsPathIncluded(const SdfPath &path, const TfToken &parentExpansionRule, TfToken *expansionRule=nullptr) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ExpressionEvaluator const & GetExpressionEvaluator() const
Return the expression evaluator associated with this query object.
TfToken GetTopExpansionRule() const
Return the top expansion rule for this query object.
bool HasExcludes() const
Returns true if the collection excludes one or more paths below an included path via the excludes rel...
UsdCollectionMembershipQuery(PathExpansionRuleMap &&pathExpansionRuleMap, SdfPathSet &&includedCollections, ExpressionEvaluator &&exprEval, TfToken const &topExpansionRule)
Constructor that additionally takes an additional expression evaluator as an rvalue reference and a t...
bool HasExpression() const
Return true if the expression evaluator associated with this query object is not empty.
Base class for Usd scenegraph objects, providing common API.
Definition: object.h:132
SdfPath GetPath() const
Return the complete scene path to this object on its UsdStage, which may (UsdPrim) or may not (all ot...
Definition: object.h:203
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,...