PageRenderTime 41ms CodeModel.GetById 13ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/algorithms/assign.cpp

http://hadesmem.googlecode.com/
C++ | 265 lines | 187 code | 58 blank | 20 comment | 31 complexity | 81469b0a2a5df76cce9df9dcff99f34f MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  6// Copyright (c) 2009-2011 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 <geometry_test_common.hpp>
 16
 17#include <boost/geometry/arithmetic/arithmetic.hpp>
 18#include <boost/geometry/algorithms/assign.hpp>
 19#include <boost/geometry/algorithms/num_points.hpp>
 20
 21#include <boost/geometry/geometries/geometries.hpp>
 22#include <boost/geometry/geometries/adapted/c_array.hpp>
 23#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
 24#include <test_common/test_point.hpp>
 25
 26BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
 27BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
 28
 29
 30template <typename Linestring>
 31void check_linestring_2d(Linestring const& line)
 32{
 33    BOOST_CHECK((boost::size(line) == 3));
 34    BOOST_CHECK((bg::num_points(line) == 3));
 35
 36    typedef typename bg::point_type<Linestring>::type point_type;
 37    point_type const& p0 = line[0];
 38    BOOST_CHECK(bg::get<0>(p0) == 1);
 39    BOOST_CHECK(bg::get<1>(p0) == 2);
 40
 41    point_type const& p1 = line[1];
 42    BOOST_CHECK(bg::get<0>(p1) == 3);
 43    BOOST_CHECK(bg::get<1>(p1) == 4);
 44
 45    point_type const& p2 = line[2];
 46    BOOST_CHECK(bg::get<0>(p2) == 5);
 47    BOOST_CHECK(bg::get<1>(p2) == 6);
 48}
 49
 50template <typename Point>
 51void test_assign_linestring_2d()
 52{
 53    bg::model::linestring<Point> line;
 54
 55    // Test assignment of plain array (note that this is only possible if adapted c-array is included!)
 56    const double coors[3][2] = { {1, 2}, {3, 4}, {5, 6} };
 57    bg::assign_points(line, coors);
 58    check_linestring_2d(line);
 59
 60    // Test assignment of point array
 61    Point points[3];
 62    bg::assign_values(points[0], 1, 2);
 63    bg::assign_values(points[1], 3, 4);
 64    bg::assign_values(points[2], 5, 6);
 65    bg::assign_points(line, points);
 66    check_linestring_2d(line);
 67
 68    // Test assignment of array with different point-type (tuple adaption should be included)
 69    boost::tuple<float, float> tuples[3];
 70    tuples[0] = boost::make_tuple(1, 2);
 71    tuples[1] = boost::make_tuple(3, 4);
 72    tuples[2] = boost::make_tuple(5, 6);
 73    bg::assign_points(line, tuples);
 74    check_linestring_2d(line);
 75}
 76
 77namespace detail
 78{
 79    template <typename BoxOrSegment>
 80    void test_assign_box_or_segment_2d()
 81    {
 82        BoxOrSegment geometry;
 83        bg::assign_values(geometry, 1, 2, 3, 4);
 84        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 1));
 85        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 2));
 86        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 3));
 87        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 4));
 88
 89        bg::assign_zero(geometry);
 90        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 0));
 91        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 0));
 92        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 0));
 93        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 0));
 94
 95        bg::assign_inverse(geometry);
 96        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) > 9999));
 97        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) > 9999));
 98        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) < 9999));
 99        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) < 9999));
