/Src/Dependencies/Boost/libs/geometry/test/algorithms/test_for_each.hpp

http://hadesmem.googlecode.com/ · C++ Header · 217 lines · 157 code · 46 blank · 14 comment · 4 complexity · b617aa2a5eba81f3777bc51689343e70 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_TEST_FOR_EACH_HPP
  8. #define BOOST_GEOMETRY_TEST_FOR_EACH_HPP
  9. #include <geometry_test_common.hpp>
  10. #include <boost/geometry/algorithms/for_each.hpp>
  11. #include <boost/geometry/algorithms/distance.hpp>
  12. #include <boost/geometry/strategies/strategies.hpp>
  13. #include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
  14. #include <boost/geometry/util/write_dsv.hpp>
  15. template<typename Point>
  16. inline void translate_x_function(Point& p)
  17. {
  18. bg::set<0>(p, bg::get<0>(p) + 100.0);
  19. }
  20. template<typename Point>
  21. struct scale_y_functor
  22. {
  23. inline void operator()(Point& p)
  24. {
  25. bg::set<1>(p, bg::get<1>(p) * 100.0);
  26. }
  27. };
  28. template<typename Point>
  29. struct sum_x_functor
  30. {
  31. typename bg::coordinate_type<Point>::type sum;
  32. sum_x_functor()
  33. : sum(0)
  34. {}
  35. inline void operator()(Point const& p)
  36. {
  37. sum += bg::get<0>(p);
  38. }
  39. };
  40. // Per segment
  41. static std::ostringstream g_out;
  42. template<typename Segment>
  43. inline void stream_segment(Segment const& s)
  44. {
  45. g_out << bg::dsv(s) << " ";
  46. }
  47. template<typename Segment>
  48. struct sum_segment_length
  49. {
  50. typename bg::coordinate_type<Segment>::type sum;
  51. sum_segment_length()
  52. : sum(0)
  53. {}
  54. inline void operator()(Segment const& s)
  55. {
  56. sum += bg::distance(s.first, s.second);
  57. }
  58. };
  59. template<typename Segment>
  60. inline void modify_segment(Segment& s)
  61. {
  62. if (bg::math::equals(bg::get<0,0>(s), 1.0))
  63. {
  64. bg::set<0,0>(s, 10.0);
  65. }
  66. }
  67. template <typename Geometry>
  68. void test_per_point_const(Geometry const& geometry, int expected)
  69. {
  70. typedef typename bg::point_type<Geometry>::type point_type;
  71. sum_x_functor<point_type> functor;
  72. functor = bg::for_each_point(geometry, functor);
  73. BOOST_CHECK_EQUAL(functor.sum, expected);
  74. }
  75. template <typename Geometry>
  76. void test_per_point_non_const(Geometry& geometry,
  77. std::string const& expected1,
  78. std::string const& expected2)
  79. {
  80. typedef typename bg::point_type<Geometry>::type point_type;
  81. // function
  82. bg::for_each_point(geometry, translate_x_function<point_type>);
  83. std::ostringstream out1;
  84. out1 << bg::wkt(geometry);
  85. BOOST_CHECK_MESSAGE(out1.str() == expected1,
  86. "for_each_point: "
  87. << " expected " << expected1
  88. << " got " << bg::wkt(geometry));
  89. // functor
  90. bg::for_each_point(geometry, scale_y_functor<point_type>());
  91. std::ostringstream out2;
  92. out2 << bg::wkt(geometry);
  93. BOOST_CHECK_MESSAGE(out2.str() == expected2,
  94. "for_each_point: "
  95. << " expected " << expected2
  96. << " got " << bg::wkt(geometry));
  97. }
  98. template <typename Geometry>
  99. void test_per_point(std::string const& wkt
  100. , int expected_sum_x
  101. , std::string const& expected1
  102. , std::string const& expected2
  103. )
  104. {
  105. Geometry geometry;
  106. bg::read_wkt(wkt, geometry);
  107. test_per_point_const(geometry, expected_sum_x);
  108. test_per_point_non_const(geometry, expected1, expected2);
  109. }
  110. template <typename Geometry>
  111. void test_per_segment_const(Geometry const& geometry,
  112. std::string const& expected_dsv,
  113. double expected_length)
  114. {
  115. typedef typename bg::point_type<Geometry>::type point_type;
  116. // function
  117. g_out.str("");
  118. g_out.clear();
  119. bg::for_each_segment(geometry,
  120. stream_segment<bg::model::referring_segment<point_type const> >);
  121. std::string out = g_out.str();
  122. boost::trim(out);
  123. BOOST_CHECK_EQUAL(out, expected_dsv);
  124. // functor
  125. sum_segment_length<bg::model::referring_segment<point_type const> > functor;
  126. functor = bg::for_each_segment(geometry, functor);
  127. BOOST_CHECK_CLOSE(functor.sum, expected_length, 0.0001);
  128. }
  129. template <typename Geometry>
  130. void test_per_segment_non_const(Geometry& geometry,
  131. std::string const& expected_wkt)
  132. {
  133. typedef typename bg::point_type<Geometry>::type point_type;
  134. // function
  135. bg::for_each_segment(geometry,
  136. modify_segment<bg::model::referring_segment<point_type> >);
  137. std::ostringstream out;
  138. out << bg::wkt(geometry);
  139. BOOST_CHECK_MESSAGE(out.str() == expected_wkt,
  140. "for_each_segment: "
  141. << " expected " << expected_wkt
  142. << " got " << bg::wkt(geometry));
  143. // function is working here, functor works for all others,
  144. // it will also work here.
  145. }
  146. template <typename Geometry>
  147. void test_per_segment(std::string const& wkt
  148. , std::string const& expected_dsv
  149. , double expected_length
  150. , std::string const& expected_wkt
  151. )
  152. {
  153. Geometry geometry;
  154. bg::read_wkt(wkt, geometry);
  155. test_per_segment_const(geometry, expected_dsv, expected_length);
  156. test_per_segment_non_const(geometry, expected_wkt);
  157. }
  158. template <typename Geometry>
  159. void test_geometry(std::string const& wkt
  160. , int expected_sum_x
  161. , std::string const& expected1
  162. , std::string const& expected2
  163. , std::string const& expected_dsv
  164. , double expected_length
  165. , std::string const& expected_wkt
  166. )
  167. {
  168. test_per_point<Geometry>(wkt, expected_sum_x, expected1, expected2);
  169. test_per_segment<Geometry>(wkt, expected_dsv, expected_length, expected_wkt);
  170. }
  171. #endif