unique.hpp 4.6 KB

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