HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
labelsQuery.h
Go to the documentation of this file.
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USDSEMANTICS_LABELSQUERY_H
8 #define PXR_USDSEMANTICS_LABELSQUERY_H
9 
10 #include "pxr/pxr.h"
11 
13 #include "pxr/usd/usd/timeCode.h"
14 #include "pxr/base/gf/interval.h"
15 #include "pxr/base/tf/token.h"
16 
17 #include <shared_mutex>
18 #include <set>
19 #include <unordered_map>
20 #include <variant>
21 
22 
24 
25 /// The `UsdSemanticsLabelsQuery` can be used to query a prim's
26 /// labels for a specified taxonomy and time from the `UsdSemanticsLabelsAPI`.
27 /// Time may be an individual time code or an interval.
28 ///
29 /// This utility requires that all prims are on the same stage.
30 ///
31 /// The query caches certain reads and computations and should be discarded
32 /// when the state of the stage changes. Queries are thread safe.
34 {
35 public:
38  const UsdSemanticsLabelsQuery&) = delete;
39 
40  /// Constructs a query for a `taxonomy` at a single `timeCode`.
41  ///
42  /// Requires that the `taxonomy` must not be empty.
44  UsdTimeCode timeCode);
45 
46  /// Construct a query for a `taxonomy` over an `interval`.
47  ///
48  /// Requires that neither the `interval` nor `taxonomy` to be empty.
49  ///
50  /// \warning Finite minimum values of the interval will return the same
51  /// result regardless of closed or open state due to held interpolation
52  /// semantics and Zeno's paradox.
54  const GfInterval& interval);
55 
56  /// Computes the values for `semantics:labels:<taxonomy>` directly applied
57  /// to this prim. If this query's specified time is a time code, returns the
58  /// values at that time, otherwise, computes the union of values
59  /// for the interval.
60  ///
61  /// The results are sorted as if by std::sort
62  ///
63  /// If no time samples are authored, the default and fallback values will
64  /// be queried.
65  ///
66  /// \sa UsdSemanticsLabelsQuery::ComputeUniqueInheritedLabels
68  VtTokenArray ComputeUniqueDirectLabels(const UsdPrim& prim) const;
69 
70  /// Computes the values for `semantics:labels:<taxonomy>` including any
71  /// labels inherited from ancestors. If this query's specified time is a
72  /// time code, returns the values at that time, otherwise, computes the
73  /// union of values for the interval.
74  ///
75  /// The results are sorted as if by std::sort
76  ///
77  /// If no time samples are authored, the default and fallback values of
78  /// the prim and every ancestor will be queried.
79  ///
80  /// \sa UsdSemanticsLabelsQuery::ComputeUniqueDirectLabels
82  VtTokenArray ComputeUniqueInheritedLabels(const UsdPrim& prim) const;
83 
84  /// Return true if a label has been specified directly on this prim for
85  /// this query's taxonomy and time
86  ///
87  /// \sa UsdSemanticsLabelsQuery::HasInheritedLabel
89  bool HasDirectLabel(const UsdPrim& prim, const TfToken& label) const;
90 
91  /// Return true if a label has been specified for a prim or its
92  /// ancestors for this query's taxonomy and time
93  ///
94  /// \sa UsdSemanticsLabelsQuery::HasDirectLabel
96  bool HasInheritedLabel(const UsdPrim& prim, const TfToken& label) const;
97 
98  /// Returns the time used by this query when computing a prim's labels.
99  const std::variant<GfInterval, UsdTimeCode>& GetTime() const & {
100  return _time; }
101 
102  /// Returns the time used by this query when computing a prim's labels.
103  std::variant<GfInterval, UsdTimeCode> GetTime() && {
104  return std::move(_time); }
105 
106  /// Returns the taxonomy used by this query when computing a prim's labels.
107  const TfToken& GetTaxonomy() const & { return _taxonomy; }
108 
109  /// Returns the taxonomy used by this query when computing a prim's labels.
110  TfToken GetTaxonomy() && { return std::move(_taxonomy); }
111 
112 private:
113  // Return true if the prim has an entry in the cache
114  bool _PopulateLabels(const UsdPrim& prim) const;
115 
116  // Return true if any of the prim's ancestors have an entry in the cache
117  bool _PopulateInheritedLabels(const UsdPrim& prim) const;
118 
119  TfToken _taxonomy;
120  std::variant<GfInterval, UsdTimeCode> _time;
121 
122  mutable std::shared_mutex _cachedLabelsMutex;
123  using _UnorderedTokenSet = std::unordered_set<TfToken, TfHash>;
124  mutable std::unordered_map<SdfPath, _UnorderedTokenSet, TfHash> _cachedLabels;
125 };
126 
128 
129 #endif
UsdSemanticsLabelsQuery(const UsdSemanticsLabelsQuery &)=delete
const std::variant< GfInterval, UsdTimeCode > & GetTime() const &
Returns the time used by this query when computing a prim's labels.
Definition: labelsQuery.h:99
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
USDSEMANTICS_API VtTokenArray ComputeUniqueDirectLabels(const UsdPrim &prim) const
USDSEMANTICS_API VtTokenArray ComputeUniqueInheritedLabels(const UsdPrim &prim) const
UsdSemanticsLabelsQuery & operator=(const UsdSemanticsLabelsQuery &)=delete
#define USDSEMANTICS_API
Definition: api.h:23
Definition: token.h:70
std::variant< GfInterval, UsdTimeCode > GetTime()&&
Returns the time used by this query when computing a prim's labels.
Definition: labelsQuery.h:103
Definition: prim.h:116
const TfToken & GetTaxonomy() const &
Returns the taxonomy used by this query when computing a prim's labels.
Definition: labelsQuery.h:107
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDSEMANTICS_API bool HasDirectLabel(const UsdPrim &prim, const TfToken &label) const
USDSEMANTICS_API bool HasInheritedLabel(const UsdPrim &prim, const TfToken &label) const
TfToken GetTaxonomy()&&
Returns the taxonomy used by this query when computing a prim's labels.
Definition: labelsQuery.h:110