summaryrefslogtreecommitdiff
path: root/libs/geometry/test/algorithms/distance/test_distance_common.hpp
blob: 11ef52c404f6e43818190f2ab50706262dc45599 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2014-2015, Oracle and/or its affiliates.

// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle

// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html

#ifndef BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP
#define BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP

#include <iostream>
#include <string>

#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>

#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/multi_linestring.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>

#include <boost/geometry/io/wkt/write.hpp>
#include <boost/geometry/io/dsv/write.hpp>

#include <boost/geometry/algorithms/num_interior_rings.hpp>
#include <boost/geometry/algorithms/distance.hpp>
#include <boost/geometry/algorithms/comparable_distance.hpp>

#include <from_wkt.hpp>
#include <string_from_type.hpp>


#ifndef BOOST_GEOMETRY_TEST_DISTANCE_HPP

namespace bg = ::boost::geometry;

// function copied from BG's test_distance.hpp

template <typename Geometry1, typename Geometry2>
void test_empty_input(Geometry1 const& geometry1, Geometry2 const& geometry2)
{
    try
    {
        bg::distance(geometry1, geometry2);
    }
    catch(bg::empty_input_exception const& )
    {
        return;
    }
    BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
}
#endif // BOOST_GEOMETRY_TEST_DISTANCE_HPP



//========================================================================



#ifdef BOOST_GEOMETRY_TEST_DEBUG
// pretty print geometry -- START
template <typename Geometry, typename GeometryTag>
struct pretty_print_geometry_dispatch
{
    template <typename Stream>
    static inline Stream& apply(Geometry const& geometry, Stream& os)
    {
        os << bg::wkt(geometry);
        return os;
    }
};

template <typename Geometry>
struct pretty_print_geometry_dispatch<Geometry, bg::segment_tag>
{
    template <typename Stream>
    static inline Stream& apply(Geometry const& geometry, Stream& os)
    {
        os << "SEGMENT" << bg::dsv(geometry);
        return os;
    }
};

template <typename Geometry>
struct pretty_print_geometry_dispatch<Geometry, bg::box_tag>
{
    template <typename Stream>
    static inline Stream& apply(Geometry const& geometry, Stream& os)
    {
        os << "BOX" << bg::dsv(geometry);
        return os;
    }
};


template <typename Geometry>
struct pretty_print_geometry
{
    template <typename Stream>
    static inline Stream& apply(Geometry const& geometry, Stream& os)
    {
        return pretty_print_geometry_dispatch
            <
                Geometry, typename bg::tag<Geometry>::type
            >::apply(geometry, os);
    }
};
// pretty print geometry -- END
#endif // BOOST_GEOMETRY_TEST_DEBUG


//========================================================================


template <typename T>
struct check_equal
{
    static inline void apply(T const& detected, T const& expected,
                             bool is_finite)
    {
        if (is_finite)
        {
            BOOST_CHECK(detected == expected);
        }
        else
        {
            BOOST_CHECK(! boost::math::isfinite(detected));
        }
    }
};

template <>
struct check_equal<double>
{
    static inline void apply(double detected, double expected,
                             bool is_finite)
    {
        if (is_finite)
        {
            BOOST_CHECK_CLOSE(detected, expected, 0.0001);
        }
        else
        {
            BOOST_CHECK(! boost::math::isfinite(detected));
        }
    }
};


//========================================================================

template
<
    typename Geometry1, typename Geometry2,
    int id1 = bg::geometry_id<Geometry1>::value,
    int id2 = bg::geometry_id<Geometry2>::value
>
struct test_distance_of_geometries
    : public test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
{};


