blob: 5a9804f4096c278fc3baed3c9d11809022a526cb [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 Schuh2cd173d2017-01-28 00:12:01 -0800124 lines_.emplace_back(
125 std::pair<Segment<2>, PixelRef>(Segment<2>(st, ed), newColor));
126 }
127
Parker Schuhef47dbf2017-03-04 16:59:30 -0800128 void DrawCross(aos::vision::Vector<2> center, int width,
129 aos::vision::PixelRef color) {
130 using namespace aos::vision;
131 AddLine(Vector<2>(center.x() - width, center.y()),
132 Vector<2>(center.x() + width, center.y()), color);
133 AddLine(Vector<2>(center.x(), center.y() - width),
134 Vector<2>(center.x(), center.y() + width), color);
135 }
136
137 void DrawBBox(const ImageBBox &box, aos::vision::PixelRef color) {
138 using namespace aos::vision;
139 AddLine(Vector<2>(box.minx, box.miny), Vector<2>(box.maxx, box.miny),
140 color);
141 AddLine(Vector<2>(box.maxx, box.miny), Vector<2>(box.maxx, box.maxy),
142 color);
143 AddLine(Vector<2>(box.maxx, box.maxy), Vector<2>(box.minx, box.maxy),
144 color);
145 AddLine(Vector<2>(box.minx, box.maxy), Vector<2>(box.minx, box.miny),
146 color);
147 }
148
149 void StartNewProfile() { start_profile = true; }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800150
151 // add a new point connected to the last point in the line
Parker Schuhef47dbf2017-03-04 16:59:30 -0800152 void AddPoint(Vector<2> pt, PixelRef newColor) {
Parker Schuh2cd173d2017-01-28 00:12:01 -0800153 if (lines_.empty() || start_profile) {
154 lines_.emplace_back(
155 std::pair<Segment<2>, PixelRef>(Segment<2>(pt, pt), newColor));
156 start_profile = false;
157 } else {
158 Vector<2> st = lines_.back().first.B();
159 lines_.emplace_back(
160 std::pair<Segment<2>, PixelRef>(Segment<2>(st, pt), newColor));
161 }
162 }
163
164 void Draw(RenderInterface *render, double /*width*/,
165 double /*hieght*/) override {
166 for (const auto &ln : lines_) {
167 PixelRef localColor = ln.second;
168 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
169 localColor.b / 255.0);
170 render->MoveTo(ln.first.A().x(), ln.first.A().y());
171 render->LineTo(ln.first.B().x(), ln.first.B().y());
172 render->Stroke();
173 }
174 }
175
176 // Empting the list will blank the whole overlay.
177 void Reset() override { lines_.clear(); }
178
179 private:
180 // Lines in this overlay.
181 std::vector<std::pair<Segment<2>, PixelRef>> lines_;
182 bool start_profile = false;
183};
184
185// Circles rendered in a coordinate system where the origin is the center
186// of the screen.
187class CircleOverlay : public OverlayBase {
188 public:
189 CircleOverlay() : OverlayBase() {}
190 ~CircleOverlay() {}
191
192 // build a circle as a point and radius
193 std::pair<Vector<2>, double> *add_circle(Vector<2> center, double radius) {
194 circles_.emplace_back(std::pair<Vector<2>, double>(center, radius));
195 return &(circles_.back());
196 }
197
198 void Draw(RenderInterface *render, double w, double h) {
199 render->Translate(w / 2.0, h / 2.0);
200 render->SetSourceRGB(color.r / 255.0, color.g / 255.0, color.b / 255.0);
201 for (const auto &circle : circles_) {
202 render->Circle(scale * circle.first.x(), -scale * circle.first.y(),
203 scale * circle.second);
204 render->Stroke();
205 }
206 }
207
208 // empting the list will blank the whole overlay
209 void Reset() { circles_.clear(); }
210
211 private:
212 // circles in this overlay
213 std::vector<std::pair<Vector<2>, double>> circles_;
214};
215
216} // vision
217} // aos
218
219#endif // _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_