1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
7 // This file was modified by Oracle on 2014, 2015, 2017.
8 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Use, modification and distribution is subject to the Boost Software License,
13 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
14 // http://www.boost.org/LICENSE_1_0.txt)
16 #ifndef BOOST_GEOMETRY_TEST_WITHIN_HPP
17 #define BOOST_GEOMETRY_TEST_WITHIN_HPP
20 #include <boost/variant/variant.hpp>
22 #include <geometry_test_common.hpp>
24 #include <boost/geometry/algorithms/within.hpp>
25 #include <boost/geometry/core/ring_type.hpp>
26 #include <boost/geometry/geometries/ring.hpp>
27 #include <boost/geometry/geometries/polygon.hpp>
28 #include <boost/geometry/geometries/multi_linestring.hpp>
29 #include <boost/geometry/io/wkt/read.hpp>
30 #include <boost/geometry/strategies/strategies.hpp>
32 struct no_strategy {};
34 template <typename Geometry1, typename Geometry2, typename Strategy>
35 bool call_within(Geometry1 const& geometry1,
36 Geometry2 const& geometry2,
37 Strategy const& strategy)
39 return bg::within(geometry1, geometry2, strategy);
42 template <typename Geometry1, typename Geometry2>
43 bool call_within(Geometry1 const& geometry1,
44 Geometry2 const& geometry2,
47 return bg::within(geometry1, geometry2);
50 template <typename Geometry1, typename Geometry2, typename Strategy>
51 void check_geometry(Geometry1 const& geometry1,
52 Geometry2 const& geometry2,
53 std::string const& wkt1,
54 std::string const& wkt2,
56 Strategy const& strategy)
58 bool detected = call_within(geometry1, geometry2, strategy);
60 BOOST_CHECK_MESSAGE(detected == expected,
63 << " -> Expected: " << expected
64 << " detected: " << detected);
67 template <typename Geometry1, typename Geometry2>
68 void test_geometry(std::string const& wkt1,
69 std::string const& wkt2, bool expected)
73 bg::read_wkt(wkt1, geometry1);
74 bg::read_wkt(wkt2, geometry2);
75 boost::variant<Geometry1> v1(geometry1);
76 boost::variant<Geometry2> v2(geometry2);
78 typedef typename bg::strategy::within::services::default_strategy
81 >::type strategy_type;
83 check_geometry(geometry1, geometry2, wkt1, wkt2, expected, strategy_type());
84 check_geometry(geometry1, geometry2, wkt1, wkt2, expected, no_strategy());
85 check_geometry(v1, geometry2, wkt1, wkt2, expected, no_strategy());
86 check_geometry(geometry1, v2, wkt1, wkt2, expected, no_strategy());
87 check_geometry(v1, v2, wkt1, wkt2, expected, no_strategy());
90 template <typename Point, bool Clockwise, bool Closed>
91 void test_ordered_ring(std::string const& wkt_point,
92 std::string const& wkt_geometry, bool expected, bool on_border)
94 typedef bg::model::ring<Point, Clockwise, Closed> ring_type;
98 bg::read_wkt(wkt_geometry, ring);
99 if ( BOOST_GEOMETRY_CONDITION(! Clockwise) )
101 std::reverse(boost::begin(ring), boost::end(ring));
104 bg::read_wkt(wkt_point, point);
106 bool detected = bg::within(point, ring);
108 BOOST_CHECK_MESSAGE(detected == expected,
109 "within: " << wkt_point
110 << " in " << wkt_geometry
111 << " -> Expected: " << expected
112 << " detected: " << detected
113 << " clockwise: " << int(Clockwise)
114 << " closed: " << int(Closed)
117 // other strategy (note that this one cannot detect OnBorder
118 // (without modifications)
120 bg::strategy::within::franklin<Point> franklin;
121 detected = bg::within(point, ring, franklin);
124 BOOST_CHECK_MESSAGE(detected == expected,
125 "within: " << wkt_point
126 << " in " << wkt_geometry
127 << " -> Expected: " << expected
128 << " detected: " << detected
129 << " clockwise: " << int(Clockwise)
130 << " closed: " << int(Closed)
135 bg::strategy::within::crossings_multiply<Point> cm;
136 detected = bg::within(point, ring, cm);
139 BOOST_CHECK_MESSAGE(detected == expected,
140 "within: " << wkt_point
141 << " in " << wkt_geometry
142 << " -> Expected: " << expected
143 << " detected: " << detected
144 << " clockwise: " << int(Clockwise)
145 << " closed: " << int(Closed)
150 template <typename Point>
151 void test_ring(std::string const& wkt_point,
152 std::string const& wkt_geometry,
153 bool expected, bool on_border)
155 test_ordered_ring<Point, true, true>(wkt_point, wkt_geometry, expected, on_border);
156 test_ordered_ring<Point, false, true>(wkt_point, wkt_geometry, expected, on_border);
157 test_ordered_ring<Point, true, false>(wkt_point, wkt_geometry, expected, on_border);
158 test_ordered_ring<Point, false, false>(wkt_point, wkt_geometry, expected, on_border);
159 test_geometry<Point, bg::model::polygon<Point> >(wkt_point, wkt_geometry, expected);