rescale_policy.cpp 4.0 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 <iostream>
  12. #include <string>
  13. #include <boost/foreach.hpp>
  14. #include <boost/geometry/algorithms/correct.hpp>
  15. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  16. #include <boost/geometry/algorithms/length.hpp>
  17. #include <boost/geometry/algorithms/num_points.hpp>
  18. #include <boost/geometry/geometries/geometries.hpp>
  19. #include <boost/geometry/geometries/point_xy.hpp>
  20. #include <boost/geometry/strategies/strategies.hpp>
  21. #include <boost/geometry/iterators/point_iterator.hpp>
  22. #include <boost/geometry/policies/robustness/get_rescale_policy.hpp>
  23. #include <boost/geometry/io/wkt/wkt.hpp>
  24. #include <geometry_test_common.hpp>
  25. template
  26. <
  27. typename RescalePolicy,
  28. typename Geometry1,
  29. typename Geometry2
  30. >
  31. void test_one(std::string const& wkt1, std::string const& wkt2,
  32. std::string const& expected_coordinates)
  33. {
  34. Geometry1 geometry1;
  35. bg::read_wkt(wkt1, geometry1);
  36. Geometry2 geometry2;
  37. bg::read_wkt(wkt2, geometry2);
  38. RescalePolicy rescale_policy
  39. = bg::get_rescale_policy<RescalePolicy>(geometry1, geometry2);
  40. typedef typename bg::point_type<Geometry1>::type point_type;
  41. typedef typename bg::robust_point_type
  42. <
  43. point_type, RescalePolicy
  44. >::type robust_point_type;
  45. {
  46. robust_point_type robust_point;
  47. bg::recalculate(robust_point, *bg::points_begin(geometry1), rescale_policy);
  48. std::ostringstream out;
  49. out << bg::get<0>(robust_point) << " " << bg::get<1>(robust_point);
  50. BOOST_CHECK_EQUAL(expected_coordinates, out.str());
  51. }
  52. {
  53. // Assuming Geometry1 is a polygon:
  54. typedef bg::model::polygon<robust_point_type> polygon_type;
  55. polygon_type geometry_out;
  56. bg::recalculate(geometry_out, geometry1, rescale_policy);
  57. robust_point_type p = *bg::points_begin(geometry_out);
  58. std::ostringstream out;
  59. out << bg::get<0>(p) << " " << bg::get<1>(p);
  60. BOOST_CHECK_EQUAL(expected_coordinates, out.str());
  61. }
  62. }
  63. static std::string simplex_normal[2] =
  64. {"POLYGON((0 1,2 5,5 3,0 1))",
  65. "POLYGON((3 0,0 3,4 5,3 0))"};
  66. static std::string simplex_large[2] =
  67. {"POLYGON((0 1000,2000 5000,5000 3000,0 1000))",
  68. "POLYGON((3000 0,0 3000,4000 5000,3000 0))"};
  69. template <bool Rescale, typename P>
  70. void test_rescale(std::string const& expected_normal, std::string const& expected_large)
  71. {
  72. typedef bg::model::polygon<P> polygon;
  73. typedef typename boost::mpl::if_c
  74. <
  75. Rescale,
  76. typename bg::rescale_policy_type<P>::type ,
  77. bg::detail::no_rescale_policy
  78. >::type rescale_policy_type;
  79. test_one<rescale_policy_type, polygon, polygon>(
  80. simplex_normal[0], simplex_normal[1],
  81. expected_normal);
  82. test_one<rescale_policy_type, polygon, polygon>(
  83. simplex_large[0], simplex_large[1],
  84. expected_large);
  85. }
  86. template <typename T>
  87. void test_all(std::string const& expected_normal, std::string const& expected_large)
  88. {
  89. typedef bg::model::d2::point_xy<T> point_type;
  90. test_rescale<true, point_type>(expected_normal, expected_large);
  91. //test_rescale<false, point_type>();
  92. }
  93. int test_main(int, char* [])
  94. {
  95. test_all<double>("-5000000 -3000000", "-5000000 -3000000");
  96. test_all<long double>("-5000000 -3000000", "-5000000 -3000000");
  97. test_all<int>("0 1", "0 1000");
  98. test_all<boost::long_long_type>("0 1", "0 1000");
  99. // test_all<short int>(); // compiles but overflows
  100. return 0;
  101. }