blob: fbd6838dcaec5b6f4624f25ef0c14d6087f08ce9 [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
120 void add_line(Vector<2> st, Vector<2> ed) { add_line(st, ed, color); }
121
122 // build a segment for this line
123 void add_line(Vector<2> st, Vector<2> ed, PixelRef newColor) {
124 lines_.emplace_back(
125 std::pair<Segment<2>, PixelRef>(Segment<2>(st, ed), newColor));
126 }
127
128 void start_new_profile() { start_profile = true; }
129
130 // add a new point connected to the last point in the line
131 void add_point(Vector<2> pt, PixelRef newColor) {
132 if (lines_.empty() || start_profile) {
133 lines_.emplace_back(
134 std::pair<Segment<2>, PixelRef>(Segment<2>(pt, pt), newColor));
135 start_profile = false;
136 } else {
137 Vector<2> st = lines_.back().first.B();
138 lines_.emplace_back(
139 std::pair<Segment<2>, PixelRef>(Segment<2>(st, pt), newColor));
140 }
141 }
142
143 void Draw(RenderInterface *render, double /*width*/,
144 double /*hieght*/) override {
145 for (const auto &ln : lines_) {
146 PixelRef localColor = ln.second;
147 render->SetSourceRGB(localColor.r / 255.0, localColor.g / 255.0,
148 localColor.b / 255.0);
149 render->MoveTo(ln.first.A().x(), ln.first.A().y());
150 render->LineTo(ln.first.B().x(), ln.first.B().y());
151 render->Stroke();
152 }
153 }
154
155 // Empting the list will blank the whole overlay.
156 void Reset() override { lines_.clear(); }
157
158 private:
159 // Lines in this overlay.
160 std::vector<std::pair<Segment<2>, PixelRef>> lines_;
161 bool start_profile = false;
162};
163
164// Circles rendered in a coordinate system where the origin is the center
165// of the screen.
166class CircleOverlay : public OverlayBase {
167 public:
168 CircleOverlay() : OverlayBase() {}
169 ~CircleOverlay() {}
170
171 // build a circle as a point and radius
172 std::pair<Vector<2>, double> *add_circle(Vector<2> center, double radius) {
173 circles_.emplace_back(std::pair<Vector<2>, double>(center, radius));
174 return &(circles_.back());
175 }
176
177 void Draw(RenderInterface *render, double w, double h) {
178 render->Translate(w / 2.0, h / 2.0);
179 render->SetSourceRGB(color.r / 255.0, color.g / 255.0, color.b / 255.0);
180 for (const auto &circle : circles_) {
181 render->Circle(scale * circle.first.x(), -scale * circle.first.y(),
182 scale * circle.second);
183 render->Stroke();
184 }
185 }
186
187 // empting the list will blank the whole overlay
188 void Reset() { circles_.clear(); }
189
190 private:
191 // circles in this overlay
192 std::vector<std::pair<Vector<2>, double>> circles_;
193};
194
195} // vision
196} // aos
197
198#endif // _AOS_VISION_IMAGE_DEBUG_OVERLAY_H_