get_ring.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2020.
  4. // Modifications copyright (c) 2020 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_RING_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_RING_HPP
  11. #include <boost/range/size.hpp>
  12. #include <boost/range/value_type.hpp>
  13. #include <boost/geometry/core/assert.hpp>
  14. #include <boost/geometry/core/exterior_ring.hpp>
  15. #include <boost/geometry/core/interior_rings.hpp>
  16. #include <boost/geometry/core/ring_type.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/segment_identifier.hpp>
  20. #include <boost/geometry/algorithms/num_points.hpp>
  21. #include <boost/geometry/geometries/concepts/check.hpp>
  22. #include <boost/geometry/util/range.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace overlay
  27. {
  28. template<typename Tag>
  29. struct get_ring
  30. {};
  31. // A range of rings (multi-ring but that does not exist)
  32. // gets the "void" tag and is dispatched here.
  33. template<>
  34. struct get_ring<void>
  35. {
  36. template<typename Range>
  37. static inline typename boost::range_value<Range>::type const&
  38. apply(ring_identifier const& id, Range const& container)
  39. {
  40. return range::at(container, id.multi_index);
  41. }
  42. };
  43. template<>
  44. struct get_ring<ring_tag>
  45. {
  46. template<typename Ring>
  47. static inline Ring const& apply(ring_identifier const& , Ring const& ring)
  48. {
  49. return ring;
  50. }
  51. };
  52. template<>
  53. struct get_ring<box_tag>
  54. {
  55. template<typename Box>
  56. static inline Box const& apply(ring_identifier const& ,
  57. Box const& box)
  58. {
  59. return box;
  60. }
  61. };
  62. template<>
  63. struct get_ring<polygon_tag>
  64. {
  65. template<typename Polygon>
  66. static inline typename ring_return_type<Polygon const>::type const apply(
  67. ring_identifier const& id,
  68. Polygon const& polygon)
  69. {
  70. BOOST_GEOMETRY_ASSERT
  71. (
  72. id.ring_index >= -1
  73. && id.ring_index < int(boost::size(interior_rings(polygon)))
  74. );
  75. return id.ring_index < 0
  76. ? exterior_ring(polygon)
  77. : range::at(interior_rings(polygon), id.ring_index);
  78. }
  79. };
  80. template<>
  81. struct get_ring<multi_polygon_tag>
  82. {
  83. template<typename MultiPolygon>
  84. static inline typename ring_type<MultiPolygon>::type const& apply(
  85. ring_identifier const& id,
  86. MultiPolygon const& multi_polygon)
  87. {
  88. BOOST_GEOMETRY_ASSERT
  89. (
  90. id.multi_index >= 0
  91. && id.multi_index < int(boost::size(multi_polygon))
  92. );
  93. return get_ring<polygon_tag>::apply(id,
  94. range::at(multi_polygon, id.multi_index));
  95. }
  96. };
  97. // Returns the number of segments on a ring (regardless whether the ring is open or closed)
  98. template <typename Geometry>
  99. inline signed_size_type segment_count_on_ring(Geometry const& geometry,
  100. ring_identifier const& ring_id)
  101. {
  102. using tag = typename geometry::tag<Geometry>::type;
  103. // A closed polygon, a triangle of 4 points, including starting point,
  104. // contains 3 segments. So handle as if it is closed, and subtract one.
  105. return geometry::num_points(detail::overlay::get_ring<tag>::apply(ring_id, geometry), true) - 1;
  106. }
  107. // Returns the number of segments on a ring (regardless whether the ring is open or closed)
  108. template <typename Geometry>
  109. inline signed_size_type segment_count_on_ring(Geometry const& geometry,
  110. segment_identifier const& seg_id)
  111. {
  112. return segment_count_on_ring(geometry, ring_identifier(0, seg_id.multi_index, seg_id.ring_index));
  113. }
  114. // Returns the distance between the second and the first segment identifier (second-first)
  115. // It supports circular behavior and for this it is necessary to pass the geometry.
  116. // It will not report negative values
  117. template <typename Geometry>
  118. inline signed_size_type segment_distance(Geometry const& geometry,
  119. segment_identifier const& first, segment_identifier const& second)
  120. {
  121. // It is an internal function, make sure the preconditions are met
  122. BOOST_ASSERT(second.source_index == first.source_index);
  123. BOOST_ASSERT(second.multi_index == first.multi_index);
  124. BOOST_ASSERT(second.ring_index == first.ring_index);
  125. signed_size_type const result = second.segment_index - first.segment_index;
  126. if (second.segment_index >= first.segment_index)
  127. {
  128. return result;
  129. }
  130. // Take wrap into account, counting segments on the ring (passing any of the ids is fine).
  131. // Suppose point_count=10 (10 points, 9 segments), first.seg_id=7, second.seg_id=2,
  132. // then distance=9-7+2=4, being segments 7,8,0,1
  133. return segment_count_on_ring(geometry, first) + result;
  134. }
  135. }} // namespace detail::overlay
  136. #endif // DOXYGEN_NO_DETAIL
  137. }} // namespace boost::geometry
  138. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_RING_HPP