-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_renderer.cpp
More file actions
193 lines (161 loc) · 6.42 KB
/
Copy pathmap_renderer.cpp
File metadata and controls
193 lines (161 loc) · 6.42 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
185
186
187
188
189
190
191
192
193
#include <algorithm>
#include <iterator>
#include "map_renderer.h"
using namespace std;
namespace render {
// ---------- Utilities ------------------
bool IsZero(double value) {
return std::abs(value) < EPSILON;
}
svg::Point MakePoint(double x, double y) {
return { x, y };
}
svg::Color MakeColor(const std::string& color) {
return color;
}
svg::Color MakeColor(int r, int g, int b) {
return svg::Rgb(r, g, b);
}
svg::Color MakeColor(int r, int g, int b, double a) {
return svg::Rgba(r, g, b, a);
}
// ---------- SphereProjector ------------------
SphereProjector::SphereProjector(const tg::detail::Coordinates& left_top,
const tg::detail::Coordinates& right_bottom,
double width, double height, double padding)
: padding_(padding) {
const double min_lat = left_top.lat;
max_lat_ = right_bottom.lat;
min_lon_ = left_top.lng;
const double max_lon = right_bottom.lng;
const double width_zoom = (!IsZero(max_lon - min_lon_)) ? (width - 2 * padding_) / (max_lon - min_lon_) : 0;
const double height_zoom = (!IsZero(max_lat_ - min_lat)) ? (height - 2 * padding_) / (max_lat_ - min_lat) : 0;
zoom_coef_ = (!IsZero(width_zoom) && !IsZero(height_zoom)) ? min(width_zoom, height_zoom) : max(width_zoom, height_zoom);
}
svg::Point SphereProjector::operator()(const tg::detail::Coordinates& coords) const {
return { (coords.lng - min_lon_) * zoom_coef_ + padding_,
(max_lat_ - coords.lat) * zoom_coef_ + padding_ };
}
// ---------- MapRenderer ------------------
svg::Document MapRenderer::GetDocument() const {
return document_;
}
void MapRenderer::SetSettings(const Settings& settings) {
settings_ = settings;
}
Settings MapRenderer::GetSettings() const {
return settings_;
}
void MapRenderer::SetBorder(const Stops& stops) {
optional<double> min_lat, max_lat, min_lng, max_lng;
for (const auto& stop : stops) {
const double lat = stop->coordinates.lat;
const double lng = stop->coordinates.lng;
min_lat = (min_lat) ? min(*min_lat, lat) : lat;
max_lat = (max_lat) ? max(*max_lat, lat) : lat;
min_lng = (min_lng) ? min(*min_lng, lng) : lng;
max_lng = (max_lng) ? max(*max_lng, lng) : lng;
}
if (!min_lat) {
min_lat = max_lat = min_lng = max_lng = 0;
}
sphere_projector_ = SphereProjector({ min_lat.value(), min_lng.value() }, { max_lat.value(), max_lng.value() }, settings_.width, settings_.height, settings_.padding);
}
void MapRenderer::SetBusRoute(const Buses& buses) {
for (const auto& bus : buses) {
RenderBusRoute(*bus);
}
index_color_ = 0;
for (const auto& bus : buses) {
RenderBusRouteName(*bus);
}
index_color_ = 0;
}
void MapRenderer::SetStation(const Stops& stops) {
for (const auto& stop : stops) {
RenderStation(*stop);
}
for (const auto& stop : stops) {
RenderStationName(*stop);
}
}
void MapRenderer::RenderBusRoute(const Bus& bus) {
using namespace svg;
document_.Add(CreateBusRoute(bus)
.SetFillColor(NoneColor)
.SetStrokeColor(GetColor())
.SetStrokeWidth(settings_.line_width)
.SetStrokeLineCap(StrokeLineCap::ROUND)
.SetStrokeLineJoin(StrokeLineJoin::ROUND));
}
void MapRenderer::RenderBusRouteName(const Bus& bus) {
using namespace svg;
const auto& point_begin = GetPoint(bus.stops.front()->coordinates);
Text text = Text().SetFillColor(GetColor())
.SetPosition(point_begin)
.SetOffset(settings_.bus_label_offset)
.SetFontSize(settings_.bus_label_font_size)
.SetFontFamily("Verdana"s)
.SetFontWeight("bold"s)
.SetData(bus.name);
Text underlayer = text;
underlayer.SetFillColor(settings_.underlayer_color)
.SetStrokeColor(settings_.underlayer_color)
.SetStrokeWidth(settings_.underlayer_width)
.SetStrokeLineCap(StrokeLineCap::ROUND)
.SetStrokeLineJoin(StrokeLineJoin::ROUND);
document_.Add(underlayer);
document_.Add(text);
if (!bus.isCircle) {
const auto& point_end = GetPoint(bus.stops[bus.stops.size()/2]->coordinates);
if (((point_begin.x != point_end.x) && (point_begin.y != point_end.y))) {
text.SetPosition(point_end);
underlayer.SetPosition(point_end);
document_.Add(underlayer);
document_.Add(text);
}
}
}
void MapRenderer::RenderStation(const Stop& stop) {
using namespace svg;
document_.Add(Circle().SetCenter(GetPoint(stop.coordinates))
.SetRadius(settings_.stop_radius)
.SetFillColor("white"s));
}
void MapRenderer::RenderStationName(const Stop& stop) {
using namespace svg;
Text text = Text().SetFillColor("black"s)
.SetPosition(GetPoint(stop.coordinates))
.SetOffset(settings_.stop_label_offset)
.SetFontSize(settings_.stop_label_font_size)
.SetFontFamily("Verdana"s)
.SetData(stop.name);
Text underlayer = text;
underlayer.SetFillColor(settings_.underlayer_color)
.SetStrokeColor(settings_.underlayer_color)
.SetStrokeWidth(settings_.underlayer_width)
.SetStrokeLineCap(StrokeLineCap::ROUND)
.SetStrokeLineJoin(StrokeLineJoin::ROUND);
document_.Add(underlayer);
document_.Add(text);
}
svg::Polyline MapRenderer::CreateBusRoute(const Bus& bus) const {
using namespace svg;
svg::Polyline line;
const auto& stops = bus.stops;
for (auto it = stops.begin(); it != stops.end(); ++it) {
const auto& point = GetPoint((*it)->coordinates);
line.AddPoint(point);
}
return line;
}
svg::Color MapRenderer::GetColor() {
return settings_.color_palette.at(GetIndexColor());
}
svg::Point MapRenderer::GetPoint(const tg::detail::Coordinates& coords) const {
return sphere_projector_(coords);
}
size_t MapRenderer::GetIndexColor() {
return index_color_++ % settings_.color_palette.size();
}
}