access.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/core/access.hpp>
  13. #include <boost/geometry/core/coordinate_type.hpp>
  14. #include <boost/geometry/algorithms/make.hpp>
  15. #include <boost/geometry/geometries/adapted/c_array.hpp>
  16. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/geometries/point.hpp>
  19. #include <boost/geometry/geometries/segment.hpp>
  20. #include <boost/geometry/geometries/box.hpp>
  21. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  22. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  23. template <typename G>
  24. void test_get_set()
  25. {
  26. typedef typename bg::coordinate_type<G>::type coordinate_type;
  27. G g;
  28. bg::set<0>(g, coordinate_type(1));
  29. bg::set<1>(g, coordinate_type(2));
  30. coordinate_type x = bg::get<0>(g);
  31. coordinate_type y = bg::get<1>(g);
  32. BOOST_CHECK_CLOSE(double(x), 1.0, 0.0001);
  33. BOOST_CHECK_CLOSE(double(y), 2.0, 0.0001);
  34. }
  35. template <typename G>
  36. void test_indexed_get_set(G& g)
  37. {
  38. bg::set<0, 0>(g, 1);
  39. bg::set<0, 1>(g, 2);
  40. bg::set<1, 0>(g, 3);
  41. bg::set<1, 1>(g, 4);
  42. typedef typename bg::coordinate_type<G>::type coordinate_type;
  43. coordinate_type x1 = bg::get<0, 0>(g);
  44. coordinate_type y1 = bg::get<0, 1>(g);
  45. coordinate_type x2 = bg::get<1, 0>(g);
  46. coordinate_type y2 = bg::get<1, 1>(g);
  47. BOOST_CHECK_CLOSE(double(x1), 1.0, 0.0001);
  48. BOOST_CHECK_CLOSE(double(y1), 2.0, 0.0001);
  49. BOOST_CHECK_CLOSE(double(x2), 3.0, 0.0001);
  50. BOOST_CHECK_CLOSE(double(y2), 4.0, 0.0001);
  51. }
  52. template <typename G, typename T>
  53. void test_indexed_get(G const& g, T a, T b, T c, T d)
  54. {
  55. T x1 = bg::get<0, 0>(g);
  56. T y1 = bg::get<0, 1>(g);
  57. T x2 = bg::get<1, 0>(g);
  58. T y2 = bg::get<1, 1>(g);
  59. BOOST_CHECK_CLOSE(double(x1), double(a), 0.0001);
  60. BOOST_CHECK_CLOSE(double(y1), double(b), 0.0001);
  61. BOOST_CHECK_CLOSE(double(x2), double(c), 0.0001);
  62. BOOST_CHECK_CLOSE(double(y2), double(d), 0.0001);
  63. }
  64. template <typename P>
  65. void test_all()
  66. {
  67. typedef typename bg::coordinate_type<P>::type coordinate_type;
  68. // POINT, setting coordinate
  69. test_get_set<P>();
  70. // BOX, setting left/right/top/bottom
  71. bg::model::box<P> b;
  72. test_indexed_get_set(b);
  73. // SEGMENT (in GGL not having default constructor; however that is not a requirement)
  74. P p1 = bg::make_zero<P>();
  75. P p2 = bg::make_zero<P>();
  76. bg::model::referring_segment<P> s(p1, p2);
  77. test_indexed_get_set(s);
  78. // CONST SEGMENT
  79. bg::set<0>(p1, 1); // we don't use assign because dim in {2,3}
  80. bg::set<1>(p1, 2);
  81. bg::set<0>(p2, 3);
  82. bg::set<1>(p2, 4);
  83. bg::model::referring_segment<P const> cs(p1, p2);
  84. test_indexed_get(cs,
  85. coordinate_type(1), coordinate_type(2),
  86. coordinate_type(3), coordinate_type(4));
  87. }
  88. int test_main(int, char* [])
  89. {
  90. test_get_set<int[2]>();
  91. test_get_set<float[2]>();
  92. test_get_set<double[2]>();
  93. test_get_set<double[3]>();
  94. test_get_set<boost::tuple<double, double> >();
  95. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  96. test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
  97. test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
  98. return 0;
  99. }