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
66  UsdCollectionMembershipQuery() = default;
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  const SdfPathSet& includedCollections);
74 
75  /// Constructor that takes a path expansion rule map as an rvalue reference
77  PathExpansionRuleMap&& pathExpansionRuleMap,
78  SdfPathSet&& includedCollections);
79 
80  /// \overload
81  /// Returns whether the given path is included in the collection from
82  /// which this UsdCollectionMembershipQuery object was computed. This is the
83  /// API that clients should use for determining if a given object is a
84  /// member of the collection. To enumerate all the members of a collection,
85  /// use \ref UsdComputeIncludedObjectsFromCollection or
86  /// \ref UsdComputeIncludedPathsFromCollection.
87  ///
88  /// If \p expansionRule is not nullptr, it is set to the expansion-
89  /// rule value that caused the path to be included in or excluded from
90  /// the collection. If \p path is not included in the collection,
91  /// \p expansionRule is set to UsdTokens->exclude.
92  ///
93  /// It is useful to specify this parameter and use this overload of
94  /// IsPathIncluded(), when you're interested in traversing a subtree
95  /// and want to know whether the root of the subtree is included in a
96  /// collection. For evaluating membership of descendants of the root,
97  /// please use the other overload of IsPathIncluded(), that takes both
98  /// a path and the parent expansionRule.
99  ///
100  /// The python version of this method only returns the boolean result.
101  /// It does not return \p expansionRule.
102  USD_API
103  bool IsPathIncluded(const SdfPath &path,
104  TfToken *expansionRule=nullptr) const;
105 
106  /// \overload
107  /// Returns whether the given path, \p path is included in the
108  /// collection from which this UsdCollectionMembershipQuery object was
109  /// computed, given the parent-path's inherited expansion rule,
110  /// \p parentExpansionRule.
111  ///
112  /// If \p expansionRule is not nullptr, it is set to the expansion-
113  /// rule value that caused the path to be included in or excluded from
114  /// the collection. If \p path is not included in the collection,
115  /// \p expansionRule is set to UsdTokens->exclude.
116  ///
117  /// The python version of this method only returns the boolean result.
118  /// It does not return \p expansionRule.
119  USD_API
120  bool IsPathIncluded(const SdfPath &path,
121  const TfToken &parentExpansionRule,
122  TfToken *expansionRule=nullptr) const;
123 
124  /// Returns true if the collection excludes one or more paths below an
125  /// included path.
126  bool HasExcludes() const {
127  return _hasExcludes;
128  }
129 
130  /// Equality operator
131  bool operator==(UsdCollectionMembershipQuery const& rhs) const {
132  return _hasExcludes == rhs._hasExcludes &&
133  _pathExpansionRuleMap == rhs._pathExpansionRuleMap &&
134  _includedCollections == rhs._includedCollections;
135  }
136 
137  /// Inequality operator
138  bool operator!=(UsdCollectionMembershipQuery const& rhs) const {
139  return !(*this == rhs);
140  }
141 
142  /// Hash functor
143  struct Hash {
144  USD_API
145  size_t operator()(UsdCollectionMembershipQuery const& query) const;
146  };
147 
148  /// Hash function
149  inline size_t GetHash() const {
150  return Hash()(*this);
151  }
152 
153  /// Returns a raw map of the paths included or excluded in the
154  /// collection along with the expansion rules for the included
155  /// paths.
157  return _pathExpansionRuleMap;
158  }
159 
160  /// Returns a set of paths for all collections that were included in the
161  /// collection from which this UsdCollectionMembershipQuery object was
162  /// computed. This set is recursive, so collections that were included
163  /// by other collections will be part of this set. The collection from
164  /// which this UsdCollectionMembershipQuery object was computed is *not*
165  /// part of this set.
167  return _includedCollections;
168  }
169 
170 private:
171  PathExpansionRuleMap _pathExpansionRuleMap;
172 
173  SdfPathSet _includedCollections;
174 
175  // A cached flag indicating whether _pathExpansionRuleMap contains
176  // any exclude rules.
177  bool _hasExcludes=false;
178 };
179 
180 /// Returns all the usd objects that satisfy the predicate, \p pred in the
181 /// collection represented by the UsdCollectionMembershipQuery object, \p
182 /// query.
183 ///
184 /// The results depends on the load state of the UsdStage, \p stage.
185 USD_API
186 std::set<UsdObject> UsdComputeIncludedObjectsFromCollection(
188  const UsdStageWeakPtr &stage,
190 
191 /// Returns all the paths that satisfy the predicate, \p pred in the
192 /// collection represented by the UsdCollectionMembershipQuery object, \p
193 /// query.
194 ///
195 /// The result depends on the load state of the UsdStage, \p stage.
196 USD_API
199  const UsdStageWeakPtr &stage,
201 
203 
204 #endif
GLenum query
Definition: glad.h:2772
const SdfPathSet & GetIncludedCollections() const
#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
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2038
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
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
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:291
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:210
USD_API const Usd_PrimFlagsConjunction UsdPrimDefaultPredicate
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#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.