add_rings.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017-2020.
  5. // Modifications copyright (c) 2017-2020, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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_ALGORITHMS_DETAIL_OVERLAY_ADD_RINGS_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ADD_RINGS_HPP
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/value_type.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/geometry/core/closure.hpp>
  17. #include <boost/geometry/core/exception.hpp>
  18. #include <boost/geometry/algorithms/area.hpp>
  19. #include <boost/geometry/algorithms/detail/overlay/convert_ring.hpp>
  20. #include <boost/geometry/algorithms/detail/overlay/get_ring.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. #ifndef DOXYGEN_NO_DETAIL
  24. namespace detail { namespace overlay
  25. {
  26. template
  27. <
  28. typename GeometryOut,
  29. typename Geometry1,
  30. typename Geometry2,
  31. typename RingCollection
  32. >
  33. inline void convert_and_add(GeometryOut& result,
  34. Geometry1 const& geometry1, Geometry2 const& geometry2,
  35. RingCollection const& collection,
  36. ring_identifier id,
  37. bool reversed, bool append)
  38. {
  39. typedef typename geometry::tag<Geometry1>::type tag1;
  40. typedef typename geometry::tag<Geometry2>::type tag2;
  41. typedef typename geometry::tag<GeometryOut>::type tag_out;
  42. if (id.source_index == 0)
  43. {
  44. convert_ring<tag_out>::apply(result,
  45. get_ring<tag1>::apply(id, geometry1),
  46. append, reversed);
  47. }
  48. else if (id.source_index == 1)
  49. {
  50. convert_ring<tag_out>::apply(result,
  51. get_ring<tag2>::apply(id, geometry2),
  52. append, reversed);
  53. }
  54. else if (id.source_index == 2)
  55. {
  56. convert_ring<tag_out>::apply(result,
  57. get_ring<void>::apply(id, collection),
  58. append, reversed);
  59. }
  60. }
  61. enum add_rings_error_handling
  62. {
  63. add_rings_ignore_unordered,
  64. add_rings_add_unordered,
  65. add_rings_throw_if_reversed
  66. };
  67. template
  68. <
  69. typename GeometryOut,
  70. typename SelectionMap,
  71. typename Geometry1,
  72. typename Geometry2,
  73. typename RingCollection,
  74. typename OutputIterator,
  75. typename Strategy
  76. >
  77. inline OutputIterator add_rings(SelectionMap const& map,
  78. Geometry1 const& geometry1, Geometry2 const& geometry2,
  79. RingCollection const& collection,
  80. OutputIterator out,
  81. Strategy const& strategy,
  82. add_rings_error_handling error_handling = add_rings_ignore_unordered)
  83. {
  84. std::size_t const min_num_points = core_detail::closure::minimum_ring_size
  85. <
  86. geometry::closure
  87. <
  88. typename boost::range_value
  89. <
  90. RingCollection const
  91. >::type
  92. >::value
  93. >::value;
  94. for (auto const& pair : map)
  95. {
  96. if (! pair.second.discarded
  97. && pair.second.parent.source_index == -1)
  98. {
  99. GeometryOut result;
  100. convert_and_add(result, geometry1, geometry2, collection,
  101. pair.first, pair.second.reversed, false);
  102. // Add children
  103. for (auto const& child : pair.second.children)
  104. {
  105. auto mit = map.find(child);
  106. if (mit != map.end() && ! mit->second.discarded)
  107. {
  108. convert_and_add(result, geometry1, geometry2, collection,
  109. child, mit->second.reversed, true);
  110. }
  111. }
  112. // Only add rings if they satisfy minimal requirements.
  113. // This cannot be done earlier (during traversal), not
  114. // everything is figured out yet (sum of positive/negative rings)
  115. if (geometry::num_points(result) >= min_num_points)
  116. {
  117. typedef typename geometry::area_result<GeometryOut, Strategy>::type area_type;
  118. area_type const area = geometry::area(result, strategy);
  119. area_type const zero = 0;
  120. // Ignore if area is 0
  121. if (! math::equals(area, zero))
  122. {
  123. if (error_handling == add_rings_add_unordered
  124. || area > zero)
  125. {
  126. *out++ = result;
  127. }
  128. else if (error_handling == add_rings_throw_if_reversed)
  129. {
  130. BOOST_THROW_EXCEPTION(invalid_output_exception());
  131. }
  132. }
  133. }
  134. }
  135. }
  136. return out;
  137. }
  138. template
  139. <
  140. typename GeometryOut,
  141. typename SelectionMap,
  142. typename Geometry,
  143. typename RingCollection,
  144. typename OutputIterator,
  145. typename Strategy
  146. >
  147. inline OutputIterator add_rings(SelectionMap const& map,
  148. Geometry const& geometry,
  149. RingCollection const& collection,
  150. OutputIterator out,
  151. Strategy const& strategy)
  152. {
  153. Geometry empty;
  154. return add_rings<GeometryOut>(map, geometry, empty, collection, out, strategy);
  155. }
  156. }} // namespace detail::overlay
  157. #endif // DOXYGEN_NO_DETAIL
  158. }} // namespace geometry
  159. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ADD_RINGS_HPP