vsg: include/vsg/animation/time_value.h Source File

vsg 1.1.10 VulkanSceneGraph library |

Loading...
Searching...
No Matches
time_value.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2024 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/animation/Animation.h>
16#include <vsg/app/ViewMatrix.h>
17#include <vsg/maths/transform.h>
18
19namespace vsg
20{
21
22 template<typename T>
23 struct time_value
24 {
25 using value_type = T;
26 double time;
27 value_type value;
28
29 bool operator<(const time_value& rhs) const { return time < rhs.time; }
30 };
31
32 using time_double = time_value<double>;
33 using time_dvec2 = time_value<dvec2>;
34 using time_dvec3 = time_value<dvec3>;
35 using time_dvec4 = time_value<dvec4>;
36 using time_dquat = time_value<dquat>;
37
38 template<typename T, typename V>
39 bool sample(double time, const T& values, V& value)
40 {
41 if (values.size() == 0) return false;
42
43 if (values.size() == 1)
44 {
45 value = values.front().value;
46 return true;
47 }
48
49 auto pos_itr = values.begin();
50 if (time <= pos_itr->time)
51 {
52 value = pos_itr->value;
53 return true;
54 }
55 else
56 {
57 using value_type = typename T::value_type;
58 pos_itr = std::lower_bound(values.begin(), values.end(), time, [](const value_type& elem, double t) -> bool { return elem.time < t; });
59
60 if (pos_itr == values.begin())
61 {
62 value = values.front().value;
63 return true;
64 }
65
66 if (pos_itr == values.end())
67 {
68 value = values.back().value;
69 return true;
70 }
71
72 auto before_pos_itr = pos_itr - 1;
73 double delta_time = (pos_itr->time - before_pos_itr->time);
74 double r = delta_time != 0.0 ? (time - before_pos_itr->time) / delta_time : 0.5;
75
76 value = mix(before_pos_itr->value, pos_itr->value, r);
77
78 return true;
79 }
80 }
81
82} // namespace vsg
vsg::time_value
Definition time_value.h:24