Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / geometry / index / test / algorithms / segment_intersection.cpp
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/segment_intersection.hpp>
13
14 //#include <boost/geometry/io/wkt/read.hpp>
15
16 template <typename Box, typename Point, typename RelativeDistance>
17 void test_segment_intersection(Box const& box, Point const& p0, Point const& p1,
18                                bool expected_result,
19                                RelativeDistance expected_rel_dist)
20 {
21     RelativeDistance rel_dist;
22     bool value = bgi::detail::segment_intersection(box, p0, p1, rel_dist);
23     BOOST_CHECK(value == expected_result);
24     if ( value && expected_result )
25         BOOST_CHECK_CLOSE(rel_dist, expected_rel_dist, 0.0001);
26 }
27
28 template <typename Box, typename Point, typename RelativeDistance>
29 void test_geometry(std::string const& wkt_g, std::string const& wkt_p0, std::string const& wkt_p1,
30                    bool expected_result,
31                    RelativeDistance expected_rel_dist)
32 {
33     Box box;
34     bg::read_wkt(wkt_g, box);
35     Point p0, p1;
36     bg::read_wkt(wkt_p0, p0);
37     bg::read_wkt(wkt_p1, p1);
38     test_segment_intersection(box, p0, p1, expected_result, expected_rel_dist);
39 }
40
41 #include <boost/geometry/geometries/point_xy.hpp>
42 #include <boost/geometry/geometries/point.hpp>
43 #include <boost/geometry/geometries/box.hpp>
44
45 void test_large_integers()
46 {
47     typedef bg::model::point<int, 2, bg::cs::cartesian> int_point_type;
48     typedef bg::model::point<double, 2, bg::cs::cartesian> double_point_type;
49
50     bg::model::box<int_point_type> int_box;
51     bg::model::box<double_point_type> double_box;
52     int_point_type int_p0, int_p1;
53     double_point_type double_p0, double_p1;
54
55     std::string const str_box = "POLYGON((1536119 192000, 1872000 528000))";
56     std::string const str_p0 = "POINT(1535000 191000)";
57     std::string const str_p1 = "POINT(1873000 529000)";
58     bg::read_wkt(str_box, int_box);
59     bg::read_wkt(str_box, double_box);
60     bg::read_wkt(str_p0, int_p0);
61     bg::read_wkt(str_p1, int_p1);
62     bg::read_wkt(str_p0, double_p0);
63     bg::read_wkt(str_p1, double_p1);
64
65     float int_value;
66     bool int_result = bgi::detail::segment_intersection(int_box, int_p0, int_p1, int_value);
67     double double_value;
68     bool double_result = bgi::detail::segment_intersection(double_box, double_p0, double_p1, double_value);
69     BOOST_CHECK(int_result == double_result);
70     if ( int_result && double_result )
71         BOOST_CHECK_CLOSE(int_value, double_value, 0.0001);
72 }
73
74 int test_main(int, char* [])
75 {
76     typedef bg::model::point<int, 2, bg::cs::cartesian> P2ic;
77     typedef bg::model::point<float, 2, bg::cs::cartesian> P2fc;
78     typedef bg::model::point<double, 2, bg::cs::cartesian> P2dc;
79
80     typedef bg::model::point<int, 3, bg::cs::cartesian> P3ic;
81     typedef bg::model::point<float, 3, bg::cs::cartesian> P3fc;
82     typedef bg::model::point<double, 3, bg::cs::cartesian> P3dc;
83     
84     test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
85     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
86     test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0/5);
87     test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
88     test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
89     test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0/7);
90
91     test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 0)", true, 1.0f/3);
92     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 2)", true, 1.0f/3);
93     test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(3 4)", "POINT(0 2)", true, 1.0/3);
94     test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0f/2);
95     test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0f/2);
96     test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(3 5 6)", "POINT(0 3 3)", true, 1.0/2);
97
98     test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 5)", true, 1.0f/5);
99     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 5)", "POINT(1 0)", true, 1.0f/5);
100     test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 5)", true, 1.0/5);
101     test_geometry<bg::model::box<P3ic>, P3ic>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 0)", "POINT(1 3 7)", true, 2.0f/7);
102     test_geometry<bg::model::box<P3fc>, P3fc>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 7)", "POINT(1 3 0)", true, 1.0f/7);
103     test_geometry<bg::model::box<P3dc>, P3dc>("POLYGON((0 1 2,2 4 6))", "POINT(1 3 0)", "POINT(1 3 7)", true, 2.0/7);
104
105     test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(0 5)", true, 0.2f);
106     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0 5)", "POINT(0 0)", true, 0.2f);
107     test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(0 5)", true, 0.2);
108
109     test_geometry<bg::model::box<P2ic>, P2ic>("POLYGON((0 1,2 4))", "POINT(3 0)", "POINT(3 5)", false, 0.0f);
110     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(3 5)", "POINT(3 0)", false, 0.0f);
111     test_geometry<bg::model::box<P2dc>, P2dc>("POLYGON((0 1,2 4))", "POINT(3 0)", "POINT(3 5)", false, 0.0);
112
113     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 0)", "POINT(1 1)", true, 1.0f);
114     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(1 4)", "POINT(1 5)", true, 0.0f);
115
116     test_geometry<bg::model::box<P2fc>, P2fc>("POLYGON((0 1,2 4))", "POINT(0.5 2)", "POINT(1.5 3)", true, 0.0f);
117     
118 #ifdef HAVE_TTMATH
119     typedef bg::model::point<ttmath_big, 2, bg::cs::cartesian> P2ttmc;
120     typedef bg::model::point<ttmath_big, 3, bg::cs::cartesian> P3ttmc;
121
122     test_geometry<bg::model::box<P2ttmc>, P2ttmc>("POLYGON((0 1,2 4))", "POINT(0 0)", "POINT(2 5)", true, 1.0f/5);
123     test_geometry<bg::model::box<P3ttmc>, P3ttmc>("POLYGON((0 1 2,2 4 6))", "POINT(0 0 0)", "POINT(2 5 7)", true, 2.0f/7);
124 #endif
125
126     test_large_integers();
127
128     return 0;
129 }