Rumba C++ SDK
AnimCurve.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2018
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include <gsl/span>
18 #include <optional>
19 
20 #include "Value.h"
21 #include "Impl.h"
22 
23 MAQUINA_IGNORE_WARNINGS_BEGIN
24 #include <gsl/gsl>
25 MAQUINA_IGNORE_WARNINGS_END
26 
27 namespace maquina
28 {
29  enum class CycleMode
30  {
35  };
36 
38  struct WrapSlice
39  {
40  using WrapFunc = std::function<std::pair<float,float>(float)>;
42 
44  float start;
45 
47  float end;
48 
51  };
52 
54 
56  {
57  public:
59 
60  AnimCurve(const Value &v);
61 
63 
64  AnimCurve(const char* key_type);
65 
67  AnimCurve create_empty() const;
68 
70  size_t size() const;
71 
73 
74  AnimCurve copy_keys(const gsl::span<const int>& keys) const;
75 
77  std::vector<int> selected_keys() const;
78 
80  int key_index(float t) const;
81 
83  int previous_key(float t) const;
84 
86  int next_key(float t) const;
87 
90  {
91  friend class AnimCurve;
92  public:
94  float time() const;
95 
97  void set_time(float t);
98 
100  float as_float() const;
101 
103  Value value() const;
104 
106  void set_value(float v);
107 
109  void set_value(const Value& v);
110 
112  bool selected() const;
113 
115  void set_selected(bool state);
116 
117  private:
118  Key(Impl* curve, size_t index) : _curve(curve), _index(index) {}
119  Impl* _curve;
120  size_t _index;
121  };
122 
124 
125  size_t set_value(float frame, const maquina::Value& value = Value::default_value);
126 
129  size_t insert_value(float frame, const maquina::Value& value = Value::default_value, bool before=true);
130 
132  void remove_key(size_t i);
133 
135  const Key operator[](size_t i) const { return {_impl.get(), i}; }
136 
138  Value interpolate(float t, CycleMode pre_cycle_mode, CycleMode post_cycle_mode) const;
139 
141  void normalize(bool repeat);
142 
143  class Iterator
144  {
145  int _index;
146  AnimCurve *_parent;
147  bool _only_selected;
148  void _skip_unselected()
149  {
150  if (_only_selected)
151  {
152  // Point on the first selected key
153  const int size = int(_parent->size());
154  while (_index < size)
155  {
156  if ((*_parent)[_index].selected())
157  break;
158  ++_index;
159  }
160  }
161  }
162  public:
163  using iterator_category = std::input_iterator_tag;
164  using value_type = Key;
165  using difference_type = std::ptrdiff_t;
166  using pointer = Key*;
167  using reference = Key&;
168 
169  Iterator(int index, AnimCurve *parent, bool only_selected) : _index(index), _parent(parent), _only_selected(only_selected)
170  {
171  _skip_unselected();
172  }
173  bool operator==(const Iterator& o) { return _index == o._index;}
174  bool operator!=(const Iterator& o) { return _index != o._index;}
176  {
177  ++_index;
178  _skip_unselected();
179  return *this;
180  }
181  Key operator*() {return (*_parent)[_index];}
182  };
183 
185  Iterator begin(bool only_selected = false) { return {0, this, only_selected}; }
186 
188  Iterator end() { return {int(size()), this, false}; }
189 
191  class Keys
192  {
193  AnimCurve *_parent;
194  bool _only_selected;
195  public:
196  Keys(AnimCurve *parent, bool only_selected) : _parent(parent), _only_selected(only_selected) {}
197  Iterator begin() { return _parent->begin(_only_selected); }
198  Iterator end() { return _parent->end(); }
199  private:
200  };
201 
204  bool only_selected = false
205  )
206  {
207  return {this, only_selected};
208  }
209 
211 
218  std::optional<Value> clip(float frame_in, float frame_out);
219 
221  Value time_wrap(const gsl::span<const WrapSlice>& wrap_slices, CycleMode pre_cycle_mode, CycleMode post_cycle_mode) const;
222 
224  AnimCurve(std::shared_ptr<Impl> impl) { _impl=impl; }
225  };
226 
228 
230  {
231  public:
232  enum class CycleMode
233  {
234  constant=0,
235  repeat,
236  repeat_continuous,
237  linear
238  };
239 
241  class Key
242  {
243  public:
244  class Tangent
245  {
246  public:
247  enum class Mode
248  {
249  linear = 0,
250  auto_,
251  custom,
252  spline,
253  flat,
254  step,
255  custom_simple,
256  };
257 
259  MAQUINA_EXPORT Tangent(Mode mode=Mode::auto_);
261  MAQUINA_EXPORT Tangent(float frame, float value, Mode mode=Mode::custom);
263  MAQUINA_EXPORT Tangent(const Imath::V2f& tangent, Mode mode=Mode::custom);
264 
268  };
269 
271  MAQUINA_EXPORT Key(float frame, float value);
272 
274  MAQUINA_EXPORT Key(const Imath::V2f& key);
275 
277 
278  MAQUINA_EXPORT Key(const Imath::V2f& key, const Tangent& right);
279 
281  MAQUINA_EXPORT Key(const Imath::V2f& key, const Tangent& left, const Tangent& right, bool broken_tangents=true, bool selected=false);
282 
285 
288 
291 
293  bool broken_tangents = false;
294 
296  bool selected = false;
297  };
298 
300  AnimCurveFloat();
301 
303 
304  AnimCurveFloat(const Value &v);
305 
307  AnimCurveFloat(const std::vector<Key> &keys, bool repeat);
308 
310  size_t size() const;
311 
313  const Key operator[](size_t n) const;
314 
315  class Iterator
316  {
317  int _index;
318  AnimCurveFloat *_parent;
319  bool _only_selected;
320  void _skip_unselected()
321  {
322  if (_only_selected)
323  {
324  // Point on the first selected key
325  const int size = int(_parent->size());
326  while (_index < size)
327  {
328  if ((*_parent)[_index].selected)
329  break;
330  ++_index;
331  }
332  }
333  }
334  public:
335  using iterator_category = std::input_iterator_tag;
336  using value_type = Key;
337  using difference_type = std::ptrdiff_t;
338  using pointer = Key*;
339  using reference = Key&;
340 
341  Iterator(int index, AnimCurveFloat *parent, bool only_selected) : _index(index), _parent(parent), _only_selected(only_selected)
342  {
343  _skip_unselected();
344  }
345  bool operator==(const Iterator& o) { return _index == o._index;}
346  bool operator!=(const Iterator& o) { return _index != o._index;}
348  {
349  ++_index;
350  _skip_unselected();
351  return *this;
352  }
353  Key operator*() {return (*_parent)[_index];}
354  };
355 
357  Iterator begin(bool only_selected = false) { return {0, this, only_selected}; }
358 
360  Iterator end() { return {int(size()), this, false}; }
361 
363  class Keys
364  {
365  AnimCurveFloat *_parent;
366  bool _only_selected;
367  public:
368  Keys(AnimCurveFloat *parent, bool only_selected) : _parent(parent), _only_selected(only_selected) {}
369  Iterator begin() { return _parent->begin(_only_selected); }
370  Iterator end() { return _parent->end(); }
371  private:
372  };
373 
376  bool only_selected = false
377  )
378  {
379  return {this, only_selected};
380  }
381 
383  void set_key(size_t n, const Key& key);
384 
386 
387  size_t set_key(const Key& key);
388 
390 
392  size_t insert_key(const Key& key, bool before=true);
393 
395  void remove_key(size_t i);
396 
398  Value interpolate(float t, CycleMode left_mode=CycleMode::constant, CycleMode right_mode=CycleMode::constant) const;
399 
401 
402  void normalize(bool repeat);
403 };
404 }
An animation curve for floating point values. That type of curves contains tangents.
Definition: AnimCurve.h:229
size_t size() const
Returns the number of keys.
Keys(AnimCurveFloat *parent, bool only_selected)
Definition: AnimCurve.h:368
const Key operator[](size_t i) const
Get a key.
Definition: AnimCurve.h:135
bool operator==(const Iterator &o)
Definition: AnimCurve.h:173
Iterator(int index, AnimCurve *parent, bool only_selected)
Definition: AnimCurve.h:169
float start
The start time in the original animation.
Definition: AnimCurve.h:44
WrapSlice(float start, float end, const WrapFunc &time_wrap_func)
Definition: AnimCurve.h:41
CycleMode
Definition: AnimCurve.h:29
WrapFunc time_wrap_func
The time wrap functor, for an original animation time, return the new animation time.
Definition: AnimCurve.h:50
A key iteration.
Definition: AnimCurve.h:363
Keys(AnimCurve *parent, bool only_selected)
Definition: AnimCurve.h:196
Iterator & operator++()
Definition: AnimCurve.h:347
std::input_iterator_tag iterator_category
Definition: AnimCurve.h:163
CycleMode
Definition: AnimCurve.h:232
Quat< T > spline(const Quat< T > &q0, const Quat< T > &q1, const Quat< T > &q2, const Quat< T > &q3, T t)
Definition: ImathQuat.h:544
Iterator begin()
Definition: AnimCurve.h:369
An anim curve key.
Definition: AnimCurve.h:241
Iterator(int index, AnimCurveFloat *parent, bool only_selected)
Definition: AnimCurve.h:341
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
An animation curve abstraction, can animate any type of value, bool, int, float or other values...
Definition: AnimCurve.h:55
Definition: AnimCurve.h:315
float end
The end time in the original animation.
Definition: AnimCurve.h:47
static const Value default_value
The default value.
Definition: Value.h:245
Mode mode
Definition: AnimCurve.h:267
A key iteration.
Definition: AnimCurve.h:191
Iterator begin(bool only_selected=false)
Begin iterator on keyframes.
Definition: AnimCurve.h:357
Iterator end()
End iterator on keyframes.
Definition: AnimCurve.h:188
A curve key.
Definition: AnimCurve.h:89
Iterator begin()
Definition: AnimCurve.h:197
Iterator end()
End iterator on keyframes.
Definition: AnimCurve.h:360
The channel is not animable.
#define MAQUINA_EXPORT
Definition: Export.h:31
Imath::V2f tangent
The key.
Definition: AnimCurve.h:266
Definition: AnimCurve.h:143
Tangent right
The right tangent.
Definition: AnimCurve.h:290
Definition: ImathVec.h:61
MAQUINA_EXPORT Value copy_keys(Node &anim_layer)
Return a copy of the active key selection in a layer.
A monotone slice of time wrapping.
Definition: AnimCurve.h:38
Imath::V2f key
The key, x is the frame, y the value.
Definition: AnimCurve.h:284
Keys keys(bool only_selected=false)
Return an iteration on the keys.
Definition: AnimCurve.h:203
Key operator*()
Definition: AnimCurve.h:181
Tangent left
The left tangent.
Definition: AnimCurve.h:287
size_t size() const
Return the number of keys.
std::function< std::pair< float, float >(float)> WrapFunc
Definition: AnimCurve.h:40
Iterator & operator++()
Definition: AnimCurve.h:175
Iterator end()
Definition: AnimCurve.h:198
bool operator==(const Iterator &o)
Definition: AnimCurve.h:345
Iterator begin(bool only_selected=false)
Begin iterator on keyframes.
Definition: AnimCurve.h:185
Iterator end()
Definition: AnimCurve.h:370
bool operator!=(const Iterator &o)
Definition: AnimCurve.h:174
Mode
Definition: AnimCurve.h:247
T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:82
std::input_iterator_tag iterator_category
Definition: AnimCurve.h:335
std::ptrdiff_t difference_type
Definition: AnimCurve.h:337
Key operator*()
Definition: AnimCurve.h:353
Keys keys(bool only_selected=false)
Return an iteration on the keys.
Definition: AnimCurve.h:375
MAQUINA_EXPORT int insert_key(const Plug &plug, float t, float v)
std::ptrdiff_t difference_type
Definition: AnimCurve.h:165
bool operator!=(const Iterator &o)
Definition: AnimCurve.h:346
MAQUINA_EXPORT void remove_key(Plug &plug, const float frame)
Remove the key at frame in the active layer.
Definition: AnimCurve.h:244
Base class of all Rumba values.
Definition: Value.h:34