side_by_triangle.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP
  12. #include <boost/mpl/if.hpp>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/geometry/arithmetic/determinant.hpp>
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/util/select_coordinate_type.hpp>
  17. #include <boost/geometry/strategies/side.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace strategy { namespace side
  21. {
  22. /*!
  23. \brief Check at which side of a segment a point lies:
  24. left of segment (> 0), right of segment (< 0), on segment (0)
  25. \ingroup strategies
  26. \tparam CalculationType \tparam_calculation
  27. */
  28. template <typename CalculationType = void>
  29. class side_by_triangle
  30. {
  31. public :
  32. // Template member function, because it is not always trivial
  33. // or convenient to explicitly mention the typenames in the
  34. // strategy-struct itself.
  35. // Types can be all three different. Therefore it is
  36. // not implemented (anymore) as "segment"
  37. template <typename P1, typename P2, typename P>
  38. static inline int apply(P1 const& p1, P2 const& p2, P const& p)
  39. {
  40. typedef typename boost::mpl::if_c
  41. <
  42. boost::is_void<CalculationType>::type::value,
  43. typename select_most_precise
  44. <
  45. typename select_most_precise
  46. <
  47. typename coordinate_type<P1>::type,
  48. typename coordinate_type<P2>::type
  49. >::type,
  50. typename coordinate_type<P>::type
  51. >::type,
  52. CalculationType
  53. >::type coordinate_type;
  54. coordinate_type const x = get<0>(p);
  55. coordinate_type const y = get<1>(p);
  56. coordinate_type const sx1 = get<0>(p1);
  57. coordinate_type const sy1 = get<1>(p1);
  58. coordinate_type const sx2 = get<0>(p2);
  59. coordinate_type const sy2 = get<1>(p2);
  60. // Promote float->double, small int->int
  61. typedef typename select_most_precise
  62. <
  63. coordinate_type,
  64. double
  65. >::type promoted_type;
  66. promoted_type const dx = sx2 - sx1;
  67. promoted_type const dy = sy2 - sy1;
  68. promoted_type const dpx = x - sx1;
  69. promoted_type const dpy = y - sy1;
  70. promoted_type const s
  71. = geometry::detail::determinant<promoted_type>
  72. (
  73. dx, dy,
  74. dpx, dpy
  75. );
  76. promoted_type const zero = promoted_type();
  77. return math::equals(s, zero) ? 0
  78. : s > zero ? 1
  79. : -1;
  80. }
  81. };
  82. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  83. namespace services
  84. {
  85. template <typename CalculationType>
  86. struct default_strategy<cartesian_tag, CalculationType>
  87. {
  88. typedef side_by_triangle<CalculationType> type;
  89. };
  90. }
  91. #endif
  92. }} // namespace strategy::side
  93. }} // namespace boost::geometry
  94. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_SIDE_BY_TRIANGLE_HPP