summaryrefslogtreecommitdiff
path: root/libs/geometry/test/algorithms/assign.cpp
blob: aca34a6875c9fe0f648ebd6fcd645de7bfbdd841 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
// Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
// Copyright (c) 2014 Samuel Debionne, Grenoble, France.

// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <geometry_test_common.hpp>

#include <boost/geometry/arithmetic/arithmetic.hpp>
#include <boost/geometry/algorithms/assign.hpp>
#include <boost/geometry/algorithms/num_points.hpp>

#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/variant/variant.hpp>
#include <test_common/test_point.hpp>

BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)


template <typename Geometry>
void check_geometry(Geometry const& geometry, std::string const& expected)
{
    std::ostringstream out;
    out << bg::wkt(geometry);
    BOOST_CHECK_EQUAL(out.str(), expected);
}

template <typename Geometry, typename Points>
void check_assign_points(Points const& points, std::string const& /*expected*/)
{
    Geometry geometry;
    bg::assign_points(geometry, points);
    check_geometry(geometry, "LINESTRING(1 2,3 4,5 6)");

    boost::variant<Geometry> v;
    bg::assign_points(v, points);
}

template <typename Point>
void test_assign_linestring_2d()
{
    bg::model::linestring<Point> line;

    // Test assignment of plain array (note that this is only possible if adapted c-array is included!)
    const double coors[3][2] = { {1, 2}, {3, 4}, {5, 6} };
    check_assign_points<bg::model::linestring<Point> >(coors, "LINESTRING(1 2,3 4,5 6)");

    // Test assignment of point array
    Point points[3];
    bg::assign_values(points[0], 1, 2);
    bg::assign_values(points[1], 3, 4);
    bg::assign_values(points[2], 5, 6);
    check_assign_points<bg::model::linestring<Point> >(points, "LINESTRING(1 2,3 4,5 6)");

    // Test assignment of array with different point-type (tuple adaption should be included)
    boost::tuple<float, float> tuples[3];
    tuples[0] = boost::make_tuple(1, 2);
    tuples[1] = boost::make_tuple(3, 4);
    tuples[2] = boost::make_tuple(5, 6);
    check_assign_points<bg::model::linestring<Point> >(tuples, "LINESTRING(1 2,3 4,5 6)");
}

namespace detail
{
    template <typename BoxOrSegment>
    void test_assign_box_or_segment_2d()
    {
        BoxOrSegment geometry;
        bg::assign_values(geometry, 1, 2, 3, 4);
        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 1));
        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 2));
        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 3));
        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 4));

        bg::assign_zero(geometry);
        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) == 0));
        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) == 0));
        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) == 0));
        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) == 0));

        bg::assign_inverse(geometry);
        BOOST_CHECK((bg::get<bg::min_corner, 0>(geometry) > 9999));
        BOOST_CHECK((bg::get<bg::min_corner, 1>(geometry) > 9999));
        BOOST_CHECK((bg::get<bg::max_corner, 0>(geometry) < 9999));
        BOOST_CHECK((bg::get<bg::max_corner, 1>(geometry) < 9999));
    }
}

template <typename Point>
void test_assign_box_or_segment_2d()
{
    detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
    detail::test_assign_box_or_segment_2d<bg::model::segment<Point> >();
}

template <typename Point>
void test_assign_box_2d()
{
    detail::test_assign_box_or_segment_2d<bg::model::box<Point> >();
}

template <typename Point>
void test_assign_point_3d()
{
    Point p;
    bg::assign_values(p, 1, 2, 3);
    check_geometry(p, "POINT(1 2 3)");

    bg::assign_value(p, 123);
    check_geometry(p, "POINT(123 123 123)");

    bg::assign_zero(p);
    check_geometry(p, "POINT(0 0 0)");
}

template <typename P>
void test_assign_conversion()
{
    typedef bg::model::box<P> box_type;
    typedef bg::model::ring<P> ring_type;
    typedef bg::model::polygon<P> polygon_type;

    P p;
    bg::assign_values(p, 1, 2);

    box_type b;
    bg::assign(b, p);

    BOOST_CHECK_CLOSE((bg::get<0, 0>(b)), 1.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<0, 1>(b)), 2.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<1, 0>(b)), 1.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<1, 1>(b)), 2.0, 0.001);


    bg::set<bg::min_corner, 0>(b, 1);
    bg::set<bg::min_corner, 1>(b, 2);
    bg::set<bg::max_corner, 0>(b, 3);
    bg::set<bg::max_corner, 1>(b, 4);

    ring_type ring;
    bg::assign(ring, b);

    {
        typedef bg::model::ring<P, false, false> ring_type_ccw;
        ring_type_ccw ring_ccw;
        // Should NOT compile (currently): bg::assign(ring_ccw, ring);

    }

    typename boost::range_const_iterator<ring_type>::type it = ring.begin();
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);

    BOOST_CHECK_EQUAL(ring.size(), 5u);


    polygon_type polygon;

    bg::assign(polygon, ring);
    BOOST_CHECK_EQUAL(bg::num_points(polygon), 5u);

    ring_type ring2;
    bg::assign(ring2, polygon);
    BOOST_CHECK_EQUAL(bg::num_points(ring2), 5u);
}


