#### /Src/Dependencies/Boost/libs/geometry/test/algorithms/test_convex_hull.hpp

C++ Header | 112 lines | 73 code | 26 blank | 13 comment | 3 complexity | eb075dc643d5b8cc53c1d04acb1313ba MD5 | raw file
```  1// Boost.Geometry (aka GGL, Generic Geometry Library)
2// Unit Test
3
4// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
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
8
9#ifndef BOOST_GEOMETRY_TEST_CONVEX_HULL_HPP
10#define BOOST_GEOMETRY_TEST_CONVEX_HULL_HPP
11
12#include <geometry_test_common.hpp>
13
14#include <boost/geometry/algorithms/convex_hull.hpp>
15#include <boost/geometry/algorithms/area.hpp>
16#include <boost/geometry/algorithms/num_points.hpp>
17
18#include <boost/geometry/strategies/strategies.hpp>
19
21#include <boost/geometry/domains/gis/io/wkt/write_wkt.hpp>
22
23#include <boost/geometry/geometries/polygon.hpp>
24
25
26template <typename Geometry, typename Hull>
27void test_convex_hull(Geometry const& geometry, Hull const& hull,
28                      std::size_t size_original, std::size_t size_hull,
29                      double expected_area, bool reverse)
30{
31
32    std::size_t n = bg::num_points(hull);
33
34    BOOST_CHECK_MESSAGE(n == size_hull,
35        "convex hull: " << bg::wkt(geometry)
36        << " -> " << bg::wkt(hull)
37        << " type "
38        << (typeid(typename bg::coordinate_type<Hull>::type).name())
39        << " -> Expected: " << size_hull
40        << " detected: " << n);
41
42
43    BOOST_CHECK(bg::num_points(geometry) == size_original);
44
45    typename bg::default_area_result<Geometry>::type ah = bg::area(hull);
46    if (reverse)
47    {
48        ah = -ah;
49    }
50
51//std::cout << "Area: " << bg::area(geometry) << std::endl;
52//std::cout << bg::wkt(hull) << std::endl;
53
54    BOOST_CHECK_CLOSE(ah, expected_area, 0.001);
55}
56
57template <typename Geometry, bool Clockwise>
58void test_geometry_order(std::string const& wkt,
59                      std::size_t size_original, std::size_t size_hull,
60                      double expected_area)
61{
62    Geometry geometry;
64
65    bg::model::polygon
66        <
67            typename bg::point_type<Geometry>::type,
68            Clockwise
69        > hull;
70
71    // Test version with output iterator
72    bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()));
73    test_convex_hull(geometry, hull,
74        size_original, size_hull, expected_area, ! Clockwise);
75
76    // Test version with ring as output
77    bg::clear(hull);
78    bg::convex_hull(geometry, hull.outer());
79    test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
80
81    // Test version with polygon as output
82    bg::clear(hull);
83    bg::convex_hull(geometry, hull);
84    test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
85
86    // Test version with strategy
87    bg::clear(hull);
88    bg::strategy::convex_hull::graham_andrew
89        <
90            Geometry,
91            typename bg::point_type<Geometry>::type
92        > graham;
93    bg::convex_hull(geometry, hull.outer(), graham);
94    test_convex_hull(geometry, hull, size_original, size_hull, expected_area, false);
95
96    // Test version with output iterator and strategy
97    bg::clear(hull);
98    bg::detail::convex_hull::convex_hull_insert(geometry, std::back_inserter(hull.outer()), graham);
99    test_convex_hull(geometry, hull, size_original, size_hull, expected_area, ! Clockwise);
100}
101
102template <typename Geometry>
103void test_geometry(std::string const& wkt,
104                      std::size_t size_original, std::size_t size_hull,
105                      double expected_area)
106{
107    test_geometry_order<Geometry, true>(wkt, size_original, size_hull, expected_area);
108    test_geometry_order<Geometry, false>(wkt, size_original, size_hull, expected_area);
109}
110
111
112#endif
```