template <typename Geometry1, typename Geometry2>
class test_distance_of_geometries<Geometry1, Geometry2, 0, 0>
{
private:
    template
    <
        typename G1,
        typename G2,
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void base_test(std::string const& header,
                   G1 const& g1, G2 const& g2,
                   DistanceType const& expected_distance,
                   ComparableDistanceType const& expected_comparable_distance,
                   Strategy const& strategy,
                   bool is_finite)
    {
        typedef typename bg::default_distance_result
            <
                G1, G2
            >::type default_distance_result;

        typedef typename bg::strategy::distance::services::return_type
            <
                Strategy, G1, G2
            >::type distance_result_from_strategy;

        static const bool same_regular = boost::is_same
            <
                default_distance_result,
                distance_result_from_strategy
            >::type::value;

        BOOST_CHECK( same_regular );
    

        typedef typename bg::default_comparable_distance_result
            <
                G1, G2
            >::type default_comparable_distance_result;

        typedef typename bg::strategy::distance::services::return_type
            <
                typename bg::strategy::distance::services::comparable_type
                    <
                        Strategy
                    >::type,
                G1,
                G2
            >::type comparable_distance_result_from_strategy;

        static const bool same_comparable = boost::is_same
            <
                default_comparable_distance_result,
                comparable_distance_result_from_strategy
            >::type::value;
        
        BOOST_CHECK( same_comparable );


        // check distance with default strategy
        default_distance_result dist_def = bg::distance(g1, g2);

        check_equal
            <
                default_distance_result
            >::apply(dist_def, expected_distance, is_finite);


        // check distance with passed strategy
        distance_result_from_strategy dist = bg::distance(g1, g2, strategy);

        check_equal
            <
                default_distance_result
            >::apply(dist, expected_distance, is_finite);


        // check comparable distance with default strategy
        default_comparable_distance_result cdist_def =
            bg::comparable_distance(g1, g2);

        check_equal
            <
                default_comparable_distance_result
            >::apply(cdist_def, expected_comparable_distance, is_finite);


        // check comparable distance with passed strategy
        comparable_distance_result_from_strategy cdist =
            bg::comparable_distance(g1, g2, strategy);

        check_equal
            <
                default_comparable_distance_result
            >::apply(cdist, expected_comparable_distance, is_finite);

#ifdef BOOST_GEOMETRY_TEST_DEBUG
        std::cout << string_from_type<typename bg::coordinate_type<Geometry1>::type>::name()
                  << string_from_type<typename bg::coordinate_type<Geometry2>::type>::name()
                  << " -> "
                  << string_from_type<default_distance_result>::name()
                  << string_from_type<default_comparable_distance_result>::name()
                  << std::endl;

        std::cout << "distance" << header
                  << " (def. strategy) = " << dist_def << " ; "
                  << "distance" << header
                  <<" (passed strategy) = " << dist << " ; "
                  << "comp. distance" << header <<" (def. strategy) = "
                  << cdist_def << " ; "
                  << "comp. distance" << header <<" (passed strategy) = "
                  << cdist << std::endl;
#endif
    }

public:
    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(std::string const& wkt1,
               std::string const& wkt2,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        Geometry1 geometry1 = from_wkt<Geometry1>(wkt1);
        Geometry2 geometry2 = from_wkt<Geometry2>(wkt2);

        apply(geometry1, geometry2,
              expected_distance, expected_comparable_distance,
              strategy, is_finite);
    }


    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(Geometry1 const& geometry1,
               Geometry2 const& geometry2,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
#ifdef BOOST_GEOMETRY_TEST_DEBUG
        typedef pretty_print_geometry<Geometry1> PPG1;
        typedef pretty_print_geometry<Geometry2> PPG2;
        PPG1::apply(geometry1, std::cout);
        std::cout << " - ";
        PPG2::apply(geometry2, std::cout);
        std::cout << std::endl;
#endif

        base_test("", geometry1, geometry2,
                  expected_distance, expected_comparable_distance,
                  strategy, is_finite);

        base_test("[reversed args]", geometry2, geometry1,
                  expected_distance, expected_comparable_distance,
                  strategy, is_finite);

#ifdef BOOST_GEOMETRY_TEST_DEBUG
        std::cout << std::endl;
#endif
    }
};


//========================================================================

template <typename Segment, typename Polygon>
struct test_distance_of_geometries
<
    Segment, Polygon,
    92 /* segment */, 3 /* polygon */
>
    : public test_distance_of_geometries<Segment, Polygon, 0, 0>
{
    typedef test_distance_of_geometries<Segment, Polygon, 0, 0> base;

    typedef typename bg::ring_type<Polygon>::type ring_type;

    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(std::string const& wkt_segment,
               std::string const& wkt_polygon,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        Segment segment = from_wkt<Segment>(wkt_segment);
        Polygon polygon = from_wkt<Polygon>(wkt_polygon);
        apply(segment,
              polygon,
              expected_distance,
              expected_comparable_distance,
              strategy,
              is_finite);
    }


    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(Segment const& segment,
               Polygon const& polygon,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        base::apply(segment, polygon, expected_distance,
                    expected_comparable_distance, strategy, is_finite);

        if ( bg::num_interior_rings(polygon) == 0 ) {
#ifdef BOOST_GEOMETRY_TEST_DEBUG
            std::cout << "... testing also exterior ring ..." << std::endl;
#endif
            test_distance_of_geometries
                <
                    Segment, ring_type
                >::apply(segment,
                         bg::exterior_ring(polygon),
                         expected_distance,
                         expected_comparable_distance,
                         strategy,
                         is_finite);
        }
    }
};

