HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
keyFrameMap.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 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 
25 #ifndef PXR_BASE_TS_KEY_FRAME_MAP_H
26 #define PXR_BASE_TS_KEY_FRAME_MAP_H
27 
28 #include "pxr/pxr.h"
29 #include "pxr/base/ts/api.h"
30 #include "pxr/base/ts/keyFrame.h"
31 #include "pxr/base/tf/stl.h"
32 
34 
35 /// \class TsKeyFrameMap
36 ///
37 /// \brief An ordered sequence of keyframes with STL-compliant API
38 /// for finding, inserting, and erasing keyframes while maintaining order.
39 ///
40 /// We use this instead of a map or set of keyframes because it allows
41 /// the keyframes to be stored with fewer heap allocation and better
42 /// data locality.
43 ///
44 /// For the sake of efficiency, this class makes two assumptions:
45 /// The keyframes are always ordered
46 /// There is never more than one key frame at a given time.
47 ///
48 /// The client (TsSpline) is responsible for maintaining these
49 /// preconditions.
51 
52 public:
53  typedef std::vector<TsKeyFrame>::iterator iterator;
54  typedef std::vector<TsKeyFrame>::const_iterator const_iterator;
55  typedef std::vector<TsKeyFrame>::reverse_iterator reverse_iterator;
56  typedef std::vector<TsKeyFrame>::const_reverse_iterator const_reverse_iterator;
57 
58  TS_API
60  }
61 
62  TS_API
63  TsKeyFrameMap(TsKeyFrameMap const& other) :
64  _data(other._data) {
65  }
66 
67  TS_API
69  _data = other._data;
70  return *this;
71  }
72 
73  TS_API
75  return _data.begin();
76  }
77 
78  TS_API
80  return _data.begin();
81  }
82 
83  TS_API
85  return _data.end();
86  }
87 
88  TS_API
89  const_iterator end() const {
90  return _data.end();
91  }
92 
93  TS_API
95  return _data.rbegin();
96  }
97 
98  TS_API
100  return _data.rbegin();
101  }
102 
103  TS_API
105  return _data.rend();
106  }
107 
108  TS_API
110  return _data.rend();
111  }
112 
113  TS_API
114  size_t size() const {
115  return _data.size();
116  }
117 
118  TS_API
119  size_t max_size() const {
120  return _data.max_size();
121  }
122 
123  TS_API
124  bool empty() const {
125  return _data.empty();
126  }
127 
128  TS_API
129  void reserve(size_t size) {
130  _data.reserve(size);
131  }
132 
133  TS_API
134  void clear() {
135  _data.clear();
136  }
137 
138  TS_API
139  void swap(TsKeyFrameMap& other) {
140  other._data.swap(_data);
141  }
142 
143  TS_API
144  void swap(std::vector<TsKeyFrame>& other) {
145  other.swap(_data);
146  }
147 
148  TS_API
150  _data.erase(first,last);
151  }
152 
153  TS_API
154  void erase(iterator i) {
155  _data.erase(i);
156  }
157 
158  TS_API
159  bool operator==(const TsKeyFrameMap& other) const {
160  return (_data == other._data);
161  }
162 
163  TS_API
164  bool operator!=(const TsKeyFrameMap& other) const {
165  return (_data != other._data);
166  }
167 
168  TS_API
170 
171  TS_API
173 
174  TS_API
176 
177  TS_API
179 
180  TS_API
181  iterator find(const TsTime &t) {
182  iterator i = lower_bound(t);
183  if (i != _data.end() && i->GetTime() == t) {
184  return i;
185  }
186  return _data.end();
187  }
188 
189  TS_API
190  const_iterator find(const TsTime &t) const {
192  if (i != _data.end() && i->GetTime() == t) {
193  return i;
194  }
195  return _data.end();
196  }
197 
198  TS_API
200  // If the inserted value comes at the end, then avoid doing the
201  // lower_bound and just insert there.
202  iterator i = end();
203  if (!empty() && value.GetTime() <= _data.back().GetTime()) {
204  i = lower_bound(value.GetTime());
205  }
206 
207  return _data.insert(i,value);
208  }
209 
210  template <class Iter>
211  void insert(Iter const& first, Iter const& last) {
212  for(Iter i = first; i != last; i++) {
213  insert(*i);
214  }
215  }
216 
217  TS_API
218  void erase(TsTime const& t) {
219  iterator i = find(t);
220  if (i != _data.end()) {
221  erase(i);
222  }
223  }
224 
225  TS_API
227  iterator i = lower_bound(t);
228  if (i != _data.end() && i->GetTime() == t) {
229  return *i;
230  }
231  TsKeyFrame &k = *(_data.insert(i,TsKeyFrame()));
232  k.SetTime(t);
233  return k;
234  }
235 
236 private:
237  std::vector<TsKeyFrame> _data;
238 };
239 
241 
242 #endif
GLint first
Definition: glcorearb.h:405
TS_API size_t size() const
Definition: keyFrameMap.h:114
TS_API void swap(TsKeyFrameMap &other)
Definition: keyFrameMap.h:139
TS_API bool empty() const
Definition: keyFrameMap.h:124
TS_API void erase(iterator first, iterator last)
Definition: keyFrameMap.h:149
std::vector< TsKeyFrame >::const_iterator const_iterator
Definition: keyFrameMap.h:54
TS_API TsKeyFrame & operator[](const TsTime &t)
Definition: keyFrameMap.h:226
TS_API iterator find(const TsTime &t)
Definition: keyFrameMap.h:181
TS_API TsKeyFrameMap(TsKeyFrameMap const &other)
Definition: keyFrameMap.h:63
TS_API const_reverse_iterator rend() const
Definition: keyFrameMap.h:109
TS_API iterator begin()
Definition: keyFrameMap.h:74
TS_API const_iterator find(const TsTime &t) const
Definition: keyFrameMap.h:190
TS_API void reserve(size_t size)
Definition: keyFrameMap.h:129
TS_API iterator lower_bound(TsTime t)
TS_API bool operator==(const TsKeyFrameMap &other) const
Definition: keyFrameMap.h:159
An ordered sequence of keyframes with STL-compliant API for finding, inserting, and erasing keyframes...
Definition: keyFrameMap.h:50
TS_API iterator end()
Definition: keyFrameMap.h:84
TS_API reverse_iterator rbegin()
Definition: keyFrameMap.h:94
std::vector< TsKeyFrame >::iterator iterator
Definition: keyFrameMap.h:53
TS_API void SetTime(const TsTime &newTime)
Sets the time of this keyframe.
Definition: keyFrame.h:162
TS_API void erase(TsTime const &t)
Definition: keyFrameMap.h:218
std::vector< TsKeyFrame >::const_reverse_iterator const_reverse_iterator
Definition: keyFrameMap.h:56
TS_API TsKeyFrameMap & operator=(TsKeyFrameMap const &other)
Definition: keyFrameMap.h:68
TS_API TsTime GetTime() const
Gets the time of this keyframe.
Definition: keyFrame.h:156
TS_API const_reverse_iterator rbegin() const
Definition: keyFrameMap.h:99
TS_API const_iterator begin() const
Definition: keyFrameMap.h:79
TS_API void erase(iterator i)
Definition: keyFrameMap.h:154
Specifies the value of an TsSpline object at a particular point in time.
Definition: keyFrame.h:66
GLdouble t
Definition: glad.h:2397
PXR_NAMESPACE_OPEN_SCOPE typedef double TsTime
The time type used by Ts.
Definition: types.h:57
#define TS_API
Definition: api.h:41
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
TS_API void clear()
Definition: keyFrameMap.h:134
TS_API bool operator!=(const TsKeyFrameMap &other) const
Definition: keyFrameMap.h:164
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
TS_API iterator insert(TsKeyFrame const &value)
Definition: keyFrameMap.h:199
TS_API reverse_iterator rend()
Definition: keyFrameMap.h:104
TS_API TsKeyFrameMap()
Definition: keyFrameMap.h:59
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TS_API const_iterator end() const
Definition: keyFrameMap.h:89
Definition: core.h:1131
void insert(Iter const &first, Iter const &last)
Definition: keyFrameMap.h:211
TS_API void swap(std::vector< TsKeyFrame > &other)
Definition: keyFrameMap.h:144
TS_API size_t max_size() const
Definition: keyFrameMap.h:119
TS_API iterator upper_bound(TsTime t)
std::vector< TsKeyFrame >::reverse_iterator reverse_iterator
Definition: keyFrameMap.h:55