]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
b32b8144 FG |
7 | // This file was modified by Oracle on 2014, 2015, 2017. |
8 | // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates. | |
9 | ||
10 | // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle | |
7c673cae FG |
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 | ||
7c673cae FG |
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 | ||
b32b8144 | 24 | #include <boost/geometry/algorithms/within.hpp> |
7c673cae | 25 | #include <boost/geometry/core/ring_type.hpp> |
7c673cae FG |
26 | #include <boost/geometry/geometries/ring.hpp> |
27 | #include <boost/geometry/geometries/polygon.hpp> | |
28 | #include <boost/geometry/geometries/multi_linestring.hpp> | |
7c673cae | 29 | #include <boost/geometry/io/wkt/read.hpp> |
b32b8144 | 30 | #include <boost/geometry/strategies/strategies.hpp> |
7c673cae | 31 | |
b32b8144 | 32 | struct no_strategy {}; |
7c673cae | 33 | |
b32b8144 FG |
34 | template <typename Geometry1, typename Geometry2, typename Strategy> |
35 | bool call_within(Geometry1 const& geometry1, | |
36 | Geometry2 const& geometry2, | |
37 | Strategy const& strategy) | |
38 | { | |
39 | return bg::within(geometry1, geometry2, strategy); | |
40 | } | |
7c673cae FG |
41 | |
42 | template <typename Geometry1, typename Geometry2> | |
b32b8144 FG |
43 | bool call_within(Geometry1 const& geometry1, |
44 | Geometry2 const& geometry2, | |
45 | no_strategy) | |
46 | { | |
47 | return bg::within(geometry1, geometry2); | |
48 | } | |
49 | ||
50 | template <typename Geometry1, typename Geometry2, typename Strategy> | |
7c673cae FG |
51 | void check_geometry(Geometry1 const& geometry1, |
52 | Geometry2 const& geometry2, | |
53 | std::string const& wkt1, | |
54 | std::string const& wkt2, | |
b32b8144 FG |
55 | bool expected, |
56 | Strategy const& strategy) | |
7c673cae | 57 | { |
b32b8144 | 58 | bool detected = call_within(geometry1, geometry2, strategy); |
7c673cae FG |
59 | |
60 | BOOST_CHECK_MESSAGE(detected == expected, | |
61 | "within: " << wkt1 | |
62 | << " in " << wkt2 | |
63 | << " -> Expected: " << expected | |
64 | << " detected: " << detected); | |
65 | } | |
66 | ||
67 | template <typename Geometry1, typename Geometry2> | |
68 | void test_geometry(std::string const& wkt1, | |
69 | std::string const& wkt2, bool expected) | |
70 | { | |
71 | Geometry1 geometry1; | |
72 | Geometry2 geometry2; | |
73 | bg::read_wkt(wkt1, geometry1); | |
74 | bg::read_wkt(wkt2, geometry2); | |
75 | boost::variant<Geometry1> v1(geometry1); | |
76 | boost::variant<Geometry2> v2(geometry2); | |
77 | ||
b32b8144 FG |
78 | typedef typename bg::strategy::within::services::default_strategy |
79 | < | |
80 | Geometry1, Geometry2 | |
81 | >::type strategy_type; | |
82 | ||
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()); | |
7c673cae FG |
88 | } |
89 | ||
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) | |
93 | { | |
94 | typedef bg::model::ring<Point, Clockwise, Closed> ring_type; | |
95 | ring_type ring; | |
96 | Point point; | |
97 | ||
98 | bg::read_wkt(wkt_geometry, ring); | |
99 | if ( BOOST_GEOMETRY_CONDITION(! Clockwise) ) | |
100 | { | |
101 | std::reverse(boost::begin(ring), boost::end(ring)); | |
102 | } | |
103 | ||
104 | bg::read_wkt(wkt_point, point); | |
105 | ||
106 | bool detected = bg::within(point, ring); | |
107 | ||
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) | |
115 | ); | |
116 | ||
117 | // other strategy (note that this one cannot detect OnBorder | |
118 | // (without modifications) | |
119 | ||
120 | bg::strategy::within::franklin<Point> franklin; | |
121 | detected = bg::within(point, ring, franklin); | |
122 | if (! on_border) | |
123 | { | |
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) | |
131 | ); | |
132 | } | |
133 | ||
134 | ||
135 | bg::strategy::within::crossings_multiply<Point> cm; | |
136 | detected = bg::within(point, ring, cm); | |
137 | if (! on_border) | |
138 | { | |
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) | |
146 | ); | |
147 | } | |
148 | } | |
149 | ||
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) | |
154 | { | |
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); | |
160 | } | |
161 | ||
162 | #endif |