check_validity.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Boost.Geometry
  2. // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_TEST_CHECK_VALIDITY_HPP
  7. #define BOOST_GEOMETRY_TEST_CHECK_VALIDITY_HPP
  8. #include <boost/foreach.hpp>
  9. #include <boost/geometry/algorithms/is_valid.hpp>
  10. template<typename Geometry, typename G1, typename G2>
  11. inline bool is_output_valid(Geometry const& geometry,
  12. std::string const& case_id,
  13. G1 const& g1, G2 const& g2,
  14. std::string& message)
  15. {
  16. bool const result = bg::is_valid(geometry, message);
  17. if (! result)
  18. {
  19. // Check if input was valid. If not, do not report output validity
  20. if (! bg::is_valid(g1) || ! bg::is_valid(g2))
  21. {
  22. std::cout << "WARNING: Input is not considered as valid; "
  23. << "this can cause that output is invalid: " << case_id
  24. << std::endl;
  25. return true;
  26. }
  27. }
  28. return result;
  29. }
  30. template
  31. <
  32. typename Geometry,
  33. typename Tag = typename bg::tag<Geometry>::type
  34. >
  35. struct check_validity
  36. {
  37. template <typename G1, typename G2>
  38. static inline
  39. bool apply(Geometry const& geometry,
  40. std::string const& case_id,
  41. G1 const& g1, G2 const& g2,
  42. std::string& message)
  43. {
  44. return is_output_valid(geometry, case_id, g1, g2, message);
  45. }
  46. };
  47. // Specialization for vector of <geometry> (e.g. rings)
  48. template <typename Geometry>
  49. struct check_validity<Geometry, void>
  50. {
  51. template <typename G1, typename G2>
  52. static inline
  53. bool apply(Geometry const& geometry,
  54. std::string const& case_id,
  55. G1 const& g1, G2 const& g2,
  56. std::string& message)
  57. {
  58. typedef typename boost::range_value<Geometry>::type single_type;
  59. BOOST_FOREACH(single_type const& element, geometry)
  60. {
  61. if (! is_output_valid(element, case_id, g1, g2, message))
  62. {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. };
  69. #endif // BOOST_GEOMETRY_TEST_CHECK_VALIDITY_HPP