1// Boost.Geometry Index
2// Unit Test
3
4// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
5
6// Use, modification and distribution is subject to the Boost Software License,
7// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10#include <geometry_index_test_common.hpp>
11
12#include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
13
14#include <boost/geometry/geometries/point_xy.hpp>
15#include <boost/geometry/geometries/point.hpp>
16#include <boost/geometry/geometries/box.hpp>
17#include <boost/geometry/geometries/linestring.hpp>
18#include <boost/geometry/geometries/segment.hpp>
19
20//#include <boost/geometry/io/wkt/read.hpp>
21
22template <typename Box, typename Linestring>
23void test_path_intersection(Box const& box, Linestring const& path,
24 bool expected_result,
25 typename bg::default_length_result<Linestring>::type expected_dist)
26{
27 typename bgi::detail::default_path_intersection_distance_type<Box, Linestring>::type dist;
28
29 bool value = bgi::detail::path_intersection(box, path, dist);
30 BOOST_CHECK(value == expected_result);
31 if ( value && expected_result )
32 BOOST_CHECK_CLOSE(dist, expected_dist, 0.0001);
33
34 if ( ::boost::size(path) == 2 )
35 {
36 typedef typename ::boost::range_value<Linestring>::type P;
37 typedef bg::model::segment<P> Seg;
38 typename bgi::detail::default_path_intersection_distance_type<Box, Seg>::type dist;
39 Seg seg(*::boost::begin(path), *(::boost::begin(path)+1));
40 bool value = bgi::detail::path_intersection(box, seg, dist);
41 BOOST_CHECK(value == expected_result);
42 if ( value && expected_result )
43 BOOST_CHECK_CLOSE(dist, expected_dist, 0.0001);
44 }
45}
46
47template <typename Box, typename Linestring>
48void test_geometry(std::string const& wkt_g, std::string const& wkt_path,
49 bool expected_result,
50 typename bg::default_length_result<Linestring>::type expected_dist)
51{
52 Box box;
53 bg::read_wkt(wkt_g, box);
54 Linestring path;
55 bg::read_wkt(wkt_path, path);
56 test_path_intersection(box, path, expected_result, expected_dist);
57}
58
59void test_large_integers()
60{
61 typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
62 typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
63
64 bg::model::box<int_point_type> int_box;
65 bg::model::box<double_point_type> double_box;
66 typedef bg::model::linestring<int_point_type> IP;
67 IP int_path;
68 typedef bg::model::linestring<double_point_type> DP;
69 DP double_path;
70
71 std::string const str_box = "POLYGON((1536119 192000, 1872000 528000))";
72 std::string const str_path = "LINESTRING(1535000 191000, 1873000 191000, 1873000 300000, 1536119 300000)";
73 bg::read_wkt(wkt: str_box, geometry&: int_box);
74 bg::read_wkt(wkt: str_box, geometry&: double_box);
75 bg::read_wkt(wkt: str_path, geometry&: int_path);
76 bg::read_wkt(wkt: str_path, geometry&: double_path);
77
78 bg::default_length_result<IP>::type int_value;
79 bool int_result = bgi::detail::path_intersection(b: int_box, path: int_path, comparable_distance&: int_value);
80 bg::default_length_result<DP>::type double_value;
81 bool double_result = bgi::detail::path_intersection(b: double_box, path: double_path, comparable_distance&: double_value);
82
83 BOOST_CHECK(int_result == double_result);
84 if ( int_result && double_result )
85 BOOST_CHECK_CLOSE(int_value, double_value, 0.0001);
86}
87
88int test_main(int, char* [])
89{
90 typedef bg::model::point<int, 2, bg::cs::cartesian> P2ic;
91 typedef bg::model::point<float, 2, bg::cs::cartesian> P2fc;
92 typedef bg::model::point<double, 2, bg::cs::cartesian> P2dc;
93
94 typedef bg::model::point<int, 3, bg::cs::cartesian> P3ic;
95 typedef bg::model::point<float, 3, bg::cs::cartesian> P3fc;
96 typedef bg::model::point<double, 3, bg::cs::cartesian> P3dc;
97
98 typedef bg::model::linestring<P2ic> L2ic;
99 typedef bg::model::linestring<P2fc> L2fc;
100 typedef bg::model::linestring<P2dc> L2dc;
101
102 typedef bg::model::linestring<P3ic> L3ic;
103 typedef bg::model::linestring<P3fc> L3fc;
104 typedef bg::model::linestring<P3dc> L3dc;
105
106 // IMPORTANT! For 2-point linestrings comparable distance optimization is enabled!
107
108 test_geometry<bg::model::box<P2ic>, L2ic>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(0 0, 2 5)", expected_result: true, expected_dist: 1.0f/5);
109 test_geometry<bg::model::box<P2fc>, L2fc>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(0 0, 2 5)", expected_result: true, expected_dist: 1.0f/5);
110 test_geometry<bg::model::box<P2dc>, L2dc>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(0 0, 2 5)", expected_result: true, expected_dist: 1.0/5);
111 test_geometry<bg::model::box<P3ic>, L3ic>(wkt_g: "POLYGON((0 1 2,2 4 6))", wkt_path: "LINESTRING(0 0 0, 2 5 7)", expected_result: true, expected_dist: 2.0f/7);
112 test_geometry<bg::model::box<P3fc>, L3fc>(wkt_g: "POLYGON((0 1 2,2 4 6))", wkt_path: "LINESTRING(0 0 0, 2 5 7)", expected_result: true, expected_dist: 2.0f/7);
113 test_geometry<bg::model::box<P3dc>, L3dc>(wkt_g: "POLYGON((0 1 2,2 4 6))", wkt_path: "LINESTRING(0 0 0, 2 5 7)", expected_result: true, expected_dist: 2.0/7);
114
115 test_geometry<bg::model::box<P2fc>, L2fc>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(0 0, 1 0, 1 5)", expected_result: true, expected_dist: 2);
116 test_geometry<bg::model::box<P2fc>, L2fc>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(0 0, 3 0, 3 2, 0 2)", expected_result: true, expected_dist: 6);
117 test_geometry<bg::model::box<P2fc>, L2fc>(wkt_g: "POLYGON((0 1,2 4))", wkt_path: "LINESTRING(1 2, 3 3, 0 3)", expected_result: true, expected_dist: 0);
118
119#ifdef HAVE_TTMATH
120 typedef bg::model::point<ttmath_big, 2, bg::cs::cartesian> P2ttmc;
121 typedef bg::model::point<ttmath_big, 3, bg::cs::cartesian> P3ttmc;
122
123 typedef bg::model::linestring<P2ttmc> L2ttmc;
124 typedef bg::model::linestring<P3ttmc> L3ttmc;
125
126 test_geometry<bg::model::box<P2ttmc>, L2ttmc>("POLYGON((0 1,2 4))", "LINESTRING(0 0, 2 5)", true, 1.0/5);
127 test_geometry<bg::model::box<P3ttmc>, L3ttmc>("POLYGON((0 1 2,2 4 6))", "LINESTRING(0 0 0, 2 5 7)", true, 2.0/7);
128#endif
129
130 test_large_integers();
131
132 return 0;
133}
134

source code of boost/libs/geometry/index/test/algorithms/path_intersection.cpp