summaryrefslogtreecommitdiff
path: root/libs/geometry/test/to_svg.hpp
blob: 026e5f92970eb36cc4b6162e82a8dd21976d4903 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.

// This file was modified by Oracle on 2014.
// Modifications copyright (c) 2014 Oracle and/or its affiliates.

// 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)

// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

#ifndef BOOST_GEOMETRY_TEST_TO_SVG_HPP
#define BOOST_GEOMETRY_TEST_TO_SVG_HPP

#include <fstream>

#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#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/foreach.hpp>

#include <boost/geometry/algorithms/detail/relate/turns.hpp>

template <typename G, typename Turns, typename Mapper>
inline void turns_to_svg(Turns const& turns, Mapper & mapper, bool /*enrich*/ = false)
{
    namespace bg = boost::geometry;

    // turn points in orange, + enrichment/traversal info
    typedef typename bg::coordinate_type<G>::type coordinate_type;
    typedef typename boost::range_value<Turns>::type turn_info;

    // 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;

    BOOST_FOREACH(turn_info const& turn, turns)
    {
        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(
                    boost::numeric_cast<int>(half
                        + ten * bg::get<0>(turn.point)),
                    boost::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;
        }

        //if (! turn.discarded && ! turn.blocked() && ! turn.both(bg::detail::overlay::operation_union))
        //if (! turn.discarded)
        {
            std::ostringstream out;
            out << index
                << ": " << bg::method_char(turn.method);

            if ( turn.discarded )
                out << " (discarded)\n";
            else if ( turn.blocked() )
                out << " (blocked)\n";
            else
                out << '\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;

            /*if ( enrich )
            {
            out << ", ";
            if (turn.operations[0].enriched.next_ip_index != -1)
            {
            out << "ip: " << turn.operations[0].enriched.next_ip_index;
            }
            else
            {
            out << "vx: " << turn.operations[0].enriched.travels_to_vertex_index
            << " -> ip: "  << turn.operations[0].enriched.travels_to_ip_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;

            /*if ( enrich )
            {
                out << ", ";
                if (turn.operations[1].enriched.next_ip_index != -1)
                {
                    out << "ip: " << turn.operations[1].enriched.next_ip_index;
                }
                else
                {
                    out << "vx: " << turn.operations[1].enriched.travels_to_vertex_index
                        << " -> ip: " << turn.operations[1].enriched.travels_to_ip_index;
                }
            }*/

            //out << std::endl;

            /*out

                << std::setprecision(3)
                << "dist: " << boost::numeric_cast<double>(turn.operations[0].enriched.distance)
                << " / "  << boost::numeric_cast<double>(turn.operations[1].enriched.distance)
                << std::endl
                << "vis: " << bg::visited_char(turn.operations[0].visited)
                << " / "  << bg::visited_char(turn.operations[1].visited);
            */

            /*
                out << index
                    << ": " << bg::operation_char(turn.operations[0].operation)
                    << " " << bg::operation_char(turn.operations[1].operation)
                    << " (" << bg::method_char(turn.method) << ")"
                    << (turn.ignore() ? " (ignore) " : " ")
                    << std::endl

                    << "ip: " << turn.operations[0].enriched.travels_to_ip_index
                    << "/"  << turn.operations[1].enriched.travels_to_ip_index;

                if (turn.operations[0].enriched.next_ip_index != -1
                    || turn.operations[1].enriched.next_ip_index != -1)
                {
                    out << " [" << turn.operations[0].enriched.next_ip_index
                        << "/"  << turn.operations[1].enriched.next_ip_index
                        << "]"
                        ;
                }
                out << std::endl;


                out
                    << "vx:" << turn.operations[0].enriched.travels_to_vertex_index
                    << "/"  << turn.operations[1].enriched.travels_to_vertex_index
                    << std::endl

                    << std::setprecision(3)
                    << "dist: " << turn.operations[0].enriched.distance
                    << " / "  << turn.operations[1].enriched.distance
                    << std::endl
                    */



                offsets[p] += lineheight;
                int offset = offsets[p];
                offsets[p] += lineheight * 3;
                mapper.text(turn.point, out.str(), style, margin, offset, lineheight);
            }
            index++;
        }
    }
}

template <typename G1, typename P>
inline void geom_to_svg(G1 const& g1,
                        boost::geometry::svg_mapper<P> & mapper)
{
    mapper.add(g1);

    mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
        "stroke:rgb(153,204,0);stroke-width:3");
}

template <typename G1, typename G2, typename P>
inline void geom_to_svg(G1 const& g1, G2 const& g2,
                        boost::geometry::svg_mapper<P> & mapper)
{
    mapper.add(g1);
    mapper.add(g2);

    mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
        "stroke:rgb(153,204,0);stroke-width:3");
    mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
        "stroke:rgb(51,51,153);stroke-width:3");
}

template <typename G1>
inline void geom_to_svg(G1 const& g1, std::string const& filename)
{
    namespace bg = boost::geometry;
    typedef typename bg::point_type<G1>::type mapper_point_type;

    std::ofstream svg(filename.c_str(), std::ios::trunc);
    bg::svg_mapper<mapper_point_type> mapper(svg, 500, 500);

    geom_to_svg(g1, mapper);
}

template <typename G1, typename G2>
inline void geom_to_svg(G1 const& g1, G2 const& g2, std::string const& filename)
{
    namespace bg = boost::geometry;
    typedef typename bg::point_type<G1>::type mapper_point_type;

    std::ofstream svg(filename.c_str(), std::ios::trunc);
    bg::svg_mapper<mapper_point_type> mapper(svg, 500, 500);

    geom_to_svg(g1, g2, mapper);
}

