1 // Boost.Geometry
2 // Unit Test
3 
4 // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
5 
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <sstream>
11 
12 #include <geometry_test_common.hpp>
13 
14 #include <boost/geometry/strategies/strategies.hpp>
15 
16 #include <boost/geometry/geometries/point_xy.hpp>
17 #include <boost/geometry/geometries/ring.hpp>
18 #include <boost/geometry/geometries/polygon.hpp>
19 
20 #include <boost/geometry/algorithms/correct_closure.hpp>
21 #include <boost/geometry/io/wkt/read.hpp>
22 #include <boost/geometry/io/wkt/write.hpp>
23 
24 #include <boost/variant/variant.hpp>
25 
26 
27 template <typename BaseGeometry, typename Geometry>
check_geometry(Geometry const & geometry,std::string const & expected)28 void check_geometry(Geometry const& geometry, std::string const& expected)
29 {
30     std::ostringstream out;
31     out << bg::wkt_manipulator<Geometry>(geometry, false);
32     BOOST_CHECK_EQUAL(out.str(), expected);
33 }
34 
35 template <typename Geometry>
test_geometry(std::string const & wkt,std::string const & expected)36 void test_geometry(std::string const& wkt, std::string const& expected)
37 {
38     Geometry geometry;
39     bg::read_wkt(wkt, geometry);
40 
41     // Test tye type
42     bg::correct_closure(geometry);
43     check_geometry<Geometry>(geometry, expected);
44 
45     // Test varianted type
46     boost::variant<Geometry> v(geometry);
47     bg::correct_closure(v);
48     check_geometry<Geometry>(v, expected);
49 }
50 
51 template <typename P>
test_all()52 void test_all()
53 {
54     typedef bg::model::ring<P, true, true> cw_closed_ring_type;
55     typedef bg::model::ring<P, true, false> cw_open_ring_type;
56     typedef bg::model::ring<P, false, true> ccw_closed_ring_type;
57     typedef bg::model::ring<P, false, false> ccw_open_ring_type;
58 
59     // Define clockwise and counter clockwise polygon
60     std::string cw_ring       = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
61     std::string cw_open_ring  = "POLYGON((0 0,0 1,1 1,1 0))";
62 
63     std::string ccw_ring      = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
64     std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";
65 
66     // Cases which should be closed or opened
67     test_geometry<cw_closed_ring_type>(cw_open_ring, cw_ring);
68     test_geometry<cw_open_ring_type>(cw_ring, cw_open_ring);
69     test_geometry<ccw_closed_ring_type>(ccw_open_ring, ccw_ring);
70     test_geometry<ccw_open_ring_type>(ccw_ring, ccw_open_ring);
71 
72     // Cases which are incorrect but should still be closed or opened
73     test_geometry<cw_closed_ring_type>(ccw_open_ring, ccw_ring);
74     test_geometry<ccw_open_ring_type>(cw_ring, cw_open_ring);
75 
76     // Cases where no action is necessary (even if order is incorrect)
77     test_geometry<cw_closed_ring_type>(cw_ring, cw_ring);
78     test_geometry<cw_closed_ring_type>(ccw_ring, ccw_ring);
79     test_geometry<cw_open_ring_type>(cw_open_ring, cw_open_ring);
80     test_geometry<cw_open_ring_type>(ccw_open_ring, ccw_open_ring);
81     test_geometry<ccw_closed_ring_type>(cw_ring, cw_ring);
82     test_geometry<ccw_closed_ring_type>(ccw_ring, ccw_ring);
83     test_geometry<ccw_open_ring_type>(cw_open_ring, cw_open_ring);
84     test_geometry<ccw_open_ring_type>(ccw_open_ring, ccw_open_ring);
85 
86     // Polygon cases
87     std::string cw_polygon =
88             "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
89 
90     std::string cw_open_polygon =
91             "POLYGON((0 0,0 4,4 4,4 0),(1 1,2 1,2 2,1 2))";
92 
93     typedef bg::model::polygon<P, true, true> cw_closed_polygon_type;
94     typedef bg::model::polygon<P, true, false> cw_open_polygon_type;
95 
96     test_geometry<cw_closed_polygon_type>(cw_open_polygon, cw_polygon);
97     test_geometry<cw_open_polygon_type>(cw_polygon, cw_open_polygon);
98 
99     test_geometry<cw_closed_polygon_type>(cw_polygon, cw_polygon);
100     test_geometry<cw_open_polygon_type>(cw_open_polygon, cw_open_polygon);
101 }
102 
103 
test_main(int,char * [])104 int test_main(int, char* [])
105 {
106     test_all<bg::model::d2::point_xy<int> >();
107     test_all<bg::model::d2::point_xy<float> >();
108     test_all<bg::model::d2::point_xy<double> >();
109 
110     test_all<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
111     test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
112 
113     return 0;
114 }
115