HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
knotMap.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 
8 #ifndef PXR_BASE_TS_KNOT_MAP_H
9 #define PXR_BASE_TS_KNOT_MAP_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/base/ts/api.h"
13 #include "pxr/base/ts/knot.h"
14 #include "pxr/base/ts/types.h"
15 #include "pxr/base/gf/interval.h"
16 
17 #include <vector>
18 #include <initializer_list>
19 #include <utility>
20 
22 
23 struct Ts_SplineData;
24 
25 
26 /// The knots in a spline. Stored as a vector, but unique and sorted like a
27 /// map. A knot's time is stored within the knot itself, but is also sometimes
28 /// used as a key. Some methods are vector-like, some are map-like, and some
29 /// are set-like.
30 ///
31 /// Separate from TsSpline in order to provide identical operations on different
32 /// collections of knots. The most straightforward collection is the authored
33 /// knots, provided by GetKnots, but collections included baked loop knots can
34 /// also be obtained.
35 ///
36 class TsKnotMap
37 {
38 public:
39  using KnotVec = std::vector<TsKnot>;
40 
41  using iterator = KnotVec::iterator;
42  using const_iterator = KnotVec::const_iterator;
43  using reverse_iterator = KnotVec::reverse_iterator;
44  using const_reverse_iterator = KnotVec::const_reverse_iterator;
45 
46 public:
47  /// \name Construction and value semantics
48  /// @{
49 
50  TS_API
51  TsKnotMap();
52 
53  TS_API
54  TsKnotMap(std::initializer_list<TsKnot> knots);
55 
56  TS_API
57  bool operator==(const TsKnotMap &other) const;
58 
59  TS_API
60  bool operator!=(const TsKnotMap &other) const;
61 
62  /// @}
63  /// \name Iteration
64  ///
65  /// These methods are <code>std::vector</code>-like.
66  ///
67  /// @{
68 
69  TS_API
70  iterator begin();
71 
72  TS_API
73  const_iterator begin() const;
74 
75  TS_API
76  const_iterator cbegin() const;
77 
78  TS_API
79  iterator end();
80 
81  TS_API
82  const_iterator end() const;
83 
84  TS_API
85  const_iterator cend() const;
86 
87  TS_API
89 
90  TS_API
92 
93  TS_API
95 
96  TS_API
98 
99  TS_API
101 
102  TS_API
104 
105  /// @}
106  /// \name Size
107  ///
108  /// These methods are <code>std::vector</code>-like.
109  ///
110  /// @{
111 
112  TS_API
113  size_t size() const;
114 
115  TS_API
116  bool empty() const;
117 
118  TS_API
119  void reserve(size_t size);
120 
121  /// @}
122  /// \name Modification
123  ///
124  /// These methods are <code>std::set</code>-like.
125  ///
126  /// @{
127 
128  TS_API
129  void clear();
130 
131  TS_API
132  void swap(TsKnotMap &other);
133 
134  /// Inserts a knot. If there is already a knot at the same time, nothing is
135  /// changed. Returns an iterator to the newly inserted knot, or the
136  /// existing one at the same time. The second member of the returned pair
137  /// indicates whether an insertion took place.
138  TS_API
139  std::pair<iterator, bool> insert(const TsKnot &knot);
140 
141  /// Removes the knot at the specified time, if it exists. Returns the
142  /// number of knots erased (0 or 1).
143  TS_API
144  size_t erase(TsTime time);
145 
146  /// Removes a knot. Returns the iterator after it.
147  TS_API
149 
150  /// Removes a range of knots. Returns the iterator after the last removed.
151  TS_API
153 
154  /// @}
155  /// \name Searching
156  ///
157  /// These methods are <code>std::map</code>-like.
158  ///
159  /// @{
160 
161  /// Exact matches only; returns end() if not found.
162  TS_API
163  iterator find(TsTime time);
164 
165  /// Const version of find().
166  TS_API
167  const_iterator find(TsTime time) const;
168 
169  /// If there is a knot at the specified time, returns that. Otherwise, if
170  /// there is a knot after the specified time, returns the first such knot.
171  /// Otherwise returns end().
172  TS_API
173  iterator lower_bound(TsTime time);
174 
175  /// Const version of lower_bound().
176  TS_API
177  const_iterator lower_bound(TsTime time) const;
178 
179  /// @}
180  /// \name Non-STL Methods
181  /// @{
182 
183  /// Returns the knot whose time most closely (or exactly) matches the
184  /// specified time. In case of ties, returns the later knot. If there are
185  /// no knots, returns end().
186  TS_API
187  iterator FindClosest(TsTime time);
188 
189  /// Const version of FindClosest().
190  TS_API
191  const_iterator FindClosest(TsTime time) const;
192 
193  /// Returns the value type of the knots, or Unknown if empty.
194  TS_API
195  TfType GetValueType() const;
196 
197  /// Returns the time interval containing the first and last knot. Returns
198  /// an empty interval if there are no knots.
199  TS_API
200  GfInterval GetTimeSpan() const;
201 
202  /// Returns whether there are any segments with curve interpolation.
203  TS_API
204  bool HasCurveSegments() const;
205 
206  /// @}
207 
208 private:
209  friend class TsSpline;
210 
211  // Constructor for copying knot data from SplineData into KnotMap.
212  TS_API
213  TsKnotMap(const Ts_SplineData *data);
214 
215 private:
216  // Knot objects.
217  KnotVec _knots;
218 };
219 
220 
222 
223 #endif
TS_API const_iterator cbegin() const
GLint first
Definition: glcorearb.h:405
std::vector< TsKnot > KnotVec
Definition: knotMap.h:39
TS_API void clear()
GT_API const UT_StringHolder time
TS_API iterator find(TsTime time)
Exact matches only; returns end() if not found.
TS_API bool operator!=(const TsKnotMap &other) const
TS_API TfType GetValueType() const
Returns the value type of the knots, or Unknown if empty.
TS_API void swap(TsKnotMap &other)
KnotVec::const_reverse_iterator const_reverse_iterator
Definition: knotMap.h:44
TS_API GfInterval GetTimeSpan() const
TS_API const_iterator cend() const
KnotVec::const_iterator const_iterator
Definition: knotMap.h:42
TS_API reverse_iterator rend()
TS_API bool HasCurveSegments() const
Returns whether there are any segments with curve interpolation.
TS_API iterator begin()
TS_API TsKnotMap()
TS_API size_t size() const
TS_API bool empty() const
TS_API iterator lower_bound(TsTime time)
TS_API const_reverse_iterator crend() const
TS_API iterator end()
TS_API void reserve(size_t size)
KnotVec::iterator iterator
Definition: knotMap.h:41
#define TS_API
Definition: api.h:24
Definition: knot.h:39
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
KnotVec::reverse_iterator reverse_iterator
Definition: knotMap.h:43
TS_API const_reverse_iterator crbegin() const
TS_API iterator FindClosest(TsTime time)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
TS_API size_t erase(TsTime time)
TS_API reverse_iterator rbegin()
TS_API std::pair< iterator, bool > insert(const TsKnot &knot)
TS_API bool operator==(const TsKnotMap &other) const
Definition: format.h:1821