File: correct_closure.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (114 lines) | stat: -rw-r--r-- 4,094 bytes parent folder | download | duplicates (17)
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
// Boost.Geometry
// Unit Test

// Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.

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

#include <sstream>

#include <geometry_test_common.hpp>

#include <boost/geometry/strategies/strategies.hpp>

#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/geometry/algorithms/correct_closure.hpp>
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/io/wkt/write.hpp>

#include <boost/variant/variant.hpp>


template <typename BaseGeometry, typename Geometry>
void check_geometry(Geometry const& geometry, std::string const& expected)
{
    std::ostringstream out;
    out << bg::wkt_manipulator<Geometry>(geometry, false);
    BOOST_CHECK_EQUAL(out.str(), expected);
}

template <typename Geometry>
void test_geometry(std::string const& wkt, std::string const& expected)
{
    Geometry geometry;
    bg::read_wkt(wkt, geometry);

    // Test tye type
    bg::correct_closure(geometry);
    check_geometry<Geometry>(geometry, expected);

    // Test varianted type
    boost::variant<Geometry> v(geometry);
    bg::correct_closure(v);
    check_geometry<Geometry>(v, expected);
}

template <typename P>
void test_all()
{
    typedef bg::model::ring<P, true, true> cw_closed_ring_type;
    typedef bg::model::ring<P, true, false> cw_open_ring_type;
    typedef bg::model::ring<P, false, true> ccw_closed_ring_type;
    typedef bg::model::ring<P, false, false> ccw_open_ring_type;

    // Define clockwise and counter clockwise polygon
    std::string cw_ring       = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
    std::string cw_open_ring  = "POLYGON((0 0,0 1,1 1,1 0))";

    std::string ccw_ring      = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
    std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";

    // Cases which should be closed or opened
    test_geometry<cw_closed_ring_type>(cw_open_ring, cw_ring);
    test_geometry<cw_open_ring_type>(cw_ring, cw_open_ring);
    test_geometry<ccw_closed_ring_type>(ccw_open_ring, ccw_ring);
    test_geometry<ccw_open_ring_type>(ccw_ring, ccw_open_ring);

    // Cases which are incorrect but should still be closed or opened
    test_geometry<cw_closed_ring_type>(ccw_open_ring, ccw_ring);
    test_geometry<ccw_open_ring_type>(cw_ring, cw_open_ring);

    // Cases where no action is necessary (even if order is incorrect)
    test_geometry<cw_closed_ring_type>(cw_ring, cw_ring);
    test_geometry<cw_closed_ring_type>(ccw_ring, ccw_ring);
    test_geometry<cw_open_ring_type>(cw_open_ring, cw_open_ring);
    test_geometry<cw_open_ring_type>(ccw_open_ring, ccw_open_ring);
    test_geometry<ccw_closed_ring_type>(cw_ring, cw_ring);
    test_geometry<ccw_closed_ring_type>(ccw_ring, ccw_ring);
    test_geometry<ccw_open_ring_type>(cw_open_ring, cw_open_ring);
    test_geometry<ccw_open_ring_type>(ccw_open_ring, ccw_open_ring);

    // Polygon cases
    std::string cw_polygon =
            "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";

    std::string cw_open_polygon =
            "POLYGON((0 0,0 4,4 4,4 0),(1 1,2 1,2 2,1 2))";

    typedef bg::model::polygon<P, true, true> cw_closed_polygon_type;
    typedef bg::model::polygon<P, true, false> cw_open_polygon_type;

    test_geometry<cw_closed_polygon_type>(cw_open_polygon, cw_polygon);
    test_geometry<cw_open_polygon_type>(cw_polygon, cw_open_polygon);

    test_geometry<cw_closed_polygon_type>(cw_polygon, cw_polygon);
    test_geometry<cw_open_polygon_type>(cw_open_polygon, cw_open_polygon);
}


int test_main(int, char* [])
{
    test_all<bg::model::d2::point_xy<int> >();
    test_all<bg::model::d2::point_xy<float> >();
    test_all<bg::model::d2::point_xy<double> >();

    test_all<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
    test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();

    return 0;
}