-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlot.h
More file actions
401 lines (337 loc) · 14.9 KB
/
Plot.h
File metadata and controls
401 lines (337 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#pragma once
#include "CoordinateMapper.h"
#include "Range.h"
#include "Circle.h"
#include "Text.h"
#include "Curve.h"
#include "Arc.h"
#include "BezierCurve.h"
#include "BezierFitter.h"
#include "plot/Connector.h"
#include "plot/Point.h"
#include "plot/Rectangle.h"
#include "plot/LinePiece.h"
#include "plot/Line.h"
#include "draw/PlotArea.h"
#include "draw/Text.h"
#include "draw/Point.h"
#include "draw/Circle.h"
#include "draw/Curve.h"
#include "draw/Connector.h"
#include "draw/Rectangle.h"
#include "draw/Arc.h"
#include "draw/BezierCurve.h"
#include "draw/BezierFitter.h"
#include "draw/Slider.h"
#include "math/cs/Point.h"
#include "utils/Vector.h"
#include "utils/Badge.h"
#include <boost/intrusive_ptr.hpp>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <type_traits>
#include <tuple>
#include <utility>
#include "debug.h"
#ifdef CWDEBUG
#include "debug_channel.h"
#endif
namespace cairowindow {
class Window;
namespace plot {
#ifdef CWDEBUG
using utils::has_print_on::operator<<;
#endif
class Plot;
//--------------------------------------------------------------------------
// Slider
class Slider
{
private:
cairowindow::Geometry geometry_;
double min_value_; // The value corresponding to position 0.
double max_value_; // The value corresponding to position 1.
public:
Slider(cairowindow::Geometry geometry, double min_value, double max_value) :
geometry_(geometry), min_value_(min_value), max_value_(max_value) { }
cairowindow::Geometry const& geometry() const { return geometry_; }
double value() const { return min_value_ + draw_object_->rel_value() * (max_value_ - min_value_); }
double min_value() const { return min_value_; }
double max_value() const { return max_value_; }
void set_value(double value);
public:
mutable std::shared_ptr<draw::Slider> draw_object_;
};
//--------------------------------------------------------------------------
class Curve : public cairowindow::Curve
{
public:
Curve(std::vector<cairowindow::Point> const& points, std::shared_ptr<draw::Curve> const& draw_object) :
cairowindow::Curve(points), draw_object_(draw_object) { }
Curve(std::vector<cairowindow::Point>&& points, std::shared_ptr<draw::Curve> const& draw_object) :
cairowindow::Curve(std::move(points)), draw_object_(draw_object) { }
explicit Curve(cairowindow::Curve const& curve) : cairowindow::Curve(curve) { }
using cairowindow::Curve::Curve;
public:
mutable std::shared_ptr<draw::Curve> draw_object_;
};
struct TitleStyleDefaults : draw::TextStyleParamsDefault
{
static constexpr draw::TextPosition position = draw::centered;
static constexpr double font_size = 24.0;
};
struct LabelStyleDefaults : draw::TextStyleParamsDefault
{
static constexpr draw::TextPosition position = draw::centered_below;
static constexpr double font_size = 18.0;
};
struct XLabelStyleDefaults : LabelStyleDefaults
{
static constexpr double offset = 10.0;
};
struct YLabelStyleDefaults : LabelStyleDefaults
{
static constexpr draw::TextPosition position = draw::centered_above;
static constexpr double rotation = -0.5 * M_PI;
static constexpr double offset = XLabelStyleDefaults::offset;
};
} // namespace plot
namespace draw {
#define cairowindow_PlotTitle_FOREACH_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_PlotTitle_FOREACH_STYLE_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_XLabel_FOREACH_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_XLabel_FOREACH_STYLE_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_YLabel_FOREACH_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_YLabel_FOREACH_STYLE_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_Label_FOREACH_MEMBER cairowindow_TextBase_FOREACH_MEMBER
#define cairowindow_Label_FOREACH_STYLE_MEMBER cairowindow_TextBase_FOREACH_MEMBER
DECLARE_STYLE_WITH_BASE(PlotTitle, TextBase, plot::TitleStyleDefaults)
DECLARE_STYLE_WITH_BASE(XLabel, TextBase, plot::XLabelStyleDefaults)
DECLARE_STYLE_WITH_BASE(YLabel, TextBase, plot::YLabelStyleDefaults)
DECLARE_STYLE_WITH_BASE(Label, TextBase, plot::LabelStyleDefaults)
#undef cairowindow_PlotTitle_FOREACH_MEMBER
#undef cairowindow_PlotTitle_FOREACH_STYLE_MEMBER
#undef cairowindow_XLabel_FOREACH_MEMBER
#undef cairowindow_XLabel_FOREACH_STYLE_MEMBER
#undef cairowindow_YLabel_FOREACH_MEMBER
#undef cairowindow_YLabel_FOREACH_STYLE_MEMBER
#undef cairowindow_Label_FOREACH_MEMBER
#undef cairowindow_Label_FOREACH_STYLE_MEMBER
} // namespace draw
namespace plot {
class Plot : public CoordinateMapper<csid::plot>
{
private:
static constexpr int number_of_axes = draw::PlotArea::number_of_axes;
draw::PlotArea plot_area_;
std::shared_ptr<draw::Text> title_;
std::shared_ptr<draw::Text> xlabel_;
std::shared_ptr<draw::Text> ylabel_;
std::array<Range, number_of_axes> range_;
std::array<int, number_of_axes> range_ticks_{{10, 10}};
std::array<std::vector<std::shared_ptr<draw::Text>>, number_of_axes> labels_;
public:
Plot(cairowindow::Geometry const& geometry, draw::PlotAreaStyle plot_area_style, std::string title, draw::PlotTitleStyle title_style) :
plot_area_(axes_geometry(geometry, plot_area_style.axes_line_width()), plot_area_style),
title_(std::make_shared<draw::Text>(title, plot_area_.geometry().offset_x() + 0.5 * plot_area_.geometry().width(),
plot_area_.geometry().offset_y() - 0.5 * plot_area_.geometry().offset_y() - title_style.offset(), title_style)) { }
Plot(cairowindow::Geometry const& geometry, draw::PlotAreaStyle plot_area_style, std::string title, draw::PlotTitleStyle title_style,
std::string xlabel, draw::XLabelStyle xlabel_style, std::string ylabel, draw::YLabelStyle ylabel_style) :
plot_area_(axes_geometry(geometry, plot_area_style.axes_line_width()), plot_area_style),
title_(std::make_shared<draw::Text>(title, plot_area_.geometry().offset_x() + 0.5 * plot_area_.geometry().width(),
plot_area_.geometry().offset_y() - 0.5 * plot_area_.geometry().offset_y() - title_style.offset(), title_style)),
xlabel_(std::make_shared<draw::Text>(xlabel, plot_area_.geometry().offset_x() + 0.5 * plot_area_.geometry().width(),
plot_area_.geometry().offset_y() + plot_area_.geometry().height() + XLabelStyleDefaults::offset, xlabel_style)),
ylabel_(std::make_shared<draw::Text>(ylabel, plot_area_.geometry().offset_x() - YLabelStyleDefaults::offset,
plot_area_.geometry().offset_y() + 0.5 * plot_area_.geometry().height(), ylabel_style)) { }
Plot(cairowindow::Geometry const& geometry, draw::PlotAreaStyle plot_area_style) :
plot_area_(axes_geometry(geometry, plot_area_style.axes_line_width()), plot_area_style) { }
void set_range(int axis, Range range)
{
DoutEntering(dc::cairowindow, "Plot::set_range(" << axis << ", " << range << ") [" << this << "]");
range_[axis] = range;
range_ticks_[axis] = draw::PlotArea::calculate_range_ticks(range_[axis]);
Dout(dc::cairowindow, "range_[" << axis << "] = " << range_[axis] << "; range_ticks_[" << axis << "] = " << range_ticks_[axis]);
update_plot_transform_pixels();
}
void set_xrange(Range x_range) { set_range(x_axis, x_range); }
void set_yrange(Range y_range) { set_range(y_axis, y_range); }
cairowindow::Point clamp_to_plot_area(cairowindow::Point const& point) const
{
return {std::clamp(point.x(), range_[x_axis].min(), range_[x_axis].max()),
std::clamp(point.y(), range_[y_axis].min(), range_[y_axis].max())};
}
Range const& xrange() const { return range_[x_axis]; }
Range const& yrange() const { return range_[y_axis]; }
cairowindow::Rectangle viewport() const
{
return {range_[x_axis].min(), range_[y_axis].min(), range_[x_axis].size(), range_[y_axis].size()};
}
double convert_horizontal_offset_from_pixel(double pixel_offset_x) const
{
return pixel_offset_x / cs_transform_pixels_.x_scale();
}
double convert_vertical_offset_from_pixel(double pixel_offset_y) const
{
return pixel_offset_y / cs_transform_pixels_.y_scale();
}
//--------------------------------------------------------------------------
// Line
// Add and draw plot_line using line_style.
void add_line(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
Line const& plot_line);
public:
// Create and draw a line through point in direction using line_style.
template<typename... Args>
[[nodiscard]] Line create_line(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
Args&&... args)
{
Line plot_line(std::forward<Args>(args)...);
add_line(layer, line_style, plot_line);
return plot_line;
}
//--------------------------------------------------------------------------
// LinePiece
// Add and draw plot_line_piece on layer using line_style and line_extend.
void add_line_piece(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style, LineExtend line_extend,
LinePiece const& plot_line_piece);
public:
// Create and draw a line piece between points from and to using line_style and line_extend.
template<typename... Args>
[[nodiscard]] LinePiece create_line_piece(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style, LineExtend line_extend,
Args&&... args)
requires requires(Args&&... args) { LinePiece{std::forward<Args>(args)...}; }
{
LinePiece plot_line_piece(std::forward<Args>(args)...);
add_line_piece(layer, line_style, line_extend, plot_line_piece);
return plot_line_piece;
}
// Same, but without a line_extend.
template<typename... Args>
[[nodiscard]] LinePiece create_line_piece(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
Args&&... args)
requires requires(Args&&... args) { LinePiece{std::forward<Args>(args)...}; }
{
LinePiece plot_line_piece(std::forward<Args>(args)...);
add_line_piece(layer, line_style, LineExtend::none, plot_line_piece);
return plot_line_piece;
}
//--------------------------------------------------------------------------
// BezierCurve
// Add and draw plot_bezier_curve on layer using bezier_style.
void add_bezier_curve(boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
BezierCurve const& plot_bezier_curve);
private:
void add_bezier_curve( // Do not pass a cairowindow::BezierCurve to this function! It must be a plot::BezierCurve.
boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
BezierCurve&& plot_bezier_curve);
public:
// Add and draw plot_bezier_curve on layer using bezier_style.
// The difference with the previous one is that in this case it is assumed that the x and y coordinates
// of the Bezier curve are in pixels already, and no conversion will take place.
void add_bezier_curve_in_px(boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
BezierCurve const& plot_bezier_curve_in_px);
private:
void add_bezier_curve_in_px( // Do not pass a cairowindow::BezierCurve to this function! It must be a plot::BezierCurve.
boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
BezierCurve&& plot_bezier_curve_in_px);
public:
// Create and draw a Bezier curve on layer using bezier_style.
template<typename... Args>
[[nodiscard]] BezierCurve create_bezier_curve(boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
Args&&... args)
{
BezierCurve plot_bezier_curve(std::forward<Args>(args)...);
add_bezier_curve(layer, bezier_style, plot_bezier_curve);
return plot_bezier_curve;
}
// Create and draw a Bezier curve on layer using bezier_style.
// The difference with the previous one is that it is assumed the Args passed are in pixels:
// the constructed BezierCurve is added using add_bezier_curve_in_px.
template<typename... Args>
[[nodiscard]] BezierCurve create_bezier_curve_in_px(boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& bezier_style,
Args&&... args)
{
BezierCurve plot_bezier_curve_in_px(std::forward<Args>(args)...);
add_bezier_curve_in_px(layer, bezier_style, plot_bezier_curve_in_px);
return plot_bezier_curve_in_px;
}
//--------------------------------------------------------------------------
// Curve
void add_bezier_fitter(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
#if CAIROWINDOW_SHOW_BEZIER_CURVE_POINTS
draw::PointStyle const& point_style,
#endif
BezierFitter const& plot_bezier_fitter);
private:
void add_bezier_fitter( // Do not pass a cairowindow::BezierFitter to this function! It must be a plot::BezierFitter.
boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
#if CAIROWINDOW_SHOW_BEZIER_CURVE_POINTS
draw::PointStyle const& point_style,
#endif
BezierFitter&& plot_bezier_fitter);
public:
#if CAIROWINDOW_SHOW_BEZIER_CURVE_POINTS
void add_bezier_fitter(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
BezierFitter const& plot_bezier_fitter)
{
add_bezier_fitter(layer, line_style, draw::PointStyle{}, plot_bezier_fitter);
}
private:
void add_bezier_fitter( // Do not pass a cairowindow::BezierFitter to this function! It must be a plot::BezierFitter.
boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
BezierFitter&& plot_bezier_fitter);
#endif
[[nodiscard]] BezierFitter create_bezier_fitter(boost::intrusive_ptr<Layer> const& layer,
draw::LineStyle const& line_style,
cairowindow::BezierFitter&& bezier_fitter)
{
BezierFitter plot_bezier_fitter(std::move(bezier_fitter));
add_bezier_fitter(layer, line_style, plot_bezier_fitter);
return plot_bezier_fitter;
}
//--------------------------------------------------------------------------
// Slider
[[nodiscard]] Slider create_slider(boost::intrusive_ptr<Layer> const& layer,
cairowindow::Geometry const& geometry, double start_value, double min_value, double max_value);
//--------------------------------------------------------------------------
private:
void update_plot_transform_pixels();
void curve_to_bezier_curves(boost::intrusive_ptr<Layer> const& layer,
Curve const& plot_curve, draw::BezierCurveStyle const& bezier_curve_style);
public:
[[nodiscard]] Curve create_curve(boost::intrusive_ptr<Layer> const& layer,
draw::BezierCurveStyle const& line_style,
std::vector<cairowindow::Point>&& points);
cairowindow::Geometry const& print_extent() const override { return plot_area_.geometry(); }
void add_to(boost::intrusive_ptr<Layer> const& layer, bool keep_ratio = false);
private:
cairowindow::Geometry axes_geometry(cairowindow::Geometry const& geometry, double axes_line_width);
public:
#ifdef CWDEBUG
friend std::ostream& operator<<(std::ostream& os, Plot const* plot_ptr)
{
os << "Plot*";
return os;
}
#endif
};
} // namespace plot
} // namespace cairowindow