4 // Copyright (c) 2019, Oracle and/or its affiliates.
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
12 #include <geometry_test_common.hpp>
14 #include <boost/geometry/algorithms/detail/calculate_point_order.hpp>
16 #include <boost/geometry/geometries/point.hpp>
17 #include <boost/geometry/geometries/ring.hpp>
19 #include <boost/geometry/io/wkt/wkt.hpp>
21 #include <boost/geometry/strategies/cartesian/point_order.hpp>
22 #include <boost/geometry/strategies/geographic/point_order.hpp>
23 #include <boost/geometry/strategies/spherical/point_order.hpp>
26 inline const char * order_str(bg::order_selector o)
28 if (o == bg::clockwise)
30 else if (o == bg::counterclockwise)
31 return "counterclockwise";
33 return "order_undetermined";
36 inline const char * cs_str(bg::cartesian_tag)
41 inline const char * cs_str(bg::spherical_equatorial_tag)
43 return "spherical_equatorial";
46 inline const char * cs_str(bg::geographic_tag)
51 template <typename Ring>
52 inline void test_one(Ring const& ring, bg::order_selector expected)
54 typedef typename bg::cs_tag<Ring>::type cs_tag;
56 bg::order_selector result = bg::detail::calculate_point_order(ring);
58 BOOST_CHECK_MESSAGE(result == expected, "Expected: " << order_str(expected) << " for " << bg::wkt(ring) << " in " << cs_str(cs_tag()));
60 if (expected != bg::order_undetermined && result != bg::order_undetermined)
64 std::reverse(boost::begin(ring_rev), boost::end(ring_rev));
66 bg::order_selector result_rev = bg::detail::calculate_point_order(ring_rev);
68 BOOST_CHECK_MESSAGE(result != result_rev, "Invalid order of reversed: " << bg::wkt(ring) << " in " << cs_str(cs_tag()));
73 inline void test_one(std::string const& ring_wkt, bg::order_selector expected)
75 //typedef typename bg::cs_tag<P>::type cs_tag;
77 bg::model::ring<P> ring;
78 bg::read_wkt(ring_wkt, ring);
80 std::size_t n = boost::size(ring);
81 for (size_t i = 1; i < n; ++i)
83 test_one(ring, expected);
84 std::rotate(boost::begin(ring), boost::begin(ring) + 1, boost::end(ring));
86 // it seems that area method doesn't work for invalid "opened" polygons
87 //if (! boost::is_same<cs_tag, bg::geographic_tag>::value)
89 P p = bg::range::front(ring);
90 bg::range::push_back(ring, p);
98 // From correct() test, rings rotated and reversed in test_one()
99 test_one<P>("POLYGON((0 0,0 1,1 1,1 0,0 0))", bg::clockwise);
100 test_one<P>("POLYGON((0 0,0 1,1 1,1 0))", bg::clockwise);
101 test_one<P>("POLYGON((0 0,0 4,4 4,4 0,0 0))", bg::clockwise);
102 test_one<P>("POLYGON((1 1,2 1,2 2,1 2,1 1))", bg::counterclockwise);
104 test_one<P>("POLYGON((0 5, 5 5, 5 0, 0 0, 0 5))", bg::clockwise);
105 test_one<P>("POLYGON((0 5, 0 5, 0 6, 0 6, 0 4, 0 5, 5 5, 5 0, 0 0, 0 6, 0 5))", bg::clockwise);
106 test_one<P>("POLYGON((2 0, 2 1, 2 -1, 2 0, 1 0, 1 -1, 0 -1, 0 1, 1 1, 1 0, 2 0))", bg::clockwise);
107 test_one<P>("POLYGON((2 0, 2 1, 2 -1, 2 0, 1 0, 1 -1, 0 -1, 0 1, 1 1, 1 0))", bg::clockwise);
108 test_one<P>("POLYGON((2 0, 2 1, 3 1, 3 -1, 2 -1, 2 0, 1 0, 1 -1, 0 -1, 0 1, 1 1, 1 0, 2 0))", bg::clockwise);
109 test_one<P>("POLYGON((0 85, 5 85, 5 84, 0 84, 0 85))", bg::clockwise);
110 test_one<P>("POLYGON((0 2, 170 2, 170 0, 0 0, 0 2))", bg::clockwise);
111 test_one<P>("POLYGON((0 2, 170 2, 170 1, 160 1, 10 1, 0 1, 0 2))", bg::clockwise);
112 test_one<P>("POLYGON((0 2, 170 2, 170 -2, 0 -2, 0 2))", bg::clockwise);
113 test_one<P>("POLYGON((5 5, 6 5, 6 6, 6 4, 6 5, 5 5, 5 4, 4 4, 4 6, 5 6, 5 5))", bg::clockwise);
114 test_one<P>("POLYGON((5 5, 6 5, 6 6, 6 4, 6 5, 5 5, 5 6, 4 6, 4 4, 5 4, 5 5))", bg::counterclockwise);
115 test_one<P>("POLYGON((5 5, 6 5, 6 5, 6 6, 6 5, 6 4, 6 5, 6 5, 5 5, 5 4, 4 4, 4 6, 5 6, 5 5))", bg::clockwise);
117 // https://github.com/boostorg/geometry/pull/554
118 test_one<P>("POLYGON((9.8591674311151110 54.602813224425063, 9.8591651519791412 54.602359676428932, 9.8584586199249316 54.602359676428932, 9.8591674311151110 54.602813224425063))",
122 template <typename P>
123 void test_cartesian()
125 test_one<P>("POLYGON((0 5, 1 5, 1 6, 1 4, 2 4, 0 4, 0 3, 0 5))", bg::clockwise);
128 template <typename P>
129 void test_spheroidal()
131 test_one<P>("POLYGON((0 5, 1 5, 1 6, 1 4, 0 4, 0 3, 0 5))", bg::clockwise);
133 test_one<P>("POLYGON((0 45, 120 45, -120 45, 0 45))", bg::counterclockwise);
136 int test_main(int, char* [])
138 test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
139 test_all<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
140 test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
142 test_cartesian<bg::model::point<double, 2, bg::cs::cartesian> >();
144 test_spheroidal<bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree> > >();
145 test_spheroidal<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();