]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/geometry/test/strategies/segment_intersection.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / geometry / test / strategies / segment_intersection.cpp
CommitLineData
7c673cae
FG
1// Boost.Geometry (aka GGL, Generic Geometry Library)
2// Unit Test
3
4// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
6// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7
20effc67
TL
8// Copyright (c) 2020, Oracle and/or its affiliates.
9// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10
7c673cae
FG
11// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
12// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
13
14// Use, modification and distribution is subject to the Boost Software License,
15// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
16// http://www.boost.org/LICENSE_1_0.txt)
17
18
19#if defined(_MSC_VER)
20// We deliberately mix float/double's here so turn off warning
21#pragma warning( disable : 4244 )
22#endif // defined(_MSC_VER)
23
7c673cae
FG
24#include <geometry_test_common.hpp>
25
26#include <boost/geometry/algorithms/assign.hpp>
27
b32b8144 28#include <boost/geometry/strategies/cartesian/intersection.hpp>
7c673cae
FG
29#include <boost/geometry/strategies/intersection_result.hpp>
30
20effc67 31#include <boost/geometry/policies/relate/intersection_policy.hpp>
7c673cae
FG
32
33#include <boost/geometry/algorithms/intersection.hpp>
92f5a8d4 34#include <boost/geometry/algorithms/detail/overlay/segment_as_subrange.hpp>
7c673cae
FG
35
36#include <boost/geometry/geometries/point.hpp>
37#include <boost/geometry/geometries/segment.hpp>
38#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
39
40BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);
41
42
7c673cae 43template <typename P>
11fdf7f2 44static void test_segment_intersection(int caseid,
7c673cae 45 int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,
11fdf7f2 46 char expected_how,
7c673cae
FG
47 int expected_x1 = -99, int expected_y1 = -99,
48 int expected_x2 = -99, int expected_y2 = -99)
49{
50 using namespace boost::geometry;
51
7c673cae
FG
52 typedef typename bg::coordinate_type<P>::type coordinate_type;
53 typedef bg::model::referring_segment<const P> segment_type;
54
55 P p1, p2, p3, p4;
56 bg::assign_values(p1, x1, y1);
57 bg::assign_values(p2, x2, y2);
58 bg::assign_values(p3, x3, y3);
59 bg::assign_values(p4, x4, y4);
60
61 segment_type s12(p1,p2);
62 segment_type s34(p3,p4);
63
92f5a8d4
TL
64 bg::detail::segment_as_subrange<segment_type> sr12(s12);
65 bg::detail::segment_as_subrange<segment_type> sr34(s34);
66
11fdf7f2 67 std::size_t expected_count = 0;
7c673cae
FG
68
69 if (expected_x1 != -99 && expected_y1 != -99)
70 {
71 expected_count++;
7c673cae
FG
72 }
73 if (expected_x2 != -99 && expected_y2 != -99)
74 {
75 expected_count++;
7c673cae 76 }
7c673cae 77
11fdf7f2 78 // Using intersection_insert
7c673cae 79
11fdf7f2 80 std::vector<P> out;
92f5a8d4
TL
81 bg::detail::intersection::intersection_insert<P>(s12, s34,
82 std::back_inserter(out));
7c673cae 83
11fdf7f2 84 // Using strategy
92f5a8d4
TL
85 typedef bg::segment_intersection_points<P> result_type;
86
11fdf7f2 87 typedef bg::policies::relate::segments_intersection_points
7c673cae 88 <
11fdf7f2
TL
89 result_type
90 > points_policy_type;
7c673cae 91
11fdf7f2
TL
92 result_type is
93 = bg::strategy::intersection::cartesian_segments<>
92f5a8d4 94 ::apply(sr12, sr34, points_policy_type());
7c673cae 95
11fdf7f2
TL
96 bg::policies::relate::direction_type dir
97 = bg::strategy::intersection::cartesian_segments<>
92f5a8d4 98 ::apply(sr12, sr34, bg::policies::relate::segments_direction());
7c673cae 99
92f5a8d4 100 //BOOST_CHECK_EQUAL(boost::size(out), expected_count);
11fdf7f2
TL
101 BOOST_CHECK_EQUAL(is.count, expected_count);
102 BOOST_CHECK_MESSAGE(dir.how == expected_how,
103 caseid
104 << " how: detected: " << dir.how
105 << " expected: " << expected_how);
106
107 if (expected_count == 2
108 && is.count == 2
109 && boost::size(out) == 2)
110 {
111 // Two intersection points, reverse expectation if necessary
112 bool const first_matches
113 = std::fabs(bg::get<0>(out[0]) - expected_x1) < 1.0e-6
114 && std::fabs(bg::get<1>(out[0]) - expected_y1) < 1.0e-6;
115
116 if (! first_matches)
117 {
118 std::swap(expected_x1, expected_x2);
119 std::swap(expected_y1, expected_y2);
120 }
121 }
7c673cae
FG
122
123 if (expected_x1 != -99 && expected_y1 != -99
7c673cae
FG
124 && boost::size(out) >= 1)
125 {
11fdf7f2
TL
126
127 BOOST_CHECK_CLOSE(bg::get<0>(out[0]), coordinate_type(expected_x1), 0.001);
128 BOOST_CHECK_CLOSE(bg::get<1>(out[0]), coordinate_type(expected_y1), 0.001);
129
130 BOOST_CHECK_CLOSE(bg::get<0>(is.intersections[0]), expected_x1, 0.001);
131 BOOST_CHECK_CLOSE(bg::get<1>(is.intersections[0]), expected_y1, 0.001);
132
7c673cae
FG
133 }
134 if (expected_x2 != -99 && expected_y2 != -99
7c673cae
FG
135 && boost::size(out) >= 2)
136 {
11fdf7f2
TL
137 BOOST_CHECK_CLOSE(bg::get<0>(out[1]), coordinate_type(expected_x2), 0.001);
138 BOOST_CHECK_CLOSE(bg::get<1>(out[1]), coordinate_type(expected_y2), 0.001);
139
140 BOOST_CHECK_CLOSE(bg::get<0>(is.intersections[1]), expected_x2, 0.001);
141 BOOST_CHECK_CLOSE(bg::get<1>(is.intersections[1]), expected_y2, 0.001);
7c673cae
FG
142 }
143}
144
145
146template <typename P>
147void test_all()
148{
11fdf7f2
TL
149 test_segment_intersection<P>( 1, 0,2, 2,0, 0,0, 2,2, 'i', 1, 1);
150 test_segment_intersection<P>( 2, 2,2, 3,1, 0,0, 2,2, 'a', 2, 2);
151 test_segment_intersection<P>( 3, 3,1, 2,2, 0,0, 2,2, 't', 2, 2);
152 test_segment_intersection<P>( 4, 0,2, 1,1, 0,0, 2,2, 'm', 1, 1);
153
154 test_segment_intersection<P>( 5, 1,1, 0,2, 0,0, 2,2, 's', 1, 1);
155 test_segment_intersection<P>( 6, 0,2, 2,0, 0,0, 1,1, 'm', 1, 1);
156 test_segment_intersection<P>( 7, 2,0, 0,2, 0,0, 1,1, 'm', 1, 1);
157 test_segment_intersection<P>( 8, 2,3, 3,2, 0,0, 2,2, 'd');
158
159 test_segment_intersection<P>( 9, 0,0, 2,2, 0,0, 2,2, 'e', 0, 0, 2, 2);
160 test_segment_intersection<P>(10, 2,2, 0,0, 0,0, 2,2, 'e', 2, 2, 0, 0);
161 test_segment_intersection<P>(11, 1,1, 3,3, 0,0, 2,2, 'c', 1, 1, 2, 2);
162 test_segment_intersection<P>(12, 3,3, 1,1, 0,0, 2,2, 'c', 1, 1, 2, 2);
163
164 test_segment_intersection<P>(13, 0,2, 2,2, 2,1, 2,3, 'm', 2, 2);
165 test_segment_intersection<P>(14, 2,2, 2,4, 2,0, 2,2, 'a', 2, 2);
166 test_segment_intersection<P>(15, 2,2, 2,4, 2,0, 2,1, 'd');
167 test_segment_intersection<P>(16, 2,4, 2,2, 2,0, 2,1, 'd');
168
169 test_segment_intersection<P>(17, 2,1, 2,3, 2,2, 2,4, 'c', 2, 2, 2, 3);
170 test_segment_intersection<P>(18, 2,3, 2,1, 2,2, 2,4, 'c', 2, 3, 2, 2);
171 test_segment_intersection<P>(19, 0,2, 2,2, 4,2, 2,2, 't', 2, 2);
172 test_segment_intersection<P>(20, 0,2, 2,2, 2,2, 4,2, 'a', 2, 2);
173
174 test_segment_intersection<P>(21, 1,2, 3,2, 2,1, 2,3, 'i', 2, 2);
175 test_segment_intersection<P>(22, 2,4, 2,1, 2,1, 2,3, 'c', 2, 1, 2, 3);
176 test_segment_intersection<P>(23, 2,4, 2,1, 2,3, 2,1, 'c', 2, 3, 2, 1);
177 test_segment_intersection<P>(24, 1,1, 3,3, 0,0, 3,3, 'c', 1, 1, 3, 3);
178
179 test_segment_intersection<P>(25, 2,0, 2,4, 2,1, 2,3, 'c', 2, 1, 2, 3);
180 test_segment_intersection<P>(26, 2,0, 2,4, 2,3, 2,1, 'c', 2, 3, 2, 1);
181 test_segment_intersection<P>(27, 0,0, 4,4, 1,1, 3,3, 'c', 1, 1, 3, 3);
182 test_segment_intersection<P>(28, 0,0, 4,4, 3,3, 1,1, 'c', 3, 3, 1, 1);
183
184 test_segment_intersection<P>(29, 1,1, 3,3, 0,0, 4,4, 'c', 1, 1, 3, 3);
185 test_segment_intersection<P>(30, 0,0, 2,2, 2,2, 3,1, 'a', 2, 2);
186 test_segment_intersection<P>(31, 0,0, 2,2, 2,2, 1,3, 'a', 2, 2);
187 test_segment_intersection<P>(32, 0,0, 2,2, 1,1, 2,0, 's', 1, 1);
188
189 test_segment_intersection<P>(33, 0,0, 2,2, 1,1, 0,2, 's', 1, 1);
190 test_segment_intersection<P>(34, 2,2, 1,3, 0,0, 2,2, 'a', 2, 2);
191 test_segment_intersection<P>(35, 2,2, 3,1, 0,0, 2,2, 'a', 2, 2);
192 test_segment_intersection<P>(36, 0,0, 2,2, 0,2, 1,1, 'm', 1, 1);
193
194 test_segment_intersection<P>(37, 0,0, 2,2, 2,0, 1,1, 'm', 1, 1);
195 test_segment_intersection<P>(38, 1,1, 0,2, 0,0, 2,2, 's', 1, 1);
196 test_segment_intersection<P>(39, 1,1, 2,0, 0,0, 2,2, 's', 1, 1);
197 test_segment_intersection<P>(40, 2,0, 1,1, 0,0, 2,2, 'm', 1, 1);
198
199 test_segment_intersection<P>(41, 1,2, 0,2, 2,2, 0,2, 'c', 1, 2, 0, 2);
200 test_segment_intersection<P>(42, 2,1, 1,1, 2,2, 0,2, 'd');
201 test_segment_intersection<P>(43, 4,1, 3,1, 2,2, 0,2, 'd');
202 test_segment_intersection<P>(44, 4,2, 3,2, 2,2, 0,2, 'd');
203
204 test_segment_intersection<P>(45, 2,0, 0,2, 0,0, 2,2, 'i', 1, 1);
7c673cae
FG
205
206 // In figure: times 2
11fdf7f2 207 test_segment_intersection<P>(46, 8,2, 4,6, 0,0, 8, 8, 'i', 5, 5);
7c673cae
FG
208}
209
210int test_main(int, char* [])
211{
11fdf7f2 212#if !defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
7c673cae
FG
213 test_all<boost::tuple<double, double> >();
214 test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
11fdf7f2 215#endif
7c673cae 216 test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
11fdf7f2 217
7c673cae
FG
218 return 0;
219}