Repository navigation
Expand file tree
/
Copy pathui_opencv.cpp
More file actions
71 lines (61 loc) · 1.68 KB
/
Copy pathui_opencv.cpp
File metadata and controls
71 lines (61 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "ui_opencv.h"
#include <opencv2/highgui/highgui.hpp>
namespace {
cv::Scalar ToCvColor(Color c) {
switch (c) {
case Color::Blue:
return {255, 0, 0};
case Color::Red:
return {0, 0, 255};
case Color::Green:
return {0, 255, 0};
default: { throw std::runtime_error("cannot convert color to cv::Scalar"); }
}
}
template <typename T>
constexpr auto ToUnderlying(T e) noexcept {
return static_cast<std::underlying_type_t<T>>(e);
}
Edge toEdge(std::vector<Vertex> vertexes, Connection c) {
return {vertexes[std::get<0>(c)], vertexes[std::get<1>(c)]};
}
} // namespace
UIOpenCV::UIOpenCV(const std::string& name): name_(name), mat_(500,500, CV_8UC3, {255,255,255}){
Update();
}
void UIOpenCV::Plot(Edge e, Color c, Thickness t){
Vertex from,to;
std::tie(from,to) = e;
int x1,y1,x2,y2;
std::tie(x1,y1) = from;
std::tie(x2,y2) = to;
cv::line(mat_, {x1,y1}, {x2,y2}, ToCvColor(c), ToUnderlying(t));
}
void UIOpenCV::Plot(const Edges &edges, Color c, Thickness t){
for(auto e: edges){
Plot(e, c ,t);
}
}
void UIOpenCV::Plot(Vertex v, Color c, Thickness t){
int x,y;
std::tie(x,y) = v;
cv::circle(mat_, {x,y}, 5, ToCvColor(c) ,ToUnderlying(t));
}
void UIOpenCV::Plot(const Vertexes &vertexes, Color c, Thickness t){
for(auto v: vertexes){
Plot(v, c ,t);
}
}
void UIOpenCV::Plot(const Graph &g){
Edges edges;
std::transform(std::begin(g.connections), std::end(g.connections), std::back_inserter(edges),
[&g](auto conn){
return toEdge(g.vertexes, conn);
}
);
Plot(edges, Color::Green, Thickness::Slim);
Plot(g.vertexes, Color::Red, Thickness::Medium);
}
void UIOpenCV::Update(){
cv::imshow(name_, mat_);
}