File indexing completed on 2025-01-18 09:35:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP
0021 #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP
0022
0023
0024 #include <algorithm>
0025 #include <functional>
0026
0027 #include <boost/range/begin.hpp>
0028 #include <boost/range/end.hpp>
0029
0030 #include <boost/geometry/algorithms/area.hpp>
0031 #include <boost/geometry/algorithms/correct_closure.hpp>
0032 #include <boost/geometry/algorithms/detail/multi_modify.hpp>
0033 #include <boost/geometry/algorithms/detail/visit.hpp>
0034
0035 #include <boost/geometry/core/exterior_ring.hpp>
0036 #include <boost/geometry/core/interior_rings.hpp>
0037 #include <boost/geometry/core/tags.hpp>
0038 #include <boost/geometry/core/visit.hpp>
0039
0040 #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
0041 #include <boost/geometry/geometries/concepts/check.hpp>
0042
0043 #include <boost/geometry/strategies/area/cartesian.hpp>
0044 #include <boost/geometry/strategies/area/geographic.hpp>
0045 #include <boost/geometry/strategies/area/spherical.hpp>
0046 #include <boost/geometry/strategies/detail.hpp>
0047
0048 #include <boost/geometry/util/algorithm.hpp>
0049
0050
0051 namespace boost { namespace geometry
0052 {
0053
0054
0055 #if defined(_MSC_VER)
0056 #pragma warning(push)
0057 #pragma warning(disable : 4127)
0058 #endif
0059
0060 #ifndef DOXYGEN_NO_DETAIL
0061 namespace detail { namespace correct
0062 {
0063
0064 struct correct_nop
0065 {
0066 template <typename Geometry, typename Strategy>
0067 static inline void apply(Geometry& , Strategy const& )
0068 {}
0069 };
0070
0071
0072
0073 struct correct_box
0074 {
0075 template <typename Box, typename Strategy>
0076 static inline void apply(Box& box, Strategy const& )
0077 {
0078 using coordinate_type = typename geometry::coordinate_type<Box>::type;
0079
0080
0081
0082
0083 detail::for_each_dimension<Box>([&](auto dimension)
0084 {
0085 if (get<min_corner, dimension>(box) > get<max_corner, dimension>(box))
0086 {
0087
0088 coordinate_type max_value = get<min_corner, dimension>(box);
0089 coordinate_type min_value = get<max_corner, dimension>(box);
0090 set<min_corner, dimension>(box, min_value);
0091 set<max_corner, dimension>(box, max_value);
0092 }
0093 });
0094 }
0095 };
0096
0097
0098
0099 template <typename Predicate = std::less<>>
0100 struct correct_ring
0101 {
0102 template <typename Ring, typename Strategy>
0103 static inline void apply(Ring& r, Strategy const& strategy)
0104 {
0105
0106 detail::correct_closure::close_or_open_ring::apply(r);
0107
0108
0109
0110
0111 using area_t = typename area_result<Ring, Strategy>::type;
0112 area_t const zero = 0;
0113 if (Predicate()(detail::area::ring_area::apply(r, strategy), zero))
0114 {
0115 std::reverse(boost::begin(r), boost::end(r));
0116 }
0117 }
0118 };
0119
0120
0121
0122 struct correct_polygon
0123 {
0124 template <typename Polygon, typename Strategy>
0125 static inline void apply(Polygon& poly, Strategy const& strategy)
0126 {
0127 correct_ring<std::less<>>::apply(exterior_ring(poly), strategy);
0128
0129 auto&& rings = interior_rings(poly);
0130 auto const end = boost::end(rings);
0131 for (auto it = boost::begin(rings); it != end; ++it)
0132 {
0133 correct_ring<std::greater<>>::apply(*it, strategy);
0134 }
0135 }
0136 };
0137
0138
0139 }}
0140 #endif
0141
0142
0143 #ifndef DOXYGEN_NO_DISPATCH
0144 namespace dispatch
0145 {
0146
0147 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
0148 struct correct: not_implemented<Tag>
0149 {};
0150
0151 template <typename Point>
0152 struct correct<Point, point_tag>
0153 : detail::correct::correct_nop
0154 {};
0155
0156 template <typename LineString>
0157 struct correct<LineString, linestring_tag>
0158 : detail::correct::correct_nop
0159 {};
0160
0161 template <typename Segment>
0162 struct correct<Segment, segment_tag>
0163 : detail::correct::correct_nop
0164 {};
0165
0166
0167 template <typename Box>
0168 struct correct<Box, box_tag>
0169 : detail::correct::correct_box
0170 {};
0171
0172 template <typename Ring>
0173 struct correct<Ring, ring_tag>
0174 : detail::correct::correct_ring<>
0175 {};
0176
0177 template <typename Polygon>
0178 struct correct<Polygon, polygon_tag>
0179 : detail::correct::correct_polygon
0180 {};
0181
0182
0183 template <typename MultiPoint>
0184 struct correct<MultiPoint, multi_point_tag>
0185 : detail::correct::correct_nop
0186 {};
0187
0188
0189 template <typename MultiLineString>
0190 struct correct<MultiLineString, multi_linestring_tag>
0191 : detail::correct::correct_nop
0192 {};
0193
0194
0195 template <typename Geometry>
0196 struct correct<Geometry, multi_polygon_tag>
0197 : detail::multi_modify<detail::correct::correct_polygon>
0198 {};
0199
0200
0201 }
0202 #endif
0203
0204
0205 namespace resolve_strategy
0206 {
0207
0208 template
0209 <
0210 typename Strategy,
0211 bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
0212 >
0213 struct correct
0214 {
0215 template <typename Geometry>
0216 static inline void apply(Geometry& geometry, Strategy const& strategy)
0217 {
0218 dispatch::correct<Geometry>::apply(geometry, strategy);
0219 }
0220 };
0221
0222 template <typename Strategy>
0223 struct correct<Strategy, false>
0224 {
0225 template <typename Geometry>
0226 static inline void apply(Geometry& geometry, Strategy const& strategy)
0227 {
0228
0229 using geometry::strategies::area::services::strategy_converter;
0230 dispatch::correct<Geometry>::apply(geometry, strategy_converter<Strategy>::get(strategy));
0231 }
0232 };
0233
0234 template <>
0235 struct correct<default_strategy, false>
0236 {
0237 template <typename Geometry>
0238 static inline void apply(Geometry& geometry, default_strategy const& )
0239 {
0240
0241 using strategy_type = typename strategies::area::services::default_strategy
0242 <
0243 Geometry
0244 >::type;
0245 dispatch::correct<Geometry>::apply(geometry, strategy_type());
0246 }
0247 };
0248
0249 }
0250
0251
0252 namespace resolve_dynamic
0253 {
0254
0255 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
0256 struct correct
0257 {
0258 template <typename Strategy>
0259 static inline void apply(Geometry& geometry, Strategy const& strategy)
0260 {
0261 concepts::check<Geometry>();
0262 resolve_strategy::correct<Strategy>::apply(geometry, strategy);
0263 }
0264 };
0265
0266 template <typename Geometry>
0267 struct correct<Geometry, dynamic_geometry_tag>
0268 {
0269 template <typename Strategy>
0270 static inline void apply(Geometry& geometry, Strategy const& strategy)
0271 {
0272 traits::visit<Geometry>::apply([&](auto & g)
0273 {
0274 correct<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
0275 }, geometry);
0276 }
0277 };
0278
0279 template <typename Geometry>
0280 struct correct<Geometry, geometry_collection_tag>
0281 {
0282 template <typename Strategy>
0283 static inline void apply(Geometry& geometry, Strategy const& strategy)
0284 {
0285 detail::visit_breadth_first([&](auto & g)
0286 {
0287 correct<util::remove_cref_t<decltype(g)>>::apply(g, strategy);
0288 return true;
0289 }, geometry);
0290 }
0291 };
0292
0293
0294 }
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309 template <typename Geometry>
0310 inline void correct(Geometry& geometry)
0311 {
0312 resolve_dynamic::correct<Geometry>::apply(geometry, default_strategy());
0313 }
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 template <typename Geometry, typename Strategy>
0332 inline void correct(Geometry& geometry, Strategy const& strategy)
0333 {
0334 resolve_dynamic::correct<Geometry>::apply(geometry, strategy);
0335 }
0336
0337 #if defined(_MSC_VER)
0338 #pragma warning(pop)
0339 #endif
0340
0341 }}
0342
0343
0344 #endif