model_data_animation_timeline.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 #pragma once
30 
31 #include "../../Core/Math/cl_math.h"
32 
33 namespace clan
34 {
35 
37 template<typename Type>
39 {
40 public:
41  std::vector<float> timestamps;
42  std::vector<Type> values;
43 
44  Type get_value(float timestamp) const
45  {
46  size_t index, index2;
47  float t = find_animation_indices(timestamp, index, index2);
48  return mix(values[index], values[index2], t);
49  }
50 
51  float find_animation_indices(float timestamp, size_t &index, size_t &index2) const
52  {
53  if (timestamps.empty())
54  {
55  index = 0;
56  index2 = 0;
57  return 0.0f;
58  }
59 
60  index = binary_search(timestamp);
61  index2 = min(index + 1, timestamps.size() - 1);
62 
63  float start = timestamps[index];
64  float end = timestamps[index2];
65  if (start != end)
66  return clamp((timestamp - start) / (end - start), 0.0f, 1.0f);
67  else
68  return 0.0f;
69  }
70 
71 private:
72  size_t binary_search(float timestamp) const
73  {
74  size_t imin = 0;
75  size_t imax = timestamps.size() - 1;
76  while (imin < imax)
77  {
78  size_t imid = imin + (imax - imin) / 2;
79  if (timestamps[imid] > timestamp)
80  imax = imid;
81  else
82  imin = imid + 1;
83  }
84  if (imin < timestamps.size() - 1)
85  return imin - 1;
86  else
87  return imin;
88  }
89 };
90 
91 template<>
93 {
94  size_t index, index2;
95  float t = find_animation_indices(timestamp, index, index2);
96  return Quaternionf::lerp(values[index], values[index2], t);
97 }
98 
99 template<>
101 {
102  size_t index, index2;
103  float t = find_animation_indices(timestamp, index, index2);
104  return values[index];
105 }
106 
107 }
108 
float find_animation_indices(float timestamp, size_t &index, size_t &index2) const
Definition: model_data_animation_timeline.h:51
A min(A a, B b)
Definition: cl_math.h:51
Type get_value(float timestamp) const
Definition: model_data_animation_timeline.h:44
std::vector< float > timestamps
Definition: model_data_animation_timeline.h:41
static Quaternionx< float > lerp(const Quaternionx< float > &quaternion_initial, const Quaternionx< float > &quaternion_final, floatlerp_time)
Linear Quaternion Interpolation.
Quaternion - Float.
Definition: quaternion.h:188
A list of time stamps and the values to be used for each.
Definition: model_data_animation_timeline.h:38
4D matrix
Definition: mat2.h:52
A mix(A a, B b, C mix)
Definition: cl_math.h:97
std::vector< Type > values
Definition: model_data_animation_timeline.h:42
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:91