template <typename G1>
inline void geom_to_svg(std::string const& wkt1, std::string const& filename)
{
    namespace bg = boost::geometry;

    G1 g1;
    bg::read_wkt(wkt1, g1);
    geom_to_svg(g1, filename);
}

template <typename G1, typename G2>
inline void geom_to_svg(std::string const& wkt1, std::string const& wkt2, std::string const& filename)
{
    namespace bg = boost::geometry;

    G1 g1;
    G2 g2;
    bg::read_wkt(wkt1, g1);
    bg::read_wkt(wkt2, g2);
    geom_to_svg(g1, g2, filename);
}

struct to_svg_assign_policy
    : boost::geometry::detail::overlay::assign_null_policy
{
    static bool const include_no_turn = false;
    static bool const include_degenerate = false;
    static bool const include_opposite = false;
};

template <typename G>
inline void to_svg(G const& g, std::string const& filename, bool /*sort*/ = true)
{
    namespace bg = boost::geometry;

    typedef typename bg::point_type<G>::type P;

    std::ofstream svg(filename.c_str(), std::ios::trunc);

    bg::svg_mapper<P> mapper(svg, 500, 500);

    mapper.add(g);

    mapper.map(g, "fill-opacity:0.5;fill:rgb(153,204,0);"
        "stroke:rgb(153,204,0);stroke-width:3");

    // GET TURNS

    typedef bg::segment_ratio<double> sr;
    typedef bg::detail::overlay::traversal_turn_info<P, sr> turn_info;
    typedef bg::detail::overlay::assign_null_policy AssignPolicy;
    //typedef to_svg_assign_policy AssignPolicy;    

    typedef std::deque<turn_info> Turns;
    typedef bg::detail::self_get_turn_points::no_interrupt_policy InterruptPolicy;

    Turns turns;
    InterruptPolicy interrupt_policy;

    typedef bg::detail::overlay::get_turn_info<AssignPolicy> TurnPolicy;

    bg::detail::self_get_turn_points::get_turns
        <
            TurnPolicy
        >::apply(g, bg::detail::no_rescale_policy(), turns, interrupt_policy);

    turns_to_svg<G>(turns, mapper);
}

template <typename G1, typename G2>
inline void to_svg(G1 const& g1, G2 const& g2, std::string const& filename, bool sort = true, bool use_old_turns_policy = false, bool enrich = false)
{
    namespace bg = boost::geometry;

    typedef typename bg::point_type<G1>::type mapper_point_type;

    std::ofstream svg(filename.c_str(), std::ios::trunc);

    bg::svg_mapper<mapper_point_type> mapper(svg, 500, 500);

    mapper.add(g1);
    mapper.add(g2);

    mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
        "stroke:rgb(153,204,0);stroke-width:3");
    mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
        "stroke:rgb(51,51,153);stroke-width:3");

    // GET TURNS
    
    typedef typename bg::detail::relate::turns::get_turns<G1, G2>::turn_info turn_info;
    //typedef bg::detail::overlay::traversal_turn_info<P1> turn_info;
    //typedef bg::detail::overlay::assign_null_policy AssignPolicy;
    typedef to_svg_assign_policy AssignPolicy;

    typedef std::deque<turn_info> Turns;
    typedef bg::detail::get_turns::no_interrupt_policy InterruptPolicy;
    static const bool Reverse1 = bg::detail::overlay::do_reverse<bg::point_order<G1>::value>::value;
    static const bool Reverse2 = bg::detail::overlay::do_reverse<bg::point_order<G2>::value>::value;

    Turns turns;
    InterruptPolicy interrupt_policy;

    if ( use_old_turns_policy )
    {
        boost::geometry::get_turns
            <
                Reverse1, Reverse2, AssignPolicy
            >(g1, g2, bg::detail::no_rescale_policy(), turns, interrupt_policy);
    }
    else
    {
        typedef bg::detail::get_turns::get_turn_info_type
            <
                G1, G2, AssignPolicy
            > TurnPolicy;

        bg::detail::relate::turns::get_turns
            <
                G1, G2, TurnPolicy
            >::apply(turns, g1, g2);
    }

    if ( sort )
    {
        typedef bg::detail::relate::turns::less<> less;
        std::sort(boost::begin(turns), boost::end(turns), less());
    }

    /*if ( enrich )
    {
        typedef typename bg::strategy::side::services::default_strategy
            <
                typename bg::cs_tag<G1>::type
            >::type side_strategy_type;

        bg::enrich_intersection_points<bg::detail::overlay::do_reverse<bg::point_order<G1>::value>::value,
                                       bg::detail::overlay::do_reverse<bg::point_order<G2>::value>::value>
                (turns, bg::detail::overlay::operation_union,
                 g1, g1,
                 bg::detail::no_rescale_policy(),
                 side_strategy_type());
    }*/

    turns_to_svg<G1>(turns, mapper, enrich);
}

template <typename G>
inline void to_svg(std::string const& wkt, std::string const& filename)
{
    G g;
    boost::geometry::read_wkt(wkt, g);
    to_svg(g, filename);
}

template <typename G1, typename G2>
inline void to_svg(std::string const& wkt1, std::string const& wkt2, std::string const& filename, bool sort = true, bool reverse_by_geometry_id = false, bool enrich = false)
{
    G1 g1;
    G2 g2;
    boost::geometry::read_wkt(wkt1, g1);
    boost::geometry::read_wkt(wkt2, g2);
    to_svg(g1, g2, filename, sort, reverse_by_geometry_id, enrich);
}

#endif // BOOST_GEOMETRY_TEST_TO_SVG_HPP