// Boost.Geometry (aka GGL, Generic Geometry Library) // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP #define BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace geometry { #ifndef DOXYGEN_NO_DETAIL namespace detail { namespace conversion { template < typename Point, typename Box, std::size_t Index, std::size_t Dimension, std::size_t DimensionCount > struct point_to_box { static inline void apply(Point const& point, Box& box) { typedef typename coordinate_type::type coordinate_type; set(box, boost::numeric_cast(get(point))); point_to_box < Point, Box, Index, Dimension + 1, DimensionCount >::apply(point, box); } }; template < typename Point, typename Box, std::size_t Index, std::size_t DimensionCount > struct point_to_box { static inline void apply(Point const& , Box& ) {} }; template struct box_to_range { static inline void apply(Box const& box, Range& range) { traits::resize::apply(range, Close ? 5 : 4); assign_box_corners_oriented(box, range); if (Close) { range[4] = range[0]; } } }; template struct segment_to_range { static inline void apply(Segment const& segment, Range& range) { traits::resize::apply(range, 2); typename boost::range_iterator::type it = boost::begin(range); assign_point_from_index<0>(segment, *it); ++it; assign_point_from_index<1>(segment, *it); } }; template < typename Range1, typename Range2, bool Reverse = false > struct range_to_range { typedef typename reversible_view < Range1 const, Reverse ? iterate_reverse : iterate_forward >::type rview_type; typedef typename closeable_view < rview_type const, geometry::closure::value >::type view_type; static inline void apply(Range1 const& source, Range2& destination) { geometry::clear(destination); rview_type rview(source); // We consider input always as closed, and skip last // point for open output. view_type view(rview); int n = boost::size(view); if (geometry::closure::value == geometry::open) { n--; } int i = 0; for (typename boost::range_iterator::type it = boost::begin(view); it != boost::end(view) && i < n; ++it, ++i) { geometry::append(destination, *it); } } }; template struct polygon_to_polygon { typedef range_to_range < typename geometry::ring_type::type, typename geometry::ring_type::type, geometry::point_order::value != geometry::point_order::value > per_ring; static inline void apply(Polygon1 const& source, Polygon2& destination) { // Clearing managed per ring, and in the resizing of interior rings per_ring::apply(geometry::exterior_ring(source), geometry::exterior_ring(destination)); // Container should be resizeable traits::resize < typename boost::remove_reference < typename traits::interior_mutable_type::type >::type >::apply(interior_rings(destination), num_interior_rings(source)); typename interior_return_type::type rings_source = interior_rings(source); typename interior_return_type::type rings_dest = interior_rings(destination); BOOST_AUTO_TPL(it_source, boost::begin(rings_source)); BOOST_AUTO_TPL(it_dest, boost::begin(rings_dest)); for ( ; it_source != boost::end(rings_source); ++it_source, ++it_dest) { per_ring::apply(*it_source, *it_dest); } } }; }} // namespace detail::conversion #endif // DOXYGEN_NO_DETAIL #ifndef DOXYGEN_NO_DISPATCH namespace dispatch { template < typename Geometry1, typename Geometry2, typename Tag1 = typename tag_cast::type, multi_tag>::type, typename Tag2 = typename tag_cast::type, multi_tag>::type, std::size_t DimensionCount = dimension::type::value, bool UseAssignment = boost::is_same::value && !boost::is_array::value > struct convert: not_implemented > {}; template < typename Geometry1, typename Geometry2, typename Tag, std::size_t DimensionCount > struct convert { // Same geometry type -> copy whole geometry static inline void apply(Geometry1 const& source, Geometry2& destination) { destination = source; } }; template < typename Geometry1, typename Geometry2, std::size_t DimensionCount > struct convert : detail::conversion::point_to_point {}; template < typename Box1, typename Box2, std::size_t DimensionCount > struct convert : detail::conversion::indexed_to_indexed {}; template < typename Segment1, typename Segment2, std::size_t DimensionCount > struct convert : detail::conversion::indexed_to_indexed {}; template struct convert : detail::conversion::segment_to_range {}; template struct convert : detail::conversion::range_to_range < Ring1, Ring2, geometry::point_order::value != geometry::point_order::value > {}; template struct convert : detail::conversion::range_to_range {}; template struct convert : detail::conversion::polygon_to_polygon {}; template struct convert : detail::conversion::box_to_range < Box, Ring, geometry::closure::value == closed, geometry::point_order::value == counterclockwise > {}; template struct convert { static inline void apply(Box const& box, Polygon& polygon) { typedef typename ring_type::type ring_type; convert < Box, ring_type, box_tag, ring_tag, 2, false >::apply(box, exterior_ring(polygon)); } }; template struct convert { static inline void apply(Point const& point, Box& box) { detail::conversion::point_to_box < Point, Box, min_corner, 0, DimensionCount >::apply(point, box); detail::conversion::point_to_box < Point, Box, max_corner, 0, DimensionCount >::apply(point, box); } }; template struct convert { static inline void apply(Ring const& ring, Polygon& polygon) { typedef typename ring_type::type ring_type; convert < Ring, ring_type, ring_tag, ring_tag, DimensionCount, false >::apply(ring, exterior_ring(polygon)); } }; template struct convert { static inline void apply(Polygon const& polygon, Ring& ring) { typedef typename ring_type::type ring_type; convert < ring_type, Ring, ring_tag, ring_tag, DimensionCount, false >::apply(exterior_ring(polygon), ring); } }; } // namespace dispatch #endif // DOXYGEN_NO_DISPATCH /*! \brief Converts one geometry to another geometry \details The convert algorithm converts one geometry, e.g. a BOX, to another geometry, e.g. a RING. This only if it is possible and applicable. If the point-order is different, or the closure is different between two geometry types, it will be converted correctly by explicitly reversing the points or closing or opening the polygon rings. \ingroup convert \tparam Geometry1 \tparam_geometry \tparam Geometry2 \tparam_geometry \param geometry1 \param_geometry (source) \param geometry2 \param_geometry (target) \qbk{[include reference/algorithms/convert.qbk]} */ template inline void convert(Geometry1 const& geometry1, Geometry2& geometry2) { concept::check_concepts_and_equal_dimensions(); dispatch::convert::apply(geometry1, geometry2); } }} // namespace boost::geometry #endif // BOOST_GEOMETRY_ALGORITHMS_CONVERT_HPP