PointArraySegment.hpp
1 #pragma once
2 #include <Curve/Segment/CurveSegmentModel.hpp>
3 
5 #include <score/serialization/VisitorInterface.hpp>
6 
7 #include <ossia/detail/flat_map.hpp>
8 
9 #include <QPoint>
10 #include <QVariant>
11 #include <QVector>
12 
13 #include <memory>
14 #include <utility>
15 #include <vector>
16 #include <verdigris>
17 
18 class QObject;
19 #include <score/model/Identifier.hpp>
20 
21 namespace Curve
22 {
23 class PointArraySegment;
24 }
25 
26 CURVE_SEGMENT_METADATA(
27  SCORE_PLUGIN_CURVE_EXPORT, Curve::PointArraySegment,
28  "c598b840-db67-4c8f-937a-46cfac87cb59", "PointArray", "PointArray", "hidden")
29 
30 namespace Curve
31 {
32 class LinearSegment;
33 struct SegmentData;
34 struct PointArraySegmentData
35 {
36  double min_x, max_x;
37  double min_y, max_y;
38  QVector<QPointF> m_points;
39 };
40 class SCORE_PLUGIN_CURVE_EXPORT PointArraySegment final : public SegmentModel
41 {
42  W_OBJECT(PointArraySegment)
43 public:
44  MODEL_METADATA_IMPL(PointArraySegment)
45 
46  using data_type = PointArraySegmentData;
47  PointArraySegment(const Id<SegmentModel>& id, QObject* parent)
48  : SegmentModel{id, parent}
49  {
50  }
51  PointArraySegment(const SegmentData& dat, QObject* parent);
52 
53  PointArraySegment(const PointArraySegment& other, const id_type& id, QObject* parent);
54 
55  PointArraySegment(DataStream::Deserializer& vis, QObject* parent)
56  : SegmentModel{vis, parent}
57  {
58  vis.writeTo(*this);
59  }
60 
61  PointArraySegment(JSONObject::Deserializer& vis, QObject* parent)
62  : SegmentModel{vis, parent}
63  {
64  vis.writeTo(*this);
65  }
66 
67  ~PointArraySegment() override;
68 
69  void on_startChanged() override;
70  void on_endChanged() override;
71 
72  void updateData(int numInterp) const override;
73  double valueAt(double x) const override;
74 
75  void addPoint(double, double);
76  void addPointUnscaled(double, double);
77  void simplify(double ratio); // 10 is a good ratio
78  std::vector<SegmentData> toLinearSegments() const;
79  std::vector<SegmentData> toPowerSegments() const;
80 
81  double min() { return min_y; }
82  double max() { return max_y; }
83 
84  void setMinX(double y) { min_x = y; }
85  void setMinY(double y) { min_y = y; }
86  void setMaxX(double y) { max_x = y; }
87  void setMaxY(double y) { max_y = y; }
88 
89  const auto& points() const { return m_points; }
90  void reserve(std::size_t p);
91 
92  QVariant toSegmentSpecificData() const override
93  {
94  PointArraySegmentData dat{min_x, max_x, min_y, max_y, {}};
95 
96  dat.m_points.reserve(m_points.size());
97  for(const auto& pt : m_points)
98  dat.m_points.push_back({pt.first, pt.second});
99 
100  return QVariant::fromValue(std::move(dat));
101  }
102 
103  // This will throw if execution is attempted.
104  ossia::curve_segment<double> makeDoubleFunction() const override;
105  ossia::curve_segment<float> makeFloatFunction() const override;
106  ossia::curve_segment<int> makeIntFunction() const override;
107  void reset();
108 
109 public:
110  void minChanged(double arg_1) E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, minChanged, arg_1)
111  void maxChanged(double arg_1) E_SIGNAL(SCORE_PLUGIN_CURVE_EXPORT, maxChanged, arg_1)
112 
113 private:
114  // Coordinates in {x, y}.
115  double min_x{}, max_x{};
116  double min_y{}, max_y{};
117 
118  double m_lastX{-1};
119 
120  ossia::flat_map<double, double> m_points;
121 };
122 }
123 
124 SCORE_SERIALIZE_DATASTREAM_DECLARE(
125  SCORE_PLUGIN_CURVE_EXPORT, Curve::PointArraySegmentData)
126 Q_DECLARE_METATYPE(Curve::PointArraySegmentData)
127 W_REGISTER_ARGTYPE(Curve::PointArraySegmentData)
Definition: DataStreamVisitor.hpp:202
Definition: JSONVisitor.hpp:423
The id_base_t class.
Definition: Identifier.hpp:57
Utilities and base classes for 1D curves.
Definition: FocusDispatcher.hpp:12