as_range.cpp 2.3 KB

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