1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 
5 // This file was modified by Oracle on 2016.
6 // Modifications copyright (c) 2016, Oracle and/or its affiliates.
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 
9 // Use, modification and distribution is subject to the Boost Software License,
10 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 
13 #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_SSF_HPP
14 #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_SSF_HPP
15 
16 
17 #include <boost/geometry/core/cs.hpp>
18 #include <boost/geometry/core/access.hpp>
19 #include <boost/geometry/core/radian_access.hpp>
20 
21 #include <boost/geometry/util/math.hpp>
22 #include <boost/geometry/util/promote_floating_point.hpp>
23 #include <boost/geometry/util/select_calculation_type.hpp>
24 
25 #include <boost/geometry/strategies/side.hpp>
26 #include <boost/geometry/strategies/spherical/disjoint_segment_box.hpp>
27 #include <boost/geometry/strategies/spherical/envelope_segment.hpp>
28 //#include <boost/geometry/strategies/concepts/side_concept.hpp>
29 
30 
31 namespace boost { namespace geometry
32 {
33 
34 
35 namespace strategy { namespace side
36 {
37 
38 #ifndef DOXYGEN_NO_DETAIL
39 namespace detail
40 {
41 
42 template <typename T>
spherical_side_formula(T const & lambda1,T const & delta1,T const & lambda2,T const & delta2,T const & lambda,T const & delta)43 int spherical_side_formula(T const& lambda1, T const& delta1,
44                            T const& lambda2, T const& delta2,
45                            T const& lambda, T const& delta)
46 {
47     // Create temporary points (vectors) on unit a sphere
48     T const cos_delta1 = cos(delta1);
49     T const c1x = cos_delta1 * cos(lambda1);
50     T const c1y = cos_delta1 * sin(lambda1);
51     T const c1z = sin(delta1);
52 
53     T const cos_delta2 = cos(delta2);
54     T const c2x = cos_delta2 * cos(lambda2);
55     T const c2y = cos_delta2 * sin(lambda2);
56     T const c2z = sin(delta2);
57 
58     // (Third point is converted directly)
59     T const cos_delta = cos(delta);
60 
61     // Apply the "Spherical Side Formula" as presented on my blog
62     T const dist
63         = (c1y * c2z - c1z * c2y) * cos_delta * cos(lambda)
64         + (c1z * c2x - c1x * c2z) * cos_delta * sin(lambda)
65         + (c1x * c2y - c1y * c2x) * sin(delta);
66 
67     T zero = T();
68     return math::equals(dist, zero) ? 0
69         : dist > zero ? 1
70         : -1; // dist < zero
71 }
72 
73 }
74 #endif // DOXYGEN_NO_DETAIL
75 
76 /*!
77 \brief Check at which side of a Great Circle segment a point lies
78          left of segment (> 0), right of segment (< 0), on segment (0)
79 \ingroup strategies
80 \tparam CalculationType \tparam_calculation
81  */
82 template <typename CalculationType = void>
83 class spherical_side_formula
84 {
85 
86 public :
87     typedef strategy::envelope::spherical_segment<CalculationType> envelope_strategy_type;
88 
get_envelope_strategy()89     static inline envelope_strategy_type get_envelope_strategy()
90     {
91         return envelope_strategy_type();
92     }
93 
94     typedef strategy::disjoint::segment_box_spherical disjoint_strategy_type;
95 
get_disjoint_strategy()96     static inline disjoint_strategy_type get_disjoint_strategy()
97     {
98         return disjoint_strategy_type();
99     }
100 
101     template <typename P1, typename P2, typename P>
apply(P1 const & p1,P2 const & p2,P const & p)102     static inline int apply(P1 const& p1, P2 const& p2, P const& p)
103     {
104         typedef typename promote_floating_point
105             <
106                 typename select_calculation_type_alt
107                     <
108                         CalculationType,
109                         P1, P2, P
110                     >::type
111             >::type calculation_type;
112 
113         calculation_type const lambda1 = get_as_radian<0>(p1);
114         calculation_type const delta1 = get_as_radian<1>(p1);
115         calculation_type const lambda2 = get_as_radian<0>(p2);
116         calculation_type const delta2 = get_as_radian<1>(p2);
117         calculation_type const lambda = get_as_radian<0>(p);
118         calculation_type const delta = get_as_radian<1>(p);
119 
120         return detail::spherical_side_formula(lambda1, delta1,
121                                               lambda2, delta2,
122                                               lambda, delta);
123     }
124 };
125 
126 
127 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
128 namespace services
129 {
130 
131 /*template <typename CalculationType>
132 struct default_strategy<spherical_polar_tag, CalculationType>
133 {
134     typedef spherical_side_formula<CalculationType> type;
135 };*/
136 
137 template <typename CalculationType>
138 struct default_strategy<spherical_equatorial_tag, CalculationType>
139 {
140     typedef spherical_side_formula<CalculationType> type;
141 };
142 
143 template <typename CalculationType>
144 struct default_strategy<geographic_tag, CalculationType>
145 {
146     typedef spherical_side_formula<CalculationType> type;
147 };
148 
149 }
150 #endif
151 
152 }} // namespace strategy::side
153 
154 }} // namespace boost::geometry
155 
156 
157 #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_SSF_HPP
158