Skip to content

Instantly share code, notes, and snippets.

@jcelerier
Created August 24, 2020 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcelerier/1e749a7f60081b16ca1bc8d6896db1fd to your computer and use it in GitHub Desktop.
Save jcelerier/1e749a7f60081b16ca1bc8d6896db1fd to your computer and use it in GitHub Desktop.
#include <ossia/network/value/value.hpp>
#include <ossia/editor/curve/curve_segment/easing.hpp>
#include <iostream>
#include <functional>
struct value_interpolator
{
double progress;
std::function<double(double start, double end, double progress)> easing;
// the cases that make sense
ossia::value operator()(float start, float end) const noexcept {
return easing(start, end, progress);
}
ossia::value operator()(int start, int end) const noexcept {
return easing(start, end, progress);
}
ossia::value operator()(int start, float end) const noexcept {
return easing(start, end, progress);
}
ossia::value operator()(float start, int end) const noexcept {
return easing(start, end, progress);
}
ossia::value operator()(ossia::vec2f start, ossia::vec2f end) const noexcept {
// I guess ?
return ossia::vec2f{
(float)easing(start[0], end[0], progress),
(float)easing(start[1], end[1], progress)
};
}
// the ones that don't (yet), e.g. (std::string, bool), (vec2f, vec3f) & stuff...
template<typename T1, typename T2>
ossia::value operator()(const T1& start, const T2& end) const noexcept {
// most likely this is between [0; 1]
if(progress < 0.5)
return start;
else
return end;
}
};
ossia::value interpolate_a_value(ossia::value start, ossia::value end, double progress)
{
if(!start.valid())
return {};
if(!end.valid())
return {};
auto easing_func = [] (double start, double end, double ratio) {
// for instance with a specific easing function, maybe the user will want to choose that.
// see <ossia/editor/curve/curve_segment/easing.hpp> for the available ones..
return ossia::easing::ease{}(start, end, ossia::easing::quadraticInOut{}(ratio));
};
return ossia::apply(value_interpolator{progress, easing_func}, start, end);
}
int main()
{
std::cerr << interpolate_a_value(ossia::value{0.}, ossia::value{10.}, 0.1) << std::endl;
std::cerr << interpolate_a_value(ossia::value{-100.0}, ossia::value{127}, 0.9) << std::endl;
std::cerr << interpolate_a_value(ossia::value{"foo"}, ossia::value{"bar"}, 0.1) << std::endl;
std::cerr << interpolate_a_value(ossia::value{"foo"}, ossia::value{"bar"}, 0.9) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment