// Boost.Geometry (aka GGL, Generic Geometry Library) test file // // Copyright Barend Gehrels 2007-2009, 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_TEST_INTERSECTION_HPP #define BOOST_GEOMETRY_TEST_INTERSECTION_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(TEST_WITH_SVG) # include #endif template typename bg::area_result::type test_intersection(std::string const& caseid, G1 const& g1, G2 const& g2, std::size_t expected_count = 0, std::size_t expected_point_count = 0, double expected_length_or_area = 0, double percentage = 0.0001, bool make_unique = true) { static const bool is_line = bg::geometry_id::type::value == 2; //std::cout << caseid << std::endl; typedef typename bg::coordinate_type::type coordinate_type; typedef typename bg::point_type::type point_type; typedef bg::strategy_intersection < typename bg::cs_tag::type, G1, G2, point_type, CalculationType > strategy; // Check both normal behaviour, and _inserter behaviour { std::vector out; bg::intersection(g1, g2, out); } std::vector clip; bg::intersection_inserter(g1, g2, std::back_inserter(clip), strategy()); typename bg::area_result::type length_or_area = 0; std::size_t n = 0; for (typename std::vector::iterator it = clip.begin(); it != clip.end(); ++it) { if (expected_point_count > 0) { if (make_unique) { // Get a correct point-count without duplicate points // (note that overlay might be adapted to avoid duplicates) bg::unique(*it); n += bg::num_points(*it, true); } else { n += bg::num_points(*it, true); } } // instead of specialization we check it run-time here length_or_area += is_line ? bg::length(*it) : bg::area(*it); /* std::cout << std::endl << "case " << caseid << " "; std::cout << std::setprecision(20) << bg::dsv(*it) << std::endl; */ } #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST) if (expected_point_count > 0) { BOOST_CHECK_MESSAGE(n == expected_point_count, "intersection: " << caseid << " #points expected: " << expected_point_count << " detected: " << n << " type: " << string_from_type::name() ); } if (expected_count > 0) { BOOST_CHECK_MESSAGE(clip.size() == expected_count, "intersection: " << caseid << " #outputs expected: " << expected_count << " detected: " << clip.size() << " type: " << string_from_type::name() ); } BOOST_CHECK_CLOSE(length_or_area, expected_length_or_area, percentage); #endif #if defined(TEST_WITH_SVG) { bool const ccw = bg::point_order::value == bg::counterclockwise || bg::point_order::value == bg::counterclockwise; bool const open = bg::closure::value == bg::open || bg::closure::value == bg::open; std::ostringstream filename; filename << "intersection_" << caseid << "_" << string_from_type::name() << string_from_type::name() << (ccw ? "_ccw" : "") << (open ? "_open" : "") << ".svg"; std::ofstream svg(filename.str().c_str()); bg::svg_mapper mapper(svg, 500, 500); mapper.add(g1); mapper.add(g2); mapper.map(g1, is_line ? "opacity:0.6;stroke:rgb(0,255,0);stroke-width:5" : "fill-opacity:0.5;fill:rgb(153,204,0);" "stroke:rgb(153,204,0);stroke-width:3"); mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);" "stroke:rgb(51,51,153);stroke-width:3"); for (typename std::vector::const_iterator it = clip.begin(); it != clip.end(); ++it) { mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);" "stroke:rgb(255,0,255);stroke-width:8"); } } #endif return length_or_area; } template typename bg::area_result::type test_one(std::string const& caseid, std::string const& wkt1, std::string const& wkt2, std::size_t expected_count = 0, std::size_t expected_point_count = 0, double expected_length_or_area = 0, double percentage = 0.0001, bool make_unique = true) { G1 g1; bg::read_wkt(wkt1, g1); G2 g2; bg::read_wkt(wkt2, g2); // Reverse if necessary bg::correct(g1); bg::correct(g2); return test_intersection(caseid, g1, g2, expected_count, expected_point_count, expected_length_or_area, percentage, make_unique); } #endif