range_in_geometry.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Boost.Geometry
  2. // Copyright (c) 2017 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_RANGE_IN_GEOMETRY_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_RANGE_IN_GEOMETRY_HPP
  9. #include <boost/geometry/algorithms/covered_by.hpp>
  10. #include <boost/geometry/core/access.hpp>
  11. #include <boost/geometry/core/tags.hpp>
  12. #include <boost/geometry/iterators/point_iterator.hpp>
  13. #include <boost/range.hpp>
  14. namespace boost { namespace geometry
  15. {
  16. #ifndef DOXYGEN_NO_DETAIL
  17. namespace detail { namespace overlay
  18. {
  19. template
  20. <
  21. typename Geometry,
  22. typename Tag = typename geometry::tag<Geometry>::type
  23. >
  24. struct points_range
  25. {
  26. typedef geometry::point_iterator<Geometry const> iterator_type;
  27. explicit points_range(Geometry const& geometry)
  28. : m_geometry(geometry)
  29. {}
  30. iterator_type begin() const
  31. {
  32. return geometry::points_begin(m_geometry);
  33. }
  34. iterator_type end() const
  35. {
  36. return geometry::points_end(m_geometry);
  37. }
  38. Geometry const& m_geometry;
  39. };
  40. // Specialized because point_iterator doesn't support boxes
  41. template <typename Box>
  42. struct points_range<Box, box_tag>
  43. {
  44. typedef typename geometry::point_type<Box>::type point_type;
  45. typedef const point_type * iterator_type;
  46. explicit points_range(Box const& box)
  47. {
  48. detail::assign_box_corners(box,
  49. m_corners[0], m_corners[1], m_corners[2], m_corners[3]);
  50. }
  51. iterator_type begin() const
  52. {
  53. return m_corners;
  54. }
  55. iterator_type end() const
  56. {
  57. return m_corners + 4;
  58. }
  59. point_type m_corners[4];
  60. };
  61. template
  62. <
  63. typename Geometry,
  64. typename Tag = typename geometry::tag<Geometry>::type
  65. >
  66. struct point_in_geometry_helper
  67. {
  68. template <typename Point, typename Strategy>
  69. static inline int apply(Point const& point, Geometry const& geometry,
  70. Strategy const& strategy)
  71. {
  72. return detail::within::point_in_geometry(point, geometry, strategy);
  73. }
  74. };
  75. // Specialized because point_in_geometry doesn't support Boxes
  76. template <typename Box>
  77. struct point_in_geometry_helper<Box, box_tag>
  78. {
  79. template <typename Point, typename Strategy>
  80. static inline int apply(Point const& point, Box const& box,
  81. Strategy const&)
  82. {
  83. return geometry::covered_by(point, box) ? 1 : -1;
  84. }
  85. };
  86. // This function returns
  87. // when it finds a point of geometry1 inside or outside geometry2
  88. template <typename Geometry1, typename Geometry2, typename Strategy>
  89. static inline int range_in_geometry(Geometry1 const& geometry1,
  90. Geometry2 const& geometry2,
  91. Strategy const& strategy,
  92. bool skip_first = false)
  93. {
  94. int result = 0;
  95. points_range<Geometry1> points(geometry1);
  96. typedef typename points_range<Geometry1>::iterator_type iterator_type;
  97. iterator_type const end = points.end();
  98. iterator_type it = points.begin();
  99. if (it == end)
  100. {
  101. return result;
  102. }
  103. else if (skip_first)
  104. {
  105. ++it;
  106. }
  107. typename Strategy::template point_in_geometry_strategy
  108. <
  109. Geometry1, Geometry2
  110. >::type const in_strategy
  111. = strategy.template get_point_in_geometry_strategy<Geometry1, Geometry2>();
  112. for ( ; it != end; ++it)
  113. {
  114. result = point_in_geometry_helper<Geometry2>::apply(*it, geometry2, in_strategy);
  115. if (result != 0)
  116. {
  117. return result;
  118. }
  119. }
  120. // all points contained entirely by the boundary
  121. return result;
  122. }
  123. // This function returns if first_point1 is inside or outside geometry2 or
  124. // when it finds a point of geometry1 inside or outside geometry2
  125. template <typename Point1, typename Geometry1, typename Geometry2, typename Strategy>
  126. inline int range_in_geometry(Point1 const& first_point1,
  127. Geometry1 const& geometry1,
  128. Geometry2 const& geometry2,
  129. Strategy const& strategy)
  130. {
  131. // check a point on border of geometry1 first
  132. int result = point_in_geometry_helper<Geometry2>::apply(first_point1, geometry2,
  133. strategy.template get_point_in_geometry_strategy<Point1, Geometry2>());
  134. if (result == 0)
  135. {
  136. // if a point is on boundary of geometry2
  137. // check points of geometry1 until point inside/outside is found
  138. // NOTE: skip first point because it should be already tested above
  139. result = range_in_geometry(geometry1, geometry2, strategy, true);
  140. }
  141. return result;
  142. }
  143. }} // namespace detail::overlay
  144. #endif // DOXYGEN_NO_DETAIL
  145. }} // namespace boost::geometry
  146. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_RANGE_IN_GEOMETRY_HPP