point_in_poly_franklin.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // This file was modified by Oracle on 2018, 2019.
  6. // Modifications copyright (c) 2018, 2019, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_POLY_FRANKLIN_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_POLY_FRANKLIN_HPP
  15. #include <boost/geometry/core/access.hpp>
  16. #include <boost/geometry/core/coordinate_type.hpp>
  17. #include <boost/geometry/util/select_calculation_type.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. namespace strategy { namespace within
  21. {
  22. /*!
  23. \brief Within detection using cross counting
  24. \ingroup strategies
  25. \tparam Point \tparam_point
  26. \tparam PointOfSegment \tparam_segment_point
  27. \tparam CalculationType \tparam_calculation
  28. \author adapted from Randolph Franklin algorithm
  29. \author Barend and Maarten, 1995
  30. \author Revised for templatized library, Barend Gehrels, 2007
  31. \return true if point is in ring, works for closed rings in both directions
  32. \note Does NOT work correctly for point ON border
  33. \qbk{
  34. [heading See also]
  35. [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
  36. }
  37. */
  38. template
  39. <
  40. typename Point_, // for backward compatibility
  41. typename PointOfSegment_ = Point_, // for backward compatibility
  42. typename CalculationType = void
  43. >
  44. class franklin
  45. {
  46. template <typename Point, typename PointOfSegment>
  47. struct calculation_type
  48. : select_calculation_type
  49. <
  50. Point,
  51. PointOfSegment,
  52. CalculationType
  53. >
  54. {};
  55. /*! subclass to keep state */
  56. class crossings
  57. {
  58. bool crosses;
  59. public :
  60. friend class franklin;
  61. inline crossings()
  62. : crosses(false)
  63. {}
  64. };
  65. public :
  66. typedef crossings state_type;
  67. template <typename Point, typename PointOfSegment>
  68. static inline bool apply(Point const& point,
  69. PointOfSegment const& seg1, PointOfSegment const& seg2,
  70. crossings& state)
  71. {
  72. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  73. calc_t const& px = get<0>(point);
  74. calc_t const& py = get<1>(point);
  75. calc_t const& x1 = get<0>(seg1);
  76. calc_t const& y1 = get<1>(seg1);
  77. calc_t const& x2 = get<0>(seg2);
  78. calc_t const& y2 = get<1>(seg2);
  79. if (
  80. ( (y2 <= py && py < y1) || (y1 <= py && py < y2) )
  81. && (px < (x1 - x2) * (py - y2) / (y1 - y2) + x2)
  82. )
  83. {
  84. state.crosses = ! state.crosses;
  85. }
  86. return true;
  87. }
  88. static inline int result(crossings const& state)
  89. {
  90. return state.crosses ? 1 : -1;
  91. }
  92. };
  93. }} // namespace strategy::within
  94. }} // namespace boost::geometry
  95. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_POINT_IN_POLY_FRANKLIN_HPP