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
8 // This file was modified by Oracle on 2015, 2017.
9 // Modifications copyright (c) 2015-2017 Oracle and/or its affiliates.
10
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15
16 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19
20
21 #include <geometry_test_common.hpp>
22
23 #include <boost/concept_check.hpp>
24
25 #include <boost/geometry/algorithms/assign.hpp>
26 #include <boost/geometry/algorithms/distance.hpp>
27 #include <boost/geometry/geometries/point.hpp>
28 #include <boost/geometry/srs/spheroid.hpp>
29 #include <boost/geometry/strategies/concepts/distance_concept.hpp>
30 #include <boost/geometry/strategies/geographic/distance_thomas.hpp>
31 #include <boost/geometry/strategies/geographic/side_thomas.hpp>
32
33 #include <test_common/test_point.hpp>
34
35 #ifdef HAVE_TTMATH
36 # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
37 #endif
38
39
40
41 template <typename P1, typename P2>
test_distance(double lon1,double lat1,double lon2,double lat2,double expected_km)42 void test_distance(double lon1, double lat1, double lon2, double lat2, double expected_km)
43 {
44 // Set radius type, but for integer coordinates we want to have floating point radius type
45 typedef typename bg::promote_floating_point
46 <
47 typename bg::coordinate_type<P1>::type
48 >::type rtype;
49
50 typedef bg::srs::spheroid<rtype> stype;
51
52 typedef bg::strategy::distance::thomas<stype> thomas_type;
53 typedef bg::strategy::distance::geographic<bg::strategy::thomas, stype> geographic_type;
54
55 BOOST_CONCEPT_ASSERT
56 (
57 (bg::concepts::PointDistanceStrategy<thomas_type, P1, P2>)
58 );
59
60 thomas_type thomas;
61 geographic_type geographic;
62 typedef typename bg::strategy::distance
63 ::services::return_type<thomas_type, P1, P2>::type return_type;
64
65
66 P1 p1;
67 P2 p2;
68
69 bg::assign_values(p1, lon1, lat1);
70 bg::assign_values(p2, lon2, lat2);
71
72 BOOST_CHECK_CLOSE(thomas.apply(p1, p2), return_type(1000.0 * expected_km), 0.001);
73 BOOST_CHECK_CLOSE(geographic.apply(p1, p2), return_type(1000.0 * expected_km), 0.001);
74 BOOST_CHECK_CLOSE(bg::distance(p1, p2, thomas), return_type(1000.0 * expected_km), 0.001);
75 }
76
77 template <typename PS, typename P>
test_side(double lon1,double lat1,double lon2,double lat2,double lon,double lat,int expected_side)78 void test_side(double lon1, double lat1,
79 double lon2, double lat2,
80 double lon, double lat,
81 int expected_side)
82 {
83 // Set radius type, but for integer coordinates we want to have floating point radius type
84 typedef typename bg::promote_floating_point
85 <
86 typename bg::coordinate_type<PS>::type
87 >::type rtype;
88
89 typedef bg::srs::spheroid<rtype> stype;
90
91 typedef bg::strategy::side::thomas<stype> strategy_type;
92 typedef bg::strategy::side::geographic<bg::strategy::thomas, stype> strategy2_type;
93
94 strategy_type strategy;
95 strategy2_type strategy2;
96
97 PS p1, p2;
98 P p;
99
100 bg::assign_values(p1, lon1, lat1);
101 bg::assign_values(p2, lon2, lat2);
102 bg::assign_values(p, lon, lat);
103
104 int side = strategy.apply(p1, p2, p);
105 int side2 = strategy2.apply(p1, p2, p);
106
107 BOOST_CHECK_EQUAL(side, expected_side);
108 BOOST_CHECK_EQUAL(side2, expected_side);
109 }
110
111 template <typename P1, typename P2>
test_all()112 void test_all()
113 {
114 test_distance<P1, P2>(0, 90, 1, 80, 1116.825795); // polar
115 test_distance<P1, P2>(0, -90, 1, -80, 1116.825795); // polar
116 test_distance<P1, P2>(4, 52, 4, 52, 0.0); // no point difference
117 test_distance<P1, P2>(4, 52, 3, 40, 1336.025365); // normal case
118
119 test_side<P1, P2>(0, 0, 0, 1, 0, 2, 0);
120 test_side<P1, P2>(0, 0, 0, 1, 0, -2, 0);
121 test_side<P1, P2>(10, 0, 10, 1, 10, 2, 0);
122 test_side<P1, P2>(10, 0, 10, -1, 10, 2, 0);
123
124 test_side<P1, P2>(10, 0, 10, 1, 0, 2, 1); // left
125 test_side<P1, P2>(10, 0, 10, -1, 0, 2, -1); // right
126
127 test_side<P1, P2>(-10, -10, 10, 10, 10, 0, -1); // right
128 test_side<P1, P2>(-10, -10, 10, 10, -10, 0, 1); // left
129 test_side<P1, P2>(170, -10, -170, 10, -170, 0, -1); // right
130 test_side<P1, P2>(170, -10, -170, 10, 170, 0, 1); // left
131 }
132
133 template <typename P>
test_all()134 void test_all()
135 {
136 test_all<P, P>();
137 }
138
test_main(int,char * [])139 int test_main(int, char* [])
140 {
141 //test_all<float[2]>();
142 //test_all<double[2]>();
143 test_all<bg::model::point<int, 2, bg::cs::geographic<bg::degree> > >();
144 test_all<bg::model::point<float, 2, bg::cs::geographic<bg::degree> > >();
145 test_all<bg::model::point<double, 2, bg::cs::geographic<bg::degree> > >();
146
147 #if defined(HAVE_TTMATH)
148 test_all<bg::model::point<ttmath::Big<1,4>, 2, bg::cs::geographic<bg::degree> > >();
149 test_all<bg::model::point<ttmath_big, 2, bg::cs::geographic<bg::degree> > >();
150 #endif
151
152 return 0;
153 }
154