blob: 0122c94216bc4e3cd586c0d7f5d1a81b0cd28bdf [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;
Parker Schuhcd258b82017-04-09 16:28:29 -0700131 AddLine(Vector<2>(center.x() - width / 2, center.y()),
132 Vector<2>(center.x() + width / 2, center.y()), color);
133 AddLine(Vector<2>(center.x(), center.y() - width / 2),
134 Vector<2>(center.x(), center.y() + width / 2), color);
Parker Schuhef47dbf2017-03-04 16:59:30 -0800135 }
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
Parker Schuhcd258b82017-04-09 16:28:29 -0700149 // Build a circle as a point and radius.
150 void DrawCircle(Vector<2> center, double radius, PixelRef newColor) {
151 circles_.emplace_back(std::pair<Vector<2>, std::pair<double, PixelRef>>(
152 center, std::pair<double, PixelRef>(radius, newColor)));
153 }
154
Parker Schuhef47dbf2017-03-04 16:59:30 -0800155 void StartNewProfile() { start_profile = true; }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800156
157 // add a new point connected to the last point in the line
Parker Schuhef47dbf2017-03-04 16:59:30 -0800158 void AddPoint(Vector<2> pt, PixelRef newColor) {
Parker Schuh2cd173d2017-01-28 00:12:01 -0800159 if (lines_.empty() || start_profile) {
160 lines_.emplace_back(
161 std::pair<Segment<2>, PixelRef>(Segment<2>(pt, pt), newColor));
162 start_profile = false;
163 } else {
164 Vector<2> st = lines_.back().first.B();
165 lines_.emplace_back(
166 std::pair<Segment<2>, PixelRef>(Segment<2>(st, pt), newColor));
167 }
168 }
169
170 void Draw(RenderInterface *render, double /*width*/,
171 double /*hieght*/) override {
172 for (const auto &ln : lines_) {
173 PixelRef localColor = ln.second;
174 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
175 localColor.b / 255.0);
176 render->MoveTo(ln.first.A().x(), ln.first.A().y());
177 render->LineTo(ln.first.B().x(), ln.first.B().y());
178 render->Stroke();
179 }
Parker Schuhcd258b82017-04-09 16:28:29 -0700180 for (const auto &circle : circles_) {
181 PixelRef localColor = circle.second.second;
182 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
183 localColor.b / 255.0);
184 render->Circle(circle.first.x(), circle.first.y(), circle.second.first);
185 render->Stroke();
186 }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800187 }
188
189 // Empting the list will blank the whole overlay.
Parker Schuhcd258b82017-04-09 16:28:29 -0700190 void Reset() override {
191 lines_.clear();
192 circles_.clear();
193 }
Parker Schuh2cd173d2017-01-28 00:12:01 -0800194
195 private:
196 // Lines in this overlay.
197 std::vector<std::pair<Segment<2>, PixelRef>> lines_;
Parker Schuhcd258b82017-04-09 16:28:29 -0700198 std::vector<std::pair<Vector<2>, std::pair<double, PixelRef>>> circles_;
Parker Schuh2cd173d2017-01-28 00:12:01 -0800199 bool start_profile = false;
200};
201
202// Circles rendered in a coordinate system where the origin is the center
203// of the screen.
204class CircleOverlay : public OverlayBase {
205 public:
206 CircleOverlay() : OverlayBase() {}
207 ~CircleOverlay() {}
208
209 // build a circle as a point and radius
210 std::pair<Vector<2>, double> *add_circle(Vector<2> center, double radius) {
211 circles_.emplace_back(std::pair<Vector<2>, double>(center, radius));
212 return &(circles_.back());
213 }
214
215 void Draw(RenderInterface *render, double w, double h) {
216 render->Translate(w / 2.0, h / 2.0);
217 render->SetSourceRGB(color.r / 255.0, color.g / 255.0, color.b / 255.0);
218 for (const auto &circle : circles_) {
219 render->Circle(scale * circle.first.x(), -scale * circle.first.y(),
220 scale * circle.second);
221 render->Stroke();
222 }
223 }
224
225 // empting the list will blank the whole overlay
226 void Reset() { circles_.clear(); }
227
228 private:
229 // circles in this overlay
230 std::vector<std::pair<Vector<2>, double>> circles_;
231};
232
233} // vision
234} // aos
235
236#endif // _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_