template <typename P>
void test_assign_conversion_variant()
{
    typedef bg::model::box<P> box_type;
    typedef bg::model::ring<P> ring_type;
    typedef bg::model::polygon<P> polygon_type;

    P p;
    bg::assign_values(p, 1, 2);

    box_type b;
    boost::variant<box_type&> variant_b(b);
    bg::assign(variant_b, p);

    BOOST_CHECK_CLOSE((bg::get<0, 0>(b)), 1.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<0, 1>(b)), 2.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<1, 0>(b)), 1.0, 0.001);
    BOOST_CHECK_CLOSE((bg::get<1, 1>(b)), 2.0, 0.001);


    bg::set<bg::min_corner, 0>(b, 1);
    bg::set<bg::min_corner, 1>(b, 2);
    bg::set<bg::max_corner, 0>(b, 3);
    bg::set<bg::max_corner, 1>(b, 4);

    ring_type ring;
    boost::variant<ring_type&> variant_ring(ring);
    bg::assign(variant_ring, boost::variant<box_type>(b));

    {
        typedef bg::model::ring<P, false, false> ring_type_ccw;
        ring_type_ccw ring_ccw;
        // Should NOT compile (currently): bg::assign(ring_ccw, ring);

    }

    typename boost::range_const_iterator<ring_type>::type it = ring.begin();
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 4.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 3.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);
    it++;
    BOOST_CHECK_CLOSE(bg::get<0>(*it), 1.0, 0.001);
    BOOST_CHECK_CLOSE(bg::get<1>(*it), 2.0, 0.001);

    BOOST_CHECK_EQUAL(ring.size(), 5u);


    polygon_type polygon;
    boost::variant<polygon_type&> variant_polygon(polygon);

    bg::assign(variant_polygon, boost::variant<ring_type>(ring));
    BOOST_CHECK_EQUAL(bg::num_points(polygon), 5u);

    ring_type ring2;
    boost::variant<ring_type&> variant_ring2(ring2);
    bg::assign(variant_ring2, boost::variant<polygon_type>(polygon));
    BOOST_CHECK_EQUAL(bg::num_points(ring2), 5u);
}


template <typename Point>
void test_assign_point_2d()
{
    Point p;
    bg::assign_values(p, 1, 2);
    check_geometry(p, "POINT(1 2)");

    bg::assign_value(p, 123);
    check_geometry(p, "POINT(123 123)");

    bg::assign_zero(p);
    check_geometry(p, "POINT(0 0)");
}





int test_main(int, char* [])
{
    test_assign_point_3d<int[3]>();
    test_assign_point_3d<float[3]>();
    test_assign_point_3d<double[3]>();
    test_assign_point_3d<test::test_point>();
    test_assign_point_3d<bg::model::point<int, 3, bg::cs::cartesian> >();
    test_assign_point_3d<bg::model::point<float, 3, bg::cs::cartesian> >();
    test_assign_point_3d<bg::model::point<double, 3, bg::cs::cartesian> >();

    test_assign_point_2d<int[2]>();
    test_assign_point_2d<float[2]>();
    test_assign_point_2d<double[2]>();
    test_assign_point_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
    test_assign_point_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
    test_assign_point_2d<bg::model::point<double, 2, bg::cs::cartesian> >();

    test_assign_conversion<bg::model::point<double, 2, bg::cs::cartesian> >();
    test_assign_conversion_variant<bg::model::point<double, 2, bg::cs::cartesian> >();


    // Segment (currently) cannot handle array's because derived from std::pair
    test_assign_box_2d<int[2]>();
    test_assign_box_2d<float[2]>();
    test_assign_box_2d<double[2]>();

    test_assign_box_or_segment_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
    test_assign_box_or_segment_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
    test_assign_box_or_segment_2d<bg::model::point<double, 2, bg::cs::cartesian> >();

    test_assign_linestring_2d<bg::model::point<int, 2, bg::cs::cartesian> >();
    test_assign_linestring_2d<bg::model::point<float, 2, bg::cs::cartesian> >();
    test_assign_linestring_2d<bg::model::point<double, 2, bg::cs::cartesian> >();

#ifdef HAVE_TTMATH
    // Next 3 need extra traits for c-array with custom type:
    // test_assign_point_2d<ttmath_big[2]>();
    // test_assign_point_3d<ttmath_big[3]>();
    // test_assign_box_2d<ttmath_big[2]>();

    test_assign_point_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
    test_assign_point_3d<bg::model::point<ttmath_big, 3, bg::cs::cartesian> >();
    test_assign_box_or_segment_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
    test_assign_linestring_2d<bg::model::point<ttmath_big, 2, bg::cs::cartesian> >();
#endif

    return 0;
}