side_by_triangle.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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, 2018, 2019.
  6. // Modifications copyright (c) 2015-2019, 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.hpp>
  24. #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
  25. #include <boost/geometry/strategies/compare.hpp>
  26. #include <boost/geometry/strategies/side.hpp>
  27. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  28. namespace boost { namespace geometry
  29. {
  30. namespace strategy { namespace side
  31. {
  32. /*!
  33. \brief Check at which side of a segment a point lies:
  34. left of segment (> 0), right of segment (< 0), on segment (0)
  35. \ingroup strategies
  36. \tparam CalculationType \tparam_calculation
  37. */
  38. template <typename CalculationType = void>
  39. class side_by_triangle
  40. {
  41. template <typename Policy>
  42. struct eps_policy
  43. {
  44. eps_policy() {}
  45. template <typename Type>
  46. eps_policy(Type const& a, Type const& b, Type const& c, Type const& d)
  47. : policy(a, b, c, d)
  48. {}
  49. Policy policy;
  50. };
  51. struct eps_empty
  52. {
  53. eps_empty() {}
  54. template <typename Type>
  55. eps_empty(Type const&, Type const&, Type const&, Type const&) {}
  56. };
  57. public :
  58. typedef cartesian_tag cs_tag;
  59. typedef strategy::envelope::cartesian<CalculationType> envelope_strategy_type;
  60. static inline envelope_strategy_type get_envelope_strategy()
  61. {
  62. return envelope_strategy_type();
  63. }
  64. typedef strategy::disjoint::segment_box disjoint_strategy_type;
  65. static inline disjoint_strategy_type get_disjoint_strategy()
  66. {
  67. return disjoint_strategy_type();
  68. }
  69. typedef strategy::within::cartesian_point_point equals_point_point_strategy_type;
  70. static inline equals_point_point_strategy_type get_equals_point_point_strategy()
  71. {
  72. return equals_point_point_strategy_type();
  73. }
  74. // Template member function, because it is not always trivial
  75. // or convenient to explicitly mention the typenames in the
  76. // strategy-struct itself.
  77. // Types can be all three different. Therefore it is
  78. // not implemented (anymore) as "segment"
  79. template
  80. <
  81. typename CoordinateType,
  82. typename PromotedType,
  83. typename P1,
  84. typename P2,
  85. typename P,
  86. typename EpsPolicy
  87. >
  88. static inline
  89. PromotedType side_value(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & eps_policy)
  90. {
  91. CoordinateType const x = get<0>(p);
  92. CoordinateType const y = get<1>(p);
  93. CoordinateType const sx1 = get<0>(p1);
  94. CoordinateType const sy1 = get<1>(p1);
  95. CoordinateType const sx2 = get<0>(p2);
  96. CoordinateType const sy2 = get<1>(p2);
  97. PromotedType const dx = sx2 - sx1;
  98. PromotedType const dy = sy2 - sy1;
  99. PromotedType const dpx = x - sx1;
  100. PromotedType const dpy = y - sy1;
  101. eps_policy = EpsPolicy(dx, dy, dpx, dpy);
  102. return geometry::detail::determinant<PromotedType>
  103. (
  104. dx, dy,
  105. dpx, dpy
  106. );
  107. }
  108. template
  109. <
  110. typename CoordinateType,
  111. typename PromotedType,
  112. typename P1,
  113. typename P2,
  114. typename P
  115. >
  116. static inline
  117. PromotedType side_value(P1 const& p1, P2 const& p2, P const& p)
  118. {
  119. eps_empty dummy;
  120. return side_value<CoordinateType, PromotedType>(p1, p2, p, dummy);
  121. }
  122. template
  123. <
  124. typename CoordinateType,
  125. typename PromotedType,
  126. bool AreAllIntegralCoordinates
  127. >
  128. struct compute_side_value
  129. {
  130. template <typename P1, typename P2, typename P, typename EpsPolicy>
  131. static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
  132. {
  133. return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
  134. }
  135. };
  136. template <typename CoordinateType, typename PromotedType>
  137. struct compute_side_value<CoordinateType, PromotedType, false>
  138. {
  139. template <typename P1, typename P2, typename P, typename EpsPolicy>
  140. static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
  141. {
  142. // For robustness purposes, first check if any two points are
  143. // the same; in this case simply return that the points are
  144. // collinear
  145. if (equals_point_point(p1, p2)
  146. || equals_point_point(p1, p)
  147. || equals_point_point(p2, p))
  148. {
  149. return PromotedType(0);
  150. }
  151. // The side_by_triangle strategy computes the signed area of
  152. // the point triplet (p1, p2, p); as such it is (in theory)
  153. // invariant under cyclic permutations of its three arguments.
  154. //
  155. // In the context of numerical errors that arise in
  156. // floating-point computations, and in order to make the strategy
  157. // consistent with respect to cyclic permutations of its three
  158. // arguments, we cyclically permute them so that the first
  159. // argument is always the lexicographically smallest point.
  160. typedef compare::cartesian<compare::less> less;
  161. if (less::apply(p, p1))
  162. {
  163. if (less::apply(p, p2))
  164. {
  165. // p is the lexicographically smallest
  166. return side_value<CoordinateType, PromotedType>(p, p1, p2, epsp);
  167. }
  168. else
  169. {
  170. // p2 is the lexicographically smallest
  171. return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
  172. }
  173. }
  174. if (less::apply(p1, p2))
  175. {
  176. // p1 is the lexicographically smallest
  177. return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
  178. }
  179. else
  180. {
  181. // p2 is the lexicographically smallest
  182. return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
  183. }
  184. }
  185. };
  186. template <typename P1, typename P2, typename P>
  187. static inline int apply(P1 const& p1, P2 const& p2, P const& p)
  188. {
  189. typedef typename coordinate_type<P1>::type coordinate_type1;
  190. typedef typename coordinate_type<P2>::type coordinate_type2;
  191. typedef typename coordinate_type<P>::type coordinate_type3;
  192. typedef typename boost::mpl::if_c
  193. <
  194. boost::is_void<CalculationType>::type::value,
  195. typename select_most_precise
  196. <
  197. typename select_most_precise
  198. <
  199. coordinate_type1, coordinate_type2
  200. >::type,
  201. coordinate_type3
  202. >::type,
  203. CalculationType
  204. >::type coordinate_type;
  205. // Promote float->double, small int->int
  206. typedef typename select_most_precise
  207. <
  208. coordinate_type,
  209. double
  210. >::type promoted_type;
  211. bool const are_all_integral_coordinates =
  212. boost::is_integral<coordinate_type1>::value
  213. && boost::is_integral<coordinate_type2>::value
  214. && boost::is_integral<coordinate_type3>::value;
  215. eps_policy< math::detail::equals_factor_policy<promoted_type> > epsp;
  216. promoted_type s = compute_side_value
  217. <
  218. coordinate_type, promoted_type, are_all_integral_coordinates
  219. >::apply(p1, p2, p, epsp);
  220. promoted_type const zero = promoted_type();
  221. return math::detail::equals_by_policy(s, zero, epsp.policy) ? 0
  222. : s > zero ? 1
  223. : -1;
  224. }
  225. private:
  226. template <typename P1, typename P2>
  227. static inline bool equals_point_point(P1 const& p1, P2 const& p2)
  228. {
  229. typedef equals_point_point_strategy_type strategy_t;
  230. return geometry::detail::equals::equals_point_point(p1, p2, strategy_t());
  231. }
  232. };
  233. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  234. namespace services
  235. {
  236. template <typename CalculationType>
  237. struct default_strategy<cartesian_tag, CalculationType>
  238. {
  239. typedef side_by_triangle<CalculationType> type;
  240. };
  241. }
  242. #endif
  243. }} // namespace strategy::side
  244. }} // namespace boost::geometry
  245. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP