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 #if defined(HAVE_TTMATH)
86     test_with_other_calculation_type<ttmath_big>(geometry, c1);
87 #endif
88 
89 #endif
90 }
91 
92 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 ())93 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())
94 {
95     Geometry geometry;
96     bg::read_wkt(wkt, geometry);
97 
98     test_centroid<Geometry, Point>(geometry, d1, d2, d3, d4, d5);
99 }
100 
101 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 ())102 void test_centroid(Geometry const& geometry, T const& d1, T const& d2, T const& d3 = T(), T const& d4 = T(), T const& d5 = T())
103 {
104     test_centroid<Geometry, typename bg::point_type<Geometry>::type>(geometry, d1, d2, d3, d4, d5);
105 }
106 
107 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 ())108 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())
109 {
110     test_centroid<Geometry, typename bg::point_type<Geometry>::type>(wkt, d1, d2, d3, d4, d5);
111 }
112 
113 template <typename Geometry>
test_centroid_exception()114 void test_centroid_exception()
115 {
116     Geometry geometry;
117     try
118     {
119         typename bg::point_type<Geometry>::type c;
120         bg::centroid(geometry, c);
121     }
122     catch(bg::centroid_exception const& )
123     {
124         return;
125     }
126     BOOST_CHECK_MESSAGE(false, "A centroid_exception should have been thrown" );
127 }
128 
129 
130 #endif
131