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