Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:35:20

0001 // Boost.Geometry (aka GGL, Generic Geometry Library)
0002 
0003 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
0004 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
0005 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
0006 // Copyright (c) 2014-2017 Adam Wulkiewicz, Lodz, Poland.
0007 
0008 // This file was modified by Oracle on 2017-2023.
0009 // Modifications copyright (c) 2017-2023 Oracle and/or its affiliates.
0010 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
0011 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
0012 
0013 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
0014 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
0015 
0016 // Use, modification and distribution is subject to the Boost Software License,
0017 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0018 // http://www.boost.org/LICENSE_1_0.txt)
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 // Silence warning C4127: conditional expression is constant
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 // Correct a box: make min/max correct
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         // Currently only for Cartesian coordinates
0081         // (or spherical without crossing dateline)
0082         // Future version: adapt using strategies
0083         detail::for_each_dimension<Box>([&](auto dimension)
0084         {
0085             if (get<min_corner, dimension>(box) > get<max_corner, dimension>(box))
0086             {
0087                 // Swap the coordinates
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 // Close a ring, if not closed
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         // Correct closure if necessary
0106         detail::correct_closure::close_or_open_ring::apply(r);
0107 
0108         // NOTE: calculate_point_order should probably be used here instead.
0109 
0110         // Check area
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 // Correct a polygon: normalizes all rings, sets outer ring clockwise, sets all
0121 // inner rings counter clockwise (or vice versa depending on orientation)
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 }} // namespace detail::correct
0140 #endif // DOXYGEN_NO_DETAIL
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 } // namespace dispatch
0202 #endif // DOXYGEN_NO_DISPATCH
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         // NOTE: calculate_point_order strategy should probably be used here instead.
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         // NOTE: calculate_point_order strategy should probably be used here instead.
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 } // namespace resolve_strategy
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 } // namespace resolve_dynamic
0295 
0296 
0297 /*!
0298 \brief Corrects a geometry
0299 \details Corrects a geometry: all rings which are wrongly oriented with respect
0300     to their expected orientation are reversed. To all rings which do not have a
0301     closing point and are typed as they should have one, the first point is
0302     appended. Also boxes can be corrected.
0303 \ingroup correct
0304 \tparam Geometry \tparam_geometry
0305 \param geometry \param_geometry which will be corrected if necessary
0306 
0307 \qbk{[include reference/algorithms/correct.qbk]}
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 \brief Corrects a geometry
0317 \details Corrects a geometry: all rings which are wrongly oriented with respect
0318     to their expected orientation are reversed. To all rings which do not have a
0319     closing point and are typed as they should have one, the first point is
0320     appended. Also boxes can be corrected.
0321 \ingroup correct
0322 \tparam Geometry \tparam_geometry
0323 \tparam Strategy \tparam_strategy{Area}
0324 \param geometry \param_geometry which will be corrected if necessary
0325 \param strategy \param_strategy{area}
0326 
0327 \qbk{distinguish,with strategy}
0328 
0329 \qbk{[include reference/algorithms/correct.qbk]}
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 }} // namespace boost::geometry
0342 
0343 
0344 #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_HPP