// Boost.Geometry (aka GGL, Generic Geometry Library) // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland // 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_DETAIL_OVERLAY_OVERLAY_HPP #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_OVERLAY_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE # include #endif #ifdef BOOST_GEOMETRY_TIME_OVERLAY # include #endif namespace boost { namespace geometry { #ifndef DOXYGEN_NO_DETAIL namespace detail { namespace overlay { // Skip for assemble process template inline bool skip(TurnInfo const& turn_info) { return (turn_info.discarded || turn_info.both(operation_union)) && ! turn_info.any_blocked() && ! turn_info.both(operation_intersection) ; } template inline void map_turns(Map& map, TurnPoints const& turn_points) { typedef typename boost::range_value::type turn_point_type; typedef typename turn_point_type::container_type container_type; for (typename boost::range_iterator::type it = boost::begin(turn_points); it != boost::end(turn_points); ++it) { if (! skip(*it)) { for (typename boost::range_iterator::type op_it = boost::begin(it->operations); op_it != boost::end(it->operations); ++op_it) { ring_identifier ring_id ( op_it->seg_id.source_index, op_it->seg_id.multi_index, op_it->seg_id.ring_index ); map[ring_id]++; } } } } template < typename GeometryOut, overlay_type Direction, bool ReverseOut, typename Geometry1, typename Geometry2, typename OutputIterator > inline OutputIterator return_if_one_input_is_empty(Geometry1 const& geometry1, Geometry2 const& geometry2, OutputIterator out) { typedef std::deque < typename geometry::ring_type::type > ring_container_type; typedef ring_properties::type> properties; // Silence warning C4127: conditional expression is constant #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable : 4127) #endif // Union: return either of them // Intersection: return nothing // Difference: return first of them if (Direction == overlay_intersection || (Direction == overlay_difference && geometry::num_points(geometry1) == 0)) { return out; } #if defined(_MSC_VER) #pragma warning(pop) #endif std::map empty; std::map all_of_one_of_them; select_rings(geometry1, geometry2, empty, all_of_one_of_them, false); ring_container_type rings; assign_parents(geometry1, geometry2, rings, all_of_one_of_them); return add_rings(all_of_one_of_them, geometry1, geometry2, rings, out); } template < typename Geometry1, typename Geometry2, bool Reverse1, bool Reverse2, bool ReverseOut, typename GeometryOut, overlay_type Direction > struct overlay { template static inline OutputIterator apply( Geometry1 const& geometry1, Geometry2 const& geometry2, RobustPolicy const& robust_policy, OutputIterator out, Strategy const& ) { if ( geometry::num_points(geometry1) == 0 && geometry::num_points(geometry2) == 0 ) { return out; } if ( geometry::num_points(geometry1) == 0 || geometry::num_points(geometry2) == 0 ) { return return_if_one_input_is_empty < GeometryOut, Direction, ReverseOut >(geometry1, geometry2, out); } typedef typename geometry::point_type::type point_type; typedef detail::overlay::traversal_turn_info < point_type, typename geometry::segment_ratio_type::type > turn_info; typedef std::deque container_type; typedef std::deque < typename geometry::ring_type::type > ring_container_type; container_type turn_points; #ifdef BOOST_GEOMETRY_TIME_OVERLAY boost::timer timer; #endif #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE std::cout << "get turns" << std::endl; #endif detail::get_turns::no_interrupt_policy policy; geometry::get_turns < Reverse1, Reverse2, detail::overlay::assign_null_policy >(geometry1, geometry2, robust_policy, turn_points, policy); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "get_turns: " << timer.elapsed() << std::endl; #endif #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE std::cout << "enrich" << std::endl; #endif typename Strategy::side_strategy_type side_strategy; geometry::enrich_intersection_points(turn_points, Direction == overlay_union ? geometry::detail::overlay::operation_union : geometry::detail::overlay::operation_intersection, geometry1, geometry2, robust_policy, side_strategy); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "enrich_intersection_points: " << timer.elapsed() << std::endl; #endif #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE std::cout << "traverse" << std::endl; #endif // Traverse through intersection/turn points and create rings of them. // Note that these rings are always in clockwise order, even in CCW polygons, // and are marked as "to be reversed" below ring_container_type rings; traverse::apply ( geometry1, geometry2, Direction == overlay_union ? geometry::detail::overlay::operation_union : geometry::detail::overlay::operation_intersection, robust_policy, turn_points, rings ); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "traverse: " << timer.elapsed() << std::endl; #endif std::map map; map_turns(map, turn_points); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "map_turns: " << timer.elapsed() << std::endl; #endif typedef ring_properties::type> properties; std::map selected; select_rings(geometry1, geometry2, map, selected, ! turn_points.empty()); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "select_rings: " << timer.elapsed() << std::endl; #endif // Add rings created during traversal { ring_identifier id(2, 0, -1); for (typename boost::range_iterator::type it = boost::begin(rings); it != boost::end(rings); ++it) { selected[id] = properties(*it, true); selected[id].reversed = ReverseOut; id.multi_index++; } } #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "add traversal rings: " << timer.elapsed() << std::endl; #endif assign_parents(geometry1, geometry2, rings, selected); #ifdef BOOST_GEOMETRY_TIME_OVERLAY std::cout << "assign_parents: " << timer.elapsed() << std::endl; #endif return add_rings(selected, geometry1, geometry2, rings, out); } }; }} // namespace detail::overlay #endif // DOXYGEN_NO_DETAIL }} // namespace boost::geometry #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_OVERLAY_HPP