• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // QuickBook Example
3 
4 // Copyright (c) 2015, Oracle and/or its affiliates
5 
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10 
11 //[is_valid_failure
12 //` Checks whether a geometry is valid and, if not valid, checks if it could be fixed by bg::correct; if so bg::correct is called on the geometry
13 
14 #include <iostream>
15 
16 #include <boost/geometry.hpp>
17 #include <boost/geometry/geometries/point_xy.hpp>
18 #include <boost/geometry/geometries/polygon.hpp>
19 /*<-*/ #include "create_svg_one.hpp" /*->*/
20 
main()21 int main()
22 {
23     typedef boost::geometry::model::d2::point_xy<double> point_type;
24     typedef boost::geometry::model::polygon<point_type> polygon_type;
25 
26     polygon_type poly;
27     boost::geometry::read_wkt("POLYGON((0 0,0 10,10 10,10 0),(0 0,9 2,9 1,0 0),(0 0,2 9,1 9,0 0))", poly);
28 
29     std::cout << "original geometry: " << boost::geometry::dsv(poly) << std::endl;
30     boost::geometry::validity_failure_type failure;
31     bool valid = boost::geometry::is_valid(poly, failure);
32 
33     // if the invalidity is only due to lack of closing points and/or wrongly oriented rings, then bg::correct can fix it
34     bool could_be_fixed = (failure == boost::geometry::failure_not_closed
35                            || boost::geometry::failure_wrong_orientation);
36     std::cout << "is valid? " << (valid ? "yes" : "no") << std::endl;
37     if (! valid)
38     {
39         std::cout << "can boost::geometry::correct remedy invalidity? " << (could_be_fixed ? "possibly yes" : "no") << std::endl;
40         if (could_be_fixed)
41         {
42             boost::geometry::correct(poly);
43             std::cout << "after correction: " << (boost::geometry::is_valid(poly) ? "valid" : "still invalid") << std::endl;
44             std::cout << "corrected geometry: " << boost::geometry::dsv(poly) << std::endl;
45         }
46     }
47     /*<-*/ create_svg("is_valid_failure_example.svg", poly); /*->*/
48     return 0;
49 }
50 
51 //]
52 
53 //[is_valid_failure_output
54 /*`
55 Output:
56 [pre
57 original geometry: (((0, 0), (0, 10), (10, 10), (10, 0)), ((0, 0), (9, 2), (9, 1), (0, 0)), ((0, 0), (2, 9), (1, 9), (0, 0)))
58 is valid? no
59 can boost::geometry::correct remedy invalidity? possibly yes
60 after correction: valid
61 corrected geometry: (((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)), ((0, 0), (9, 1), (9, 2), (0, 0)), ((0, 0), (2, 9), (1, 9), (0, 0)))
62 
63 [$img/algorithms/is_valid_failure_example.png]
64 
65 ]
66 
67 */
68 //]
69