blob: 8eb8a23c8b1951fd4f529ee2b1b6a3684e475391 [file] [log] [blame]
Parker Schuh2cd173d2017-01-28 00:12:01 -08001#ifndef _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_
2#define _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_
3
4#include <string>
5#include <vector>
6
7#include "aos/vision/image/image_types.h"
8#include "aos/vision/math/segment.h"
9#include "aos/vision/math/vector.h"
10
11namespace aos {
12namespace vision {
13
14// Abstract away rendering to avoid compiling gtk for arm.
15// This should match a reduced cairo rendering api.
16class RenderInterface {
17 public:
18 RenderInterface() {}
19 RenderInterface(RenderInterface &&other) = delete;
20 RenderInterface(const RenderInterface &other) = delete;
21 ~RenderInterface() {}
22
23 virtual void Translate(double x, double y) = 0;
24 virtual void SetSourceRGB(double r, double g, double b) = 0;
25 virtual void MoveTo(double x, double y) = 0;
26 virtual void LineTo(double x, double y) = 0;
27 virtual void Circle(double x, double y, double r) = 0;
28 // negative in x, y, text_x, text_y measures from max in those value
29 virtual void Text(int x, int y, int text_x, int text_y,
30 const std::string &text) = 0;
31 virtual void Stroke() = 0;
32};
33
34// Interface for a list of overlays to be drawn onto a debug image.
35// These will be passed into the running vision algorithms to output debug info,
36// so they must not have costly side-effects.
37class OverlayBase {
38 public:
39 OverlayBase() {}
40 virtual ~OverlayBase() {}
41
42 // Draws this overlay to the given canvas.
43 virtual void Draw(RenderInterface *render, double /* width */,
44 double /* height */) = 0;
45
46 // Clears the entire overlay.
47 virtual void Reset() = 0;
48
49 PixelRef color = {255, 0, 0};
50 double scale = 1.0;
51};
52
53// A lambda that renders directly to the render interface.
54class LambdaOverlay : public OverlayBase {
55 public:
56 std::function<void(RenderInterface *, double, double)> draw_fn;
57 void Draw(RenderInterface *render, double width, double height) override {
58 if (draw_fn) draw_fn(render, width, height);
59 }
60 void Reset() override {}
61};
62
63// Lines rendered in a coordinate system where the origin is the center
64// of the screen
65class LinesOverlay : public OverlayBase {
66 public:
67 LinesOverlay() : OverlayBase() {}
68 ~LinesOverlay() {}
69
70 // build a segment for this line
71 void add_line(Vector<2> st, Vector<2> ed) { add_line(st, ed, color); }
72
73 // build a segment for this line
74 void add_line(Vector<2> st, Vector<2> ed, PixelRef newColor) {
75 lines_.emplace_back(
76 std::pair<Segment<2>, PixelRef>(Segment<2>(st, ed), newColor));
77 }
78
79 void add_point(Vector<2> pt) { add_point(pt, color); }
80
81 // add a new point connected to the last point in the line
82 void add_point(Vector<2> pt, PixelRef newColor) {
83 if (lines_.empty()) {
84 lines_.emplace_back(
85 std::pair<Segment<2>, PixelRef>(Segment<2>(pt, pt), newColor));
86 } else {
87 Vector<2> st = lines_.back().first.B();
88 lines_.emplace_back(
89 std::pair<Segment<2>, PixelRef>(Segment<2>(st, pt), newColor));
90 }
91 }
92
93 void Draw(RenderInterface *render, double w, double h) override {
94 render->Translate(w / 2.0, h / 2.0);
95 for (const auto &ln : lines_) {
96 PixelRef localColor = ln.second;
97 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
98 localColor.b / 255.0);
99 render->MoveTo(scale * ln.first.A().x(), -scale * ln.first.A().y());
100 render->LineTo(scale * ln.first.B().x(), -scale * ln.first.B().y());
101 render->Stroke();
102 }
103 }
104
105 // Empting the list will blank the whole overlay
106 void Reset() override { lines_.clear(); }
107
108 private:
109 // lines in this over lay
110 std::vector<std::pair<Segment<2>, PixelRef>> lines_;
111};
112
113// Lines rendered in pixel coordinates (Should match up with the screen.)
114class PixelLinesOverlay : public OverlayBase {
115 public:
116 PixelLinesOverlay() : OverlayBase() {}
117 ~PixelLinesOverlay() {}
118
119 // build a segment for this line
Parker Schuhef47dbf2017-03-04 16:59:30 -0800120 void AddLine(Vector<2> st, Vector<2> ed) { AddLine(st, ed, color); }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800121
122 // build a segment for this line
Parker Schuhef47dbf2017-03-04 16:59:30 -0800123 void AddLine(Vector<2> st, Vector<2> ed, PixelRef newColor) {
Parker Schuh02f13f62019-02-16 16:42:41 -0800124 AddLine(Segment<2>(st, ed), newColor);
125 }
126
127 // draw a segment.
128 void AddLine(Segment<2> seg, PixelRef newColor) {
129 lines_.emplace_back(std::pair<Segment<2>, PixelRef>(seg, newColor));
Parker Schuh2cd173d2017-01-28 00:12:01 -0800130 }
131
Parker Schuhef47dbf2017-03-04 16:59:30 -0800132 void DrawCross(aos::vision::Vector<2> center, int width,
133 aos::vision::PixelRef color) {
134 using namespace aos::vision;
Parker Schuhcd258b82017-04-09 16:28:29 -0700135 AddLine(Vector<2>(center.x() - width / 2, center.y()),
136 Vector<2>(center.x() + width / 2, center.y()), color);
137 AddLine(Vector<2>(center.x(), center.y() - width / 2),
138 Vector<2>(center.x(), center.y() + width / 2), color);
Parker Schuhef47dbf2017-03-04 16:59:30 -0800139 }
140
141 void DrawBBox(const ImageBBox &box, aos::vision::PixelRef color) {
142 using namespace aos::vision;
143 AddLine(Vector<2>(box.minx, box.miny), Vector<2>(box.maxx, box.miny),
144 color);
145 AddLine(Vector<2>(box.maxx, box.miny), Vector<2>(box.maxx, box.maxy),
146 color);
147 AddLine(Vector<2>(box.maxx, box.maxy), Vector<2>(box.minx, box.maxy),
148 color);
149 AddLine(Vector<2>(box.minx, box.maxy), Vector<2>(box.minx, box.miny),
150 color);
151 }
152
Parker Schuhcd258b82017-04-09 16:28:29 -0700153 // Build a circle as a point and radius.
154 void DrawCircle(Vector<2> center, double radius, PixelRef newColor) {
155 circles_.emplace_back(std::pair<Vector<2>, std::pair<double, PixelRef>>(
156 center, std::pair<double, PixelRef>(radius, newColor)));
157 }
158
Parker Schuhef47dbf2017-03-04 16:59:30 -0800159 void StartNewProfile() { start_profile = true; }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800160
161 // add a new point connected to the last point in the line
Parker Schuhef47dbf2017-03-04 16:59:30 -0800162 void AddPoint(Vector<2> pt, PixelRef newColor) {
Parker Schuh2cd173d2017-01-28 00:12:01 -0800163 if (lines_.empty() || start_profile) {
164 lines_.emplace_back(
165 std::pair<Segment<2>, PixelRef>(Segment<2>(pt, pt), newColor));
166 start_profile = false;
167 } else {
168 Vector<2> st = lines_.back().first.B();
169 lines_.emplace_back(
170 std::pair<Segment<2>, PixelRef>(Segment<2>(st, pt), newColor));
171 }
172 }
173
174 void Draw(RenderInterface *render, double /*width*/,
175 double /*hieght*/) override {
176 for (const auto &ln : lines_) {
177 PixelRef localColor = ln.second;
178 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
179 localColor.b / 255.0);
180 render->MoveTo(ln.first.A().x(), ln.first.A().y());
181 render->LineTo(ln.first.B().x(), ln.first.B().y());
182 render->Stroke();
183 }
Parker Schuhcd258b82017-04-09 16:28:29 -0700184 for (const auto &circle : circles_) {
185 PixelRef localColor = circle.second.second;
186 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
187 localColor.b / 255.0);
188 render->Circle(circle.first.x(), circle.first.y(), circle.second.first);
189 render->Stroke();
190 }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800191 }
192
193 // Empting the list will blank the whole overlay.
Parker Schuhcd258b82017-04-09 16:28:29 -0700194 void Reset() override {
195 lines_.clear();
196 circles_.clear();
197 }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800198
199 private:
200 // Lines in this overlay.
201 std::vector<std::pair<Segment<2>, PixelRef>> lines_;
Parker Schuhcd258b82017-04-09 16:28:29 -0700202 std::vector<std::pair<Vector<2>, std::pair<double, PixelRef>>> circles_;
Parker Schuh2cd173d2017-01-28 00:12:01 -0800203 bool start_profile = false;
204};
205
206// Circles rendered in a coordinate system where the origin is the center
207// of the screen.
208class CircleOverlay : public OverlayBase {
209 public:
210 CircleOverlay() : OverlayBase() {}
211 ~CircleOverlay() {}
212
213 // build a circle as a point and radius
214 std::pair<Vector<2>, double> *add_circle(Vector<2> center, double radius) {
215 circles_.emplace_back(std::pair<Vector<2>, double>(center, radius));
216 return &(circles_.back());
217 }
218
219 void Draw(RenderInterface *render, double w, double h) {
220 render->Translate(w / 2.0, h / 2.0);
221 render->SetSourceRGB(color.r / 255.0, color.g / 255.0, color.b / 255.0);
222 for (const auto &circle : circles_) {
223 render->Circle(scale * circle.first.x(), -scale * circle.first.y(),
224 scale * circle.second);
225 render->Stroke();
226 }
227 }
228
229 // empting the list will blank the whole overlay
230 void Reset() { circles_.clear(); }
231
232 private:
233 // circles in this overlay
234 std::vector<std::pair<Vector<2>, double>> circles_;
235};
236
237} // vision
238} // aos
239
240#endif // _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_