transformer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/strategies/transform/inverse_transformer.hpp>
  13. #include <boost/geometry/strategies/transform/map_transformer.hpp>
  14. #include <boost/geometry/strategies/transform/matrix_transformers.hpp>
  15. #include <boost/geometry/algorithms/make.hpp>
  16. #include <boost/geometry/algorithms/transform.hpp>
  17. #include <boost/geometry/geometries/point.hpp>
  18. #include <boost/geometry/geometries/adapted/c_array.hpp>
  19. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  20. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  21. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  22. template <typename P, typename T>
  23. void check_inverse(P const& p, T const& trans)
  24. {
  25. typedef typename bg::coordinate_type<P>::type coordinate_type;
  26. const std::size_t dim = bg::dimension<P>::value;
  27. bg::strategy::transform::inverse_transformer<coordinate_type, dim, dim> inverse(trans);
  28. P i;
  29. bg::transform(p, i, inverse);
  30. BOOST_CHECK_CLOSE(double(bg::get<0>(i)), 1.0, 0.001);
  31. BOOST_CHECK_CLOSE(double(bg::get<1>(i)), 1.0, 0.001);
  32. }
  33. template <typename P>
  34. void test_all()
  35. {
  36. typedef typename bg::coordinate_type<P>::type coordinate_type;
  37. const std::size_t dim = bg::dimension<P>::value;
  38. P p;
  39. bg::assign_values(p, 1, 1);
  40. {
  41. bg::strategy::transform::translate_transformer<coordinate_type, dim, dim> trans(1, 1);
  42. P tp;
  43. bg::transform(p, tp, trans);
  44. BOOST_CHECK_CLOSE(double(bg::get<0>(tp)), 2.0, 0.001);
  45. BOOST_CHECK_CLOSE(double(bg::get<1>(tp)), 2.0, 0.001);
  46. check_inverse(tp, trans);
  47. }
  48. {
  49. bg::strategy::transform::scale_transformer<coordinate_type, dim, dim> trans(10, 10);
  50. P tp;
  51. bg::transform(p, tp, trans);
  52. BOOST_CHECK_CLOSE(double(bg::get<0>(tp)), 10.0, 0.001);
  53. BOOST_CHECK_CLOSE(double(bg::get<1>(tp)), 10.0, 0.001);
  54. check_inverse(tp, trans);
  55. }
  56. {
  57. bg::strategy::transform::rotate_transformer<bg::degree, double, dim, dim> trans(90.0);
  58. P tp;
  59. bg::transform(p, tp, trans);
  60. BOOST_CHECK_CLOSE(double(bg::get<0>(tp)), 1.0, 0.001);
  61. BOOST_CHECK_CLOSE(double(bg::get<1>(tp)), -1.0, 0.001);
  62. check_inverse(tp, trans);
  63. }
  64. {
  65. // Map from 0,0,2,2 to 0,0,500,500
  66. bg::strategy::transform::map_transformer<coordinate_type, dim, dim, false> trans
  67. (
  68. 0.0, 0.0, 2.0, 2.0, 500, 500
  69. );
  70. P tp;
  71. bg::transform(p, tp, trans);
  72. BOOST_CHECK_CLOSE(double(bg::get<0>(tp)), 250.0, 0.001);
  73. BOOST_CHECK_CLOSE(double(bg::get<1>(tp)), 250.0, 0.001);
  74. check_inverse(tp, trans);
  75. }
  76. }
  77. int test_main(int, char* [])
  78. {
  79. //test_all<int[2]>();
  80. //test_all<float[2]>();
  81. //test_all<double[2]>();
  82. test_all<boost::tuple<float, float> >();
  83. //test_all<point<int, 2, cs::cartesian> >();
  84. test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
  85. test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
  86. return 0;
  87. }