RADLib
RADical C++ application framework
RADPlot.h
Go to the documentation of this file.
1 #ifndef RADPLOT_H
2 #define RADPLOT_H
3 
9 #pragma once
10 
11 #include <RADUi/RADUi.h>
12 
13 #include <array>
14 #include <cstddef>
15 #include <cstdint>
16 #include <limits>
17 #include <memory>
18 #include <optional>
19 #include <string>
20 #include <vector>
21 
22 namespace RADUi {
23 
25  struct PlotPoint {
27  double x = 0.0;
29  double y = 0.0;
30  };
31 
33  struct PlotColor {
35  uint8_t r = 255;
37  uint8_t g = 255;
39  uint8_t b = 255;
41  uint8_t a = 255;
42  };
43 
45  struct PlotRange {
47  double min = 0.0;
49  double max = 1.0;
50  };
51 
53  struct PlotSeriesStyle {
55  PlotColor color{33, 150, 243, 255};
57  int lineWidth = 1;
59  bool drawLines = true;
61  bool drawPoints = false;
62  };
63 
67  std::string name;
71  std::vector<PlotPoint> points;
72  };
73 
77  std::vector<PlotSeriesSnapshot> series;
83  bool hasData = false;
84  };
85 
87  enum class PlotAxisFormat {
89  Numeric,
92  };
93 
95  enum class PlotTickMode {
100  };
101 
103  enum class PlotAxisLabelMode {
105  AxisValue,
108  };
109 
111  struct PlotViewport {
113  int width = 640;
115  int height = 320;
117  int leftMargin = 48;
119  int rightMargin = 16;
121  int topMargin = 16;
123  int bottomMargin = 32;
125  int xTickCount = 5;
127  int yTickCount = 5;
129  bool drawXAxisLabels = false;
135  double xTickInterval = 1.0;
138  };
139 
141  struct PlotPixel {
143  uint8_t r = 0;
145  uint8_t g = 0;
147  uint8_t b = 0;
149  uint8_t a = 255;
150  };
151 
153  struct PlotImage {
155  int width = 0;
157  int height = 0;
159  std::vector<PlotPixel> pixels;
160  };
161 
163  enum class PlotRenderMode {
165  Software,
167  OpenGL
168  };
169 
171  class PlotModel : public RADCore::RADObject {
172  public:
173  RADEvents:
176 
180  ~PlotModel() override;
181 
183  size_t addSeries(std::string name, PlotSeriesStyle style = {});
185  size_t seriesCount() const;
187  void clear();
189  void clearSeries(size_t seriesIndex);
190 
192  void append(size_t seriesIndex, double x, double y);
194  void append(size_t seriesIndex, PlotPoint point);
196  void appendBatch(size_t seriesIndex, const std::vector<PlotPoint>& points);
197 
201  size_t maxPointCount() const;
202 
204  void setRollingXWindow(double width);
206  double rollingXWindow() const;
207 
209  void setAutoRange(bool enabled);
211  bool autoRange() const;
213  void setXRange(PlotRange range);
215  void setYRange(PlotRange range);
217  PlotRange xRange() const;
219  PlotRange yRange() const;
220 
222  void setSeriesStyle(size_t seriesIndex, PlotSeriesStyle style);
225 
226  private:
227  struct Series;
228  struct Impl;
229  std::unique_ptr<Impl> pImpl_;
230  };
231 
234  public:
237 
239  PlotModel& panel(size_t index);
241  const PlotModel& panel(size_t index) const;
243  void setSharedRollingXWindow(double width);
245  void setMaxPointCount(size_t maxPointCount);
247  void clear();
248 
249  private:
250  std::array<std::shared_ptr<PlotModel>, 4> panels_;
251  };
252 
254  class PlotRenderer {
255  public:
257  virtual ~PlotRenderer() = default;
259  virtual PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) = 0;
260  };
261 
263  class SoftwarePlotRenderer final : public PlotRenderer {
264  public:
266  PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) override;
267 
268  private:
269  PlotColor background_{18, 22, 26, 255};
270  PlotColor plotArea_{24, 30, 36, 255};
271  PlotColor grid_{55, 65, 75, 255};
272  PlotColor axis_{150, 160, 170, 255};
273  };
274 
276  class OpenGLPlotRenderer final : public PlotRenderer {
277  public:
279  PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) override;
280 
281  private:
282  SoftwarePlotRenderer fallback_;
283  };
284 
286  std::unique_ptr<PlotRenderer> createPlotRenderer(PlotRenderMode mode);
287 
288 } // namespace RADUi
289 
290 #endif
#define RAD_EVENT(Name, Signature)
Declares a typed RADCore event; example: RAD_EVENT(done, (int)).
Definition: RADCore.h:65
#define RADEvents
Marks a public event declaration block in RADObject-derived classes.
Definition: RADCore.h:43
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:931
OpenGL renderer facade with software fallback.
Definition: RADPlot.h:276
PlotImage render(const PlotModelSnapshot &snapshot, const PlotViewport &viewport) override
Renders with OpenGL when implemented, otherwise delegates to software.
Mutable X/Y plot data model that emits dataChanged on edits.
Definition: RADPlot.h:171
~PlotModel() override
Destroys the model implementation.
PlotModel()
Creates an empty plot model.
void setAutoRange(bool enabled)
Enables or disables automatic range calculation.
void setXRange(PlotRange range)
Sets the manual X-axis range.
void setSeriesStyle(size_t seriesIndex, PlotSeriesStyle style)
Updates the rendering style for one series.
size_t maxPointCount() const
Returns the maximum retained points per series.
void setYRange(PlotRange range)
Sets the manual Y-axis range.
void setRollingXWindow(double width)
Sets a rolling X-axis window width; 0 disables rolling range.
bool autoRange() const
Returns true when ranges are calculated from data.
PlotRange yRange() const
Returns the current Y-axis range.
PlotModelSnapshot snapshot() const
Returns an immutable model snapshot for rendering.
size_t addSeries(std::string name, PlotSeriesStyle style={})
Adds a named series and returns its index.
RADCore::Event< void() > dataChanged
Raised when series data, ranges, or styles change.
Definition: RADPlot.h:175
void clear()
Removes all series data.
double rollingXWindow() const
Returns the rolling X-axis window width.
void appendBatch(size_t seriesIndex, const std::vector< PlotPoint > &points)
Appends a batch of points to a series.
void append(size_t seriesIndex, double x, double y)
Appends one X/Y point to a series.
size_t seriesCount() const
Returns the number of series in the model.
void setMaxPointCount(size_t maxPointCount)
Sets the maximum retained points per series; 0 means unlimited.
PlotRange xRange() const
Returns the current X-axis range.
void clearSeries(size_t seriesIndex)
Removes all points from one series.
void append(size_t seriesIndex, PlotPoint point)
Appends one point to a series.
Abstract renderer that converts plot snapshots into images.
Definition: RADPlot.h:254
virtual PlotImage render(const PlotModelSnapshot &snapshot, const PlotViewport &viewport)=0
Renders snapshot into an image using viewport.
virtual ~PlotRenderer()=default
Destroys renderer implementations through the base class.
Four-panel plot model for I/Q, quad, or dashboard-style views.
Definition: RADPlot.h:233
const PlotModel & panel(size_t index) const
Returns a const panel by index 0..3.
PlotModel & panel(size_t index)
Returns a mutable panel by index 0..3.
QuadPlotModel()
Creates four empty panels.
void setMaxPointCount(size_t maxPointCount)
Applies a shared max point count to all panels.
void setSharedRollingXWindow(double width)
Applies a shared rolling X window to all panels.
void clear()
Clears all panels.
CPU renderer used by examples and as the OpenGL fallback.
Definition: RADPlot.h:263
PlotImage render(const PlotModelSnapshot &snapshot, const PlotViewport &viewport) override
Renders the plot entirely on the CPU.
Definition: RADAppKit.h:20
std::unique_ptr< PlotRenderer > createPlotRenderer(PlotRenderMode mode)
Creates a renderer for mode.
PlotAxisFormat
X-axis label formatting mode.
Definition: RADPlot.h:87
@ Numeric
Render values as plain numbers.
@ SecondsMilliseconds
Render values as seconds plus milliseconds.
PlotTickMode
X-axis tick placement mode.
Definition: RADPlot.h:95
@ EvenlySpaced
Distribute ticks evenly across the viewport.
@ FixedInterval
Place ticks on fixed intervals in model units.
PlotAxisLabelMode
X-axis label value interpretation.
Definition: RADPlot.h:103
@ AxisValue
Labels show actual axis values.
@ WindowRelative
Labels show values relative to the current visible window.
PlotRenderMode
Renderer implementation selection.
Definition: RADPlot.h:163
@ Software
CPU software renderer.
@ OpenGL
OpenGL renderer when available, with software fallback.
RGBA color used by plot renderers.
Definition: RADPlot.h:33
uint8_t g
Green component.
Definition: RADPlot.h:37
uint8_t a
Alpha component.
Definition: RADPlot.h:41
uint8_t b
Blue component.
Definition: RADPlot.h:39
uint8_t r
Red component.
Definition: RADPlot.h:35
RGBA image returned by plot renderers.
Definition: RADPlot.h:153
int width
Image width in pixels.
Definition: RADPlot.h:155
std::vector< PlotPixel > pixels
Pixel buffer in row-major order.
Definition: RADPlot.h:159
int height
Image height in pixels.
Definition: RADPlot.h:157
Immutable copy of a complete plot model.
Definition: RADPlot.h:75
PlotRange yRange
Y-axis range used for rendering.
Definition: RADPlot.h:81
std::vector< PlotSeriesSnapshot > series
Series included in the snapshot.
Definition: RADPlot.h:77
PlotRange xRange
X-axis range used for rendering.
Definition: RADPlot.h:79
bool hasData
True when at least one point exists.
Definition: RADPlot.h:83
One output pixel.
Definition: RADPlot.h:141
uint8_t b
Blue component.
Definition: RADPlot.h:147
uint8_t a
Alpha component.
Definition: RADPlot.h:149
uint8_t r
Red component.
Definition: RADPlot.h:143
uint8_t g
Green component.
Definition: RADPlot.h:145
One X/Y sample in plot coordinate space.
Definition: RADPlot.h:25
double y
Vertical coordinate.
Definition: RADPlot.h:29
double x
Horizontal coordinate.
Definition: RADPlot.h:27
Inclusive numeric range for an axis.
Definition: RADPlot.h:45
double max
Maximum axis value.
Definition: RADPlot.h:49
double min
Minimum axis value.
Definition: RADPlot.h:47
Immutable copy of one series for renderer consumption.
Definition: RADPlot.h:65
std::string name
Display name for the series.
Definition: RADPlot.h:67
PlotSeriesStyle style
Rendering style copied from the model.
Definition: RADPlot.h:69
std::vector< PlotPoint > points
Series points copied from the model.
Definition: RADPlot.h:71
Rendering style for one plot series.
Definition: RADPlot.h:53
bool drawPoints
Draw discrete sample points.
Definition: RADPlot.h:61
int lineWidth
Line width in pixels for software rendering.
Definition: RADPlot.h:57
bool drawLines
Draw connected line segments.
Definition: RADPlot.h:59
PlotColor color
Series color.
Definition: RADPlot.h:55
Pixel viewport and axis rendering configuration.
Definition: RADPlot.h:111
int width
Output image width in pixels.
Definition: RADPlot.h:113
bool drawXAxisLabels
Draw X-axis text labels.
Definition: RADPlot.h:129
PlotAxisLabelMode xAxisLabelMode
X-axis label value mode.
Definition: RADPlot.h:137
PlotTickMode xTickMode
X-axis tick placement mode.
Definition: RADPlot.h:133
int yTickCount
Target number of Y-axis ticks.
Definition: RADPlot.h:127
double xTickInterval
Fixed interval used when xTickMode is FixedInterval.
Definition: RADPlot.h:135
PlotAxisFormat xAxisFormat
X-axis label formatting.
Definition: RADPlot.h:131
int leftMargin
Left margin reserved for labels.
Definition: RADPlot.h:117
int topMargin
Top margin in pixels.
Definition: RADPlot.h:121
int height
Output image height in pixels.
Definition: RADPlot.h:115
int xTickCount
Target number of X-axis ticks.
Definition: RADPlot.h:125
int bottomMargin
Bottom margin reserved for labels.
Definition: RADPlot.h:123
int rightMargin
Right margin in pixels.
Definition: RADPlot.h:119