1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
7 
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 
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 
15 #ifndef BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP
16 #define BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP
17 
18 #include <algorithm>
19 
20 #include <boost/range.hpp>
21 #include <boost/type_traits/remove_reference.hpp>
22 
23 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
24 #include <boost/geometry/core/interior_rings.hpp>
25 #include <boost/geometry/core/mutable_range.hpp>
26 #include <boost/geometry/core/tags.hpp>
27 #include <boost/geometry/geometries/concepts/check.hpp>
28 #include <boost/geometry/policies/compare.hpp>
29 
30 
31 namespace boost { namespace geometry
32 {
33 
34 
35 #ifndef DOXYGEN_NO_DETAIL
36 namespace detail { namespace unique
37 {
38 
39 
40 struct range_unique
41 {
42     template <typename Range, typename ComparePolicy>
applyboost::geometry::detail::unique::range_unique43     static inline void apply(Range& range, ComparePolicy const& policy)
44     {
45         typename boost::range_iterator<Range>::type it
46             = std::unique
47                 (
48                     boost::begin(range),
49                     boost::end(range),
50                     policy
51                 );
52 
53         traits::resize<Range>::apply(range, it - boost::begin(range));
54     }
55 };
56 
57 
58 struct polygon_unique
59 {
60     template <typename Polygon, typename ComparePolicy>
applyboost::geometry::detail::unique::polygon_unique61     static inline void apply(Polygon& polygon, ComparePolicy const& policy)
62     {
63         range_unique::apply(exterior_ring(polygon), policy);
64 
65         typename interior_return_type<Polygon>::type
66             rings = interior_rings(polygon);
67 
68         for (typename detail::interior_iterator<Polygon>::type
69                 it = boost::begin(rings); it != boost::end(rings); ++it)
70         {
71             range_unique::apply(*it, policy);
72         }
73     }
74 };
75 
76 
77 template <typename Policy>
78 struct multi_unique
79 {
80     template <typename MultiGeometry, typename ComparePolicy>
applyboost::geometry::detail::unique::multi_unique81     static inline void apply(MultiGeometry& multi, ComparePolicy const& compare)
82     {
83         for (typename boost::range_iterator<MultiGeometry>::type
84                 it = boost::begin(multi);
85             it != boost::end(multi);
86             ++it)
87         {
88             Policy::apply(*it, compare);
89         }
90     }
91 };
92 
93 
94 }} // namespace detail::unique
95 #endif // DOXYGEN_NO_DETAIL
96 
97 
98 
99 #ifndef DOXYGEN_NO_DISPATCH
100 namespace dispatch
101 {
102 
103 
104 template
105 <
106     typename Geometry,
107     typename Tag = typename tag<Geometry>::type
108 >
109 struct unique
110 {
111     template <typename ComparePolicy>
applyboost::geometry::dispatch::unique112     static inline void apply(Geometry&, ComparePolicy const& )
113     {}
114 };
115 
116 
117 template <typename Ring>
118 struct unique<Ring, ring_tag>
119     : detail::unique::range_unique
120 {};
121 
122 
123 template <typename LineString>
124 struct unique<LineString, linestring_tag>
125     : detail::unique::range_unique
126 {};
127 
128 
129 template <typename Polygon>
130 struct unique<Polygon, polygon_tag>
131     : detail::unique::polygon_unique
132 {};
133 
134 
135 // For points, unique is not applicable and does nothing
136 // (Note that it is not "spatially unique" but that it removes duplicate coordinates,
137 //  like std::unique does). Spatially unique is "dissolve" which can (or will be)
138 //  possible for multi-points as well, removing points at the same location.
139 
140 
141 template <typename MultiLineString>
142 struct unique<MultiLineString, multi_linestring_tag>
143     : detail::unique::multi_unique<detail::unique::range_unique>
144 {};
145 
146 
147 template <typename MultiPolygon>
148 struct unique<MultiPolygon, multi_polygon_tag>
149     : detail::unique::multi_unique<detail::unique::polygon_unique>
150 {};
151 
152 
153 } // namespace dispatch
154 #endif
155 
156 
157 /*!
158 \brief \brief_calc{minimal set}
159 \ingroup unique
160 \details \details_calc{unique,minimal set (where duplicate consecutive points are removed)}.
161 \tparam Geometry \tparam_geometry
162 \param geometry \param_geometry which will be made unique
163 
164 \qbk{[include reference/algorithms/unique.qbk]}
165 */
166 template <typename Geometry>
unique(Geometry & geometry)167 inline void unique(Geometry& geometry)
168 {
169     concept::check<Geometry>();
170 
171     // Default strategy is the default point-comparison policy
172     typedef geometry::equal_to
173         <
174             typename geometry::point_type<Geometry>::type
175         > policy;
176 
177 
178     dispatch::unique<Geometry>::apply(geometry, policy());
179 }
180 
181 }} // namespace boost::geometry
182 
183 
184 #endif // BOOST_GEOMETRY_ALGORITHMS_UNIQUE_HPP
185