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,
94  };
95 
97  enum class PlotTickMode {
102  };
103 
105  enum class PlotAxisLabelMode {
107  AxisValue,
110  };
111 
113  struct PlotViewport {
115  int width = 640;
117  int height = 320;
119  int leftMargin = 48;
121  int rightMargin = 16;
123  int topMargin = 16;
125  int bottomMargin = 32;
127  int xTickCount = 5;
129  int yTickCount = 5;
131  bool drawXAxisLabels = false;
137  double xTickInterval = 1.0;
140  };
141 
143  struct PlotPixel {
145  uint8_t r = 0;
147  uint8_t g = 0;
149  uint8_t b = 0;
151  uint8_t a = 255;
152  };
153 
155  struct PlotImage {
157  int width = 0;
159  int height = 0;
161  std::vector<PlotPixel> pixels;
162  };
163 
165  enum class PlotRenderMode {
167  Software,
169  OpenGL
170  };
171 
173  class PlotModel : public RADCore::RADObject {
174  public:
175  RADEvents:
178 
182  ~PlotModel() override;
183 
185  size_t addSeries(std::string name, PlotSeriesStyle style = {});
187  size_t seriesCount() const;
189  void clear();
191  void clearSeries(size_t seriesIndex);
192 
194  void append(size_t seriesIndex, double x, double y);
196  void append(size_t seriesIndex, PlotPoint point);
198  void appendBatch(size_t seriesIndex, const std::vector<PlotPoint>& points);
199 
203  size_t maxPointCount() const;
204 
206  void setRollingXWindow(double width);
208  double rollingXWindow() const;
209 
211  void setAutoRange(bool enabled);
213  bool autoRange() const;
215  void setXRange(PlotRange range);
217  void setYRange(PlotRange range);
219  PlotRange xRange() const;
221  PlotRange yRange() const;
222 
224  void setSeriesStyle(size_t seriesIndex, PlotSeriesStyle style);
227 
228  private:
229  struct Series;
230  struct Impl;
231  std::unique_ptr<Impl> pImpl_;
232  };
233 
236  public:
239 
241  PlotModel& panel(size_t index);
243  const PlotModel& panel(size_t index) const;
245  void setSharedRollingXWindow(double width);
247  void setMaxPointCount(size_t maxPointCount);
249  void clear();
250 
251  private:
252  std::array<std::shared_ptr<PlotModel>, 4> panels_;
253  };
254 
256  class PlotRenderer {
257  public:
259  virtual ~PlotRenderer() = default;
261  virtual PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) = 0;
262  };
263 
265  class SoftwarePlotRenderer final : public PlotRenderer {
266  public:
268  PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) override;
269 
270  private:
271  PlotColor background_{18, 22, 26, 255};
272  PlotColor plotArea_{24, 30, 36, 255};
273  PlotColor grid_{55, 65, 75, 255};
274  PlotColor axis_{150, 160, 170, 255};
275  };
276 
278  class OpenGLPlotRenderer final : public PlotRenderer {
279  public:
281  PlotImage render(const PlotModelSnapshot& snapshot, const PlotViewport& viewport) override;
282 
283  private:
284  SoftwarePlotRenderer fallback_;
285  };
286 
288  std::unique_ptr<PlotRenderer> createPlotRenderer(PlotRenderMode mode);
289 
290 } // namespace RADUi
291 
292 #endif
#define RAD_EVENT(Name, Signature)
Declares a typed RADCore event; example: RAD_EVENT(done, (int)).
Definition: RADCore.h:97
#define RADEvents
Marks a public event declaration block in RADObject-derived classes.
Definition: RADCore.h:75
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:963
OpenGL renderer facade with software fallback.
Definition: RADPlot.h:278
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:173
~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:177
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:256
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:235
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:265
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.
@ LogFrequencyHz
Render values as a log10(Hz) coordinate labeled in Hz or kHz.
PlotTickMode
X-axis tick placement mode.
Definition: RADPlot.h:97
@ 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:105
@ AxisValue
Labels show actual axis values.
@ WindowRelative
Labels show values relative to the current visible window.
PlotRenderMode
Renderer implementation selection.
Definition: RADPlot.h:165
@ 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:155
int width
Image width in pixels.
Definition: RADPlot.h:157
std::vector< PlotPixel > pixels
Pixel buffer in row-major order.
Definition: RADPlot.h:161
int height
Image height in pixels.
Definition: RADPlot.h:159
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:143
uint8_t b
Blue component.
Definition: RADPlot.h:149
uint8_t a
Alpha component.
Definition: RADPlot.h:151
uint8_t r
Red component.
Definition: RADPlot.h:145
uint8_t g
Green component.
Definition: RADPlot.h:147
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:113
int width
Output image width in pixels.
Definition: RADPlot.h:115
bool drawXAxisLabels
Draw X-axis text labels.
Definition: RADPlot.h:131
PlotAxisLabelMode xAxisLabelMode
X-axis label value mode.
Definition: RADPlot.h:139
PlotTickMode xTickMode
X-axis tick placement mode.
Definition: RADPlot.h:135
int yTickCount
Target number of Y-axis ticks.
Definition: RADPlot.h:129
double xTickInterval
Fixed interval used when xTickMode is FixedInterval.
Definition: RADPlot.h:137
PlotAxisFormat xAxisFormat
X-axis label formatting.
Definition: RADPlot.h:133
int leftMargin
Left margin reserved for labels.
Definition: RADPlot.h:119
int topMargin
Top margin in pixels.
Definition: RADPlot.h:123
int height
Output image height in pixels.
Definition: RADPlot.h:117
int xTickCount
Target number of X-axis ticks.
Definition: RADPlot.h:127
int bottomMargin
Bottom margin reserved for labels.
Definition: RADPlot.h:125
int rightMargin
Right margin in pixels.
Definition: RADPlot.h:121