test_centroid.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2015.
  5. // Modifications copyright (c) 2015, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_TEST_CENTROID_HPP
  11. #define BOOST_GEOMETRY_TEST_CENTROID_HPP
  12. // Test-functionality, shared between single and multi tests
  13. #include <boost/variant/variant.hpp>
  14. #include <geometry_test_common.hpp>
  15. #include <boost/geometry/strategies/strategies.hpp>
  16. #include <boost/geometry/algorithms/centroid.hpp>
  17. #include <boost/geometry/algorithms/distance.hpp>
  18. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  19. #include <boost/geometry/io/wkt/read.hpp>
  20. template <std::size_t D>
  21. struct check_result
  22. {
  23. template <typename Point1, typename Point2>
  24. static void apply(Point1 const& actual, Point2 const& expected)
  25. {
  26. check_result<D-1>::apply(actual, expected);
  27. BOOST_CHECK_CLOSE(bg::get<D-1>(actual), bg::get<D-1>(expected), 0.001);
  28. }
  29. };
  30. template <>
  31. struct check_result<0>
  32. {
  33. template <typename Point1, typename Point2>
  34. static void apply(Point1 const&, Point2 const&)
  35. {}
  36. };
  37. template <typename CalculationType, typename Geometry, typename Point>
  38. void test_with_other_calculation_type(Geometry const& geometry, Point& c1)
  39. {
  40. typedef typename bg::point_type<Geometry>::type point_type;
  41. // Calculate it with user defined strategy
  42. Point c2;
  43. bg::centroid(geometry, c2,
  44. bg::strategy::centroid::bashein_detmer<Point, point_type, CalculationType>());
  45. std::cout << typeid(CalculationType).name() << ": " << std::setprecision(20)
  46. << bg::get<0>(c2) << " " << bg::get<1>(c2)
  47. << " -> difference: " << bg::distance(c1, c2)
  48. << std::endl;
  49. }
  50. template <typename Geometry, typename Point, typename T>
  51. void test_centroid(Geometry const& geometry, T const& d1, T const& d2, T const& d3 = T(), T const& d4 = T(), T const& d5 = T())
  52. {
  53. Point c1;
  54. bg::centroid(geometry, c1);
  55. check_result<bg::dimension<Geometry>::type::value>::apply(c1, boost::make_tuple(d1, d2, d3, d4, d5));
  56. boost::variant<Geometry> v(geometry);
  57. bg::centroid(v, c1);
  58. #ifdef REPORT_RESULTS
  59. std::cout << "normal: " << std::setprecision(20) << bg::get<0>(c1) << " " << bg::get<1>(c1) << std::endl;
  60. //test_with_other_calculation_type<long long>(geometry, c1);
  61. test_with_other_calculation_type<float>(geometry, c1);
  62. test_with_other_calculation_type<long double>(geometry, c1);
  63. #if defined(HAVE_TTMATH)
  64. test_with_other_calculation_type<ttmath_big>(geometry, c1);
  65. #endif
  66. #endif
  67. }
  68. template <typename Geometry, typename Point, typename T>
  69. void test_centroid(std::string const& wkt, T const& d1, T const& d2, T const& d3 = T(), T const& d4 = T(), T const& d5 = T())
  70. {
  71. Geometry geometry;
  72. bg::read_wkt(wkt, geometry);
  73. test_centroid<Geometry, Point>(geometry, d1, d2, d3, d4, d5);
  74. }
  75. template <typename Geometry, typename T>
  76. void test_centroid(Geometry const& geometry, T const& d1, T const& d2, T const& d3 = T(), T const& d4 = T(), T const& d5 = T())
  77. {
  78. test_centroid<Geometry, typename bg::point_type<Geometry>::type>(geometry, d1, d2, d3, d4, d5);
  79. }
  80. template <typename Geometry, typename T>
  81. void test_centroid(std::string const& wkt, T const& d1, T const& d2, T const& d3 = T(), T const& d4 = T(), T const& d5 = T())
  82. {
  83. test_centroid<Geometry, typename bg::point_type<Geometry>::type>(wkt, d1, d2, d3, d4, d5);
  84. }
  85. template <typename Geometry>
  86. void test_centroid_exception()
  87. {
  88. Geometry geometry;
  89. try
  90. {
  91. typename bg::point_type<Geometry>::type c;
  92. bg::centroid(geometry, c);
  93. }
  94. catch(bg::centroid_exception const& )
  95. {
  96. return;
  97. }
  98. BOOST_CHECK_MESSAGE(false, "A centroid_exception should have been thrown" );
  99. }
  100. template <typename Geometry>
  101. void test_centroid_exception(std::string const& wkt)
  102. {
  103. Geometry geometry;
  104. bg::read_wkt(wkt, geometry);
  105. try
  106. {
  107. typename bg::point_type<Geometry>::type c;
  108. bg::centroid(geometry, c);
  109. }
  110. catch(bg::centroid_exception const& )
  111. {
  112. return;
  113. }
  114. BOOST_CHECK_MESSAGE(false, "A centroid_exception should have been thrown" );
  115. }
  116. #endif