-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathto_svg.hpp
208 lines (171 loc) · 7.37 KB
/
to_svg.hpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// This file was modified by Oracle on 2014-2021.
// Modifications copyright (c) 2014-2021 Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_GEOMETRY_TEST_TO_SVG_HPP
#define BOOST_GEOMETRY_TEST_TO_SVG_HPP
#include <fstream>
#include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
#include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
#include <boost/geometry/algorithms/detail/overlay/traversal_info.hpp>
#include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
#include <boost/geometry/algorithms/detail/overlay/enrichment_info.hpp>
#include <boost/geometry/algorithms/detail/overlay/enrich_intersection_points.hpp>
#include <boost/geometry/algorithms/detail/relate/turns.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#include <boost/geometry/io/wkt/read.hpp>
#include <string_from_type.hpp>
template <typename Turns, typename Mapper>
inline void turns_to_svg(Turns const& turns, Mapper& mapper)
{
namespace bg = boost::geometry;
// Simple map to avoid two texts at same place (note that can still overlap!)
std::map<std::pair<int, int>, int> offsets;
int index = 0;
int const margin = 5;
for (auto const& turn : turns)
{
using coordinate_type
= typename bg::coordinate_type<decltype(turn.point)>::type;
int lineheight = 10;
mapper.map(turn.point, "fill:rgb(255,128,0);"
"stroke:rgb(0,0,0);stroke-width:1", 3);
{
coordinate_type half = 0.5;
coordinate_type ten = 10;
// Map characteristics
// Create a rounded off point
std::pair<int, int> p
= std::make_pair(
util::numeric_cast<int>(half
+ ten * bg::get<0>(turn.point)),
util::numeric_cast<int>(half
+ ten * bg::get<1>(turn.point))
);
std::string style = "fill:rgb(0,0,0);font-family:Arial;font-size:12px";
if (turn.discarded)
{
style = "fill:rgb(92,92,92);font-family:Arial;font-size:10px";
lineheight = 6;
}
{
std::ostringstream out;
out << index
<< ": " << bg::method_char(turn.method)
<< (turn.discarded ? " (discarded)" : "")
<< (turn.blocked() ? " (blocked)" : "")
<< '\n';
out << bg::operation_char(turn.operations[0].operation)
<<": seg: " << turn.operations[0].seg_id.source_index
<< ' ' << turn.operations[0].seg_id.multi_index
<< ' ' << turn.operations[0].seg_id.ring_index
<< ' ' << turn.operations[0].seg_id.segment_index << ", ";
out << "other: " << turn.operations[1].seg_id.source_index
<< ' ' << turn.operations[1].seg_id.multi_index
<< ' ' << turn.operations[1].seg_id.ring_index
<< ' ' << turn.operations[1].seg_id.segment_index;
out << '\n';
out << bg::operation_char(turn.operations[1].operation)
<< ": seg: " << turn.operations[1].seg_id.source_index
<< ' ' << turn.operations[1].seg_id.multi_index
<< ' ' << turn.operations[1].seg_id.ring_index
<< ' ' << turn.operations[1].seg_id.segment_index << ", ";
out << "other: " << turn.operations[0].seg_id.source_index
<< ' ' << turn.operations[0].seg_id.multi_index
<< ' ' << turn.operations[0].seg_id.ring_index
<< ' ' << turn.operations[0].seg_id.segment_index;
offsets[p] += lineheight;
int offset = offsets[p];
offsets[p] += lineheight * 3;
mapper.text(turn.point, out.str(), style, margin, offset, lineheight);
}
index++;
}
}
}
struct to_svg_assign_policy
{
static bool const include_no_turn = false;
static bool const include_degenerate = false;
static bool const include_opposite = false;
static bool const include_start_turn = false;
};
template <typename G1, typename G2, typename G3>
inline void to_svg(G1 const& g1, G2 const& g2, G3 const& g3,
std::string const& caseid)
{
namespace bg = boost::geometry;
using point_type = typename bg::point_type<G1>::type;
using coordinate_type = typename bg::coordinate_type<point_type>::type;
std::ostringstream filename;
filename << "case_"
<< caseid << "_"
<< string_from_type<coordinate_type>::name()
<< ".svg";
std::ofstream svg(filename.str());
bg::svg_mapper<point_type> mapper(svg, 500, 500);
mapper.add(g1);
mapper.add(g2);
mapper.add(g3);
std::string const green = "rgb(153,204,0)";
std::string const blue = "rgb(51,51,153)";
std::string const red = "rgb(255,0,0)";
if (bg::util::is_linestring<G1>::value)
{
auto style = [](auto const fo, auto const& c)
{ return "stroke:" + c + ";stroke-width:3;stroke-opacity:"
+ std::to_string(fo) + ";"; };
mapper.map(g1, style(0.5, green));
mapper.map(g2, style(0.3, blue));
mapper.map(g3, style(0.5, red));
}
else
{
auto style = [](auto const fo, auto const& c)
{ return "fill-opacity:" + std::to_string(fo) + ";fill:" + c
+ ";stroke:rgb(153,204,0);stroke-width:1;stroke-opacity:0.7;"; };
mapper.map(g1, style(0.5, green));
mapper.map(g2, style(0.3, blue));
mapper.map(g3, style(0.5, red));
}
// GET TURNS
using strategy_type
= typename bg::strategies::relate::services::default_strategy
<G1, G2>::type;
using ratio_type = bg::segment_ratio<coordinate_type>;
using turn_type = bg::detail::overlay::turn_info
<
point_type,
ratio_type,
bg::detail::overlay::turn_operation_linear<point_type, ratio_type>
>;
strategy_type strategy;
std::deque<turn_type> turns;
bg::detail::get_turns::no_interrupt_policy interrupt_policy;
using turn_policy = bg::detail::get_turns::get_turn_info_type
<
G1, G2, to_svg_assign_policy
>;
bg::detail::relate::turns::get_turns
<
G1, G2, turn_policy
>::apply(turns, g1, g2, interrupt_policy, strategy);
{
using less = bg::detail::relate::turns::less
<
0,
bg::detail::relate::turns::less_op_xxx_linear
<
0, bg::detail::relate::turns::op_to_int<>
>,
strategy_type
>;
std::sort(boost::begin(turns), boost::end(turns), less());
}
turns_to_svg(turns, mapper);
}
#endif // BOOST_GEOMETRY_TEST_TO_SVG_HPP