-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimitives.cpp
More file actions
184 lines (154 loc) · 3.66 KB
/
Copy pathprimitives.cpp
File metadata and controls
184 lines (154 loc) · 3.66 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "random.hpp"
#include "primitives.hpp"
/**
* Draw functions
*/
Draw::Draw (const Draw &d) {
depth = d.depth;
}
Draw::Draw (int d) {
depth = d;
}
Draw::~Draw(){
for(Draw *d: drawings){
delete d;
}
drawings.clear();
}
Draw* Draw::clone() const {
auto d = new Draw(*this);
for(auto const &c: this->drawings){
d->drawings.push_back(c->clone());
}
return d;
}
void Draw::print(std::ostream& out) const {
out << "Draw(";
for(unsigned i=0; i<drawings.size();i++){
out << drawings[i];
if(i<drawings.size()-1){
out << ",";
}
}
out << ")";
};
void Draw::draw(cv::Mat const& img) const {
for(auto const& c: drawings){
c->draw(img);
}
}
bool Draw::equals(const Draw &d) const {
if (depth != d.depth) return false;
if (drawings.size() != d.drawings.size()) return false;
for(unsigned i=0; i<drawings.size(); i++){
if(drawings[i] != d.drawings[i]) return false;
}
return true;
}
std::vector<Draw*> Draw::generate(int const& maxDepth, int const& w, int const& h){
R rand = R(w, h);
std::vector<Draw*> nodes;
auto root = new Draw(0);
nodes.push_back(root);
std::list<Draw*> L;
L.push_back(root);
while(!L.empty()){
auto curr = L.front();
L.pop_front();
if(curr->depth == maxDepth || rand.runif() > 0.5) {
for(int i=0; i < MAX_PRIMITIVES; i++){
auto child = new Line(
cv::Point(rand.x(), rand.y()),
cv::Point(rand.x(), rand.y()),
cv::Scalar(rand.gray()),
rand.thickness()
// 1
);
curr->drawings.push_back(child);
nodes.push_back(child);
}
} else {
for(int i=0; i < MAX_DRAWS; i++){
auto child = new Draw(curr->depth+1);
curr->drawings.push_back(child);
nodes.push_back(child);
L.push_back(child);
}
}
}
return nodes;
}
/**
* Line funcions
*/
Line::Line(const Line &l) : Draw (0) {
p1 = l.p1;
p2 = l.p2;
color = l.color;
thickness = l.thickness;
}
Line::Line (cv::Point const& p1, cv::Point const& p2, cv::Scalar const& color, int const& thickness)
: Draw (0) {
this->p1 = p1;
this->p2 = p2;
this->color = color;
this->thickness = thickness;
}
Line* Line::clone() const {
return new Line(*this);
}
void Line::print(std::ostream& out) const {
out << "Line( " << p1 << "," << p2 << "," << color << " )";
}
void Line::draw(cv::Mat const& img) const {
// cv::Mat overlay (img.size[0], img.size[1], CV_8UC1, cv::Scalar(255));
// cv::Mat overlay = cv::Mat::ones (img.size[0], img.size[1], CV_8UC1);
// cv::Mat overlay;
// img.copyTo(overlay);
// double opacity = 0.5;
// cv::line(overlay, p1, p2, color, thickness, CV_AA);
// cv::addWeighted(overlay, opacity, img, 1 - opacity, 0, img);
cv::line(img, p1, p2, color, thickness, CV_AA);
}
bool Line::equals(const Draw &d) const {
if (auto const& l = dynamic_cast<const Line*>(&d)){
return (p1 == l->p1) && (p2 == l->p2) && (color == l->color);
} else {
return false;
}
}
/**
* Triangle functions
*/
/*
Triangle::Triangle(const Triangle &l)
: Draw(0) {
p1 = l.p1;
p2 = l.p2;
p3 = l.p3;
color = l.color;
}
Triangle::Triangle (cv::Point const& p1, cv::Point const& p2, cv::Point const& p3, cv::Scalar const& color)
: Draw(0) {
this->p1 = p1;
this->p2 = p2;
this->p3 = p3;
this->color = color;
}
Triangle* Triangle::clone() const {
return new Triangle(*this);
}
void Triangle::print(std::ostream& out) const {
out << "Triangle( " << p1 << "," << p2 << "," << p3 << "," << color << " )";
}
// void Triangle::draw(cv::Mat const& img) const {
// cv::line(img, p1, p2, color, thickness, CV_AA);
// }
bool Triangle::equals(const Draw &d) const {
if (auto const& l = dynamic_cast<const Triangle*>(&d)){
return (p1 == l->p1) && (p2 == l->p2) && (p3 == l->p3) && (color == l->color);
} else {
return false;
}
}
*/