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 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/sdf/path.h"
32 #include "pxr/usd/usd/common.h"
33 #include "pxr/usd/usd/primFlags.h"
34 
35 #include <unordered_map>
36 
38 
39 // -------------------------------------------------------------------------- //
40 // UsdCollectionMembershipQuery //
41 // -------------------------------------------------------------------------- //
42 /// \class UsdCollectionMembershipQuery
43 ///
44 /// \brief Represents a flattened view of a collection. For more information
45 /// about collections, please see UsdCollectionAPI as a way to encode and
46 /// retrieve a collection from scene description. A
47 /// UsdCollectionMembershipQuery object can be used to answer queries about
48 /// membership of paths in the collection efficiently.
50 {
51 public:
52  /// Holds an unordered map describing membership of paths in this collection
53  /// and the associated expansionRule for how the paths are to be expanded.
54  /// Valid expansionRules are UsdTokens->explicitOnly,
55  /// UsdTokens->expandPrims, and UsdTokens->expandPrimsAndProperties. For
56  /// more information on the expansion rules, please see the expansionRule
57  /// attribute on UsdCollectionAPI.
58  /// If a collection includes another collection, the included collection's
59  /// PathExpansionRuleMap is merged into this one. If a path is excluded,
60  /// its expansion rule is set to UsdTokens->exclude.
61  using PathExpansionRuleMap = std::unordered_map<SdfPath,
63 
64  /// Default Constructor, creates an empty UsdCollectionMembershipQuery
65  /// object
67 
68  /// Constructor that takes a path expansion rule map. The map is scanned
69  /// for 'excludes' when the UsdCollectionMembershipQuery object is
70  /// constructed.
72  const PathExpansionRuleMap& pathExpansionRuleMap);
73 
74  /// Constructor that takes a path expansion rule map as an rvalue reference
76  PathExpansionRuleMap&& pathExpansionRuleMap);
77 
78  /// \overload
79  /// Returns whether the given path is included in the collection from
80  /// which this UsdCollectionMembershipQuery object was computed. This is the
81  /// API that clients should use for determining if a given object is a
82  /// member of the collection. To enumerate all the members of a collection,
83  /// use \ref UsdComputeIncludedObjectsFromCollection or
84  /// \ref UsdComputeIncludedPathsFromCollection.
85  ///
86  /// If \p expansionRule is not nullptr, it is set to the expansion-
87  /// rule value that caused the path to be included in or excluded from
88  /// the collection. If \p path is not included in the collection,
89  /// \p expansionRule is set to UsdTokens->exclude.
90  ///
91  /// It is useful to specify this parameter and use this overload of
92  /// IsPathIncluded(), when you're interested in traversing a subtree
93  /// and want to know whether the root of the subtree is included in a
94  /// collection. For evaluating membership of descendants of the root,
95  /// please use the other overload of IsPathIncluded(), that takes both
96  /// a path and the parent expansionRule.
97  ///
98  /// The python version of this method only returns the boolean result.
99  /// It does not return \p expansionRule.
100  USD_API
101  bool IsPathIncluded(const SdfPath &path,
102  TfToken *expansionRule=nullptr) const;
103 
104  /// \overload
105  /// Returns whether the given path, \p path is included in the
106  /// collection from which this UsdCollectionMembershipQuery object was
107  /// computed, given the parent-path's inherited expansion rule,
108  /// \p parentExpansionRule.
109  ///
110  /// If \p expansionRule is not nullptr, it is set to the expansion-
111  /// rule value that caused the path to be included in or excluded from
112  /// the collection. If \p path is not included in the collection,
113  /// \p expansionRule is set to UsdTokens->exclude.
114  ///
115  /// The python version of this method only returns the boolean result.
116  /// It does not return \p expansionRule.
117  USD_API
118  bool IsPathIncluded(const SdfPath &path,
119  const TfToken &parentExpansionRule,
120  TfToken *expansionRule=nullptr) const;
121 
122  /// Returns true if the collection excludes one or more paths below an
123  /// included path.
124  bool HasExcludes() const {
125  return _hasExcludes;
126  }
127 
128  /// Equality operator
129  bool operator==(UsdCollectionMembershipQuery const& rhs) const {
130  return _hasExcludes == rhs._hasExcludes &&
131  _pathExpansionRuleMap == rhs._pathExpansionRuleMap;
132  }
133 
134  /// Inequality operator
135  bool operator!=(UsdCollectionMembershipQuery const& rhs) const {
136  return !(*this == rhs);
137  }
138 
139  /// Hash functor
140  struct Hash {
141  USD_API
142  size_t operator()(UsdCollectionMembershipQuery const& query) const;
143  };
144 
145  /// Hash function
146  inline size_t GetHash() const {
147  return Hash()(*this);
148  }
149 
150  /// Returns a raw map of the paths included or excluded in the
151  /// collection along with the expansion rules for the included
152  /// paths.
154  return _pathExpansionRuleMap;
155  }
156 
157 private:
158  PathExpansionRuleMap _pathExpansionRuleMap;
159 
160  // A cached flag indicating whether _pathExpansionRuleMap contains
161  // any exclude rules.
162  bool _hasExcludes=false;
163 };
164 
165 /// Returns all the usd objects that satisfy the predicate, \p pred in the
166 /// collection represented by the UsdCollectionMembershipQuery object, \p
167 /// query.
168 ///
169 /// The results depends on the load state of the UsdStage, \p stage.
170 USD_API
171 std::set<UsdObject> UsdComputeIncludedObjectsFromCollection(
173  const UsdStageWeakPtr &stage,
175 
176 /// Returns all the paths that satisfy the predicate, \p pred in the
177 /// collection represented by the UsdCollectionMembershipQuery object, \p
178 /// query.
179 ///
180 /// The result depends on the load state of the UsdStage, \p stage.
181 USD_API
184  const UsdStageWeakPtr &stage,
186 
188 
189 #endif
#define USD_API
Definition: api.h:40
std::unordered_map< SdfPath, TfToken, SdfPath::Hash > PathExpansionRuleMap
USD_API std::set< UsdObject > UsdComputeIncludedObjectsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
const PathExpansionRuleMap & GetAsPathExpansionRuleMap() const
GLenum query
Definition: glew.h:5704
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.
size_t OIIO_API Hash(const char *s, size_t len)
bool operator!=(UsdCollectionMembershipQuery const &rhs) const
Inequality operator.
Definition: token.h:87
USD_API bool IsPathIncluded(const SdfPath &path, TfToken *expansionRule=nullptr) const
USD_API size_t operator()(UsdCollectionMembershipQuery const &query) const
size_t GetHash() const
Hash function.
Definition: path.h:288
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:207
GLsizei const GLchar *const * path
Definition: glew.h:6461
UsdStagePtr UsdStageWeakPtr
Definition: common.h:55
USD_API const Usd_PrimFlagsConjunction UsdPrimDefaultPredicate
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
USD_API SdfPathSet UsdComputeIncludedPathsFromCollection(const UsdCollectionMembershipQuery &query, const UsdStageWeakPtr &stage, const Usd_PrimFlagsPredicate &pred=UsdPrimDefaultPredicate)
bool operator==(UsdCollectionMembershipQuery const &rhs) const
Equality operator.