// Boost.Geometry (aka GGL, Generic Geometry Library) // Robustness Test // Copyright (c) 2012-2015 Barend Gehrels, 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) #if defined(_MSC_VER) # pragma warning( disable : 4244 ) # pragma warning( disable : 4267 ) #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace bg = boost::geometry; template void create_svg(std::string const& filename , Geometry1 const& mp , Geometry2 const& ls , Geometry3 const& difference , Geometry3 const& intersection ) { typedef typename boost::geometry::point_type::type point_type; std::ofstream svg(filename.c_str()); boost::geometry::svg_mapper mapper(svg, 800, 800); boost::geometry::model::box box; bg::envelope(mp, box); bg::buffer(box, box, 1.0); mapper.add(box); bg::envelope(ls, box); bg::buffer(box, box, 1.0); mapper.add(box); mapper.map(mp, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3"); mapper.map(ls, "stroke-opacity:0.9;stroke:rgb(0,0,0);stroke-width:1"); mapper.map(intersection,"opacity:0.6;stroke:rgb(0,128,0);stroke-width:5"); mapper.map(difference, "opacity:0.6;stroke:rgb(255,0,255);stroke-width:5"); //;stroke-dasharray:1,7;stroke-linecap:round } template inline void make_random_linestring(Linestring& line, Generator& generator, Settings const& settings) { using namespace boost::geometry; typedef typename point_type::type point_type; typedef typename coordinate_type::type coordinate_type; coordinate_type x, y; x = generator(); y = generator(); int count = 3 + generator() % 6; int d = 0; // direction for (int i = 0; i <= count && x <= settings.field_size; i++, x++, d = 1 - d) { append(line, make(x, y + d)); append(line, make(x, y + 1 - d)); } if (d == 0 && generator() % 4 < 3 && y >= 2) { d = 1 - d; x--; y -= 2; count = 3 + generator() % 6; for (int i = 0; i <= count && x >= 0; i++, x--, d = 1 - d) { append(line, make(x, y + d)); append(line, make(x, y + 1 - d)); } } //if (settings.triangular) //{ // // Remove a point, generator says which // int c = generator() % 4; // if (c >= 1 && c <= 3) // { // ring.erase(ring.begin() + c); // } //} } template class inside_check { Geometry const& m_geo; bool& m_result; public : inside_check(Geometry const& geo, bool& result) : m_geo(geo) , m_result(result) {} inline inside_check operator=(inside_check const& input) { return inside_check(input.m_geo, input.m_result); } template inline void operator()(Point const& p) { if (! bg::covered_by(p, m_geo)) { if (m_result) { std::cout << "Does not fulfill inside check" << std::endl; } m_result = false; } } }; template class outside_check { Geometry const& m_geo; bool& m_result; public : outside_check(Geometry const& geo, bool& result) : m_geo(geo) , m_result(result) {} inline outside_check operator=(outside_check const& input) { return outside_check(input.m_geo, input.m_result); } template inline void operator()(Point const& p) { if (bg::within(p, m_geo)) { if (m_result) { std::cout << "Does not fulfill outside check" << std::endl; } m_result = false; } } }; template class border2_check { Segment const& m_segment; bool& m_result; public : border2_check(Segment const& seg, bool& result) : m_segment(seg) , m_result(result) {} inline border2_check operator=(border2_check const& input) { return border2_check(input.m_segment, input.m_result); } template inline void operator()(Segment2 const& segment) { // Create copies (TODO: find out why referring_segment does not compile) typedef typename bg::point_type::type pt; typedef bg::model::segment segment_type; typedef bg::strategy::intersection::relate_cartesian_segments < bg::policies::relate::segments_intersection_points < segment_type, segment_type, bg::segment_intersection_points > > policy; segment_type seg1, seg2; bg::convert(m_segment, seg1); bg::convert(segment, seg2); bg::segment_intersection_points is = policy::apply(seg1, seg2); if (is.count == 2) { if (m_result) { std::cout << "Does not fulfill border2 check" << std::endl; } m_result = true; } } }; template class border_check { Geometry const& m_geo; bool& m_result; public : border_check(Geometry const& geo, bool& result) : m_geo(geo) , m_result(result) {} inline border_check operator=(border_check const& input) { return border_check(input.m_geo, input.m_result); } template inline void operator()(Segment const& s) { bool on_border = false; border2_check checker(s, on_border); bg::for_each_segment(m_geo, checker); if (on_border) { if (m_result) { std::cout << "Does not fulfill border check" << std::endl; } m_result = false; } } }; template bool verify(std::string const& caseid, MultiPolygon const& mp, Linestring const& ls, Settings const& settings) { bg::model::multi_linestring difference, intersection; bg::difference(ls, mp, difference); bg::intersection(ls, mp, intersection); //typedef typename bg::length_result_type::type length_type; bool result = true; // 1) Check by length typedef double length_type; length_type len_input = bg::length(ls); length_type len_difference = bg::length(difference); length_type len_intersection = bg::length(intersection); if (! bg::math::equals(len_input, len_difference + len_intersection)) { std::cout << "Input: " << len_input << " difference: " << len_difference << " intersection: " << len_intersection << std::endl; std::cout << "Does not fulfill length check" << std::endl; result = false; } // 2) Check by within and covered by inside_check ic(mp, result); bg::for_each_point(intersection, ic); outside_check oc(mp, result); bg::for_each_point(difference, oc); border_check bc(mp, result); bg::for_each_segment(difference, bc); // 3) check also the mid-points from the difference to remove false positives BOOST_FOREACH(Linestring const& d, difference) { Linestring difference_midpoints; bg::midpoints(d, false, std::back_inserter(difference_midpoints)); outside_check ocm(mp, result); bg::for_each_point(difference_midpoints, ocm); } bool svg = settings.svg; bool wkt = settings.wkt; if (! result) { std::cout << "ERROR " << caseid << std::endl; std::cout << bg::wkt(mp) << std::endl; std::cout << bg::wkt(ls) << std::endl; svg = true; wkt = true; } if (svg) { std::ostringstream filename; filename << caseid << "_" << typeid(typename bg::coordinate_type::type).name() << ".svg"; create_svg(filename.str(), mp, ls, difference, intersection); } if (wkt) { std::ostringstream filename; filename << caseid << "_" << typeid(typename bg::coordinate_type::type).name() << ".wkt"; std::ofstream stream(filename.str().c_str()); stream << bg::wkt(mp) << std::endl; stream << bg::wkt(ls) << std::endl; } return result; } template bool test_linear_areal(MultiPolygon& result, int& index, Generator& generator, int level, common_settings const& settings) { MultiPolygon p, q; // Generate two boxes if (level == 0) { p.resize(1); q.resize(1); make_square_polygon(p.front(), generator, settings); make_square_polygon(q.front(), generator, settings); bg::correct(p); bg::correct(q); } else { bg::correct(p); bg::correct(q); if (! test_linear_areal(p, index, generator, level - 1, settings) || ! test_linear_areal(q, index, generator, level - 1, settings)) { return false; } } typedef typename boost::range_value::type polygon; MultiPolygon mp; bg::detail::union_::union_insert < polygon >(p, q, std::back_inserter(mp)); bg::unique(mp); bg::simplify(mp, result, 0.01); bg::correct(mp); // Generate a linestring typedef typename bg::point_type::type point_type; typedef bg::model::linestring linestring_type; linestring_type ls; make_random_linestring(ls, generator, settings); std::ostringstream out; out << "recursive_la_" << index++ << "_" << level; return verify(out.str(), mp, ls, settings); } template void test_all(int seed, int count, int level, common_settings const& settings) { boost::timer t; typedef boost::minstd_rand base_generator_type; base_generator_type generator(seed); boost::uniform_int<> random_coordinate(0, settings.field_size - 1); boost::variate_generator > coordinate_generator(generator, random_coordinate); typedef bg::model::polygon < bg::model::d2::point_xy, Clockwise, Closed > polygon; typedef bg::model::multi_polygon mp; int index = 0; for(int i = 0; i < count; i++) { mp p; test_linear_areal(p, index, coordinate_generator, level, settings); } std::cout << "geometries: " << index << " type: " << typeid(T).name() << " time: " << t.elapsed() << std::endl; } int main(int argc, char** argv) { try { namespace po = boost::program_options; po::options_description description("=== recursive_polygons_linear_areal ===\nAllowed options"); int count = 1; int seed = static_cast(std::time(0)); int level = 3; bool ccw = false; bool open = false; common_settings settings; std::string form = "box"; description.add_options() ("help", "Help message") ("seed", po::value(&seed), "Initialization seed for random generator") ("count", po::value(&count)->default_value(1), "Number of tests") ("diff", po::value(&settings.also_difference)->default_value(false), "Include testing on difference") ("level", po::value(&level)->default_value(3), "Level to reach (higher->slower)") ("form", po::value(&form)->default_value("box"), "Form of the polygons (box, triangle)") ("ccw", po::value(&ccw)->default_value(false), "Counter clockwise polygons") ("open", po::value(&open)->default_value(false), "Open polygons") ("size", po::value(&settings.field_size)->default_value(10), "Size of the field") ("wkt", po::value(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests") ("svg", po::value(&settings.svg)->default_value(false), "Create a SVG for all tests") ; po::variables_map varmap; po::store(po::parse_command_line(argc, argv, description), varmap); po::notify(varmap); if (varmap.count("help") || (form != "box" && form != "triangle")) { std::cout << description << std::endl; return 1; } settings.triangular = form != "box"; if (ccw && open) { test_all(seed, count, level, settings); } else if (ccw) { test_all(seed, count, level, settings); } else if (open) { test_all(seed, count, level, settings); } else { test_all(seed, count, level, settings); } #if defined(HAVE_TTMATH) // test_all(seed, count, max, svg, level); #endif } catch(std::exception const& e) { std::cout << "Exception " << e.what() << std::endl; } catch(...) { std::cout << "Other exception" << std::endl; } return 0; }