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