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, 2018, 2019.
8 // Modifications copyright (c) 2015-2019, 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.hpp>
33 #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
34 #include <boost/geometry/strategies/compare.hpp>
35 #include <boost/geometry/strategies/side.hpp>
37 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
40 namespace boost { namespace geometry
43 namespace strategy { namespace side
47 \brief Check at which side of a segment a point lies:
48 left of segment (> 0), right of segment (< 0), on segment (0)
50 \tparam CalculationType \tparam_calculation
52 template <typename CalculationType = void>
53 class side_by_triangle
55 template <typename Policy>
59 template <typename Type>
60 eps_policy(Type const& a, Type const& b, Type const& c, Type const& d)
69 template <typename Type>
70 eps_empty(Type const&, Type const&, Type const&, Type const&) {}
74 typedef cartesian_tag cs_tag;
76 typedef strategy::envelope::cartesian<CalculationType> envelope_strategy_type;
78 static inline envelope_strategy_type get_envelope_strategy()
80 return envelope_strategy_type();
83 typedef strategy::disjoint::segment_box disjoint_strategy_type;
85 static inline disjoint_strategy_type get_disjoint_strategy()
87 return disjoint_strategy_type();
90 typedef strategy::within::cartesian_point_point equals_point_point_strategy_type;
91 static inline equals_point_point_strategy_type get_equals_point_point_strategy()
93 return equals_point_point_strategy_type();
96 // Template member function, because it is not always trivial
97 // or convenient to explicitly mention the typenames in the
98 // strategy-struct itself.
100 // Types can be all three different. Therefore it is
101 // not implemented (anymore) as "segment"
105 typename CoordinateType,
106 typename PromotedType,
113 PromotedType side_value(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & eps_policy)
115 CoordinateType const x = get<0>(p);
116 CoordinateType const y = get<1>(p);
118 CoordinateType const sx1 = get<0>(p1);
119 CoordinateType const sy1 = get<1>(p1);
120 CoordinateType const sx2 = get<0>(p2);
121 CoordinateType const sy2 = get<1>(p2);
123 PromotedType const dx = sx2 - sx1;
124 PromotedType const dy = sy2 - sy1;
125 PromotedType const dpx = x - sx1;
126 PromotedType const dpy = y - sy1;
128 eps_policy = EpsPolicy(dx, dy, dpx, dpy);
130 return geometry::detail::determinant<PromotedType>
140 typename CoordinateType,
141 typename PromotedType,
147 PromotedType side_value(P1 const& p1, P2 const& p2, P const& p)
150 return side_value<CoordinateType, PromotedType>(p1, p2, p, dummy);
156 typename CoordinateType,
157 typename PromotedType,
158 bool AreAllIntegralCoordinates
160 struct compute_side_value
162 template <typename P1, typename P2, typename P, typename EpsPolicy>
163 static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
165 return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
169 template <typename CoordinateType, typename PromotedType>
170 struct compute_side_value<CoordinateType, PromotedType, false>
172 template <typename P1, typename P2, typename P, typename EpsPolicy>
173 static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
175 // For robustness purposes, first check if any two points are
176 // the same; in this case simply return that the points are
178 if (equals_point_point(p1, p2)
179 || equals_point_point(p1, p)
180 || equals_point_point(p2, p))
182 return PromotedType(0);
185 // The side_by_triangle strategy computes the signed area of
186 // the point triplet (p1, p2, p); as such it is (in theory)
187 // invariant under cyclic permutations of its three arguments.
189 // In the context of numerical errors that arise in
190 // floating-point computations, and in order to make the strategy
191 // consistent with respect to cyclic permutations of its three
192 // arguments, we cyclically permute them so that the first
193 // argument is always the lexicographically smallest point.
195 typedef compare::cartesian<compare::less> less;
197 if (less::apply(p, p1))
199 if (less::apply(p, p2))
201 // p is the lexicographically smallest
202 return side_value<CoordinateType, PromotedType>(p, p1, p2, epsp);
206 // p2 is the lexicographically smallest
207 return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
211 if (less::apply(p1, p2))
213 // p1 is the lexicographically smallest
214 return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
218 // p2 is the lexicographically smallest
219 return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
225 template <typename P1, typename P2, typename P>
226 static inline int apply(P1 const& p1, P2 const& p2, P const& p)
228 typedef typename coordinate_type<P1>::type coordinate_type1;
229 typedef typename coordinate_type<P2>::type coordinate_type2;
230 typedef typename coordinate_type<P>::type coordinate_type3;
232 typedef typename boost::mpl::if_c
234 boost::is_void<CalculationType>::type::value,
235 typename select_most_precise
237 typename select_most_precise
239 coordinate_type1, coordinate_type2
244 >::type coordinate_type;
246 // Promote float->double, small int->int
247 typedef typename select_most_precise
251 >::type promoted_type;
253 bool const are_all_integral_coordinates =
254 boost::is_integral<coordinate_type1>::value
255 && boost::is_integral<coordinate_type2>::value
256 && boost::is_integral<coordinate_type3>::value;
258 eps_policy< math::detail::equals_factor_policy<promoted_type> > epsp;
259 promoted_type s = compute_side_value
261 coordinate_type, promoted_type, are_all_integral_coordinates
262 >::apply(p1, p2, p, epsp);
264 promoted_type const zero = promoted_type();
265 return math::detail::equals_by_policy(s, zero, epsp.policy) ? 0
271 template <typename P1, typename P2>
272 static inline bool equals_point_point(P1 const& p1, P2 const& p2)
274 typedef equals_point_point_strategy_type strategy_t;
275 return geometry::detail::equals::equals_point_point(p1, p2, strategy_t());
280 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
284 template <typename CalculationType>
285 struct default_strategy<cartesian_tag, CalculationType>
287 typedef side_by_triangle<CalculationType> type;
293 }} // namespace strategy::side
295 }} // namespace boost::geometry
298 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP