unique.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2020.
  7. // Modifications copyright (c) 2020 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP
  16. #include <algorithm>
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
  20. #include <boost/geometry/core/interior_rings.hpp>
  21. #include <boost/geometry/core/mutable_range.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/geometries/concepts/check.hpp>
  24. #include <boost/geometry/policies/compare.hpp>
  25. namespace boost { namespace geometry
  26. {
  27. #ifndef DOXYGEN_NO_DETAIL
  28. namespace detail { namespace unique
  29. {
  30. struct range_unique
  31. {
  32. template <typename Range, typename ComparePolicy>
  33. static inline void apply(Range& range, ComparePolicy const& policy)
  34. {
  35. typename boost::range_iterator<Range>::type it
  36. = std::unique
  37. (
  38. boost::begin(range),
  39. boost::end(range),
  40. policy
  41. );
  42. traits::resize<Range>::apply(range, it - boost::begin(range));
  43. }
  44. };
  45. struct polygon_unique
  46. {
  47. template <typename Polygon, typename ComparePolicy>
  48. static inline void apply(Polygon& polygon, ComparePolicy const& policy)
  49. {
  50. range_unique::apply(exterior_ring(polygon), policy);
  51. typename interior_return_type<Polygon>::type
  52. rings = interior_rings(polygon);
  53. for (typename detail::interior_iterator<Polygon>::type
  54. it = boost::begin(rings); it != boost::end(rings); ++it)
  55. {
  56. range_unique::apply(*it, policy);
  57. }
  58. }
  59. };
  60. template <typename Policy>
  61. struct multi_unique
  62. {
  63. template <typename MultiGeometry, typename ComparePolicy>
  64. static inline void apply(MultiGeometry& multi, ComparePolicy const& compare)
  65. {
  66. for (typename boost::range_iterator<MultiGeometry>::type
  67. it = boost::begin(multi);
  68. it != boost::end(multi);
  69. ++it)
  70. {
  71. Policy::apply(*it, compare);
  72. }
  73. }
  74. };
  75. }} // namespace detail::unique
  76. #endif // DOXYGEN_NO_DETAIL
  77. #ifndef DOXYGEN_NO_DISPATCH
  78. namespace dispatch
  79. {
  80. template
  81. <
  82. typename Geometry,
  83. typename Tag = typename tag<Geometry>::type
  84. >
  85. struct unique
  86. {
  87. template <typename ComparePolicy>
  88. static inline void apply(Geometry&, ComparePolicy const& )
  89. {}
  90. };
  91. template <typename Ring>
  92. struct unique<Ring, ring_tag>
  93. : detail::unique::range_unique
  94. {};
  95. template <typename LineString>
  96. struct unique<LineString, linestring_tag>
  97. : detail::unique::range_unique
  98. {};
  99. template <typename Polygon>
  100. struct unique<Polygon, polygon_tag>
  101. : detail::unique::polygon_unique
  102. {};
  103. // For points, unique is not applicable and does nothing
  104. // (Note that it is not "spatially unique" but that it removes duplicate coordinates,
  105. // like std::unique does). Spatially unique is "dissolve" which can (or will be)
  106. // possible for multi-points as well, removing points at the same location.
  107. template <typename MultiLineString>
  108. struct unique<MultiLineString, multi_linestring_tag>
  109. : detail::unique::multi_unique<detail::unique::range_unique>
  110. {};
  111. template <typename MultiPolygon>
  112. struct unique<MultiPolygon, multi_polygon_tag>
  113. : detail::unique::multi_unique<detail::unique::polygon_unique>
  114. {};
  115. } // namespace dispatch
  116. #endif
  117. /*!
  118. \brief \brief_calc{minimal set}
  119. \ingroup unique
  120. \details \details_calc{unique,minimal set (where duplicate consecutive points are removed)}.
  121. \tparam Geometry \tparam_geometry
  122. \param geometry \param_geometry which will be made unique
  123. \qbk{[include reference/algorithms/unique.qbk]}
  124. */
  125. template <typename Geometry>
  126. inline void unique(Geometry& geometry)
  127. {
  128. concepts::check<Geometry>();
  129. // Default strategy is the default point-comparison policy
  130. typedef geometry::equal_to
  131. <
  132. typename geometry::point_type<Geometry>::type
  133. > policy;
  134. dispatch::unique<Geometry>::apply(geometry, policy());
  135. }
  136. }} // namespace boost::geometry
  137. #endif // BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP