/
DrawPolygon2.cpp
186 lines (148 loc) · 5.78 KB
/
DrawPolygon2.cpp
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
185
186
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include "MakePolygon.h"
#include <cmath>
#include <vector>
#include <iostream>
#include <boost/foreach.hpp>
#include <vector>
#include <chrono>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::point<float, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
typedef std::pair<box, unsigned> value;
typedef bgi::rtree< value, bgi::rstar<16, 4> > RTREE;
int main()
{
int pmap[WIDTH][HEIGHT];
//fill all member = -1
memset(pmap, -1, sizeof(pmap) );
// polygons
std::vector<polygon> polygons;
std::vector<value> values;
// create some polygons
for ( unsigned i = 0 ; i < 10 ; ++i )
{
// create a polygon
polygon p;
for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
{
int x = 30 * i + int(20 *::cos(a));
int y = 30 * i + int(20 *::sin(a));
p.outer().push_back(point(x, y));
}
// add polygon
polygons.push_back(p);
}
for(int i=0;i<100;i++)
{
polygon p = makePolygon(WIDTH,HEIGHT);
polygons.push_back(p);
}
// display polygons
std::cout << "generated polygons:" << std::endl;
BOOST_FOREACH(polygon const & p, polygons)
std::cout << bg::wkt<polygon>(p) << std::endl;
// fill the spatial index
for ( unsigned i = 0 ; i < polygons.size() ; ++i )
{
// calculate polygon bounding box
box b = bg::return_envelope<box>(polygons[i]);
// insert new value
values.push_back(std::make_pair(b, i));
}
RTREE rtree(values.begin(), values.end());
for (int i = 0; i < WIDTH; i++)
for (int j = 0; j < HEIGHT; j++)
{
if (pmap[i][j] == -1)
{
std::vector<value> returned_values;
point sought = point(i, j);
for ( RTREE::const_query_iterator it = rtree.qbegin(bgi::nearest(sought, 1) ) ; it != rtree.qend() ; ++it )
{
if (!bg::within(sought, polygons[it->second]))
{
break;
}
returned_values.push_back(*it);
}
if (returned_values.size() > 0)
{
int count = 0;
int id = returned_values[0].second;
pmap[i][j] = id;
point pmin = returned_values[0].first.min_corner();
point pmax = returned_values[0].first.max_corner();
std::cout<<pmin.get<0>()<<", "<<pmin.get<1>()<<" "<<pmax.get<0>()<<", "<<pmax.get<1>()
<<std::endl;
// for (int w = pmin.get<0>(); w < pmax.get<0>(); w++)
// for (int h = pmin.get<1>(); h < pmax.get<1>(); h++)
// {
// if (w >= WIDTH || h >= HEIGHT)
// continue;
// if (bg::within(point(w, h), polygons[id]) != 0)
// {
// pmap[w][h] = id;
// }
// }
for (int w = 0; w < WIDTH; w++)
for (int h = 0; h < HEIGHT; h++)
{
if (w >= WIDTH || h >= HEIGHT)
continue;
if (bg::within(point(w, h), polygons[id]) != 0)
{
pmap[w][h] = id;
}
}
}
}
}
//random mau
std::vector<int> colors;
for ( unsigned i = 0 ; i < polygons.size() ; ++i )
{
colors.push_back(i % 3 + 1);
}
//DrawHinh
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
cv::Mat mat(HEIGHT, WIDTH, CV_8UC4);
mat = cv::Scalar(0, 0, 0, 0);
for (int i = 0; i < WIDTH; i++)
for (int j = 0; j < HEIGHT; j++)
{
if (pmap[i][j] != -1) {
int c = colors[pmap[i][j]];
auto color = c == 1 ? RED : c == 2 ? GREEN : BLUE;
line( mat, cv::Point(i, j), cv::Point(i, j), color , 1, 8 );
}
}
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
float duration = (float)(std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count()) / 1000;
std::cout << "duration DrawPolygon: " << duration << std::endl;
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
imwrite("DrawPolygon2.png", mat, compression_params);
// find 5 nearest values to a point
std::vector<value> result_n;
rtree.query(bgi::nearest(point(0, 0), 1), std::back_inserter(result_n));
// note: in Boost.Geometry the WKT representation of a box is polygon
// note: the values store the bounding boxes of polygons
// the polygons aren't used for querying but are printed
std::cout << "knn query point:" << std::endl;
std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
std::cout << "knn query result:" << std::endl;
BOOST_FOREACH(value const & v, result_n)
std::cout << bg::wkt<polygon>(polygons[v.second]) << std::endl;
return 0;
}