le2d v0.4.3
2D game engine framework
 
Loading...
Searching...
No Matches
sampler.hpp
Go to the documentation of this file.
1#pragma once
5#include <algorithm>
6#include <concepts>
7#include <span>
8
9namespace le::anim {
11template <typename Type, typename PayloadT>
12concept SamplerT = requires(Type const& t, std::span<Keyframe<PayloadT> const> keyframes, kvf::Seconds time) {
13 { t.sample(keyframes, time) } -> std::same_as<PayloadT>;
14};
15
17template <typename PayloadT>
19 [[nodiscard]] constexpr auto sample(std::span<Keyframe<PayloadT> const> keyframes, kvf::Seconds const time) const -> PayloadT {
20 if (keyframes.empty()) { return {}; }
21 auto const it = std::ranges::upper_bound(keyframes, time, {}, [](Keyframe<PayloadT> const& kf) { return kf.timestamp; });
22 if (it == keyframes.end()) { return keyframes.back().payload; }
23 if (it == keyframes.begin()) { return it->payload; }
24 return (it - 1)->payload;
25 }
26};
27
29template <typename PayloadT, typename InterpolatorT = Interpolator<PayloadT>>
31 [[nodiscard]] constexpr auto sample(std::span<Keyframe<PayloadT> const> keyframes, kvf::Seconds const time) const -> PayloadT {
32 if (keyframes.empty()) { return {}; }
33 auto const it = std::ranges::upper_bound(keyframes, time, {}, [](Keyframe<PayloadT> const& kf) { return kf.timestamp; });
34 if (it == keyframes.begin()) { return it->payload; }
35 if (it == keyframes.end()) { return (it - 1)->payload; }
36 auto const& a = *(it - 1);
37 auto const& b = *it;
38 auto const total_t = b.timestamp - a.timestamp;
39 auto const t = (time - a.timestamp) / total_t;
40 return InterpolatorT{}(a.payload, b.payload, t);
41 }
42};
43
48
51} // namespace le::anim
Concept for Animation Sampler.
Definition sampler.hpp:12
Definition animation.hpp:9
Class template for an animation keyframe.
Definition keyframe.hpp:7
Quantized Animation Sampler.
Definition sampler.hpp:18
constexpr auto sample(std::span< Keyframe< PayloadT > const > keyframes, kvf::Seconds const time) const -> PayloadT
Definition sampler.hpp:19
Interpolation Animation Sampler.
Definition sampler.hpp:30
constexpr auto sample(std::span< Keyframe< PayloadT > const > keyframes, kvf::Seconds const time) const -> PayloadT
Definition sampler.hpp:31