unique.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/core/interior_rings.hpp>
  20. #include <boost/geometry/core/mutable_range.hpp>
  21. #include <boost/geometry/core/tags.hpp>
  22. #include <boost/geometry/geometries/concepts/check.hpp>
  23. #include <boost/geometry/policies/compare.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace unique
  28. {
  29. struct range_unique
  30. {
  31. template <typename Range, typename ComparePolicy>
  32. static inline void apply(Range& range, ComparePolicy const& policy)
  33. {
  34. auto it
  35. = std::unique
  36. (
  37. boost::begin(range),
  38. boost::end(range),
  39. policy
  40. );
  41. traits::resize<Range>::apply(range, it - boost::begin(range));
  42. }
  43. };
  44. struct polygon_unique
  45. {
  46. template <typename Polygon, typename ComparePolicy>
  47. static inline void apply(Polygon& polygon, ComparePolicy const& policy)
  48. {
  49. range_unique::apply(exterior_ring(polygon), policy);
  50. auto&& rings = interior_rings(polygon);
  51. for (auto it = boost::begin(rings); it != boost::end(rings); ++it)
  52. {
  53. range_unique::apply(*it, policy);
  54. }
  55. }
  56. };
  57. template <typename Policy>
  58. struct multi_unique
  59. {
  60. template <typename MultiGeometry, typename ComparePolicy>
  61. static inline void apply(MultiGeometry& multi, ComparePolicy const& compare)
  62. {
  63. for (auto it = boost::begin(multi); it != boost::end(multi); ++it)
  64. {
  65. Policy::apply(*it, compare);
  66. }
  67. }
  68. };
  69. }} // namespace detail::unique
  70. #endif // DOXYGEN_NO_DETAIL
  71. #ifndef DOXYGEN_NO_DISPATCH
  72. namespace dispatch
  73. {
  74. template
  75. <
  76. typename Geometry,
  77. typename Tag = typename tag<Geometry>::type
  78. >
  79. struct unique
  80. {
  81. template <typename ComparePolicy>
  82. static inline void apply(Geometry&, ComparePolicy const& )
  83. {}
  84. };
  85. template <typename Ring>
  86. struct unique<Ring, ring_tag>
  87. : detail::unique::range_unique
  88. {};
  89. template <typename LineString>
  90. struct unique<LineString, linestring_tag>
  91. : detail::unique::range_unique
  92. {};
  93. template <typename Polygon>
  94. struct unique<Polygon, polygon_tag>
  95. : detail::unique::polygon_unique
  96. {};
  97. // For points, unique is not applicable and does nothing
  98. // (Note that it is not "spatially unique" but that it removes duplicate coordinates,
  99. // like std::unique does). Spatially unique is "dissolve" which can (or will be)
  100. // possible for multi-points as well, removing points at the same location.
  101. template <typename MultiLineString>
  102. struct unique<MultiLineString, multi_linestring_tag>
  103. : detail::unique::multi_unique<detail::unique::range_unique>
  104. {};
  105. template <typename MultiPolygon>
  106. struct unique<MultiPolygon, multi_polygon_tag>
  107. : detail::unique::multi_unique<detail::unique::polygon_unique>
  108. {};
  109. } // namespace dispatch
  110. #endif
  111. /*!
  112. \brief \brief_calc{minimal set}
  113. \ingroup unique
  114. \details \details_calc{unique,minimal set (where duplicate consecutive points are removed)}.
  115. \tparam Geometry \tparam_geometry
  116. \param geometry \param_geometry which will be made unique
  117. \qbk{[include reference/algorithms/unique.qbk]}
  118. */
  119. template <typename Geometry>
  120. inline void unique(Geometry& geometry)
  121. {
  122. concepts::check<Geometry>();
  123. // Default strategy is the default point-comparison policy
  124. typedef geometry::equal_to
  125. <
  126. typename geometry::point_type<Geometry>::type
  127. > policy;
  128. dispatch::unique<Geometry>::apply(geometry, policy());
  129. }
  130. }} // namespace boost::geometry
  131. #endif // BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP