]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | ||
3 | // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. | |
4 | ||
5 | // Use, modification and distribution is subject to the Boost Software License, | |
6 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP | |
10 | #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP | |
11 | ||
12 | #include <boost/math/constants/constants.hpp> | |
13 | ||
14 | #include <boost/geometry/core/cs.hpp> | |
15 | #include <boost/geometry/core/tags.hpp> | |
16 | #include <boost/geometry/strategies/compare.hpp> | |
17 | #include <boost/geometry/util/math.hpp> | |
18 | ||
19 | ||
20 | namespace boost { namespace geometry | |
21 | { | |
22 | ||
23 | ||
24 | namespace strategy { namespace compare | |
25 | { | |
26 | ||
27 | ||
28 | #ifndef DOXYGEN_NO_DETAIL | |
29 | namespace detail | |
30 | { | |
31 | ||
32 | template <typename Units> | |
33 | struct shift | |
34 | { | |
35 | }; | |
36 | ||
37 | template <> | |
38 | struct shift<degree> | |
39 | { | |
40 | static inline double full() { return 360.0; } | |
41 | static inline double half() { return 180.0; } | |
42 | }; | |
43 | ||
44 | template <> | |
45 | struct shift<radian> | |
46 | { | |
47 | static inline double full() { return 2.0 * boost::math::constants::pi<double>(); } | |
48 | static inline double half() { return boost::math::constants::pi<double>(); } | |
49 | }; | |
50 | ||
51 | } // namespace detail | |
52 | #endif | |
53 | ||
54 | /*! | |
55 | \brief Compare (in one direction) strategy for spherical coordinates | |
56 | \ingroup strategies | |
57 | \tparam Point point-type | |
58 | \tparam Dimension dimension | |
59 | */ | |
60 | template <typename CoordinateType, typename Units, typename Compare> | |
61 | struct circular_comparator | |
62 | { | |
63 | static inline CoordinateType put_in_range(CoordinateType const& c, | |
64 | double min_border, double max_border) | |
65 | { | |
66 | CoordinateType value = c; | |
67 | while (value < min_border) | |
68 | { | |
69 | value += detail::shift<Units>::full(); | |
70 | } | |
71 | while (value > max_border) | |
72 | { | |
73 | value -= detail::shift<Units>::full(); | |
74 | } | |
75 | return value; | |
76 | } | |
77 | ||
78 | inline bool operator()(CoordinateType const& c1, CoordinateType const& c2) const | |
79 | { | |
80 | Compare compare; | |
81 | ||
82 | // Check situation that one of them is e.g. std::numeric_limits. | |
83 | static const double full = detail::shift<Units>::full(); | |
84 | double mx = 10.0 * full; | |
85 | if (c1 < -mx || c1 > mx || c2 < -mx || c2 > mx) | |
86 | { | |
87 | // do normal comparison, using circular is not useful | |
88 | return compare(c1, c2); | |
89 | } | |
90 | ||
91 | static const double half = full / 2.0; | |
92 | CoordinateType v1 = put_in_range(c1, -half, half); | |
93 | CoordinateType v2 = put_in_range(c2, -half, half); | |
94 | ||
95 | // Two coordinates on a circle are | |
96 | // at max <= half a circle away from each other. | |
97 | // So if it is more, shift origin. | |
98 | CoordinateType diff = geometry::math::abs(v1 - v2); | |
99 | if (diff > half) | |
100 | { | |
101 | v1 = put_in_range(v1, 0, full); | |
102 | v2 = put_in_range(v2, 0, full); | |
103 | } | |
104 | ||
105 | return compare(v1, v2); | |
106 | } | |
107 | }; | |
108 | ||
109 | }} // namespace strategy::compare | |
110 | ||
111 | #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS | |
112 | ||
113 | // Specialize for the longitude (dim 0) | |
114 | template | |
115 | < | |
116 | typename Point, | |
117 | template<typename> class CoordinateSystem, | |
118 | typename Units | |
119 | > | |
120 | struct strategy_compare<spherical_polar_tag, 1, Point, CoordinateSystem<Units>, 0> | |
121 | { | |
122 | typedef typename coordinate_type<Point>::type coordinate_type; | |
123 | typedef strategy::compare::circular_comparator | |
124 | < | |
125 | coordinate_type, | |
126 | Units, | |
127 | std::less<coordinate_type> | |
128 | > type; | |
129 | }; | |
130 | ||
131 | template | |
132 | < | |
133 | typename Point, | |
134 | template<typename> class CoordinateSystem, | |
135 | typename Units | |
136 | > | |
137 | struct strategy_compare<spherical_polar_tag, -1, Point, CoordinateSystem<Units>, 0> | |
138 | { | |
139 | typedef typename coordinate_type<Point>::type coordinate_type; | |
140 | typedef strategy::compare::circular_comparator | |
141 | < | |
142 | coordinate_type, | |
143 | Units, | |
144 | std::greater<coordinate_type> | |
145 | > type; | |
146 | }; | |
147 | ||
148 | #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS | |
149 | ||
150 | }} // namespace boost::geometry | |
151 | ||
152 | #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP |