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
16 #include <geometry_test_common.hpp>
17
18
19 #include <boost/geometry/core/access.hpp>
20
21
22 #include <boost/geometry/core/coordinate_type.hpp>
23 #include <boost/geometry/algorithms/make.hpp>
24
25 #include <boost/geometry/geometries/adapted/c_array.hpp>
26 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
27
28
29 #include <boost/geometry/core/cs.hpp>
30 #include <boost/geometry/geometries/point.hpp>
31 #include <boost/geometry/geometries/segment.hpp>
32 #include <boost/geometry/geometries/box.hpp>
33
34 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)35 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
36
37
38 template <typename G>
39 void test_get_set()
40 {
41 typedef typename bg::coordinate_type<G>::type coordinate_type;
42
43 G g;
44 bg::set<0>(g, coordinate_type(1));
45 bg::set<1>(g, coordinate_type(2));
46
47 coordinate_type x = bg::get<0>(g);
48 coordinate_type y = bg::get<1>(g);
49
50 BOOST_CHECK_CLOSE(double(x), 1.0, 0.0001);
51 BOOST_CHECK_CLOSE(double(y), 2.0, 0.0001);
52 }
53
54 template <typename G>
test_indexed_get_set(G & g)55 void test_indexed_get_set(G& g)
56 {
57 bg::set<0, 0>(g, 1);
58 bg::set<0, 1>(g, 2);
59 bg::set<1, 0>(g, 3);
60 bg::set<1, 1>(g, 4);
61
62 typedef typename bg::coordinate_type<G>::type coordinate_type;
63 coordinate_type x1 = bg::get<0, 0>(g);
64 coordinate_type y1 = bg::get<0, 1>(g);
65 coordinate_type x2 = bg::get<1, 0>(g);
66 coordinate_type y2 = bg::get<1, 1>(g);
67
68 BOOST_CHECK_CLOSE(double(x1), 1.0, 0.0001);
69 BOOST_CHECK_CLOSE(double(y1), 2.0, 0.0001);
70 BOOST_CHECK_CLOSE(double(x2), 3.0, 0.0001);
71 BOOST_CHECK_CLOSE(double(y2), 4.0, 0.0001);
72 }
73
74 template <typename G, typename T>
test_indexed_get(G const & g,T a,T b,T c,T d)75 void test_indexed_get(G const& g, T a, T b, T c, T d)
76 {
77 T x1 = bg::get<0, 0>(g);
78 T y1 = bg::get<0, 1>(g);
79 T x2 = bg::get<1, 0>(g);
80 T y2 = bg::get<1, 1>(g);
81
82 BOOST_CHECK_CLOSE(double(x1), double(a), 0.0001);
83 BOOST_CHECK_CLOSE(double(y1), double(b), 0.0001);
84 BOOST_CHECK_CLOSE(double(x2), double(c), 0.0001);
85 BOOST_CHECK_CLOSE(double(y2), double(d), 0.0001);
86 }
87
88 template <typename P>
test_all()89 void test_all()
90 {
91 typedef typename bg::coordinate_type<P>::type coordinate_type;
92
93 // POINT, setting coordinate
94 test_get_set<P>();
95
96
97 // BOX, setting left/right/top/bottom
98 bg::model::box<P> b;
99 test_indexed_get_set(b);
100
101 // SEGMENT (in GGL not having default constructor; however that is not a requirement)
102 P p1 = bg::make_zero<P>();
103 P p2 = bg::make_zero<P>();
104 bg::model::referring_segment<P> s(p1, p2);
105 test_indexed_get_set(s);
106
107 // CONST SEGMENT
108 bg::set<0>(p1, 1); // we don't use assign because dim in {2,3}
109 bg::set<1>(p1, 2);
110 bg::set<0>(p2, 3);
111 bg::set<1>(p2, 4);
112 bg::model::referring_segment<P const> cs(p1, p2);
113 test_indexed_get(cs,
114 coordinate_type(1), coordinate_type(2),
115 coordinate_type(3), coordinate_type(4));
116 }
117
118
test_main(int,char * [])119 int test_main(int, char* [])
120 {
121 test_get_set<int[2]>();
122 test_get_set<float[2]>();
123 test_get_set<double[2]>();
124 test_get_set<double[3]>();
125
126 test_get_set<boost::tuple<double, double> >();
127
128 test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
129 test_all<bg::model::point<float, 2, bg::cs::cartesian> >();
130 test_all<bg::model::point<double, 2, bg::cs::cartesian> >();
131
132 return 0;
133 }
134