/Src/Dependencies/Boost/libs/geometry/test/algorithms/overlay/robustness/intersection_stars.cpp

http://hadesmem.googlecode.com/ · C++ · 195 lines · 150 code · 33 blank · 12 comment · 14 complexity · 62839b772af54ce52bf7a2c2a4e18035 MD5 · raw file

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