intersection_stars.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include <iostream>
  8. #include <string>
  9. #define BOOST_GEOMETRY_NO_BOOST_TEST
  10. #include <test_overlay_p_q.hpp>
  11. #include <boost/program_options.hpp>
  12. #include <boost/timer.hpp>
  13. template <typename Polygon>
  14. inline void make_star(Polygon& polygon,
  15. int count, double factor1, double factor2, long double offset = 0)
  16. {
  17. typedef typename bg::point_type<Polygon>::type p;
  18. typedef typename bg::select_most_precise
  19. <
  20. typename bg::coordinate_type<Polygon>::type,
  21. long double
  22. >::type coordinate_type;
  23. // Create star
  24. coordinate_type cx = 25.0;
  25. coordinate_type cy = 25.0;
  26. coordinate_type dx = 50.0;
  27. coordinate_type dy = 50.0;
  28. coordinate_type half = 0.5;
  29. coordinate_type two = 2.0;
  30. coordinate_type a1 = coordinate_type(factor1) * half * dx;
  31. coordinate_type b1 = coordinate_type(factor1) * half * dy;
  32. coordinate_type a2 = coordinate_type(factor2) * half * dx;
  33. coordinate_type b2 = coordinate_type(factor2) * half * dy;
  34. coordinate_type pi = boost::math::constants::pi<long double>();
  35. coordinate_type delta = pi * two / coordinate_type(count - 1);
  36. coordinate_type angle = coordinate_type(offset) * delta;
  37. for (int i = 0; i < count - 1; i++, angle += delta)
  38. {
  39. bool even = i % 2 == 0;
  40. coordinate_type s = sin(angle);
  41. coordinate_type c = cos(angle);
  42. coordinate_type x = cx + (even ? a1 : a2) * s;
  43. coordinate_type y = cy + (even ? b1 : b2) * c;
  44. bg::exterior_ring(polygon).push_back(bg::make<p>(x, y));
  45. }
  46. bg::exterior_ring(polygon).push_back(bg::exterior_ring(polygon).front());
  47. }
  48. template <typename T, typename CalculationType>
  49. void test_star(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
  50. {
  51. boost::timer t;
  52. typedef bg::model::d2::point_xy<T> point_type;
  53. typedef bg::model::polygon<point_type> polygon;
  54. int n = 0;
  55. for (int c = 0; c < count; c++)
  56. {
  57. for (int i = min_points; i <= max_points; i++)
  58. {
  59. std::ostringstream out;
  60. out << "_" << string_from_type<T>::name() << "_"
  61. << string_from_type<CalculationType>::name() << "_"
  62. << i << "_int";
  63. polygon p;
  64. make_star(p, i * 2 + 1, 0.5, 1.0);
  65. polygon q;
  66. make_star(q, i * 2 + 1, 0.5, 1.0, rotation);
  67. if (! test_overlay_p_q
  68. <
  69. polygon,
  70. CalculationType
  71. >(out.str(), p, q, settings))
  72. {
  73. return;
  74. }
  75. n++;
  76. }
  77. }
  78. std::cout
  79. << "polygons: " << n
  80. << " type: " << string_from_type<T>::name()
  81. << " time: " << t.elapsed() << std::endl;
  82. }
  83. template <typename T, typename CalculationType>
  84. void test_type(int count, int min_points, int max_points, T rotation, p_q_settings const& settings)
  85. {
  86. test_star<T, CalculationType>(count, min_points, max_points, rotation, settings);
  87. }
  88. template <typename T>
  89. void test_all(std::string const& type, int count, int min_points, int max_points, T rotation, p_q_settings settings)
  90. {
  91. if (type == "float")
  92. {
  93. settings.tolerance = 1.0e-3;
  94. test_type<float, float>(count, min_points, max_points, rotation, settings);
  95. }
  96. else if (type == "double")
  97. {
  98. test_type<double, double>(count, min_points, max_points, rotation, settings);
  99. }
  100. #if defined(HAVE_TTMATH)
  101. else if (type == "ttmath")
  102. {
  103. test_type<ttmath_big, ttmath_big>(count, min_points, max_points, rotation, settings);
  104. }
  105. #endif
  106. }
  107. int main(int argc, char** argv)
  108. {
  109. try
  110. {
  111. namespace po = boost::program_options;
  112. po::options_description description("=== recursive_polygons ===\nAllowed options");
  113. int count = 1;
  114. //int seed = static_cast<unsigned int>(std::time(0));
  115. std::string type = "float";
  116. int min_points = 9;
  117. int max_points = 9;
  118. bool ccw = false;
  119. bool open = false;
  120. double rotation = 1.0e-13;
  121. p_q_settings settings;
  122. description.add_options()
  123. ("help", "Help message")
  124. //("seed", po::value<int>(&seed), "Initialization seed for random generator")
  125. ("count", po::value<int>(&count)->default_value(1), "Number of tests")
  126. ("diff", po::value<bool>(&settings.also_difference)->default_value(false), "Include testing on difference")
  127. ("min_points", po::value<int>(&min_points)->default_value(9), "Minimum number of points")
  128. ("max_points", po::value<int>(&max_points)->default_value(9), "Maximum number of points")
  129. ("rotation", po::value<double>(&rotation)->default_value(1.0e-13), "Rotation angle")
  130. ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
  131. ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
  132. ("type", po::value<std::string>(&type)->default_value("float"), "Type (float,double)")
  133. ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
  134. ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
  135. ;
  136. po::variables_map varmap;
  137. po::store(po::parse_command_line(argc, argv, description), varmap);
  138. po::notify(varmap);
  139. if (varmap.count("help"))
  140. {
  141. std::cout << description << std::endl;
  142. return 1;
  143. }
  144. test_all(type, count, min_points, max_points, rotation, settings);
  145. }
  146. catch(std::exception const& e)
  147. {
  148. std::cout << "Exception " << e.what() << std::endl;
  149. }
  150. catch(...)
  151. {
  152. std::cout << "Other exception" << std::endl;
  153. }
  154. return 0;
  155. }