1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
6
7 // This file was modified by Oracle on 2014, 2015, 2017, 2019.
8 // Modifications copyright (c) 2014-2019 Oracle and/or its affiliates.
9
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11
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)
15
16 #ifndef BOOST_GEOMETRY_TEST_WITHIN_HPP
17 #define BOOST_GEOMETRY_TEST_WITHIN_HPP
18
19
20 #include <boost/variant/variant.hpp>
21
22 #include <geometry_test_common.hpp>
23
24 #include <boost/geometry/algorithms/covered_by.hpp>
25 #include <boost/geometry/algorithms/within.hpp>
26 #include <boost/geometry/core/ring_type.hpp>
27 #include <boost/geometry/geometries/ring.hpp>
28 #include <boost/geometry/geometries/polygon.hpp>
29 #include <boost/geometry/geometries/multi_linestring.hpp>
30 #include <boost/geometry/io/wkt/read.hpp>
31 #include <boost/geometry/strategies/strategies.hpp>
32
33 struct no_strategy {};
34
35 template <typename Geometry1, typename Geometry2, typename Strategy>
call_within(Geometry1 const & geometry1,Geometry2 const & geometry2,Strategy const & strategy)36 bool call_within(Geometry1 const& geometry1,
37 Geometry2 const& geometry2,
38 Strategy const& strategy)
39 {
40 return bg::within(geometry1, geometry2, strategy);
41 }
42
43 template <typename Geometry1, typename Geometry2>
call_within(Geometry1 const & geometry1,Geometry2 const & geometry2,no_strategy)44 bool call_within(Geometry1 const& geometry1,
45 Geometry2 const& geometry2,
46 no_strategy)
47 {
48 return bg::within(geometry1, geometry2);
49 }
50
51 template <typename Geometry1, typename Geometry2, typename Strategy>
check_geometry(Geometry1 const & geometry1,Geometry2 const & geometry2,std::string const & wkt1,std::string const & wkt2,bool expected,Strategy const & strategy)52 void check_geometry(Geometry1 const& geometry1,
53 Geometry2 const& geometry2,
54 std::string const& wkt1,
55 std::string const& wkt2,
56 bool expected,
57 Strategy const& strategy)
58 {
59 bool detected = call_within(geometry1, geometry2, strategy);
60
61 BOOST_CHECK_MESSAGE(detected == expected,
62 "within: " << wkt1
63 << " in " << wkt2
64 << " -> Expected: " << expected
65 << " detected: " << detected);
66 }
67
68 template <typename Geometry1, typename Geometry2>
test_geometry(std::string const & wkt1,std::string const & wkt2,bool expected)69 void test_geometry(std::string const& wkt1,
70 std::string const& wkt2, bool expected)
71 {
72 Geometry1 geometry1;
73 Geometry2 geometry2;
74 bg::read_wkt(wkt1, geometry1);
75 bg::read_wkt(wkt2, geometry2);
76 boost::variant<Geometry1> v1(geometry1);
77 boost::variant<Geometry2> v2(geometry2);
78
79 typedef typename bg::strategy::within::services::default_strategy
80 <
81 Geometry1, Geometry2
82 >::type strategy_type;
83
84 check_geometry(geometry1, geometry2, wkt1, wkt2, expected, strategy_type());
85 check_geometry(geometry1, geometry2, wkt1, wkt2, expected, no_strategy());
86 check_geometry(v1, geometry2, wkt1, wkt2, expected, no_strategy());
87 check_geometry(geometry1, v2, wkt1, wkt2, expected, no_strategy());
88 check_geometry(v1, v2, wkt1, wkt2, expected, no_strategy());
89 }
90
91 template <typename Point, bool Clockwise, bool Closed>
test_ordered_ring(std::string const & wkt_point,std::string const & wkt_geometry,bool expected,bool on_border)92 void test_ordered_ring(std::string const& wkt_point,
93 std::string const& wkt_geometry, bool expected, bool on_border)
94 {
95 typedef bg::model::ring<Point, Clockwise, Closed> ring_type;
96 ring_type ring;
97 Point point;
98
99 bg::read_wkt(wkt_geometry, ring);
100 if ( BOOST_GEOMETRY_CONDITION(! Clockwise) )
101 {
102 std::reverse(boost::begin(ring), boost::end(ring));
103 }
104
105 bg::read_wkt(wkt_point, point);
106
107 bool detected = bg::within(point, ring);
108
109 BOOST_CHECK_MESSAGE(detected == expected,
110 "within: " << wkt_point
111 << " in " << wkt_geometry
112 << " -> Expected: " << expected
113 << " detected: " << detected
114 << " clockwise: " << int(Clockwise)
115 << " closed: " << int(Closed)
116 );
117
118 // other strategy (note that this one cannot detect OnBorder
119 // (without modifications)
120
121 bg::strategy::within::franklin<Point> franklin;
122 detected = bg::within(point, ring, franklin);
123 if (! on_border)
124 {
125 BOOST_CHECK_MESSAGE(detected == expected,
126 "within: " << wkt_point
127 << " in " << wkt_geometry
128 << " -> Expected: " << expected
129 << " detected: " << detected
130 << " clockwise: " << int(Clockwise)
131 << " closed: " << int(Closed)
132 );
133 }
134
135
136 bg::strategy::within::crossings_multiply<Point> cm;
137 detected = bg::within(point, ring, cm);
138 if (! on_border)
139 {
140 BOOST_CHECK_MESSAGE(detected == expected,
141 "within: " << wkt_point
142 << " in " << wkt_geometry
143 << " -> Expected: " << expected
144 << " detected: " << detected
145 << " clockwise: " << int(Clockwise)
146 << " closed: " << int(Closed)
147 );
148 }
149 }
150
151 template <typename Point>
test_ring(std::string const & wkt_point,std::string const & wkt_geometry,bool expected,bool on_border)152 void test_ring(std::string const& wkt_point,
153 std::string const& wkt_geometry,
154 bool expected, bool on_border)
155 {
156 test_ordered_ring<Point, true, true>(wkt_point, wkt_geometry, expected, on_border);
157 test_ordered_ring<Point, false, true>(wkt_point, wkt_geometry, expected, on_border);
158 test_ordered_ring<Point, true, false>(wkt_point, wkt_geometry, expected, on_border);
159 test_ordered_ring<Point, false, false>(wkt_point, wkt_geometry, expected, on_border);
160 test_geometry<Point, bg::model::polygon<Point> >(wkt_point, wkt_geometry, expected);
161 }
162
163 #endif
164