random_ellipses_stars.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2009-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 <test_overlay_p_q.hpp>
  10. #include <boost/program_options.hpp>
  11. #include <boost/timer.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. struct star_params
  17. {
  18. int count; // points of ellipse, not of star
  19. double factor_1;
  20. double factor_2;
  21. double center_x;
  22. double center_y;
  23. double rotation;
  24. star_params(int c, double f1, double f2, double x, double y, double r = 0)
  25. : count(c)
  26. , factor_1(f1)
  27. , factor_2(f2)
  28. , center_x(x)
  29. , center_y(y)
  30. , rotation(r)
  31. {}
  32. };
  33. template <typename Polygon>
  34. inline void make_star(Polygon& polygon, star_params const& p)
  35. {
  36. typedef typename bg::point_type<Polygon>::type P;
  37. typedef typename bg::select_most_precise
  38. <
  39. typename bg::coordinate_type<Polygon>::type,
  40. long double
  41. >::type coordinate_type;
  42. // Create star
  43. coordinate_type cx = 25.0;
  44. coordinate_type cy = 25.0;
  45. coordinate_type dx = 50.0;
  46. coordinate_type dy = 50.0;
  47. coordinate_type half = 0.5;
  48. coordinate_type two = 2.0;
  49. coordinate_type a1 = coordinate_type(p.factor_1) * half * dx;
  50. coordinate_type b1 = coordinate_type(p.factor_1) * half * dy;
  51. coordinate_type a2 = coordinate_type(p.factor_2) * half * dx;
  52. coordinate_type b2 = coordinate_type(p.factor_2) * half * dy;
  53. coordinate_type pi = boost::math::constants::pi<long double>();
  54. coordinate_type delta = pi * two / coordinate_type(p.count - 1);
  55. coordinate_type angle = coordinate_type(p.rotation) * delta;
  56. for (int i = 0; i < p.count - 1; i++, angle += delta)
  57. {
  58. bool even = i % 2 == 0;
  59. coordinate_type s = sin(angle);
  60. coordinate_type c = cos(angle);
  61. coordinate_type x = p.center_x + cx + (even ? a1 : a2) * s;
  62. coordinate_type y = p.center_y + cy + (even ? b1 : b2) * c;
  63. bg::exterior_ring(polygon).push_back(bg::make<P>(x, y));
  64. }
  65. bg::exterior_ring(polygon).push_back(bg::exterior_ring(polygon).front());
  66. bg::correct(polygon);
  67. }
  68. template <typename T, bool Clockwise, bool Closed>
  69. void test_star_ellipse(int seed, int index, star_params const& par_p,
  70. star_params const& par_q, p_q_settings const& settings)
  71. {
  72. typedef bg::model::d2::point_xy<T> point_type;
  73. typedef bg::model::polygon<point_type, Clockwise, Closed> polygon;
  74. polygon p, q;
  75. make_star(p, par_p);
  76. make_star(q, par_q);
  77. std::ostringstream out;
  78. out << "rse_" << seed << "_" << index;
  79. test_overlay_p_q<polygon, T>(out.str(), p, q, settings);
  80. }
  81. template <typename T, bool Clockwise, bool Closed>
  82. void test_type(int seed, int count, p_q_settings const& settings)
  83. {
  84. boost::timer t;
  85. typedef boost::minstd_rand base_generator_type;
  86. //boost::uniform_real<> random_factor(0.5, 1.2);
  87. //boost::uniform_real<> random_location(-10.0, 10.0);
  88. //boost::uniform_int<> random_points(5, 20);
  89. // This set (next 4 lines) are now solved for the most part
  90. // 2009-12-03, 3 or 4 errors in 1000000 calls
  91. // 2009-12-07, no errors in 1000000 calls
  92. //boost::uniform_real<> random_factor(1.0 - 1e-3, 1.0 + 1e-3);
  93. //boost::uniform_real<> random_location(-1e-3, 1e-3);
  94. //boost::uniform_real<> random_rotation(-1e-3, 1e-3);
  95. //boost::uniform_int<> random_points(3, 3);
  96. // 2009-12-08, still errors, see notes
  97. // 2009-12-09, (probably) solved by order on side
  98. // 2010-01-16: solved (no errors in 1000000 calls)
  99. //boost::uniform_real<> random_factor(1.0 - 1e-3, 1.0 + 1e-3);
  100. //boost::uniform_real<> random_location(-1e-3, -1e-3);
  101. //boost::uniform_real<> random_rotation(-1e-3, 1e-3);
  102. //boost::uniform_int<> random_points(3, 4);
  103. // This set (next 4 lines) are now solved ("distance-zero"/"merge iiii" problem)
  104. // 2009-12-03: 5,50 -> 2:1 000 000 wrong (2009-12-03)
  105. // 2010-01-16: solved (no errors in 10000000 calls)
  106. boost::uniform_real<> random_factor(0.3, 1.2);
  107. boost::uniform_real<> random_location(-20.0, +20.0); // -25.0, +25.0
  108. boost::uniform_real<> random_rotation(0, 0.5);
  109. boost::uniform_int<> random_points(5, 15);
  110. base_generator_type generator(seed);
  111. boost::variate_generator<base_generator_type&, boost::uniform_real<> >
  112. factor_generator(generator, random_factor);
  113. boost::variate_generator<base_generator_type&, boost::uniform_real<> >
  114. location_generator(generator, random_location);
  115. boost::variate_generator<base_generator_type&, boost::uniform_real<> >
  116. rotation_generator(generator, random_rotation);
  117. boost::variate_generator<base_generator_type&, boost::uniform_int<> >
  118. int_generator(generator, random_points);
  119. for(int i = 0; i < count; i++)
  120. {
  121. test_star_ellipse<T, Clockwise, Closed>(seed, i + 1,
  122. star_params(int_generator() * 2 + 1,
  123. factor_generator(), factor_generator(),
  124. location_generator(), location_generator(), rotation_generator()),
  125. star_params(int_generator() * 2 + 1,
  126. factor_generator(), factor_generator(),
  127. location_generator(), location_generator(), rotation_generator()),
  128. settings);
  129. }
  130. std::cout
  131. << "type: " << string_from_type<T>::name()
  132. << " time: " << t.elapsed() << std::endl;
  133. }
  134. template <bool Clockwise, bool Closed>
  135. void test_all(std::string const& type, int seed, int count, p_q_settings settings)
  136. {
  137. if (type == "float")
  138. {
  139. test_type<float, Clockwise, Closed>(seed, count, settings);
  140. }
  141. else if (type == "double")
  142. {
  143. test_type<double, Clockwise, Closed>(seed, count, settings);
  144. }
  145. #if defined(HAVE_TTMATH)
  146. else if (type == "ttmath")
  147. {
  148. test_type<ttmath_big, Clockwise, Closed>(seed, count, settings);
  149. }
  150. #endif
  151. }
  152. int main(int argc, char** argv)
  153. {
  154. try
  155. {
  156. namespace po = boost::program_options;
  157. po::options_description description("=== random_ellipses_stars ===\nAllowed options");
  158. int count = 1;
  159. int seed = static_cast<unsigned int>(std::time(0));
  160. std::string type = "float";
  161. bool ccw = false;
  162. bool open = false;
  163. p_q_settings settings;
  164. description.add_options()
  165. ("help", "Help message")
  166. ("seed", po::value<int>(&seed), "Initialization seed for random generator")
  167. ("count", po::value<int>(&count)->default_value(1), "Number of tests")
  168. ("diff", po::value<bool>(&settings.also_difference)->default_value(false), "Include testing on difference")
  169. ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
  170. ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
  171. ("type", po::value<std::string>(&type)->default_value("float"), "Type (float,double)")
  172. ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
  173. ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
  174. ;
  175. po::variables_map varmap;
  176. po::store(po::parse_command_line(argc, argv, description), varmap);
  177. po::notify(varmap);
  178. if (varmap.count("help"))
  179. {
  180. std::cout << description << std::endl;
  181. return 1;
  182. }
  183. if (ccw && open)
  184. {
  185. test_all<false, false>(type, seed, count, settings);
  186. }
  187. else if (ccw)
  188. {
  189. test_all<false, true>(type, seed, count, settings);
  190. }
  191. else if (open)
  192. {
  193. test_all<true, false>(type, seed, count, settings);
  194. }
  195. else
  196. {
  197. test_all<true, true>(type, seed, count, settings);
  198. }
  199. }
  200. catch(std::exception const& e)
  201. {
  202. std::cout << "Exception " << e.what() << std::endl;
  203. }
  204. catch(...)
  205. {
  206. std::cout << "Other exception" << std::endl;
  207. }
  208. return 0;
  209. }