]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/algorithms/transform.cpp
f0414ff5169bba8811e39376113fe09ba12084ab
[ceph.git] / ceph / src / boost / libs / geometry / test / algorithms / transform.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
6 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7
8 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
9 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
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 #include <iostream>
16 #include <sstream>
17
18
19 #include <geometry_test_common.hpp>
20
21 #include <boost/geometry/algorithms/equals.hpp>
22 #include <boost/geometry/algorithms/make.hpp>
23 #include <boost/geometry/algorithms/transform.hpp>
24 #include <boost/geometry/geometries/geometries.hpp>
25 #include <boost/geometry/geometries/point_xy.hpp>
26 #include <boost/geometry/io/wkt/wkt.hpp>
27 #include <boost/geometry/strategies/strategies.hpp>
28 #include <boost/variant/variant.hpp>
29
30 #include <test_common/test_point.hpp>
31
32 template <typename Geometry1, typename Geometry2>
33 void check_transform(Geometry1 const& geometry1,
34 Geometry2 const& expected)
35 {
36 Geometry2 geometry2;
37 BOOST_CHECK(bg::transform(geometry1, geometry2));
38
39 std::ostringstream result_wkt, expected_wkt;
40 result_wkt << bg::wkt(geometry2);
41 expected_wkt << bg::wkt(expected);
42 BOOST_CHECK_EQUAL(result_wkt.str(), expected_wkt.str());
43 }
44
45 template <typename P1, typename P2, typename Value>
46 void test_transform_point(Value value)
47 {
48 P1 p1;
49 bg::set<0>(p1, 1);
50 bg::set<1>(p1, 2);
51 boost::variant<P1> v(p1);
52
53 P2 expected;
54 bg::assign(expected, p1);
55 bg::multiply_value(expected, value);
56
57 check_transform(p1, expected);
58 check_transform(v, expected);
59 }
60
61 template <typename P1, typename P2, typename Value>
62 void test_transform_linestring(Value value)
63 {
64 typedef bg::model::linestring<P1> line1_type;
65 typedef bg::model::linestring<P2> line2_type;
66
67 line1_type line1;
68 line1.push_back(bg::make<P1>(1, 1));
69 line1.push_back(bg::make<P1>(2, 2));
70 boost::variant<line1_type> v(line1);
71
72 line2_type expected;
73 for (BOOST_AUTO(p, line1.begin()); p != line1.end(); ++p)
74 {
75 P2 new_point;
76 bg::assign(new_point, *p);
77 bg::multiply_value(new_point, value);
78 expected.push_back(new_point);
79 }
80
81 check_transform(line1, expected);
82 check_transform(v, expected);
83 }
84
85
86 template <typename P1, typename P2, typename Value>
87 void test_all(Value value)
88 {
89 test_transform_point<P1, P2>(value);
90 test_transform_linestring<P1, P2>(value);
91 }
92
93 template <typename T, typename DegreeOrRadian>
94 void test_transformations(double phi, double theta, double r)
95 {
96 typedef bg::model::point<T, 3, bg::cs::cartesian> cartesian_type;
97 cartesian_type p;
98
99 // 1: using spherical coordinates
100 {
101 typedef bg::model::point<T, 3, bg::cs::spherical<DegreeOrRadian> > spherical_type;
102 spherical_type sph1;
103 assign_values(sph1, phi, theta, r);
104 BOOST_CHECK(transform(sph1, p));
105
106 spherical_type sph2;
107 BOOST_CHECK(transform(p, sph2));
108
109 BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
110 BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
111 }
112
113 // 2: using spherical coordinates on unit sphere
114 {
115 typedef bg::model::point<T, 2, bg::cs::spherical<DegreeOrRadian> > spherical_type;
116 spherical_type sph1, sph2;
117 assign_values(sph1, phi, theta);
118 BOOST_CHECK(transform(sph1, p));
119 BOOST_CHECK(transform(p, sph2));
120
121 BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
122 BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
123 }
124 }
125
126 int test_main(int, char* [])
127 {
128 typedef bg::model::d2::point_xy<double > P;
129 test_all<P, P>(1.0);
130 test_all<bg::model::d2::point_xy<int>, bg::model::d2::point_xy<float> >(1.0);
131
132 test_all<bg::model::point<double, 2, bg::cs::spherical<bg::degree> >,
133 bg::model::point<double, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<double>());
134 test_all<bg::model::point<double, 2, bg::cs::spherical<bg::radian> >,
135 bg::model::point<double, 2, bg::cs::spherical<bg::degree> > >(bg::math::r2d<double>());
136
137 test_all<bg::model::point<int, 2, bg::cs::spherical<bg::degree> >,
138 bg::model::point<float, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<float>());
139
140 test_transformations<float, bg::degree>(4, 52, 1);
141 test_transformations<double, bg::degree>(4, 52, 1);
142
143 test_transformations
144 <
145 float, bg::radian
146 >(3 * bg::math::d2r<float>(), 51 * bg::math::d2r<float>(), 1);
147
148 test_transformations
149 <
150 double, bg::radian
151 >(3 * bg::math::d2r<double>(), 51 * bg::math::d2r<double>(), 1);
152
153 #if defined(HAVE_TTMATH)
154 typedef bg::model::d2::point_xy<ttmath_big > PT;
155 test_all<PT, PT>();
156 test_transformations<ttmath_big, bg::degree>(4, 52, 1);
157 test_transformations
158 <
159 ttmath_big, bg::radian
160 >(3 * bg::math::d2r<ttmath_big>(), 51 * bg::math::d2r<ttmath_big>(), 1);
161 #endif
162
163 return 0;
164 }