blob: 437c71e02123cd4062e930759240d43d6e70d866 [file] [log] [blame]
Stephan Pleines85b295c2024-02-04 17:50:26 -08001#include "aos/analysis/in_process_plotter.h"
James Kuszmaul48671362020-12-24 13:54:16 -08002
Stephan Pleines31f98da2024-05-22 17:31:23 -07003#include <algorithm>
4#include <ostream>
5
Austin Schuh99f7c6a2024-06-25 22:07:44 -07006#include "absl/log/check.h"
7#include "absl/log/log.h"
Stephan Pleines31f98da2024-05-22 17:31:23 -07008#include "flatbuffers/flatbuffer_builder.h"
9#include "flatbuffers/vector.h"
Stephan Pleines31f98da2024-05-22 17:31:23 -070010
James Kuszmaul48671362020-12-24 13:54:16 -080011#include "aos/configuration.h"
12
Stephan Pleines9e40c8e2024-02-07 20:58:28 -080013namespace aos::analysis {
James Kuszmaul48671362020-12-24 13:54:16 -080014
15namespace {
James Kuszmaulac0912d2024-05-21 15:56:59 -070016const char *kDataPath = "../" AOS_REPO_NAME "/aos/analysis/cpp_plot";
17const char *kConfigPath = "../" AOS_REPO_NAME "/aos/analysis/plotter.json";
James Kuszmaul48671362020-12-24 13:54:16 -080018} // namespace
19
20Plotter::Plotter()
21 : config_(aos::configuration::ReadConfig(kConfigPath)),
22 event_loop_factory_(&config_.message()),
23 event_loop_(event_loop_factory_.MakeEventLoop("plotter")),
24 plot_sender_(event_loop_->MakeSender<Plot>("/analysis")),
James Kuszmaulb67409b2022-06-20 16:25:03 -070025 web_proxy_(event_loop_.get(), event_loop_factory_.scheduler_epoll(),
26 aos::web_proxy::StoreHistory::kYes, -1),
James Kuszmaul48671362020-12-24 13:54:16 -080027 builder_(plot_sender_.MakeBuilder()) {
28 web_proxy_.SetDataPath(kDataPath);
29 event_loop_->SkipTimingReport();
Austin Schuhea62f602022-07-18 16:53:04 -070030
31 color_wheel_.emplace_back(ColorWheelColor{.name = "red", .color = {1, 0, 0}});
32 color_wheel_.emplace_back(
33 ColorWheelColor{.name = "green", .color = {0, 1, 0}});
34 color_wheel_.emplace_back(
35 ColorWheelColor{.name = "purple", .color = {0.54, 0.3, 0.75}});
36 color_wheel_.emplace_back(
37 ColorWheelColor{.name = "blue", .color = {0, 0, 1}});
38 color_wheel_.emplace_back(
39 ColorWheelColor{.name = "yellow", .color = {1, 1, 0}});
40 color_wheel_.emplace_back(
41 ColorWheelColor{.name = "teal", .color = {0, 1, 1}});
42 color_wheel_.emplace_back(
43 ColorWheelColor{.name = "pink", .color = {1, 0, 1}});
44 color_wheel_.emplace_back(
45 ColorWheelColor{.name = "orange", .color = {1, 0.6, 0}});
46 color_wheel_.emplace_back(
47 ColorWheelColor{.name = "brown", .color = {0.6, 0.3, 0}});
48 color_wheel_.emplace_back(
49 ColorWheelColor{.name = "white", .color = {1, 1, 1}});
James Kuszmaul48671362020-12-24 13:54:16 -080050}
51
James Kuszmaulb67409b2022-06-20 16:25:03 -070052void Plotter::Spin() {
53 // Set non-infinite replay rate to avoid pegging a full CPU.
54 event_loop_factory_.SetRealtimeReplayRate(1.0);
55 event_loop_factory_.Run();
56}
James Kuszmaul48671362020-12-24 13:54:16 -080057
58void Plotter::Title(std::string_view title) {
59 title_ = builder_.fbb()->CreateString(title);
60}
61
62void Plotter::AddFigure(std::string_view title, double width, double height) {
63 MaybeFinishFigure();
64
65 if (!title.empty()) {
66 figure_title_ = builder_.fbb()->CreateString(title);
67 }
68
69 // For positioning, just stack figures vertically.
70 auto position_builder = builder_.MakeBuilder<Position>();
71 position_builder.add_top(next_top_);
72 position_builder.add_left(0);
73 position_builder.add_width(width);
74 position_builder.add_height(height);
75 position_ = position_builder.Finish();
76
77 next_top_ += height;
78}
79
80void Plotter::XLabel(std::string_view label) {
81 xlabel_ = builder_.fbb()->CreateString(label);
82}
83
84void Plotter::YLabel(std::string_view label) {
85 ylabel_ = builder_.fbb()->CreateString(label);
86}
87
88void Plotter::AddLine(const std::vector<double> &x,
Austin Schuhea62f602022-07-18 16:53:04 -070089 const std::vector<double> &y, LineOptions options) {
Austin Schuh0318e5e2023-02-20 17:39:34 -080090 CHECK_EQ(x.size(), y.size()) << ": " << options.label;
James Kuszmaul48671362020-12-24 13:54:16 -080091 CHECK(!position_.IsNull())
92 << "You must call AddFigure() before calling AddLine().";
93
94 flatbuffers::Offset<flatbuffers::String> label_offset;
Austin Schuhea62f602022-07-18 16:53:04 -070095 if (!options.label.empty()) {
96 label_offset = builder_.fbb()->CreateString(options.label);
James Kuszmaul48671362020-12-24 13:54:16 -080097 }
98
99 std::vector<Point> points;
100 for (size_t ii = 0; ii < x.size(); ++ii) {
101 points.emplace_back(x[ii], y[ii]);
102 }
milind1f1dca32021-07-03 13:50:07 -0700103 const flatbuffers::Offset<flatbuffers::Vector<const Point *>> points_offset =
104 builder_.fbb()->CreateVectorOfStructs(points);
James Kuszmaul48671362020-12-24 13:54:16 -0800105
Austin Schuhea62f602022-07-18 16:53:04 -0700106 const Color *color;
107 if (options.color.empty()) {
108 color = &color_wheel_.at(color_wheel_position_).color;
109 color_wheel_position_ = (color_wheel_position_ + 1) % color_wheel_.size();
110 } else {
111 auto it = std::find_if(
112 color_wheel_.begin(), color_wheel_.end(),
113 [options_color = options.color](const ColorWheelColor &color) {
114 return color.name == options_color;
115 });
116 CHECK(it != color_wheel_.end()) << ": Failed to find " << options.color;
117 color = &(it->color);
118 }
James Kuszmaul48671362020-12-24 13:54:16 -0800119
James Kuszmaul19217a42022-06-17 10:54:29 -0700120 LineStyle::Builder style_builder = builder_.MakeBuilder<LineStyle>();
Austin Schuhea62f602022-07-18 16:53:04 -0700121 if (options.line_style.find('*') != options.line_style.npos) {
Austin Schuh69d0b732022-07-20 21:19:32 -0700122 style_builder.add_point_size(options.point_size);
James Kuszmaul19217a42022-06-17 10:54:29 -0700123 } else {
124 style_builder.add_point_size(0.0);
125 }
Austin Schuhea62f602022-07-18 16:53:04 -0700126 style_builder.add_draw_line(options.line_style.find('-') !=
127 options.line_style.npos);
James Kuszmaul19217a42022-06-17 10:54:29 -0700128 const flatbuffers::Offset<LineStyle> style_offset = style_builder.Finish();
129
James Kuszmaul48671362020-12-24 13:54:16 -0800130 auto line_builder = builder_.MakeBuilder<Line>();
131 line_builder.add_label(label_offset);
132 line_builder.add_points(points_offset);
133 line_builder.add_color(color);
James Kuszmaul19217a42022-06-17 10:54:29 -0700134 line_builder.add_style(style_offset);
James Kuszmaul48671362020-12-24 13:54:16 -0800135 lines_.push_back(line_builder.Finish());
136}
137
138void Plotter::MaybeFinishFigure() {
139 if (!lines_.empty()) {
140 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Line>>>
141 lines_offset = builder_.fbb()->CreateVector(lines_);
142 auto figure_builder = builder_.MakeBuilder<Figure>();
143 figure_builder.add_title(figure_title_);
144 figure_builder.add_position(position_);
145 figure_builder.add_lines(lines_offset);
146 figure_builder.add_xlabel(xlabel_);
147 figure_builder.add_share_x_axis(share_x_axis_);
148 figure_builder.add_ylabel(ylabel_);
149 figures_.push_back(figure_builder.Finish());
150 }
151 lines_.clear();
152 figure_title_.o = 0;
153 xlabel_.o = 0;
154 ylabel_.o = 0;
155 position_.o = 0;
156 share_x_axis_ = false;
157 color_wheel_position_ = 0;
158}
159
160void Plotter::Publish() {
161 MaybeFinishFigure();
162 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Figure>>>
163 figures_offset = builder_.fbb()->CreateVector(figures_);
164
165 auto plot_builder = builder_.MakeBuilder<Plot>();
166 plot_builder.add_title(title_);
167 plot_builder.add_figures(figures_offset);
168
Austin Schuhea62f602022-07-18 16:53:04 -0700169 CHECK_EQ(builder_.Send(plot_builder.Finish()), aos::RawSender::Error::kOk);
James Kuszmaul48671362020-12-24 13:54:16 -0800170
171 builder_ = plot_sender_.MakeBuilder();
172
173 title_.o = 0;
174 figures_.clear();
175 next_top_ = 0;
176}
177
Stephan Pleines9e40c8e2024-02-07 20:58:28 -0800178} // namespace aos::analysis