Loading...
Searching...
No Matches
CurveConversion.hpp
1#pragma once
2#include <Curve/Segment/Linear/LinearSegment.hpp>
3#include <Curve/Segment/Power/PowerSegment.hpp>
4
5#include <ossia/editor/curve/curve.hpp>
6
7namespace Engine
8{
9namespace score_to_ossia
10{
11
12template <typename Y_T>
14
15template <>
16struct CurveTraits<int>
17{
18 static const constexpr auto fun = &Curve::SegmentModel::makeIntFunction;
19};
20
21template <>
22struct CurveTraits<float>
23{
24 static const constexpr auto fun = &Curve::SegmentModel::makeFloatFunction;
25};
26
27// TODO
28template <>
29struct CurveTraits<double>
30{
31 static const constexpr auto fun = &Curve::SegmentModel::makeDoubleFunction;
32};
33
34template <
35 typename X_T, typename Y_T, typename XScaleFun, typename YScaleFun,
36 typename Segments>
37std::shared_ptr<ossia::curve<X_T, Y_T>> curve(
38 XScaleFun scale_x, YScaleFun scale_y, const Segments& segments,
39 const std::optional<ossia::destination>& tween)
40{
41 auto curve = std::make_shared<ossia::curve<X_T, Y_T>>();
42 curve->reserve(segments.size() + 1);
43
44 // Where the curve begins, wherever that is. The first segment is not
45 // required to start at 0: leaving x0/y0 at their defaults there made the
46 // ossia curve ramp from (0, 0) to the first segment's end, which throws that
47 // segment away and shifts everything after it.
48 auto start = segments[0]->start();
49 curve->set_x0(scale_x(start.x()));
50 curve->set_y0(scale_y(start.y()));
51
52 for(const auto& score_segment : segments)
53 {
54 auto end = score_segment->end();
55 curve->add_point(
56 (score_segment->*CurveTraits<Y_T>::fun)(), scale_x(end.x()), scale_y(end.y()));
57 }
58
59 if(tween)
60 {
61 curve->set_y0_destination(*tween);
62 }
63
64 return curve;
65}
66
67// Simpler curve, between [0; 1]
68template <typename Segments>
69ossia::curve<double, float>
70floatCurve(const Segments& segments, const std::optional<ossia::destination>& tween)
71{
72 ossia::curve<double, float> curve;
73
74 // See above: the first segment need not start at 0.
75 auto start = segments[0]->start();
76 curve.set_x0(start.x());
77 curve.set_y0(start.y());
78
79 for(const auto& score_segment : segments)
80 {
81 auto end = score_segment->end();
82 curve.add_point((score_segment->*CurveTraits<float>::fun)(), end.x(), end.y());
83 }
84
85 if(tween)
86 {
87 curve.set_y0_destination(*tween);
88 }
89
90 return curve;
91}
92
93}
94}
Link of score with the OSSIA API execution engine.
Definition CurveConversion.hpp:8
Definition CurveConversion.hpp:13