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 
7 namespace Engine
8 {
9 namespace score_to_ossia
10 {
11 
12 template <typename Y_T>
13 struct CurveTraits;
14 
15 template <>
16 struct CurveTraits<int>
17 {
18  static const constexpr auto fun = &Curve::SegmentModel::makeIntFunction;
19 };
20 
21 template <>
22 struct CurveTraits<float>
23 {
24  static const constexpr auto fun = &Curve::SegmentModel::makeFloatFunction;
25 };
26 
27 // TODO
28 template <>
29 struct CurveTraits<double>
30 {
31  static const constexpr auto fun = &Curve::SegmentModel::makeDoubleFunction;
32 };
33 
34 template <
35  typename X_T, typename Y_T, typename XScaleFun, typename YScaleFun,
36  typename Segments>
37 std::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  auto start = segments[0]->start();
45  if(start.x() == 0.)
46  {
47  curve->set_x0(scale_x(start.x()));
48  curve->set_y0(scale_y(start.y()));
49  }
50 
51  for(const auto& score_segment : segments)
52  {
53  auto end = score_segment->end();
54  curve->add_point(
55  (score_segment->*CurveTraits<Y_T>::fun)(), scale_x(end.x()), scale_y(end.y()));
56  }
57 
58  if(tween)
59  {
60  curve->set_y0_destination(*tween);
61  }
62 
63  return curve;
64 }
65 
66 // Simpler curve, between [0; 1]
67 template <typename Segments>
68 ossia::curve<double, float>
69 floatCurve(const Segments& segments, const std::optional<ossia::destination>& tween)
70 {
71  ossia::curve<double, float> curve;
72 
73  auto start = segments[0]->start();
74  if(start.x() == 0.)
75  {
76  curve.set_x0(start.x());
77  curve.set_y0(start.y());
78  }
79 
80  for(const auto& score_segment : segments)
81  {
82  auto end = score_segment->end();
83  curve.add_point((score_segment->*CurveTraits<float>::fun)(), end.x(), end.y());
84  }
85 
86  if(tween)
87  {
88  curve.set_y0_destination(*tween);
89  }
90 
91  return curve;
92 }
93 
94 }
95 }
Link of score with the OSSIA API execution engine.
Definition: CurveConversion.hpp:8
Definition: CurveConversion.hpp:13