1 // Boost.Geometry (aka GGL, Generic Geometry Library)
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.
8 // Copyright (c) 2017, Oracle and/or its affiliates.
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
12 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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)
18 #define BOOST_GEOMETRY_DEFINE_STREAM_OPERATOR_SEGMENT_RATIO
20 #include <geometry_test_common.hpp>
22 #include <boost/geometry/algorithms/assign.hpp>
24 #include <boost/geometry/strategies/cartesian/intersection.hpp>
25 #include <boost/geometry/strategies/intersection_result.hpp>
27 #include <boost/geometry/policies/relate/intersection_points.hpp>
28 #include <boost/geometry/policies/relate/direction.hpp>
29 #include <boost/geometry/policies/relate/tupled.hpp>
31 #include <boost/geometry/algorithms/intersection.hpp>
34 #include <boost/geometry/geometries/point.hpp>
35 #include <boost/geometry/geometries/segment.hpp>
38 template <typename IntersectionPoints
>
39 static int check(IntersectionPoints
const& is
,
40 std::size_t index
, double expected_x
, double expected_y
)
42 if (expected_x
!= -99 && expected_y
!= -99 && is
.count
> index
)
44 double x
= bg::get
<0>(is
.intersections
[index
]);
45 double y
= bg::get
<1>(is
.intersections
[index
]);
47 BOOST_CHECK_CLOSE(x
, expected_x
, 0.001);
48 BOOST_CHECK_CLOSE(y
, expected_y
, 0.001);
56 static void test_segment_intersection(std::string
const& case_id
,
57 int x1
, int y1
, int x2
, int y2
,
58 int x3
, int y3
, int x4
, int y4
,
59 char expected_how
, bool expected_opposite
,
60 int expected_arrival1
, int expected_arrival2
,
61 int expected_x1
, int expected_y1
,
62 int expected_x2
= -99, int expected_y2
= -99)
65 boost::ignore_unused_variable_warning(case_id
);
67 typedef bg::model::referring_segment
<const P
> segment_type
;
70 bg::assign_values(p1
, x1
, y1
);
71 bg::assign_values(p2
, x2
, y2
);
72 bg::assign_values(p3
, x3
, y3
);
73 bg::assign_values(p4
, x4
, y4
);
75 segment_type
s12(p1
,p2
);
76 segment_type
s34(p3
,p4
);
78 typedef bg::detail::no_rescale_policy rescale_policy_type
;
79 rescale_policy_type rescale_policy
;
81 typedef bg::segment_intersection_points
84 typename
bg::segment_ratio_type
91 typedef bg::policies::relate::segments_intersection_points
96 // Get the intersection point (or two points)
98 = bg::strategy::intersection::cartesian_segments
<>
99 ::apply(s12
, s34
, points_policy_type(), rescale_policy
, p1
, p2
, p3
, p4
);
101 // Get just a character for Left/Right/intersects/etc, purpose is more for debugging
102 bg::policies::relate::direction_type dir
103 = bg::strategy::intersection::cartesian_segments
<>
104 ::apply(s12
, s34
, bg::policies::relate::segments_direction(),
105 rescale_policy
, p1
, p2
, p3
, p4
);
107 std::size_t expected_count
=
108 check(is
, 0, expected_x1
, expected_y1
)
109 + check(is
, 1, expected_x2
, expected_y2
);
111 BOOST_CHECK_EQUAL(is
.count
, expected_count
);
112 BOOST_CHECK_EQUAL(dir
.how
, expected_how
);
113 BOOST_CHECK_EQUAL(dir
.opposite
, expected_opposite
);
114 BOOST_CHECK_EQUAL(dir
.arrival
[0], expected_arrival1
);
115 BOOST_CHECK_EQUAL(dir
.arrival
[1], expected_arrival2
);
118 template <typename P
, typename Pair
>
119 static void test_segment_ratio(std::string
const& case_id
,
120 int x1
, int y1
, int x2
, int y2
,
121 int x3
, int y3
, int x4
, int y4
,
122 Pair expected_pair_a1
, Pair expected_pair_a2
,
123 Pair expected_pair_b1
, Pair expected_pair_b2
,
124 int exp_ax1
, int exp_ay1
, int exp_ax2
, int exp_ay2
,
125 std::size_t expected_count
= 2)
128 boost::ignore_unused_variable_warning(case_id
);
130 typedef bg::model::referring_segment
<const P
> segment_type
;
133 bg::assign_values(p1
, x1
, y1
);
134 bg::assign_values(p2
, x2
, y2
);
135 bg::assign_values(p3
, x3
, y3
);
136 bg::assign_values(p4
, x4
, y4
);
138 segment_type
s12(p1
, p2
);
139 segment_type
s34(p3
, p4
);
141 typedef bg::detail::no_rescale_policy rescale_policy_type
;
142 rescale_policy_type rescale_policy
;
144 typedef typename
bg::segment_ratio_type
<P
, rescale_policy_type
>::type ratio_type
;
145 typedef bg::segment_intersection_points
151 typedef bg::policies::relate::segments_intersection_points
154 > points_policy_type
;
156 // Get the intersection point (or two points)
158 = bg::strategy::intersection::cartesian_segments
<>
159 ::apply(s12
, s34
, points_policy_type(), rescale_policy
, p1
, p2
, p3
, p4
);
161 ratio_type
expected_a1(expected_pair_a1
.first
, expected_pair_a1
.second
);
162 ratio_type
expected_a2(expected_pair_a2
.first
, expected_pair_a2
.second
);
163 ratio_type
expected_b1(expected_pair_b1
.first
, expected_pair_b1
.second
);
164 ratio_type
expected_b2(expected_pair_b2
.first
, expected_pair_b2
.second
);
166 BOOST_CHECK_EQUAL(is
.count
, expected_count
);
168 BOOST_CHECK_EQUAL(is
.fractions
[0].robust_ra
, expected_a1
);
169 BOOST_CHECK_EQUAL(is
.fractions
[0].robust_rb
, expected_b1
);
170 BOOST_CHECK_EQUAL(bg::get
<0>(is
.intersections
[0]), exp_ax1
);
171 BOOST_CHECK_EQUAL(bg::get
<1>(is
.intersections
[0]), exp_ay1
);
173 if (expected_count
== 2)
175 BOOST_CHECK_EQUAL(bg::get
<0>(is
.intersections
[1]), exp_ax2
);
176 BOOST_CHECK_EQUAL(bg::get
<1>(is
.intersections
[1]), exp_ay2
);
177 BOOST_CHECK_EQUAL(is
.fractions
[1].robust_ra
, expected_a2
);
178 BOOST_CHECK_EQUAL(is
.fractions
[1].robust_rb
, expected_b2
);
183 template <typename P
>
186 // Collinear - non opposite
190 test_segment_intersection
<P
>("n1",
198 test_segment_intersection
<P
>("n2",
206 test_segment_intersection
<P
>("n3",
214 test_segment_intersection
<P
>("n4",
222 test_segment_intersection
<P
>("n5",
230 test_segment_intersection
<P
>("n6",
238 test_segment_intersection
<P
>("n7",
244 // Collinear - opposite
247 test_segment_intersection
<P
>("o1",
255 test_segment_intersection
<P
>("o2",
263 test_segment_intersection
<P
>("o3",
271 test_segment_intersection
<P
>("o4",
279 test_segment_intersection
<P
>("o5",
287 test_segment_intersection
<P
>("o6",
295 test_segment_intersection
<P
>("o7",
303 test_segment_intersection
<P
>("e1",
311 test_segment_intersection
<P
>("e1",
317 // Disjoint (in vertical direction, picture still horizontal)
320 test_segment_intersection
<P
>("case_recursive_boxes_1",
329 template <typename P
>
335 test_segment_ratio
<P
>("n4",
338 std::make_pair(1, 5), std::make_pair(3, 5), // IP located on 1/5, 3/5 w.r.t A
339 std::make_pair(0, 1), std::make_pair(1, 1), // IP located on 0, 1 w.r.t. B
340 // IP's are ordered as in A (currently)
345 test_segment_ratio
<P
>("o4",
348 std::make_pair(1, 5), std::make_pair(3, 5),
349 std::make_pair(1, 1), std::make_pair(0, 1),
354 test_segment_ratio
<P
>("o4b",
357 std::make_pair(2, 5), std::make_pair(4, 5),
358 std::make_pair(0, 1), std::make_pair(1, 1),
363 test_segment_ratio
<P
>("o4c",
366 std::make_pair(2, 5), std::make_pair(4, 5),
367 std::make_pair(1, 1), std::make_pair(0, 1),
373 test_segment_ratio
<P
>("n3",
376 std::make_pair(0, 1), std::make_pair(2, 5),
377 std::make_pair(0, 1), std::make_pair(1, 1),
380 // a2<-------------a1
382 test_segment_ratio
<P
>("n3b",
385 std::make_pair(0, 1), std::make_pair(2, 5),
386 std::make_pair(0, 1), std::make_pair(1, 1),
393 test_segment_ratio
<P
>("rn4",
396 std::make_pair(0, 1), std::make_pair(1, 1),
397 std::make_pair(1, 5), std::make_pair(3, 5),
402 test_segment_ratio
<P
>("ro4",
405 std::make_pair(0, 1), std::make_pair(1, 1),
406 std::make_pair(3, 5), std::make_pair(1, 5),
411 test_segment_ratio
<P
>("ro4b",
414 std::make_pair(0, 1), std::make_pair(1, 1),
415 std::make_pair(2, 5), std::make_pair(4, 5),
420 test_segment_ratio
<P
>("ro4c",
423 std::make_pair(0, 1), std::make_pair(1, 1),
424 std::make_pair(4, 5), std::make_pair(2, 5),
427 // B inside A, boundaries intersect
428 // We change the coordinates a bit (w.r.t. n3 above) to have it asymmetrical
431 test_segment_ratio
<P
>("n3",
434 std::make_pair(0, 1), std::make_pair(2, 5),
435 std::make_pair(0, 1), std::make_pair(1, 1),
440 test_segment_ratio
<P
>("o3",
443 std::make_pair(0, 1), std::make_pair(2, 5),
444 std::make_pair(1, 1), std::make_pair(0, 1),
449 test_segment_ratio
<P
>("n5",
452 std::make_pair(3, 5), std::make_pair(1, 1),
453 std::make_pair(0, 1), std::make_pair(1, 1),
458 test_segment_ratio
<P
>("o5",
461 std::make_pair(3, 5), std::make_pair(1, 1),
462 std::make_pair(1, 1), std::make_pair(0, 1),
465 // Generic (overlaps)
468 test_segment_ratio
<P
>("n2",
471 std::make_pair(0, 1), std::make_pair(2, 5),
472 std::make_pair(1, 3), std::make_pair(1, 1),
476 test_segment_ratio
<P
>("n2_b",
479 std::make_pair(0, 1), std::make_pair(2, 5),
480 std::make_pair(2, 3), std::make_pair(0, 1),
483 // Same, both reversed
484 test_segment_ratio
<P
>("n2_c",
487 std::make_pair(3, 5), std::make_pair(1, 1),
488 std::make_pair(0, 1), std::make_pair(2, 3),
493 test_segment_ratio
<P
>("n6",
496 std::make_pair(3, 4), std::make_pair(1, 1),
497 std::make_pair(0, 1), std::make_pair(1, 3),
503 const int ignored
= 99;
504 test_segment_ratio
<P
>("degenerated1",
507 std::make_pair(3, 4), // IP located on 3/4 w.r.t A
508 std::make_pair(ignored
, 1), // not checked
509 std::make_pair(0, 1), // IP located at any place w.r.t B, so 0
510 std::make_pair(ignored
, 1), // not checked
515 test_segment_ratio
<P
>("degenerated2",
518 std::make_pair(0, 1), std::make_pair(ignored
, 1),
519 std::make_pair(3, 4), std::make_pair(ignored
, 1),
524 // Vertical one like in box_poly5 but in integer
525 test_segment_ratio
<P
>("box_poly5",
528 std::make_pair(3, 10), std::make_pair(6, 10),
529 std::make_pair(0, 1), std::make_pair(1, 1),
533 int test_main(int, char* [])
535 // We don't rescale but use integer points as, by nature, robust points
536 test_all
<bg::model::point
<int, 2, bg::cs::cartesian
> >();
537 test_ratios
<bg::model::point
<int, 2, bg::cs::cartesian
> >();