/Src/Dependencies/Boost/libs/geometry/test/algorithms/assign.cpp

http://hadesmem.googlecode.com/ · C++ · 265 lines · 187 code · 58 blank · 20 comment · 31 complexity · 81469b0a2a5df76cce9df9dcff99f34f 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. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2011 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/arithmetic/arithmetic.hpp>
  13. #include <boost/geometry/algorithms/assign.hpp>
  14. #include <boost/geometry/algorithms/num_points.hpp>
  15. #include <boost/geometry/geometries/geometries.hpp>
  16. #include <boost/geometry/geometries/adapted/c_array.hpp>
  17. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  18. #include <test_common/test_point.hpp>
  19. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  20. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  21. template <typename Linestring>
  22. void check_linestring_2d(Linestring const& line)
  23. {
  24. BOOST_CHECK((boost::size(line) == 3));
  25. BOOST_CHECK((bg::num_points(line) == 3));
  26. typedef typename bg::point_type<Linestring>::type point_type;
  27. point_type const& p0 = line[0];
  28. BOOST_CHECK(bg::get<0>(p0) == 1);
  29. BOOST_CHECK(bg::get<1>(p0) == 2);
  30. point_type const& p1 = line[1];
  31. BOOST_CHECK(bg::get<0>(p1) == 3);
  32. BOOST_CHECK(bg::get<1>(p1) == 4);
  33. point_type const& p2 = line[2];
  34. BOOST_CHECK(bg::get<0>(p2) == 5);
  35. BOOST_CHECK(bg::get<1>(p2) == 6);
  36. }
  37. template <typename Point>
  38. void test_assign_linestring_2d()
  39. {
  40. bg::model::linestring<Point> line;
  41. // Test assignment of plain array (note that this is only possible if adapted c-array is included!)
  42. const double coors[3][2] = { {1, 2}, {3, 4}, {5, 6} };
  43. bg::assign_points(line, coors);
  44. check_linestring_2d(line);
  45. // Test assignment of point array
  46. Point points[3];
  47. bg::assign_values(points[0], 1, 2);
  48. bg::assign_values(points[1], 3, 4);
  49. bg::assign_values(points[2], 5, 6);
  50. bg::assign_points(line, points);
  51. check_linestring_2d(line);
  52. // Test assignment of array with different point-type (tuple adaption should be included)
  53. boost::tuple<float, float> tuples[3];
  54. tuples[0] = boost::make_tuple(1, 2);
  55. tuples[1] = boost::make_tuple(3, 4);
  56. tuples[2] = boost::make_tuple(5, 6);
  57. bg::assign_points(line, tuples);
  58. check_linestring_2d(line);
  59. }
  60. namespace detail
  61. {
  62. template <typename BoxOrSegment>
  63. void test_assign_box_or_segment_2d()
  64. {
  65. BoxOrSegment geometry;
  66. bg::assign_values(geometry, 1, 2, 3, 4);
  67. BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 1));
  68. BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 2));
  69. BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 3));
  70. BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 4));
  71. bg::assign_zero(geometry);
  72. BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 0));
  73. BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 0));
  74. BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 0));
  75. BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 0));
  76. bg::assign_inverse(geometry);
  77. BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) > 9999));
  78. BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) > 9999));
  79. BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) < 9999));
  80. BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) < 9999));
  81. }
  82. }
  83. template <typename Point>
  84. void test_assign_box_or_segment_2d()
  85. {
  86. detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
  87. detail::test_assign_box_or_segment_2d<bg::model::segment<Point> >();
  88. }
  89. template <typename Point>
  90. void test_assign_box_2d()
  91. {
  92. detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
  93. }
  94. template <typename Point>
  95. void test_assign_point_3d()
  96. {
  97. Point p;
  98. bg::assign_values(p, 1, 2, 3);
  99. BOOST_CHECK(bg::get<0>(p) == 1);
  100. BOOST_CHECK(bg::get<1>(p) == 2);
  101. BOOST_CHECK(bg::get<2>(p) == 3);
  102. bg::assign_value(p, 123);
  103. BOOST_CHECK(bg::get<0>(p) == 123);
  104. BOOST_CHECK(bg::get<1>(p) == 123);
  105. BOOST_CHECK(bg::get<2>(p) == 123);
  106. bg::assign_zero(p);
  107. BOOST_CHECK(bg::get<0>(p) == 0);
  108. BOOST_CHECK(bg::get<1>(p) == 0);
  109. BOOST_CHECK(bg::get<2>(p) == 0);
  110. }
  111. template <typename P>
  112. void test_assign_conversion()
  113. {
  114. typedef bg::model::box<P> box_type;
  115. typedef bg::model::ring<P> ring_type;
  116. typedef bg::model::polygon<P> polygon_type;
  117. P p;
  118. bg::assign_values(p, 1, 2);
  119. box_type b;
  120. bg::assign(b, p);
  121. BOOST_CHECK_CLOSE((bg::get<0, 0>(b)), 1.0, 0.001);
  122. BOOST_CHECK_CLOSE((bg::get<0, 1>(b)), 2.0, 0.001);
  123. BOOST_CHECK_CLOSE((bg::get<1, 0>(b)), 1.0, 0.001);
  124. BOOST_CHECK_CLOSE((bg::get<1, 1>(b)), 2.0, 0.001);
  125. bg::set<bg::min_corner, 0>(b, 1);
  126. bg::set<bg::min_corner, 1>(b, 2);
  127. bg::set<bg::max_corner, 0>(b, 3);
  128. bg::set<bg::max_corner, 1>(b, 4);
  129. ring_type ring;
  130. bg::assign(ring, b);
  131. //std::cout << bg::wkt(b) << std::endl;
  132. //std::cout << bg::wkt(ring) << std::endl;
  133. typename boost::range_const_iterator<ring_type>::type it = ring.begin();
  134. BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
  135. BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
  136. it++;
  137. BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
  138. BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
  139. it++;
  140. BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
  141. BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
  142. it++;
  143. BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
  144. BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
  145. it++;
  146. BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
  147. BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
  148. BOOST_CHECK_EQUAL(ring.size(), 5u);
  149. polygon_type polygon;
  150. bg::assign(polygon, ring);
  151. BOOST_CHECK_EQUAL(bg::num_points(polygon), 5u);
  152. ring_type ring2;
  153. bg::assign(ring2, polygon);
  154. BOOST_CHECK_EQUAL(bg::num_points(ring2), 5u);
  155. }
  156. template <typename Point>
  157. void test_assign_point_2d()
  158. {
  159. Point p;
  160. bg::assign_values(p, 1, 2);
  161. BOOST_CHECK(bg::get<0>(p) == 1);
  162. BOOST_CHECK(bg::get<1>(p) == 2);
  163. bg::assign_value(p, 123);
  164. BOOST_CHECK(bg::get<0>(p) == 123);
  165. BOOST_CHECK(bg::get<1>(p) == 123);
  166. bg::assign_zero(p);
  167. BOOST_CHECK(bg::get<0>(p) == 0);
  168. BOOST_CHECK(bg::get<1>(p) == 0);
  169. }
  170. int test_main(int, char* [])
  171. {
  172. test_assign_point_3d<int[3]>();
  173. test_assign_point_3d<float[3]>();
  174. test_assign_point_3d<double[3]>();
  175. test_assign_point_3d<test::test_point>();
  176. test_assign_point_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
  177. test_assign_point_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
  178. test_assign_point_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
  179. test_assign_point_2d<int[2]>();
  180. test_assign_point_2d<float[2]>();
  181. test_assign_point_2d<double[2]>();
  182. test_assign_point_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
  183. test_assign_point_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
  184. test_assign_point_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
  185. test_assign_conversion<bg::model::point<double, 2, bg::cs::cartesian> >();
  186. // Segment (currently) cannot handle array's because derived from std::pair
  187. test_assign_box_2d<int[2]>();
  188. test_assign_box_2d<float[2]>();
  189. test_assign_box_2d<double[2]>();
  190. test_assign_box_or_segment_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
  191. test_assign_box_or_segment_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
  192. test_assign_box_or_segment_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
  193. test_assign_linestring_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
  194. test_assign_linestring_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
  195. test_assign_linestring_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
  196. #ifdef HAVE_TTMATH
  197. // Next 3 need extra traits for c-array with custom type:
  198. // test_assign_point_2d<ttmath_big[2]>();
  199. // test_assign_point_3d<ttmath_big[3]>();
  200. // test_assign_box_2d<ttmath_big[2]>();
  201. test_assign_point_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
  202. test_assign_point_3d<bg::model::point<ttmath_big, 3, bg::cs::cartesian> >();
  203. test_assign_box_or_segment_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
  204. test_assign_linestring_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
  205. #endif
  206. return 0;
  207. }