polygon_set_data_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Boost.Polygon library polygon_set_data_test.cpp file
  2. // Copyright Andrii Sydorchuk 2015.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <boost/core/lightweight_test.hpp>
  8. #include <boost/polygon/polygon.hpp>
  9. #include <vector>
  10. using namespace boost::polygon;
  11. using namespace boost::polygon::operators;
  12. void polygon_set_data_test1()
  13. {
  14. typedef point_data<int> point_type;
  15. typedef polygon_with_holes_data<int> polygon_with_holes_type;
  16. typedef polygon_set_data<int> polygon_set_type;
  17. polygon_set_type pset;
  18. std::vector<point_type> outbox;
  19. outbox.push_back(point_type(0, 0));
  20. outbox.push_back(point_type(100, 0));
  21. outbox.push_back(point_type(100, 100));
  22. outbox.push_back(point_type(0, 100));
  23. pset.insert_vertex_sequence(outbox.begin(), outbox.end(), COUNTERCLOCKWISE, false);
  24. std::vector<point_type> inbox;
  25. inbox.push_back(point_type(20, 20));
  26. inbox.push_back(point_type(80, 20));
  27. inbox.push_back(point_type(80, 80));
  28. inbox.push_back(point_type(20, 80));
  29. pset.insert_vertex_sequence(inbox.begin(), inbox.end(), COUNTERCLOCKWISE, true);
  30. BOOST_TEST(!pset.empty());
  31. BOOST_TEST(!pset.sorted());
  32. BOOST_TEST(pset.dirty());
  33. BOOST_TEST_EQ(8, pset.size());
  34. std::vector<polygon_with_holes_type> vpoly;
  35. pset.get(vpoly);
  36. BOOST_TEST_EQ(1, vpoly.size());
  37. polygon_with_holes_type poly = vpoly[0];
  38. BOOST_TEST_EQ(5, poly.size());
  39. BOOST_TEST_EQ(1, poly.size_holes());
  40. }
  41. void polygon_set_data_test2()
  42. {
  43. typedef point_data<int> point_type;
  44. typedef polygon_data<int> polygon_type;
  45. typedef polygon_set_data<int> polygon_set_type;
  46. std::vector<point_type> data;
  47. data.push_back(point_type(2,0));
  48. data.push_back(point_type(4,0));
  49. data.push_back(point_type(4,3));
  50. data.push_back(point_type(0,3));
  51. data.push_back(point_type(0,0));
  52. data.push_back(point_type(2,0));
  53. data.push_back(point_type(2,1));
  54. data.push_back(point_type(1,1));
  55. data.push_back(point_type(1,2));
  56. data.push_back(point_type(3,2));
  57. data.push_back(point_type(3,1));
  58. data.push_back(point_type(2,1));
  59. data.push_back(point_type(2,0));
  60. polygon_type polygon;
  61. set_points(polygon, data.begin(), data.end());
  62. polygon_set_type pset;
  63. pset.insert(polygon);
  64. std::vector<polygon_type> traps;
  65. get_trapezoids(traps, pset, HORIZONTAL);
  66. BOOST_TEST_EQ(4, traps.size());
  67. }
  68. void polygon_set_data_test3()
  69. {
  70. typedef point_data<int> point_type;
  71. typedef polygon_data<int> polygon_type;
  72. typedef polygon_set_data<int> polygon_set_type;
  73. std::vector<point_type> data;
  74. data.push_back(point_type(0,0));
  75. data.push_back(point_type(6,0));
  76. data.push_back(point_type(6,4));
  77. data.push_back(point_type(4,6));
  78. data.push_back(point_type(0,6));
  79. data.push_back(point_type(0,0));
  80. data.push_back(point_type(4,4));
  81. data.push_back(point_type(5,4));
  82. polygon_type polygon(data.begin(), data.end());
  83. polygon_set_type pset;
  84. pset += polygon;
  85. BOOST_TEST_EQ(32.0, area(polygon));
  86. BOOST_TEST_EQ(32.0, area(polygon));
  87. }
  88. int main()
  89. {
  90. polygon_set_data_test1();
  91. polygon_set_data_test2();
  92. polygon_set_data_test3();
  93. return boost::report_errors();
  94. }