transform.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <iostream>
  12. #include <sstream>
  13. #include <boost/typeof/typeof.hpp>
  14. #include <boost/variant/variant.hpp>
  15. #include <geometry_test_common.hpp>
  16. #include <boost/geometry/algorithms/equals.hpp>
  17. #include <boost/geometry/algorithms/make.hpp>
  18. #include <boost/geometry/algorithms/transform.hpp>
  19. #include <boost/geometry/geometries/geometries.hpp>
  20. #include <boost/geometry/geometries/point_xy.hpp>
  21. #include <boost/geometry/io/wkt/wkt.hpp>
  22. #include <boost/geometry/strategies/strategies.hpp>
  23. #include <test_common/test_point.hpp>
  24. template <typename Geometry1, typename Geometry2>
  25. void check_transform(Geometry1 const& geometry1,
  26. Geometry2 const& expected)
  27. {
  28. Geometry2 geometry2;
  29. BOOST_CHECK(bg::transform(geometry1, geometry2));
  30. std::ostringstream result_wkt, expected_wkt;
  31. result_wkt << bg::wkt(geometry2);
  32. expected_wkt << bg::wkt(expected);
  33. BOOST_CHECK_EQUAL(result_wkt.str(), expected_wkt.str());
  34. }
  35. template <typename P1, typename P2, typename Value>
  36. void test_transform_point(Value value)
  37. {
  38. P1 p1;
  39. bg::set<0>(p1, 1);
  40. bg::set<1>(p1, 2);
  41. boost::variant<P1> v(p1);
  42. P2 expected;
  43. bg::assign(expected, p1);
  44. bg::multiply_value(expected, value);
  45. check_transform(p1, expected);
  46. check_transform(v, expected);
  47. }
  48. template <typename P1, typename P2, typename Value>
  49. void test_transform_linestring(Value value)
  50. {
  51. typedef bg::model::linestring<P1> line1_type;
  52. typedef bg::model::linestring<P2> line2_type;
  53. line1_type line1;
  54. line1.push_back(bg::make<P1>(1, 1));
  55. line1.push_back(bg::make<P1>(2, 2));
  56. boost::variant<line1_type> v(line1);
  57. line2_type expected;
  58. for (BOOST_AUTO(p, line1.begin()); p != line1.end(); ++p)
  59. {
  60. P2 new_point;
  61. bg::assign(new_point, *p);
  62. bg::multiply_value(new_point, value);
  63. expected.push_back(new_point);
  64. }
  65. check_transform(line1, expected);
  66. check_transform(v, expected);
  67. }
  68. template <typename P1, typename P2, typename Value>
  69. void test_all(Value value)
  70. {
  71. test_transform_point<P1, P2>(value);
  72. test_transform_linestring<P1, P2>(value);
  73. }
  74. template <typename T, typename DegreeOrRadian>
  75. void test_transformations(double phi, double theta, double r)
  76. {
  77. typedef bg::model::point<T, 3, bg::cs::cartesian> cartesian_type;
  78. cartesian_type p;
  79. // 1: using spherical coordinates
  80. {
  81. typedef bg::model::point<T, 3, bg::cs::spherical<DegreeOrRadian> > spherical_type;
  82. spherical_type sph1;
  83. assign_values(sph1, phi, theta, r);
  84. BOOST_CHECK(transform(sph1, p));
  85. spherical_type sph2;
  86. BOOST_CHECK(transform(p, sph2));
  87. BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
  88. BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
  89. }
  90. // 2: using spherical coordinates on unit sphere
  91. {
  92. typedef bg::model::point<T, 2, bg::cs::spherical<DegreeOrRadian> > spherical_type;
  93. spherical_type sph1, sph2;
  94. assign_values(sph1, phi, theta);
  95. BOOST_CHECK(transform(sph1, p));
  96. BOOST_CHECK(transform(p, sph2));
  97. BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
  98. BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
  99. }
  100. }
  101. int test_main(int, char* [])
  102. {
  103. typedef bg::model::d2::point_xy<double > P;
  104. test_all<P, P>(1.0);
  105. test_all<bg::model::d2::point_xy<int>, bg::model::d2::point_xy<float> >(1.0);
  106. test_all<bg::model::point<double, 2, bg::cs::spherical<bg::degree> >,
  107. bg::model::point<double, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<double>());
  108. test_all<bg::model::point<double, 2, bg::cs::spherical<bg::radian> >,
  109. bg::model::point<double, 2, bg::cs::spherical<bg::degree> > >(bg::math::r2d<double>());
  110. test_all<bg::model::point<int, 2, bg::cs::spherical<bg::degree> >,
  111. bg::model::point<float, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<float>());
  112. test_transformations<float, bg::degree>(4, 52, 1);
  113. test_transformations<double, bg::degree>(4, 52, 1);
  114. test_transformations
  115. <
  116. float, bg::radian
  117. >(3 * bg::math::d2r<float>(), 51 * bg::math::d2r<float>(), 1);
  118. test_transformations
  119. <
  120. double, bg::radian
  121. >(3 * bg::math::d2r<double>(), 51 * bg::math::d2r<double>(), 1);
  122. #if defined(HAVE_TTMATH)
  123. typedef bg::model::d2::point_xy<ttmath_big > PT;
  124. test_all<PT, PT>();
  125. test_transformations<ttmath_big, bg::degree>(4, 52, 1);
  126. test_transformations
  127. <
  128. ttmath_big, bg::radian
  129. >(3 * bg::math::d2r<ttmath_big>(), 51 * bg::math::d2r<ttmath_big>(), 1);
  130. #endif
  131. return 0;
  132. }