intersects.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2011-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 <sstream>
  9. #include <fstream>
  10. #include <iomanip>
  11. #include <string>
  12. #define BOOST_GEOMETRY_REPORT_OVERLAY_ERROR
  13. #define BOOST_GEOMETRY_NO_BOOST_TEST
  14. //#define BOOST_GEOMETRY_TIME_OVERLAY
  15. #include <test_overlay_p_q.hpp>
  16. #include <boost/program_options.hpp>
  17. #include <boost/random/linear_congruential.hpp>
  18. #include <boost/random/uniform_int.hpp>
  19. #include <boost/random/uniform_real.hpp>
  20. #include <boost/random/variate_generator.hpp>
  21. #include <boost/timer.hpp>
  22. template <typename MultiPolygon>
  23. inline void make_polygon(MultiPolygon& mp, int count_x, int count_y, int index, int width_x)
  24. {
  25. typedef typename bg::point_type<MultiPolygon>::type point_type;
  26. for(int j = 0; j < count_x; ++j)
  27. {
  28. for(int k = 0; k < count_y; ++k)
  29. {
  30. mp.push_back(typename MultiPolygon::value_type());
  31. mp.back().outer().push_back(point_type(width_x + j * 10 + 1, k * 10 + 1));
  32. mp.back().outer().push_back(point_type(width_x + j * 10 + width_x, k * 10 + 5 + index));
  33. mp.back().outer().push_back(point_type(width_x + j * 10 + 5 + index, k * 10 + 7));
  34. mp.back().outer().push_back(point_type(width_x + j * 10 + 1, k * 10 + 1));
  35. }
  36. }
  37. }
  38. template <typename MultiPolygon>
  39. void test_intersects(int count_x, int count_y, int width_x, p_q_settings const& settings)
  40. {
  41. MultiPolygon mp;
  42. make_polygon(mp, count_x, count_y, 0, width_x);
  43. bool const b = bg::intersects(mp);
  44. if (b)
  45. {
  46. std::cout << " YES";
  47. }
  48. if(settings.svg)
  49. {
  50. typedef typename bg::coordinate_type<MultiPolygon>::type coordinate_type;
  51. typedef typename bg::point_type<MultiPolygon>::type point_type;
  52. std::ostringstream filename;
  53. filename << "intersects_"
  54. << string_from_type<coordinate_type>::name()
  55. << ".svg";
  56. std::ofstream svg(filename.str().c_str());
  57. bg::svg_mapper<point_type> mapper(svg, 500, 500);
  58. mapper.add(mp);
  59. mapper.map(mp, "fill-opacity:0.5;fill:rgb(153,204,0);"
  60. "stroke:rgb(153,204,0);stroke-width:3");
  61. }
  62. }
  63. template <typename T, bool Clockwise, bool Closed>
  64. void test_all(int count, int count_x, int count_y, int width_x, p_q_settings const& settings)
  65. {
  66. boost::timer t;
  67. typedef bg::model::polygon
  68. <
  69. bg::model::d2::point_xy<T>, Clockwise, Closed
  70. > polygon;
  71. typedef bg::model::multi_polygon
  72. <
  73. polygon
  74. > multi_polygon;
  75. for(int i = 0; i < count; i++)
  76. {
  77. test_intersects<multi_polygon>(count_x, count_y, width_x, settings);
  78. }
  79. std::cout
  80. << " type: " << string_from_type<T>::name()
  81. << " time: " << t.elapsed() << std::endl;
  82. }
  83. int main(int argc, char** argv)
  84. {
  85. try
  86. {
  87. namespace po = boost::program_options;
  88. po::options_description description("=== intersects ===\nAllowed options");
  89. int width_x = 7;
  90. int count = 1;
  91. int count_x = 10;
  92. int count_y = 10;
  93. bool ccw = false;
  94. bool open = false;
  95. p_q_settings settings;
  96. description.add_options()
  97. ("help", "Help message")
  98. ("count", po::value<int>(&count)->default_value(1), "Number of tests")
  99. ("count_x", po::value<int>(&count_x)->default_value(10), "Triangle count in x-direction")
  100. ("count_y", po::value<int>(&count_y)->default_value(10), "Triangle count in y-direction")
  101. ("width_x", po::value<int>(&width_x)->default_value(7), "Width of triangle in x-direction")
  102. ("ccw", po::value<bool>(&ccw)->default_value(false), "Counter clockwise polygons")
  103. ("open", po::value<bool>(&open)->default_value(false), "Open polygons")
  104. ("wkt", po::value<bool>(&settings.wkt)->default_value(false), "Create a WKT of the inputs, for all tests")
  105. ("svg", po::value<bool>(&settings.svg)->default_value(false), "Create a SVG for all tests")
  106. ;
  107. po::variables_map varmap;
  108. po::store(po::parse_command_line(argc, argv, description), varmap);
  109. po::notify(varmap);
  110. if (varmap.count("help"))
  111. {
  112. std::cout << description << std::endl;
  113. return 1;
  114. }
  115. if (ccw && open)
  116. {
  117. test_all<double, false, false>(count, count_x, count_y, width_x, settings);
  118. }
  119. else if (ccw)
  120. {
  121. test_all<double, false, true>(count, count_x, count_y, width_x, settings);
  122. }
  123. else if (open)
  124. {
  125. test_all<double, true, false>(count, count_x, count_y, width_x, settings);
  126. }
  127. else
  128. {
  129. test_all<double, true, true>(count, count_x, count_y, width_x, settings);
  130. }
  131. #if defined(HAVE_TTMATH)
  132. // test_all<ttmath_big, true, true>(seed, count, max, svg, level);
  133. #endif
  134. }
  135. catch(std::exception const& e)
  136. {
  137. std::cout << "Exception " << e.what() << std::endl;
  138. }
  139. catch(...)
  140. {
  141. std::cout << "Other exception" << std::endl;
  142. }
  143. return 0;
  144. }