random_multi_points.cpp 6.3 KB

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