/libs/geometry/test/robustness/convex_hull/random_multi_points.cpp

https://github.com/delalond/boost_1_54_0-bgq · C++ · 225 lines · 179 code · 39 blank · 7 comment · 18 complexity · d27dab88a0c2a8e2d7fd45f848acb5c4 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Robustness Test - convex_hull
  3. // Copyright (c) 2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR
  8. #define BOOST_GEOMETRY_NO_BOOST_TEST
  9. #include <sstream>
  10. #include <fstream>
  11. #include <boost/program_options.hpp>
  12. #include <boost/random/linear_congruential.hpp>
  13. #include <boost/random/uniform_int.hpp>
  14. #include <boost/random/uniform_real.hpp>
  15. #include <boost/random/variate_generator.hpp>
  16. #include <boost/timer.hpp>
  17. #include <boost/geometry.hpp>
  18. #include <boost/geometry/geometries/geometries.hpp>
  19. #include <boost/geometry/geometries/point_xy.hpp>
  20. #include <boost/geometry/multi/geometries/multi_geometries.hpp>
  21. #include <boost/geometry/io/svg/svg_mapper.hpp>
  22. struct settings_type
  23. {
  24. bool svg;
  25. bool wkt;
  26. settings_type()
  27. : svg(false)
  28. , wkt(false)
  29. {}
  30. };
  31. namespace bg = boost::geometry;
  32. template <typename Geometry1, typename Geometry2>
  33. void create_svg(std::string const& filename, Geometry1 const& points, Geometry2 const& hull)
  34. {
  35. typedef typename boost::geometry::point_type<Geometry1>::type point_type;
  36. boost::geometry::model::box<point_type> box;
  37. bg::envelope(hull, box);
  38. bg::buffer(box, box, 1.0);
  39. std::ofstream svg(filename.c_str());
  40. boost::geometry::svg_mapper<point_type> mapper(svg, 800, 800);
  41. mapper.add(box);
  42. mapper.map(hull, "opacity:0.8;fill:none;stroke:rgb(255,0,255);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
  43. mapper.map(points, "fill-opacity:0.5;fill:rgb(0,0,255);", 5);
  44. }
  45. template <typename MultiPoint, typename Generator>
  46. inline void make_multi_point(MultiPoint& mp, Generator& generator, int pcount)
  47. {
  48. typedef typename bg::point_type<MultiPoint>::type point_type;
  49. typedef typename bg::coordinate_type<MultiPoint>::type coordinate_type;
  50. for(int i = 0; i < pcount; i++)
  51. {
  52. coordinate_type x, y;
  53. x = generator();
  54. y = generator();
  55. point_type p;
  56. bg::set<0>(p, x);
  57. bg::set<1>(p, y);
  58. mp.push_back(p);
  59. }
  60. }
  61. template <typename MultiPoint, typename Polygon>
  62. bool check_hull(MultiPoint const& mp, Polygon const& poly)
  63. {
  64. for(typename boost::range_iterator<MultiPoint const>::type it = boost::begin(mp);
  65. it != boost::end(mp);
  66. ++it)
  67. {
  68. if (! bg::covered_by(*it, poly))
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. template <typename MultiPoint, typename Generator>
  76. void test_random_multi_points(MultiPoint& result, int& index,
  77. Generator& generator,
  78. int pcount, settings_type const& settings)
  79. {
  80. typedef typename bg::point_type<MultiPoint>::type point_type;
  81. MultiPoint mp;
  82. bg::model::polygon<point_type> hull;
  83. make_multi_point(mp, generator, pcount);
  84. bg::convex_hull(mp, hull);
  85. // Check if each point lies in the hull
  86. bool correct = check_hull(mp, hull);
  87. if (! correct)
  88. {
  89. std::cout << "ERROR! " << std::endl
  90. << bg::wkt(mp) << std::endl
  91. << bg::wkt(hull) << std::endl
  92. << std::endl;
  93. ;
  94. }
  95. if (settings.svg || ! correct)
  96. {
  97. std::ostringstream out;
  98. out << "random_mp_" << index++ << "_" << pcount << ".svg";
  99. create_svg(out.str(), mp, hull);
  100. }
  101. if (settings.wkt)
  102. {
  103. std::cout
  104. << "input: " << bg::wkt(mp) << std::endl
  105. << "output: " << bg::wkt(hull) << std::endl
  106. << std::endl;
  107. ;
  108. }
  109. }
  110. template <typename T>
  111. void test_all(int seed, int count, int field_size, int pcount, settings_type const& settings)
  112. {
  113. boost::timer t;
  114. typedef boost::minstd_rand base_generator_type;
  115. base_generator_type generator(seed);
  116. boost::uniform_int<> random_coordinate(0, field_size - 1);
  117. boost::variate_generator<base_generator_type&, boost::uniform_int<> >
  118. coordinate_generator(generator, random_coordinate);
  119. typedef bg::model::multi_point
  120. <
  121. bg::model::d2::point_xy<T>
  122. > mp;
  123. int index = 0;
  124. for(int i = 0; i < count; i++)
  125. {
  126. mp p;
  127. test_random_multi_points<mp>(p, index, coordinate_generator, pcount, settings);
  128. }
  129. std::cout
  130. << "points: " << index
  131. << " type: " << typeid(T).name()
  132. << " time: " << t.elapsed() << std::endl;
  133. }
  134. int main(int argc, char** argv)
  135. {
  136. try
  137. {
  138. namespace po = boost::program_options;
  139. po::options_description description("=== random_multi_points ===\nAllowed options");
  140. std::string type = "double";
  141. int count = 1;
  142. int seed = static_cast<unsigned int>(std::time(0));
  143. int pcount = 3;
  144. int field_size = 10;
  145. settings_type settings;
  146. description.add_options()
  147. ("help", "Help message")
  148. ("seed", po::value<int>(&seed), "Initialization seed for random generator")
  149. ("count", po::value<int>(&count)->default_value(1), "Number of tests")
  150. ("number", po::value<int>(&pcount)->default_value(30), "Number of points")
  151. ("size", po::value<int>(&field_size)->default_value(10), "Size of the field")
  152. ("type", po::value<std::string>(&type)->default_value("double"), "Type (int,float,double)")
  153. ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
  154. ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
  155. ;
  156. po::variables_map varmap;
  157. po::store(po::parse_command_line(argc, argv, description), varmap);
  158. po::notify(varmap);
  159. if (varmap.count("help"))
  160. {
  161. std::cout << description << std::endl;
  162. return 1;
  163. }
  164. if (type == "float")
  165. {
  166. test_all<float>(seed, count, field_size, pcount, settings);
  167. }
  168. else if (type == "double")
  169. {
  170. test_all<double>(seed, count, field_size, pcount, settings);
  171. }
  172. else if (type == "int")
  173. {
  174. test_all<int>(seed, count, field_size, pcount, settings);
  175. }
  176. }
  177. catch(std::exception const& e)
  178. {
  179. std::cout << "Exception " << e.what() << std::endl;
  180. }
  181. catch(...)
  182. {
  183. std::cout << "Other exception" << std::endl;
  184. }
  185. return 0;
  186. }