// Boost.Geometry (aka GGL, Generic Geometry Library) test file // // Copyright Barend Gehrels 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) #include #include #include #include #include #define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR #define BOOST_GEOMETRY_NO_BOOST_TEST #include #include #include #include #include #include #include #include #include //#include #include #include #include template inline void make_box(Polygon& polygon, Generator& generator) { namespace bg = boost::geometry; typedef typename bg::point_type::type point_type; typedef typename bg::coordinate_type::type coordinate_type; coordinate_type x, y; x = generator(); y = generator(); typename bg::ring_type::type& ring = bg::exterior_ring(polygon); point_type p; bg::set<0>(p, x); bg::set<1>(p, y); ring.push_back(p); bg::set<0>(p, x); bg::set<1>(p, y + 1); ring.push_back(p); bg::set<0>(p, x + 1); bg::set<1>(p, y + 1); ring.push_back(p); bg::set<0>(p, x + 1); bg::set<1>(p, y); ring.push_back(p); bg::set<0>(p, x); bg::set<1>(p, y); ring.push_back(p); if (true) { // Remove a point depending on generator int c = generator() % 4; if (c >= 1 && c <= 3) { ring.erase(ring.begin() + c); } } } template bool test_recursive_boxes(MultiPolygon& result, int& index, Generator& generator, bool svg, int level = 3) { namespace bg = boost::geometry; MultiPolygon p, q; // Generate two boxes if (level == 0) { p.resize(1); q.resize(1); make_box(p.front(), generator); make_box(q.front(), generator); } else { if (! test_recursive_boxes(p, index, generator, svg, level - 1) || ! test_recursive_boxes(q, index, generator, svg, level - 1)) { return false; } } typedef typename boost::range_value::type polygon; std::ostringstream out; out << "recursive_box_" << index++ << "_" << level; if (! test_overlay_p_q < polygon, typename bg::coordinate_type::type >(out.str(), p, q, svg, 0.001)) { return false; } MultiPolygon mp; bg::union_inserter < polygon >(p, q, std::back_inserter(mp)); bg::unique(mp); //result = mp; bg::simplify(mp, result, 0.01); return true; } template void test_all(int seed, int count, bool svg, int level) { boost::timer t; typedef boost::minstd_rand base_generator_type; base_generator_type generator(seed); boost::uniform_int<> random_coordinate(0, 9); boost::variate_generator > coordinate_generator(generator, random_coordinate); typedef boost::geometry::polygon < boost::geometry::point_xy > polygon; typedef boost::geometry::multi_polygon mp; int index = 0; for(int i = 0; i < count; i++) { mp p; test_recursive_boxes(p, index, coordinate_generator, svg, level); } std::cout << "boxes " << index << " type: " << string_from_type::name() << " time: " << t.elapsed() << std::endl; } int main(int argc, char** argv) { try { int count = argc > 1 ? boost::lexical_cast(argv[1]) : 10; int seed = (argc > 2 && std::string(argv[2]) != std::string("#")) ? boost::lexical_cast(argv[2]) : static_cast(std::time(0)); bool svg = argc > 3 && std::string(argv[3]) == std::string("svg"); int level = argc > 4 && std::string(argv[4]) != std::string("#") ? boost::lexical_cast(argv[4]): 3; //test_all(seed, count, svg, 1e-3); test_all(seed, count, svg, level); #if defined(HAVE_CLN) //test_recursive_boxes("c", #endif #if defined(HAVE_GMP) // test_recursive_boxes(selection, "g"); #endif } catch(std::exception const& e) { std::cout << "Exception " << e.what() << std::endl; } catch(...) { std::cout << "Other exception" << std::endl; } return 0; }