side_by_triangle.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2015, 2017.
  6. // Modifications copyright (c) 2015-2017, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/type_traits/is_integral.hpp>
  18. #include <boost/type_traits/is_void.hpp>
  19. #include <boost/geometry/arithmetic/determinant.hpp>
  20. #include <boost/geometry/core/access.hpp>
  21. #include <boost/geometry/util/select_coordinate_type.hpp>
  22. #include <boost/geometry/strategies/cartesian/disjoint_segment_box.hpp>
  23. #include <boost/geometry/strategies/cartesian/envelope_segment.hpp>
  24. #include <boost/geometry/strategies/compare.hpp>
  25. #include <boost/geometry/strategies/side.hpp>
  26. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  27. namespace boost { namespace geometry
  28. {
  29. namespace strategy { namespace side
  30. {
  31. /*!
  32. \brief Check at which side of a segment a point lies:
  33. left of segment (> 0), right of segment (< 0), on segment (0)
  34. \ingroup strategies
  35. \tparam CalculationType \tparam_calculation
  36. */
  37. template <typename CalculationType = void>
  38. class side_by_triangle
  39. {
  40. template <typename Policy>
  41. struct eps_policy
  42. {
  43. eps_policy() {}
  44. template <typename Type>
  45. eps_policy(Type const& a, Type const& b, Type const& c, Type const& d)
  46. : policy(a, b, c, d)
  47. {}
  48. Policy policy;
  49. };
  50. struct eps_empty
  51. {
  52. eps_empty() {}
  53. template <typename Type>
  54. eps_empty(Type const&, Type const&, Type const&, Type const&) {}
  55. };
  56. public :
  57. typedef strategy::envelope::cartesian_segment<CalculationType> envelope_strategy_type;
  58. static inline envelope_strategy_type get_envelope_strategy()
  59. {
  60. return envelope_strategy_type();
  61. }
  62. typedef strategy::disjoint::segment_box disjoint_strategy_type;
  63. static inline disjoint_strategy_type get_disjoint_strategy()
  64. {
  65. return disjoint_strategy_type();
  66. }
  67. // Template member function, because it is not always trivial
  68. // or convenient to explicitly mention the typenames in the
  69. // strategy-struct itself.
  70. // Types can be all three different. Therefore it is
  71. // not implemented (anymore) as "segment"
  72. template
  73. <
  74. typename CoordinateType,
  75. typename PromotedType,
  76. typename P1,
  77. typename P2,
  78. typename P,
  79. typename EpsPolicy
  80. >
  81. static inline
  82. PromotedType side_value(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & eps_policy)
  83. {
  84. CoordinateType const x = get<0>(p);
  85. CoordinateType const y = get<1>(p);
  86. CoordinateType const sx1 = get<0>(p1);
  87. CoordinateType const sy1 = get<1>(p1);
  88. CoordinateType const sx2 = get<0>(p2);
  89. CoordinateType const sy2 = get<1>(p2);
  90. PromotedType const dx = sx2 - sx1;
  91. PromotedType const dy = sy2 - sy1;
  92. PromotedType const dpx = x - sx1;
  93. PromotedType const dpy = y - sy1;
  94. eps_policy = EpsPolicy(dx, dy, dpx, dpy);
  95. return geometry::detail::determinant<PromotedType>
  96. (
  97. dx, dy,
  98. dpx, dpy
  99. );
  100. }
  101. template
  102. <
  103. typename CoordinateType,
  104. typename PromotedType,
  105. typename P1,
  106. typename P2,
  107. typename P
  108. >
  109. static inline
  110. PromotedType side_value(P1 const& p1, P2 const& p2, P const& p)
  111. {
  112. eps_empty dummy;
  113. return side_value<CoordinateType, PromotedType>(p1, p2, p, dummy);
  114. }
  115. template
  116. <
  117. typename CoordinateType,
  118. typename PromotedType,
  119. bool AreAllIntegralCoordinates
  120. >
  121. struct compute_side_value
  122. {
  123. template <typename P1, typename P2, typename P, typename EpsPolicy>
  124. static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
  125. {
  126. return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
  127. }
  128. };
  129. template <typename CoordinateType, typename PromotedType>
  130. struct compute_side_value<CoordinateType, PromotedType, false>
  131. {
  132. template <typename P1, typename P2, typename P, typename EpsPolicy>
  133. static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
  134. {
  135. // For robustness purposes, first check if any two points are
  136. // the same; in this case simply return that the points are
  137. // collinear
  138. if (geometry::detail::equals::equals_point_point(p1, p2)
  139. || geometry::detail::equals::equals_point_point(p1, p)
  140. || geometry::detail::equals::equals_point_point(p2, p))
  141. {
  142. return PromotedType(0);
  143. }
  144. // The side_by_triangle strategy computes the signed area of
  145. // the point triplet (p1, p2, p); as such it is (in theory)
  146. // invariant under cyclic permutations of its three arguments.
  147. //
  148. // In the context of numerical errors that arise in
  149. // floating-point computations, and in order to make the strategy
  150. // consistent with respect to cyclic permutations of its three
  151. // arguments, we cyclically permute them so that the first
  152. // argument is always the lexicographically smallest point.
  153. typedef compare::cartesian<compare::less> less;
  154. if (less::apply(p, p1))
  155. {
  156. if (less::apply(p, p2))
  157. {
  158. // p is the lexicographically smallest
  159. return side_value<CoordinateType, PromotedType>(p, p1, p2, epsp);
  160. }
  161. else
  162. {
  163. // p2 is the lexicographically smallest
  164. return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
  165. }
  166. }
  167. if (less::apply(p1, p2))
  168. {
  169. // p1 is the lexicographically smallest
  170. return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
  171. }
  172. else
  173. {
  174. // p2 is the lexicographically smallest
  175. return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
  176. }
  177. }
  178. };
  179. template <typename P1, typename P2, typename P>
  180. static inline int apply(P1 const& p1, P2 const& p2, P const& p)
  181. {
  182. typedef typename coordinate_type<P1>::type coordinate_type1;
  183. typedef typename coordinate_type<P2>::type coordinate_type2;
  184. typedef typename coordinate_type<P>::type coordinate_type3;
  185. typedef typename boost::mpl::if_c
  186. <
  187. boost::is_void<CalculationType>::type::value,
  188. typename select_most_precise
  189. <
  190. typename select_most_precise
  191. <
  192. coordinate_type1, coordinate_type2
  193. >::type,
  194. coordinate_type3
  195. >::type,
  196. CalculationType
  197. >::type coordinate_type;
  198. // Promote float->double, small int->int
  199. typedef typename select_most_precise
  200. <
  201. coordinate_type,
  202. double
  203. >::type promoted_type;
  204. bool const are_all_integral_coordinates =
  205. boost::is_integral<coordinate_type1>::value
  206. && boost::is_integral<coordinate_type2>::value
  207. && boost::is_integral<coordinate_type3>::value;
  208. eps_policy< math::detail::equals_factor_policy<promoted_type> > epsp;
  209. promoted_type s = compute_side_value
  210. <
  211. coordinate_type, promoted_type, are_all_integral_coordinates
  212. >::apply(p1, p2, p, epsp);
  213. promoted_type const zero = promoted_type();
  214. return math::detail::equals_by_policy(s, zero, epsp.policy) ? 0
  215. : s > zero ? 1
  216. : -1;
  217. }
  218. };
  219. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  220. namespace services
  221. {
  222. template <typename CalculationType>
  223. struct default_strategy<cartesian_tag, CalculationType>
  224. {
  225. typedef side_by_triangle<CalculationType> type;
  226. };
  227. }
  228. #endif
  229. }} // namespace strategy::side
  230. }} // namespace boost::geometry
  231. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP