// 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_DIFFERENCE_HPP #define BOOST_GEOMETRY_TEST_DIFFERENCE_HPP #include #include #include #include #include #include #include #include #include #include #include #include #if defined(TEST_WITH_SVG) # include #endif template void test_difference(std::string const& caseid, G1 const& g1, G2 const& g2, std::size_t expected_count, std::size_t expected_point_count, double expected_area, double percentage = 0.0001, bool sym = false) { std::vector clip; typedef typename boost::geometry::coordinate_type::type coordinate_type; typedef typename boost::geometry::point_type::type point_type; if (sym) { boost::geometry::sym_difference(g1, g2, clip); } else { boost::geometry::difference(g1, g2, clip); } double area = 0; std::size_t n = 0; for (typename std::vector::iterator it = clip.begin(); it != clip.end(); ++it) { if (expected_point_count > 0) { // Get a correct point-count without duplicate points // (note that overlay might be adapted to avoid duplicates) boost::geometry::unique(*it); n += boost::geometry::num_points(*it); } area += boost::geometry::area(*it); } #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST) /*if (expected_point_count > 0) { BOOST_CHECK_MESSAGE(n == expected_point_count, "difference: " << caseid << " #points expected: " << expected_point_count << " detected: " << n << " type: " << string_from_type::name() ); }*/ if (expected_count > 0) { BOOST_CHECK_MESSAGE(clip.size() == expected_count, "difference: " << caseid << " #outputs expected: " << expected_count << " detected: " << clip.size() << " type: " << string_from_type::name() ); } BOOST_CHECK_CLOSE(area, expected_area, percentage); #endif #if defined(TEST_WITH_SVG) { std::ostringstream filename; filename << "difference_" << caseid << "_" << string_from_type::name() << string_from_type::name() << ".svg"; std::ofstream svg(filename.str().c_str()); boost::geometry::svg_mapper mapper(svg, 500, 500); mapper.add(g1); mapper.add(g2); mapper.map(g1, "opacity:0.6;fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1"); mapper.map(g2, "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1"); for (typename std::vector::const_iterator it = clip.begin(); it != clip.end(); ++it) { mapper.map(*it, sym ? "opacity:0.6;fill:rgb(255,255,0);stroke:rgb(255,0,0);stroke-width:5" : "opacity:0.6;fill:none;stroke:rgb(255,0,0);stroke-width:5"); } } #endif } #ifdef BOOST_GEOMETRY_CHECK_WITH_POSTGIS static int counter = 0; #endif template void test_one(std::string const& caseid, std::string const& wkt1, std::string const& wkt2, std::size_t expected_count1, std::size_t expected_point_count1, double expected_area1, std::size_t expected_count2, std::size_t expected_point_count2, double expected_area2, double percentage = 0.0001) { G1 g1; boost::geometry::read_wkt(wkt1, g1); G2 g2; boost::geometry::read_wkt(wkt2, g2); test_difference(caseid + "_a", g1, g2, expected_count1, expected_point_count1, expected_area1, percentage); test_difference(caseid + "_b", g2, g1, expected_count2, expected_point_count2, expected_area2, percentage); test_difference(caseid + "_s", g1, g2, expected_count1 + expected_count2, expected_point_count1 + expected_point_count2, expected_area1 + expected_area2, percentage, true); #ifdef BOOST_GEOMETRY_CHECK_WITH_POSTGIS std::cout << (counter > 0 ? "union " : "") << "select " << counter++ << ", '" << caseid << "' as caseid" << ", ST_NumPoints(ST_Difference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " << ", ST_NumGeometries(ST_Difference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " << ", ST_Area(ST_Difference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " //<< ", " << expected_area1 << " as expected_area_a" //<< ", " << expected_count1 << " as expected_count_a" << ", ST_NumPoints(ST_Difference(ST_GeomFromText('" << wkt2 << "'), " << " ST_GeomFromText('" << wkt1 << "'))) " << ", ST_NumGeometries(ST_Difference(ST_GeomFromText('" << wkt2 << "'), " << " ST_GeomFromText('" << wkt1 << "'))) " << ", ST_Area(ST_Difference(ST_GeomFromText('" << wkt2 << "'), " << " ST_GeomFromText('" << wkt1 << "'))) " //<< ", " << expected_area2 << " as expected_area_b" //<< ", " << expected_count2 << " as expected_count_b" << ", ST_NumPoints(ST_SymDifference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " << ", ST_NumGeometries(ST_SymDifference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " << ", ST_Area(ST_SymDifference(ST_GeomFromText('" << wkt1 << "'), " << " ST_GeomFromText('" << wkt2 << "'))) " //<< ", " << expected_area1 + expected_area2 << " as expected_area_s" //<< ", " << expected_count1 + expected_count2 << " as expected_count_s" << std::endl; #endif } #endif