num_points.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  6. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <iostream>
  11. #include <geometry_test_common.hpp>
  12. #include <boost/geometry/algorithms/num_points.hpp>
  13. #include <boost/geometry/geometries/geometries.hpp>
  14. #include <boost/geometry/io/wkt/read.hpp>
  15. template <std::size_t D, typename T = double>
  16. struct box_dD
  17. {
  18. typedef boost::geometry::model::box
  19. <
  20. boost::geometry::model::point<T, D, boost::geometry::cs::cartesian>
  21. > type;
  22. };
  23. template <typename Geometry>
  24. inline void test_num_points(std::string const& wkt,
  25. std::size_t expected,
  26. std::size_t expected_add_for_open)
  27. {
  28. namespace bg = boost::geometry;
  29. Geometry geometry;
  30. boost::geometry::read_wkt(wkt, geometry);
  31. std::size_t detected = bg::num_points(geometry);
  32. BOOST_CHECK_EQUAL(expected, detected);
  33. detected = bg::num_points(geometry, false);
  34. BOOST_CHECK_EQUAL(expected, detected);
  35. detected = bg::num_points(geometry, true);
  36. BOOST_CHECK_EQUAL(expected_add_for_open, detected);
  37. }
  38. template <typename Geometry>
  39. inline void test_num_points(std::string const& wkt, std::size_t expected)
  40. {
  41. test_num_points<Geometry>(wkt, expected, expected);
  42. }
  43. int test_main(int, char* [])
  44. {
  45. typedef bg::model::point<double,2,bg::cs::cartesian> point;
  46. typedef bg::model::linestring<point> linestring;
  47. typedef bg::model::segment<point> segment;
  48. typedef bg::model::box<point> box;
  49. typedef bg::model::ring<point> ring;
  50. typedef bg::model::polygon<point> polygon;
  51. typedef bg::model::multi_point<point> multi_point;
  52. typedef bg::model::multi_linestring<linestring> multi_linestring;
  53. typedef bg::model::multi_polygon<polygon> multi_polygon;
  54. // open geometries
  55. typedef bg::model::ring<point, true, false> open_ring;
  56. typedef bg::model::polygon<point, true, false> open_polygon;
  57. typedef bg::model::multi_polygon<open_polygon> open_multi_polygon;
  58. test_num_points<point>("POINT(0 0)", 1u);
  59. test_num_points<linestring>("LINESTRING(0 0,1 1)", 2u);
  60. test_num_points<segment>("LINESTRING(0 0,1 1)", 2u);
  61. test_num_points<box>("POLYGON((0 0,10 10))", 4u);
  62. test_num_points<box_dD<3>::type>("BOX(0 0 0,1 1 1)", 8u);
  63. test_num_points<box_dD<4>::type>("BOX(0 0 0 0,1 1 1 1)", 16u);
  64. test_num_points<box_dD<5>::type>("BOX(0 0 0 0 0,1 1 1 1 1)", 32u);
  65. test_num_points<ring>("POLYGON((0 0,1 1,0 1,0 0))", 4u);
  66. test_num_points<polygon>("POLYGON((0 0,10 10,0 10,0 0))", 4u);
  67. test_num_points<polygon>("POLYGON((0 0,0 10,10 10,10 0,0 0),(4 4,6 4,6 6,4 6,4 4))", 10u);
  68. test_num_points<multi_point>("MULTIPOINT((0 0),(1 1))", 2u);
  69. test_num_points<multi_linestring>("MULTILINESTRING((0 0,1 1),(2 2,3 3,4 4))", 5u);
  70. test_num_points<multi_polygon>("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)),((0 10,1 10,1 9,0 10)))", 9u);
  71. // test open geometries
  72. test_num_points<open_ring>("POLYGON((0 0,1 1,0 1))", 3u, 4u);
  73. test_num_points<open_ring>("POLYGON((0 0,1 1,0 1,0 0))", 3u, 4u);
  74. test_num_points<open_polygon>("POLYGON((0 0,10 10,0 10))", 3u, 4u);
  75. test_num_points<open_polygon>("POLYGON((0 0,10 10,0 10,0 0))", 3u, 4u);
  76. test_num_points<open_multi_polygon>("MULTIPOLYGON(((0 0,0 10,10 10,10 0)),((0 10,1 10,1 9)))", 7u, 9u);
  77. test_num_points<open_multi_polygon>("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)),((0 10,1 10,1 9,0 10)))", 7u, 9u);
  78. return 0;
  79. }