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 #include <boost/typeof/typeof.hpp>
19 #include <boost/variant/variant.hpp>
20 
21 #include <geometry_test_common.hpp>
22 
23 #include <boost/geometry/algorithms/equals.hpp>
24 #include <boost/geometry/algorithms/make.hpp>
25 #include <boost/geometry/algorithms/transform.hpp>
26 #include <boost/geometry/geometries/geometries.hpp>
27 #include <boost/geometry/geometries/point_xy.hpp>
28 #include <boost/geometry/io/wkt/wkt.hpp>
29 #include <boost/geometry/strategies/strategies.hpp>
30 
31 #include <test_common/test_point.hpp>
32 
33 template <typename Geometry1, typename Geometry2>
check_transform(Geometry1 const & geometry1,Geometry2 const & expected)34 void check_transform(Geometry1 const& geometry1,
35                      Geometry2 const& expected)
36 {
37     Geometry2 geometry2;
38     BOOST_CHECK(bg::transform(geometry1, geometry2));
39 
40     std::ostringstream result_wkt, expected_wkt;
41     result_wkt << bg::wkt(geometry2);
42     expected_wkt << bg::wkt(expected);
43     BOOST_CHECK_EQUAL(result_wkt.str(), expected_wkt.str());
44 }
45 
46 template <typename P1, typename P2, typename Value>
test_transform_point(Value value)47 void test_transform_point(Value value)
48 {
49     P1 p1;
50     bg::set<0>(p1, 1);
51     bg::set<1>(p1, 2);
52     boost::variant<P1> v(p1);
53 
54     P2 expected;
55     bg::assign(expected, p1);
56     bg::multiply_value(expected, value);
57 
58     check_transform(p1, expected);
59     check_transform(v, expected);
60 }
61 
62 template <typename P1, typename P2, typename Value>
test_transform_linestring(Value value)63 void test_transform_linestring(Value value)
64 {
65     typedef bg::model::linestring<P1> line1_type;
66     typedef bg::model::linestring<P2> line2_type;
67 
68     line1_type line1;
69     line1.push_back(bg::make<P1>(1, 1));
70     line1.push_back(bg::make<P1>(2, 2));
71     boost::variant<line1_type> v(line1);
72 
73     line2_type expected;
74     for (BOOST_AUTO(p, line1.begin()); p != line1.end(); ++p)
75     {
76         P2 new_point;
77         bg::assign(new_point, *p);
78         bg::multiply_value(new_point, value);
79         expected.push_back(new_point);
80     }
81 
82     check_transform(line1, expected);
83     check_transform(v, expected);
84 }
85 
86 
87 template <typename P1, typename P2, typename Value>
test_all(Value value)88 void test_all(Value value)
89 {
90     test_transform_point<P1, P2>(value);
91     test_transform_linestring<P1, P2>(value);
92 }
93 
94 template <typename T, typename DegreeOrRadian>
test_transformations(double phi,double theta,double r)95 void test_transformations(double phi, double theta, double r)
96 {
97     typedef bg::model::point<T, 3, bg::cs::cartesian> cartesian_type;
98     cartesian_type p;
99 
100     // 1: using spherical coordinates
101     {
102         typedef bg::model::point<T, 3, bg::cs::spherical<DegreeOrRadian> >  spherical_type;
103         spherical_type sph1;
104         assign_values(sph1, phi, theta, r);
105         BOOST_CHECK(transform(sph1, p));
106 
107         spherical_type sph2;
108         BOOST_CHECK(transform(p, sph2));
109 
110         BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
111         BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
112     }
113 
114     // 2: using spherical coordinates on unit sphere
115     {
116         typedef bg::model::point<T, 2, bg::cs::spherical<DegreeOrRadian> >  spherical_type;
117         spherical_type sph1, sph2;
118         assign_values(sph1, phi, theta);
119         BOOST_CHECK(transform(sph1, p));
120         BOOST_CHECK(transform(p, sph2));
121 
122         BOOST_CHECK_CLOSE(bg::get<0>(sph1), bg::get<0>(sph2), 0.001);
123         BOOST_CHECK_CLOSE(bg::get<1>(sph1), bg::get<1>(sph2), 0.001);
124     }
125 }
126 
test_main(int,char * [])127 int test_main(int, char* [])
128 {
129     typedef bg::model::d2::point_xy<double > P;
130     test_all<P, P>(1.0);
131     test_all<bg::model::d2::point_xy<int>, bg::model::d2::point_xy<float> >(1.0);
132 
133     test_all<bg::model::point<double, 2, bg::cs::spherical<bg::degree> >,
134         bg::model::point<double, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<double>());
135     test_all<bg::model::point<double, 2, bg::cs::spherical<bg::radian> >,
136         bg::model::point<double, 2, bg::cs::spherical<bg::degree> > >(bg::math::r2d<double>());
137 
138     test_all<bg::model::point<int, 2, bg::cs::spherical<bg::degree> >,
139         bg::model::point<float, 2, bg::cs::spherical<bg::radian> > >(bg::math::d2r<float>());
140 
141     test_transformations<float, bg::degree>(4, 52, 1);
142     test_transformations<double, bg::degree>(4, 52, 1);
143 
144     test_transformations
145         <
146             float, bg::radian
147         >(3 * bg::math::d2r<float>(), 51 * bg::math::d2r<float>(), 1);
148 
149     test_transformations
150         <
151             double, bg::radian
152         >(3 * bg::math::d2r<double>(), 51 * bg::math::d2r<double>(), 1);
153 
154     return 0;
155 }
156