100    }
101}
102
103template <typename Point>
104void test_assign_box_or_segment_2d()
105{
106    detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
107    detail::test_assign_box_or_segment_2d<bg::model::segment<Point> >();
108}
109
110template <typename Point>
111void test_assign_box_2d()
112{
113    detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
114}
115
116
117template <typename Point>
118void test_assign_point_3d()
119{
120    Point p;
121    bg::assign_values(p, 1, 2, 3);
122    BOOST_CHECK(bg::get<0>(p) == 1);
123    BOOST_CHECK(bg::get<1>(p) == 2);
124    BOOST_CHECK(bg::get<2>(p) == 3);
125
126    bg::assign_value(p, 123);
127    BOOST_CHECK(bg::get<0>(p) == 123);
128    BOOST_CHECK(bg::get<1>(p) == 123);
129    BOOST_CHECK(bg::get<2>(p) == 123);
130
131    bg::assign_zero(p);
132    BOOST_CHECK(bg::get<0>(p) == 0);
133    BOOST_CHECK(bg::get<1>(p) == 0);
134    BOOST_CHECK(bg::get<2>(p) == 0);
135
136}
137
138template <typename P>
139void test_assign_conversion()
140{
141    typedef bg::model::box<P> box_type;
142    typedef bg::model::ring<P> ring_type;
143    typedef bg::model::polygon<P> polygon_type;
144
145    P p;
146    bg::assign_values(p, 1, 2);
147
148    box_type b;
149    bg::assign(b, p);
150
151    BOOST_CHECK_CLOSE((bg::get<0, 0>(b)), 1.0, 0.001);
152    BOOST_CHECK_CLOSE((bg::get<0, 1>(b)), 2.0, 0.001);
153    BOOST_CHECK_CLOSE((bg::get<1, 0>(b)), 1.0, 0.001);
154    BOOST_CHECK_CLOSE((bg::get<1, 1>(b)), 2.0, 0.001);
155
156
157    bg::set<bg::min_corner, 0>(b, 1);
158    bg::set<bg::min_corner, 1>(b, 2);
159    bg::set<bg::max_corner, 0>(b, 3);
160    bg::set<bg::max_corner, 1>(b, 4);
161	
162    ring_type ring;
163    bg::assign(ring, b);
164
165    //std::cout << bg::wkt(b) << std::endl;
166    //std::cout << bg::wkt(ring) << std::endl;
167
168    typename boost::range_const_iterator<ring_type>::type it = ring.begin();
169    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
170    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
171    it++;
172    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
173    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
174    it++;
175    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
176    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
177    it++;
178    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
179    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
180    it++;
181    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
182    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
183
184    BOOST_CHECK_EQUAL(ring.size(), 5u);
185
186
187    polygon_type polygon;
188
189    bg::assign(polygon, ring);
190    BOOST_CHECK_EQUAL(bg::num_points(polygon), 5u);
191
192    ring_type ring2;
193    bg::assign(ring2, polygon);
194    BOOST_CHECK_EQUAL(bg::num_points(ring2), 5u);
195}
196
197
198template <typename Point>
199void test_assign_point_2d()
200{
201    Point p;
202    bg::assign_values(p, 1, 2);
203    BOOST_CHECK(bg::get<0>(p) == 1);
204    BOOST_CHECK(bg::get<1>(p) == 2);
205
206    bg::assign_value(p, 123);
207    BOOST_CHECK(bg::get<0>(p) == 123);
208    BOOST_CHECK(bg::get<1>(p) == 123);
209
210    bg::assign_zero(p);
211    BOOST_CHECK(bg::get<0>(p) == 0);
212    BOOST_CHECK(bg::get<1>(p) == 0);
213}
214
215
216
217
218
219int test_main(int, char* [])
220{
221    test_assign_point_3d<int[3]>();
222    test_assign_point_3d<float[3]>();
223    test_assign_point_3d<double[3]>();
224    test_assign_point_3d<test::test_point>();
225    test_assign_point_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
226    test_assign_point_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
227    test_assign_point_3d<bg::model::point<double, 3, bg::cs::cartesian> >();
228
229    test_assign_point_2d<int[2]>();
230    test_assign_point_2d<float[2]>();
231    test_assign_point_2d<double[2]>();
232    test_assign_point_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
233    test_assign_point_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
234    test_assign_point_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
235
236    test_assign_conversion<bg::model::point<double, 2, bg::cs::cartesian> >();
237
238
239    // Segment (currently) cannot handle array's because derived from std::pair
240    test_assign_box_2d<int[2]>();
241    test_assign_box_2d<float[2]>();
242    test_assign_box_2d<double[2]>();
243
244    test_assign_box_or_segment_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
245    test_assign_box_or_segment_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
246    test_assign_box_or_segment_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
247
248    test_assign_linestring_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
249    test_assign_linestring_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
250    test_assign_linestring_2d<bg::model::point<double, 2, bg::cs::cartesian> >();
251
252#ifdef HAVE_TTMATH
253    // Next 3 need extra traits for c-array with custom type:
254    // test_assign_point_2d<ttmath_big[2]>();
255    // test_assign_point_3d<ttmath_big[3]>();
256    // test_assign_box_2d<ttmath_big[2]>();
257
258    test_assign_point_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
259    test_assign_point_3d<bg::model::point<ttmath_big, 3, bg::cs::cartesian> >();
260    test_assign_box_or_segment_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
261    test_assign_linestring_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
262#endif
263
264    return 0;
265}