1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2013, 2014, 2015, 2017.
6 // Modifications copyright (c) 2013-2017 Oracle and/or its affiliates.
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9 // Use, modification and distribution is subject to the Boost Software License,
10 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_GEOMETRY_TEST_RELATE_HPP
14 #define BOOST_GEOMETRY_TEST_RELATE_HPP
16 #include <geometry_test_common.hpp>
18 #include <boost/variant.hpp>
20 #include <boost/geometry/core/ring_type.hpp>
21 #include <boost/geometry/algorithms/relate.hpp>
22 #include <boost/geometry/algorithms/relation.hpp>
23 #include <boost/geometry/strategies/strategies.hpp>
24 #include <boost/geometry/geometries/geometries.hpp>
25 #include <boost/geometry/geometries/point_xy.hpp>
27 #include <boost/geometry/io/wkt/read.hpp>
29 #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
30 #include <boost/geometry/strategies/cartesian/box_in_box.hpp>
31 #include <boost/geometry/strategies/agnostic/point_in_box_by_side.hpp>
33 namespace bgdr = bg::detail::relate;
35 std::string transposed(std::string matrix)
37 if ( !matrix.empty() )
39 std::swap(matrix[1], matrix[3]);
40 std::swap(matrix[2], matrix[6]);
41 std::swap(matrix[5], matrix[7]);
46 bool matrix_compare(std::string const& m1, std::string const& m2)
48 BOOST_ASSERT(m1.size() == 9 && m2.size() == 9);
49 for ( size_t i = 0 ; i < 9 ; ++i )
51 if ( m1[i] == '*' || m2[i] == '*' )
60 bool matrix_compare(std::string const& m, std::string const& res1, std::string const& res2)
62 return matrix_compare(m, res1)
63 || ( !res2.empty() ? matrix_compare(m, res2) : false );
66 std::string matrix_format(std::string const& matrix1, std::string const& matrix2)
69 + ( !matrix2.empty() ? " || " : "" ) + matrix2;
73 char get_ii(M const& m)
75 using bg::detail::relate::interior;
76 return m.template get<interior, interior>();
80 char get_ee(M const& m)
82 using bg::detail::relate::exterior;
83 return m.template get<exterior, exterior>();
88 bg::de9im::mask m1("");
89 bg::de9im::mask m2("TTT");
90 bg::de9im::mask m3("000111222");
91 bg::de9im::mask m4("000111222FFFF");
92 bg::de9im::mask m5(std::string(""));
93 bg::de9im::mask m6(std::string("TTT"));
94 bg::de9im::mask m7(std::string("000111222"));
95 bg::de9im::mask m8(std::string("000111222FFFF"));
97 using bg::detail::relate::interior;
98 using bg::detail::relate::exterior;
100 BOOST_CHECK(get_ii(m1) == '*' && get_ee(m1) == '*');
101 BOOST_CHECK(get_ii(m2) == 'T' && get_ee(m2) == '*');
102 BOOST_CHECK(get_ii(m3) == '0' && get_ee(m3) == '2');
103 BOOST_CHECK(get_ii(m4) == '0' && get_ee(m4) == '2');
104 BOOST_CHECK(get_ii(m5) == '*' && get_ee(m5) == '*');
105 BOOST_CHECK(get_ii(m6) == 'T' && get_ee(m6) == '*');
106 BOOST_CHECK(get_ii(m7) == '0' && get_ee(m7) == '2');
107 BOOST_CHECK(get_ii(m8) == '0' && get_ee(m8) == '2');
110 template <typename Geometry1, typename Geometry2>
111 void check_geometry(Geometry1 const& geometry1,
112 Geometry2 const& geometry2,
113 std::string const& wkt1,
114 std::string const& wkt2,
115 std::string const& expected1,
116 std::string const& expected2 = std::string())
118 boost::variant<Geometry1> variant1 = geometry1;
119 boost::variant<Geometry2> variant2 = geometry2;
122 std::string res_str = bg::relation(geometry1, geometry2).str();
123 bool ok = matrix_compare(res_str, expected1, expected2);
124 BOOST_CHECK_MESSAGE(ok,
127 << " -> Expected: " << matrix_format(expected1, expected2)
128 << " detected: " << res_str);
130 typedef typename bg::strategy::relate::services::default_strategy
133 >::type strategy_type;
134 std::string res_str0 = bg::relation(geometry1, geometry2, strategy_type()).str();
135 BOOST_CHECK(res_str == res_str0);
138 boost::variant<Geometry1> v1 = geometry1;
139 boost::variant<Geometry2> v2 = geometry2;
140 std::string res_str1 = bg::relation(geometry1, variant2).str();
141 std::string res_str2 = bg::relation(variant1, geometry2).str();
142 std::string res_str3 = bg::relation(variant1, variant2).str();
143 BOOST_CHECK(res_str == res_str1);
144 BOOST_CHECK(res_str == res_str2);
145 BOOST_CHECK(res_str == res_str3);
148 // changed sequence of geometries - transposed result
150 std::string res_str = bg::relation(geometry2, geometry1).str();
151 std::string expected1_tr = transposed(expected1);
152 std::string expected2_tr = transposed(expected2);
153 bool ok = matrix_compare(res_str, expected1_tr, expected2_tr);
154 BOOST_CHECK_MESSAGE(ok,
157 << " -> Expected: " << matrix_format(expected1_tr, expected2_tr)
158 << " detected: " << res_str);
161 if ( expected2.empty() )
164 bool result = bg::relate(geometry1, geometry2, bg::de9im::mask(expected1));
165 // TODO: SHOULD BE !interrupted - CHECK THIS!
166 BOOST_CHECK_MESSAGE(result,
169 << " -> Expected: " << expected1);
171 typedef typename bg::strategy::relate::services::default_strategy
174 >::type strategy_type;
175 bool result0 = bg::relate(geometry1, geometry2, bg::de9im::mask(expected1), strategy_type());
176 BOOST_CHECK(result == result0);
179 bool result1 = bg::relate(geometry1, variant2, bg::de9im::mask(expected1));
180 bool result2 = bg::relate(variant1, geometry2, bg::de9im::mask(expected1));
181 bool result3 = bg::relate(variant1, variant2, bg::de9im::mask(expected1));
182 BOOST_CHECK(result == result1);
183 BOOST_CHECK(result == result2);
184 BOOST_CHECK(result == result3);
187 if ( BOOST_GEOMETRY_CONDITION((
188 bg::detail::relate::interruption_enabled<Geometry1, Geometry2>::value )) )
190 // brake the expected output
191 std::string expected_interrupt = expected1;
192 bool changed = false;
193 BOOST_FOREACH(char & c, expected_interrupt)
195 if ( c >= '0' && c <= '9' )
208 bool result = bg::relate(geometry1, geometry2, bg::de9im::mask(expected_interrupt));
209 // TODO: SHOULD BE interrupted - CHECK THIS!
210 BOOST_CHECK_MESSAGE(!result,
213 << " -> Expected interrupt for:" << expected_interrupt);
219 template <typename Geometry1, typename Geometry2>
220 void test_geometry(std::string const& wkt1,
221 std::string const& wkt2,
222 std::string const& expected1,
223 std::string const& expected2 = std::string())
227 bg::read_wkt(wkt1, geometry1);
228 bg::read_wkt(wkt2, geometry2);
229 check_geometry(geometry1, geometry2, wkt1, wkt2, expected1, expected2);
232 #endif // BOOST_GEOMETRY_TEST_RELATE_HPP