1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2015, 2017.
8 // Modifications copyright (c) 2015-2017, Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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)
20 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
21 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
23 #include <boost/mpl/if.hpp>
24 #include <boost/type_traits/is_integral.hpp>
25 #include <boost/type_traits/is_void.hpp>
27 #include <boost/geometry/arithmetic/determinant.hpp>
28 #include <boost/geometry/core/access.hpp>
29 #include <boost/geometry/util/select_coordinate_type.hpp>
31 #include <boost/geometry/strategies/cartesian/disjoint_segment_box.hpp>
32 #include <boost/geometry/strategies/cartesian/envelope_segment.hpp>
33 #include <boost/geometry/strategies/compare.hpp>
34 #include <boost/geometry/strategies/side.hpp>
36 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
39 namespace boost { namespace geometry
42 namespace strategy { namespace side
46 \brief Check at which side of a segment a point lies:
47 left of segment (> 0), right of segment (< 0), on segment (0)
49 \tparam CalculationType \tparam_calculation
51 template <typename CalculationType = void>
52 class side_by_triangle
54 template <typename Policy>
58 template <typename Type>
59 eps_policy(Type const& a, Type const& b, Type const& c, Type const& d)
68 template <typename Type>
69 eps_empty(Type const&, Type const&, Type const&, Type const&) {}
73 typedef strategy::envelope::cartesian_segment<CalculationType> envelope_strategy_type;
75 static inline envelope_strategy_type get_envelope_strategy()
77 return envelope_strategy_type();
80 typedef strategy::disjoint::segment_box disjoint_strategy_type;
82 static inline disjoint_strategy_type get_disjoint_strategy()
84 return disjoint_strategy_type();
87 // Template member function, because it is not always trivial
88 // or convenient to explicitly mention the typenames in the
89 // strategy-struct itself.
91 // Types can be all three different. Therefore it is
92 // not implemented (anymore) as "segment"
96 typename CoordinateType,
97 typename PromotedType,
104 PromotedType side_value(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & eps_policy)
106 CoordinateType const x = get<0>(p);
107 CoordinateType const y = get<1>(p);
109 CoordinateType const sx1 = get<0>(p1);
110 CoordinateType const sy1 = get<1>(p1);
111 CoordinateType const sx2 = get<0>(p2);
112 CoordinateType const sy2 = get<1>(p2);
114 PromotedType const dx = sx2 - sx1;
115 PromotedType const dy = sy2 - sy1;
116 PromotedType const dpx = x - sx1;
117 PromotedType const dpy = y - sy1;
119 eps_policy = EpsPolicy(dx, dy, dpx, dpy);
121 return geometry::detail::determinant<PromotedType>
131 typename CoordinateType,
132 typename PromotedType,
138 PromotedType side_value(P1 const& p1, P2 const& p2, P const& p)
141 return side_value<CoordinateType, PromotedType>(p1, p2, p, dummy);
147 typename CoordinateType,
148 typename PromotedType,
149 bool AreAllIntegralCoordinates
151 struct compute_side_value
153 template <typename P1, typename P2, typename P, typename EpsPolicy>
154 static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
156 return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
160 template <typename CoordinateType, typename PromotedType>
161 struct compute_side_value<CoordinateType, PromotedType, false>
163 template <typename P1, typename P2, typename P, typename EpsPolicy>
164 static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
166 // For robustness purposes, first check if any two points are
167 // the same; in this case simply return that the points are
169 if (geometry::detail::equals::equals_point_point(p1, p2)
170 || geometry::detail::equals::equals_point_point(p1, p)
171 || geometry::detail::equals::equals_point_point(p2, p))
173 return PromotedType(0);
176 // The side_by_triangle strategy computes the signed area of
177 // the point triplet (p1, p2, p); as such it is (in theory)
178 // invariant under cyclic permutations of its three arguments.
180 // In the context of numerical errors that arise in
181 // floating-point computations, and in order to make the strategy
182 // consistent with respect to cyclic permutations of its three
183 // arguments, we cyclically permute them so that the first
184 // argument is always the lexicographically smallest point.
186 typedef compare::cartesian<compare::less> less;
188 if (less::apply(p, p1))
190 if (less::apply(p, p2))
192 // p is the lexicographically smallest
193 return side_value<CoordinateType, PromotedType>(p, p1, p2, epsp);
197 // p2 is the lexicographically smallest
198 return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
202 if (less::apply(p1, p2))
204 // p1 is the lexicographically smallest
205 return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
209 // p2 is the lexicographically smallest
210 return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
216 template <typename P1, typename P2, typename P>
217 static inline int apply(P1 const& p1, P2 const& p2, P const& p)
219 typedef typename coordinate_type<P1>::type coordinate_type1;
220 typedef typename coordinate_type<P2>::type coordinate_type2;
221 typedef typename coordinate_type<P>::type coordinate_type3;
223 typedef typename boost::mpl::if_c
225 boost::is_void<CalculationType>::type::value,
226 typename select_most_precise
228 typename select_most_precise
230 coordinate_type1, coordinate_type2
235 >::type coordinate_type;
237 // Promote float->double, small int->int
238 typedef typename select_most_precise
242 >::type promoted_type;
244 bool const are_all_integral_coordinates =
245 boost::is_integral<coordinate_type1>::value
246 && boost::is_integral<coordinate_type2>::value
247 && boost::is_integral<coordinate_type3>::value;
249 eps_policy< math::detail::equals_factor_policy<promoted_type> > epsp;
250 promoted_type s = compute_side_value
252 coordinate_type, promoted_type, are_all_integral_coordinates
253 >::apply(p1, p2, p, epsp);
255 promoted_type const zero = promoted_type();
256 return math::detail::equals_by_policy(s, zero, epsp.policy) ? 0
264 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
268 template <typename CalculationType>
269 struct default_strategy<cartesian_tag, CalculationType>
271 typedef side_by_triangle<CalculationType> type;
277 }} // namespace strategy::side
279 }} // namespace boost::geometry
282 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP