test_for_each.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 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/config.hpp>
  11. #include <boost/geometry/algorithms/for_each.hpp>
  12. #include <boost/geometry/algorithms/distance.hpp>
  13. #include <boost/geometry/strategies/strategies.hpp>
  14. #include <boost/geometry/io/wkt/wkt.hpp>
  15. #include <boost/geometry/io/dsv/write.hpp>
  16. template<typename Point>
  17. inline void translate_x_function(Point& p)
  18. {
  19. bg::set<0>(p, bg::get<0>(p) + 100.0);
  20. }
  21. template<typename Point>
  22. struct scale_y_functor
  23. {
  24. inline void operator()(Point& p)
  25. {
  26. bg::set<1>(p, bg::get<1>(p) * 100.0);
  27. }
  28. };
  29. template<typename Point>
  30. struct sum_x_functor
  31. {
  32. typename bg::coordinate_type<Point>::type sum;
  33. sum_x_functor()
  34. : sum(0)
  35. {}
  36. inline void operator()(Point const& p)
  37. {
  38. sum += bg::get<0>(p);
  39. }
  40. };
  41. // Per segment
  42. static std::ostringstream g_out;
  43. template<typename Segment>
  44. inline void stream_segment(Segment const& s)
  45. {
  46. g_out << bg::dsv(s) << " ";
  47. }
  48. template<typename Segment>
  49. struct sum_segment_length
  50. {
  51. typename bg::coordinate_type<Segment>::type sum;
  52. sum_segment_length()
  53. : sum(0)
  54. {}
  55. inline void operator()(Segment const& s)
  56. {
  57. sum += bg::distance(s.first, s.second);
  58. }
  59. };
  60. template<typename Segment>
  61. inline void modify_segment(Segment& s)
  62. {
  63. if (bg::math::equals(bg::get<0,0>(s), 1.0))
  64. {
  65. bg::set<0,0>(s, 10.0);
  66. }
  67. }
  68. template <typename Geometry>
  69. void test_per_point_const(Geometry const& geometry, int expected)
  70. {
  71. typedef typename bg::point_type<Geometry>::type point_type;
  72. // Class (functor)
  73. sum_x_functor<point_type> functor;
  74. functor = bg::for_each_point(geometry, functor);
  75. BOOST_CHECK_EQUAL(functor.sum, expected);
  76. // Lambda
  77. #if !defined(BOOST_NO_CXX11_LAMBDAS)
  78. typename bg::coordinate_type<point_type>::type sum_x = 0;
  79. bg::for_each_point
  80. (
  81. geometry,
  82. [&sum_x](point_type const& p)
  83. {
  84. sum_x += bg::get<0>(p);
  85. }
  86. );
  87. BOOST_CHECK_EQUAL(sum_x, expected);
  88. #endif
  89. }
  90. template <typename Geometry>
  91. void test_per_point_non_const(Geometry& geometry,
  92. std::string const& expected1,
  93. std::string const& expected2)
  94. {
  95. #if !defined(BOOST_NO_CXX11_LAMBDAS)
  96. Geometry copy = geometry;
  97. #endif
  98. typedef typename bg::point_type<Geometry>::type point_type;
  99. // function
  100. bg::for_each_point(geometry, translate_x_function<point_type>);
  101. std::ostringstream out1;
  102. out1 << bg::wkt(geometry);
  103. BOOST_CHECK_MESSAGE(out1.str() == expected1,
  104. "for_each_point: "
  105. << " expected " << expected1
  106. << " got " << bg::wkt(geometry));
  107. // functor
  108. bg::for_each_point(geometry, scale_y_functor<point_type>());
  109. std::ostringstream out2;
  110. out2 << bg::wkt(geometry);
  111. BOOST_CHECK_MESSAGE(out2.str() == expected2,
  112. "for_each_point: "
  113. << " expected " << expected2
  114. << " got " << bg::wkt(geometry));
  115. #if !defined(BOOST_NO_CXX11_LAMBDAS)
  116. // Lambda, both functions above together. Without / with capturing
  117. geometry = copy;
  118. bg::for_each_point
  119. (
  120. geometry,
  121. [](point_type& p)
  122. {
  123. bg::set<0>(p, bg::get<0>(p) + 100);
  124. }
  125. );
  126. typename bg::coordinate_type<point_type>::type scale = 100;
  127. bg::for_each_point
  128. (
  129. geometry,
  130. [&](point_type& p)
  131. {
  132. bg::set<1>(p, bg::get<1>(p) * scale);
  133. }
  134. );
  135. std::ostringstream out3;
  136. out3 << bg::wkt(geometry);
  137. BOOST_CHECK_MESSAGE(out3.str() == expected2,
  138. "for_each_point (lambda): "
  139. << " expected " << expected2
  140. << " got " << bg::wkt(geometry));
  141. #endif
  142. }
  143. template <typename Geometry>
  144. void test_per_point(std::string const& wkt
  145. , int expected_sum_x
  146. , std::string const& expected1
  147. , std::string const& expected2
  148. )
  149. {
  150. Geometry geometry;
  151. bg::read_wkt(wkt, geometry);
  152. test_per_point_const(geometry, expected_sum_x);
  153. test_per_point_non_const(geometry, expected1, expected2);
  154. }
  155. template <typename Geometry>
  156. void test_per_segment_const(Geometry const& geometry,
  157. std::string const& expected_dsv,
  158. double expected_length)
  159. {
  160. typedef typename bg::point_type<Geometry>::type point_type;
  161. // function
  162. g_out.str("");
  163. g_out.clear();
  164. bg::for_each_segment(geometry,
  165. stream_segment<bg::model::referring_segment<point_type const> >);
  166. std::string out = g_out.str();
  167. boost::trim(out);
  168. BOOST_CHECK_EQUAL(out, expected_dsv);
  169. // functor
  170. sum_segment_length<bg::model::referring_segment<point_type const> > functor;
  171. functor = bg::for_each_segment(geometry, functor);
  172. BOOST_CHECK_CLOSE(functor.sum, expected_length, 0.0001);
  173. }
  174. template <typename Geometry>
  175. void test_per_segment_non_const(Geometry& geometry,
  176. std::string const& expected_wkt)
  177. {
  178. typedef typename bg::point_type<Geometry>::type point_type;
  179. // function
  180. bg::for_each_segment(geometry,
  181. modify_segment<bg::model::referring_segment<point_type> >);
  182. std::ostringstream out;
  183. out << bg::wkt(geometry);
  184. BOOST_CHECK_MESSAGE(out.str() == expected_wkt,
  185. "for_each_segment: "
  186. << " expected " << expected_wkt
  187. << " got " << bg::wkt(geometry));
  188. // function is working here, functor works for all others,
  189. // it will also work here.
  190. }
  191. template <typename Geometry>
  192. void test_per_segment(std::string const& wkt
  193. , std::string const& expected_dsv
  194. , double expected_length
  195. , std::string const& expected_wkt
  196. )
  197. {
  198. Geometry geometry;
  199. bg::read_wkt(wkt, geometry);
  200. test_per_segment_const(geometry, expected_dsv, expected_length);
  201. test_per_segment_non_const(geometry, expected_wkt);
  202. }
  203. template <typename Geometry>
  204. void test_geometry(std::string const& wkt
  205. , int expected_sum_x
  206. , std::string const& expected1
  207. , std::string const& expected2
  208. , std::string const& expected_dsv
  209. , double expected_length
  210. , std::string const& expected_wkt
  211. )
  212. {
  213. test_per_point<Geometry>(wkt, expected_sum_x, expected1, expected2);
  214. test_per_segment<Geometry>(wkt, expected_dsv, expected_length, expected_wkt);
  215. }
  216. #endif