]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Unit Test | |
3 | ||
4 | // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | // Copyright (c) 2008-2012 Bruno Lalande, Paris, France. | |
6 | // Copyright (c) 2009-2012 Mateusz Loskot, London, UK. | |
7 | ||
8 | // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library | |
9 | // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. | |
10 | ||
11 | // Use, modification and distribution is subject to the Boost Software License, | |
12 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
13 | // http://www.boost.org/LICENSE_1_0.txt) | |
14 | ||
15 | ||
16 | #include <geometry_test_common.hpp> | |
17 | ||
18 | ||
19 | #include <boost/geometry/views/detail/range_type.hpp> | |
20 | #include <boost/geometry/algorithms/detail/as_range.hpp> | |
21 | ||
22 | #include <boost/geometry/core/cs.hpp> | |
23 | #include <boost/geometry/geometries/geometries.hpp> | |
24 | ||
25 | #include <boost/geometry/io/wkt/read.hpp> | |
26 | ||
27 | template <int D, typename Range> | |
28 | double sum(Range const& range) | |
29 | { | |
30 | double s = 0.0; | |
31 | for (typename boost::range_const_iterator<Range>::type it = boost::begin(range); | |
32 | it != boost::end(range); ++it) | |
33 | { | |
34 | s += bg::get<D>(*it); | |
35 | } | |
36 | return s; | |
37 | } | |
38 | ||
39 | template <typename G> | |
40 | void test_geometry(std::string const& wkt, double expected_x, double expected_y) | |
41 | { | |
42 | G geometry; | |
43 | ||
44 | // Declare a range-type, compatible with boost::range, | |
45 | // such that range_iterator etc could be called | |
46 | typedef typename bg::detail::range_type<G>::type range_type; | |
47 | ||
48 | bg::read_wkt(wkt, geometry); | |
49 | ||
50 | double s = sum<0>(bg::detail::as_range<range_type>(geometry)); | |
51 | BOOST_CHECK_CLOSE(s, expected_x, 0.001); | |
52 | ||
53 | s = sum<1>(bg::detail::as_range<range_type>(geometry)); | |
54 | BOOST_CHECK_CLOSE(s, expected_y, 0.001); | |
55 | } | |
56 | ||
57 | ||
58 | template <typename P> | |
59 | void test_all() | |
60 | { | |
61 | // As-range utility should consider a geometry as a range, so | |
62 | // linestring stays linestring | |
63 | test_geometry<bg::model::linestring<P> >("LINESTRING(1 2,3 4)", 4, 6); | |
64 | ||
65 | // polygon will only be outer-ring | |
66 | test_geometry<bg::model::polygon<P> >("POLYGON((1 2,3 4))", 4, 6); | |
67 | test_geometry<bg::model::polygon<P> >("POLYGON((1 2,3 4),(5 6,7 8,9 10))", 4, 6); | |
68 | ||
69 | // the utility is useful for: | |
70 | // - convex hull (holes do not count) | |
71 | // - envelope (idem) | |
72 | } | |
73 | ||
74 | int test_main(int, char* []) | |
75 | { | |
76 | test_all<bg::model::point<double, 2, bg::cs::cartesian> >(); | |
77 | ||
78 | return 0; | |
79 | } |