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