point_on_border.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 2017, 2018.
  6. // Modifications copyright (c) 2017-2018 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.Dimension. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_ON_BORDER_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_ON_BORDER_HPP
  15. #include <cstddef>
  16. #include <boost/range.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/geometry/core/tags.hpp>
  19. #include <boost/geometry/core/point_type.hpp>
  20. #include <boost/geometry/core/ring_type.hpp>
  21. #include <boost/geometry/geometries/concepts/check.hpp>
  22. #include <boost/geometry/algorithms/assign.hpp>
  23. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  24. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  25. #include <boost/geometry/util/condition.hpp>
  26. namespace boost { namespace geometry
  27. {
  28. #ifndef DOXYGEN_NO_DETAIL
  29. namespace detail { namespace point_on_border
  30. {
  31. struct get_point
  32. {
  33. template <typename Point>
  34. static inline bool apply(Point& destination, Point const& source)
  35. {
  36. destination = source;
  37. return true;
  38. }
  39. };
  40. struct point_on_range
  41. {
  42. // Version with iterator
  43. template<typename Point, typename Iterator>
  44. static inline bool apply(Point& point, Iterator begin, Iterator end)
  45. {
  46. if (begin == end)
  47. {
  48. return false;
  49. }
  50. geometry::detail::conversion::convert_point_to_point(*begin, point);
  51. return true;
  52. }
  53. // Version with range
  54. template<typename Point, typename Range>
  55. static inline bool apply(Point& point, Range const& range)
  56. {
  57. return apply(point, boost::begin(range), boost::end(range));
  58. }
  59. };
  60. struct point_on_polygon
  61. {
  62. template<typename Point, typename Polygon>
  63. static inline bool apply(Point& point, Polygon const& polygon)
  64. {
  65. return point_on_range::apply(point, exterior_ring(polygon));
  66. }
  67. };
  68. struct point_on_box
  69. {
  70. template<typename Point, typename Box>
  71. static inline bool apply(Point& point, Box const& box)
  72. {
  73. detail::assign::assign_box_2d_corner<min_corner, min_corner>(box, point);
  74. return true;
  75. }
  76. };
  77. template <typename Policy>
  78. struct point_on_multi
  79. {
  80. template<typename Point, typename MultiGeometry>
  81. static inline bool apply(Point& point, MultiGeometry const& multi)
  82. {
  83. // Take a point on the first multi-geometry
  84. // (i.e. the first that is not empty)
  85. for (typename boost::range_iterator
  86. <
  87. MultiGeometry const
  88. >::type it = boost::begin(multi);
  89. it != boost::end(multi);
  90. ++it)
  91. {
  92. if (Policy::apply(point, *it))
  93. {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. };
  100. }} // namespace detail::point_on_border
  101. #endif // DOXYGEN_NO_DETAIL
  102. #ifndef DOXYGEN_NO_DISPATCH
  103. namespace dispatch
  104. {
  105. template <typename GeometryTag>
  106. struct point_on_border
  107. {};
  108. template <>
  109. struct point_on_border<point_tag>
  110. : detail::point_on_border::get_point
  111. {};
  112. template <>
  113. struct point_on_border<linestring_tag>
  114. : detail::point_on_border::point_on_range
  115. {};
  116. template <>
  117. struct point_on_border<ring_tag>
  118. : detail::point_on_border::point_on_range
  119. {};
  120. template <>
  121. struct point_on_border<polygon_tag>
  122. : detail::point_on_border::point_on_polygon
  123. {};
  124. template <>
  125. struct point_on_border<box_tag>
  126. : detail::point_on_border::point_on_box
  127. {};
  128. template <>
  129. struct point_on_border<multi_polygon_tag>
  130. : detail::point_on_border::point_on_multi
  131. <
  132. detail::point_on_border::point_on_polygon
  133. >
  134. {};
  135. template <>
  136. struct point_on_border<multi_linestring_tag>
  137. : detail::point_on_border::point_on_multi
  138. <
  139. detail::point_on_border::point_on_range
  140. >
  141. {};
  142. } // namespace dispatch
  143. #endif // DOXYGEN_NO_DISPATCH
  144. /*!
  145. \brief Take point on a border
  146. \ingroup overlay
  147. \tparam Geometry geometry type. This also defines the type of the output point
  148. \param point to assign
  149. \param geometry geometry to take point from
  150. \return TRUE if successful, else false.
  151. It is only false if polygon/line have no points
  152. \note for a polygon, it is always a point on the exterior ring
  153. */
  154. template <typename Point, typename Geometry>
  155. inline bool point_on_border(Point& point, Geometry const& geometry)
  156. {
  157. concepts::check<Point>();
  158. concepts::check<Geometry const>();
  159. return dispatch::point_on_border
  160. <
  161. typename tag<Geometry>::type
  162. >::apply(point, geometry);
  163. }
  164. }} // namespace boost::geometry
  165. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_ON_BORDER_HPP