//========================================================================

template <typename Box, typename Segment>
struct test_distance_of_geometries
<
    Box, Segment,
    94 /* box */, 92 /* segment */
>
{
    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(std::string const& wkt_box,
               std::string const& wkt_segment,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        test_distance_of_geometries
            <
                Segment, Box, 92, 94
            >::apply(wkt_segment,
                     wkt_box,
                     expected_distance,
                     expected_comparable_distance,
                     strategy,
                     is_finite);
    }
};


template <typename Segment, typename Box>
struct test_distance_of_geometries
<
    Segment, Box,
    92 /* segment */, 94 /* box */
>
    : public test_distance_of_geometries<Segment, Box, 0, 0>
{
    typedef test_distance_of_geometries<Segment, Box, 0, 0> base;

    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(std::string const& wkt_segment,
               std::string const& wkt_box,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        Segment segment = from_wkt<Segment>(wkt_segment);
        Box box = from_wkt<Box>(wkt_box);
        apply(segment,
              box,
              expected_distance,
              expected_comparable_distance,
              strategy,
              is_finite);
    }


    template
    <
        typename DistanceType,
        typename ComparableDistanceType,
        typename Strategy
    >
    static inline
    void apply(Segment const& segment,
               Box const& box,
               DistanceType const& expected_distance,
               ComparableDistanceType const& expected_comparable_distance,
               Strategy const& strategy,
               bool is_finite = true)
    {
        typedef typename bg::strategy::distance::services::return_type
            <
                Strategy, Segment, Box
            >::type distance_result_type;

        typedef typename bg::strategy::distance::services::comparable_type
            <
                Strategy
            >::type comparable_strategy;

        typedef typename bg::strategy::distance::services::return_type
            <
                comparable_strategy, Segment, Box
            >::type comparable_distance_result_type;


        base::apply(segment, box, expected_distance,
                    expected_comparable_distance, strategy, is_finite);

        comparable_strategy cstrategy =
            bg::strategy::distance::services::get_comparable
                <
                    Strategy
                >::apply(strategy);

        distance_result_type distance_generic =
            bg::detail::distance::segment_to_box_2D_generic
                <
                    Segment, Box, Strategy
                >::apply(segment, box, strategy);

        comparable_distance_result_type comparable_distance_generic =
            bg::detail::distance::segment_to_box_2D_generic
                <
                    Segment, Box, comparable_strategy
                >::apply(segment, box, cstrategy);


        check_equal
            <
                distance_result_type
            >::apply(distance_generic, expected_distance, is_finite);

        check_equal
            <
                comparable_distance_result_type
            >::apply(comparable_distance_generic,
                     expected_comparable_distance,
                     is_finite);

#ifdef BOOST_GEOMETRY_TEST_DEBUG
        std::cout << "... testing with naive seg-box distance algorithm..."
                  << std::endl;
        std::cout << "distance (generic algorithm) = "
                  << distance_generic << " ; "
                  << "comp. distance (generic algorithm) = "            
                  << comparable_distance_generic
                  << std::endl;
        std::cout << std::endl << std::endl;
#endif
    }
};

//========================================================================


template <typename Geometry1, typename Geometry2, typename Strategy>
void test_empty_input(Geometry1 const& geometry1,
                      Geometry2 const& geometry2,
                      Strategy const& strategy)
{
    try
    {
        bg::distance(geometry1, geometry2, strategy);
    }
    catch(bg::empty_input_exception const& )
    {
        return;
    }
    BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );

    try
    {
        bg::distance(geometry2, geometry1, strategy);
    }
    catch(bg::empty_input_exception const& )
    {
        return;
    }
    BOOST_CHECK_MESSAGE(false, "A empty_input_exception should have been thrown" );
}

#endif // BOOST_GEOMETRY_TEST_DISTANCE_COMMON_HPP