-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_util.cc
More file actions
164 lines (129 loc) · 4.12 KB
/
image_util.cc
File metadata and controls
164 lines (129 loc) · 4.12 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
#include "image_util.h"
#include <cairo.h>
namespace rtree {
namespace util {
void print_tree(cairo_t* cr, const StaticRTreeData<2, 3>& rtree) {
auto set_color_by_level = [cr](auto level) {
switch(level%3) {
case 0:
cairo_set_source_rgba(cr, 0, 0, 0, 255);
break;
case 1:
cairo_set_source_rgba(cr, 255, 0, 0, 255);
break;
case 2:
cairo_set_source_rgba(cr, 0, 255, 0, 255);
break;
}
};
auto level = 0;
set_color_by_level(level);
cairo_set_line_width(cr, 1);
std::vector<int> tmp_rects;
tmp_rects.emplace_back(rtree.root_rect_offset_);
while(!tmp_rects.empty()) {
std::vector<int> next_rects;
for(auto r : tmp_rects) {
auto& box = rtree.rects_[r].box_;
cairo_rectangle (cr, box.min_[0], box.min_[1],
box.max_[0] - box.min_[0], box.max_[1] - box.min_[1]);
cairo_stroke (cr);
cairo_save(cr);
cairo_set_font_size(cr, 30);
cairo_move_to(cr, box.min_[0], box.min_[1]);
std::string rect_no = std::to_string(r);
cairo_show_text(cr, rect_no.c_str());
cairo_restore(cr);
if (rtree.rects_[r].child_ < 0)
continue;
auto& node = rtree.nodes_[rtree.rects_[r].child_];
for(auto i=0; i<node.size_; ++i) {
next_rects.emplace_back(node.rects_[i]);
}
}
tmp_rects = std::move(next_rects);
++level;
set_color_by_level(level);
}
}
void print_query(cairo_t* cr, const Box& query) {
cairo_set_source_rgba(cr, 255, 0, 0, 255);
cairo_set_line_width(cr, 2);
cairo_rectangle (cr, query.min_[0], query.min_[1],
query.max_[0] - query.min_[0], query.max_[1] - query.min_[1]);
cairo_stroke (cr);
cairo_save(cr);
cairo_set_font_size(cr, 30);
cairo_move_to(cr, query.min_[0], query.min_[1]);
cairo_show_text(cr, "Q");
cairo_restore(cr);
}
void print_as_image(const std::string& filename, const StaticRTreeData<2, 3>& rtree) {
cairo_surface_t *surface;
cairo_t *cr;
cairo_status_t status;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 1000);
status = cairo_surface_status(surface);
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
return;
}
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 255, 255, 255);
cairo_paint(cr);
print_tree(cr, rtree);
status = cairo_surface_write_to_png(surface, filename.c_str());
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
}
cairo_destroy(cr);
}
void print_as_image_with_query(const std::string& filename, const StaticRTreeData<2, 3>& rtree, const Box& query) {
cairo_surface_t *surface;
cairo_t *cr;
cairo_status_t status;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 1000);
status = cairo_surface_status(surface);
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
return;
}
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 255, 255, 255);
cairo_paint(cr);
print_tree(cr, rtree);
print_query(cr, query);
status = cairo_surface_write_to_png(surface, filename.c_str());
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
}
cairo_destroy(cr);
}
void print_as_image_with_query_point(const std::string& filename, const StaticRTreeData<2, 3>& rtree, const Point& p) {
cairo_surface_t *surface;
cairo_t *cr;
cairo_status_t status;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 1000);
status = cairo_surface_status(surface);
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
return;
}
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 255, 255, 255);
cairo_paint(cr);
print_tree(cr, rtree);
Box box;
for(int i=0; i<2; ++i) {
box.max_[i] = p.value_[i];
box.min_[i] = p.value_[i];
}
print_query(cr, box);
status = cairo_surface_write_to_png(surface, filename.c_str());
if (status != CAIRO_STATUS_SUCCESS) {
std::cout << cairo_status_to_string(status) << std::endl;
}
cairo_destroy(cr);
}
}
}