File: test_difference.hpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (516 lines) | stat: -rw-r--r-- 17,368 bytes parent folder | download | duplicates (5)
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
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test

// Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
// Copyright (c) 2017-2023 Adam Wulkiewicz, Lodz, Poland.

// This file was modified by Oracle on 2016-2020.
// Modifications copyright (c) 2016-2020, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// 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)

#ifndef BOOST_GEOMETRY_TEST_DIFFERENCE_HPP
#define BOOST_GEOMETRY_TEST_DIFFERENCE_HPP

#include <fstream>
#include <iomanip>

#include <geometry_test_common.hpp>
#include <count_set.hpp>
#include <expectation_limits.hpp>
#include <algorithms/check_validity.hpp>
#include "../setop_output_type.hpp"

#include <boost/core/ignore_unused.hpp>

#include <boost/range/algorithm/copy.hpp>
#include <boost/range/size.hpp>

#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/algorithms/difference.hpp>
#include <boost/geometry/algorithms/sym_difference.hpp>

#include <boost/geometry/algorithms/area.hpp>
#include <boost/geometry/algorithms/is_valid.hpp>
#include <boost/geometry/algorithms/length.hpp>
#include <boost/geometry/algorithms/num_points.hpp>
#include <boost/geometry/algorithms/num_interior_rings.hpp>
#include <boost/geometry/algorithms/remove_spikes.hpp>

#include <boost/geometry/geometries/geometries.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/strategies/strategies.hpp>

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


#if defined(TEST_WITH_SVG)
#  define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER
#  define BOOST_GEOMETRY_DEBUG_IDENTIFIER
#  include <boost/geometry/io/svg/svg_mapper.hpp>
#  include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
#endif

enum difference_type
{
    difference_a, difference_b, difference_sym
};

struct ut_settings : ut_base_settings
{
    double percentage;
    bool sym_difference;
    bool validity_of_sym = true;
    bool remove_spikes = false;

    // The validity check gives sometimes false negatives.
    // boost::geometry::is_valid can return FALSE while it is valid.
    // (especially at touch in combination with a u/u turn)
    // For now, the cases where this is the case are skipped for validity check.
    bool validity_false_negative_a = false;
    bool validity_false_negative_b = false;
    bool validity_false_negative_sym = false;

    static constexpr double default_tolerance = 0.0001;

    explicit ut_settings(double p = default_tolerance, bool validity = true, bool sd = true)
        : ut_base_settings(validity)
        , percentage(p)
        , sym_difference(sd)
    {}

    bool test_validity_of_diff(difference_type dtype) const
    {
        bool const sym = dtype == difference_sym;
        bool const a = dtype == difference_a;
        bool const b = dtype == difference_b;

        return sym && validity_false_negative_sym ? false
            : a && validity_false_negative_a ? false
            : b && validity_false_negative_b ? false
            : sym ? (validity_of_sym || BG_IF_TEST_FAILURES)
            : test_validity();
    }

};

inline ut_settings tolerance(double percentage)
{
    ut_settings result;
    result.percentage = percentage;
    return result;
}


template <typename Output, typename G1, typename G2>
void difference_output(std::string const& caseid, G1 const& g1, G2 const& g2, Output const& output)
{
    boost::ignore_unused(caseid, g1, g2, output);

#if defined(TEST_WITH_SVG)
    {
        typedef typename bg::coordinate_type<G1>::type coordinate_type;
        typedef typename bg::point_type<G1>::type point_type;

        bool const ccw =
            bg::point_order<G1>::value == bg::counterclockwise
            || bg::point_order<G2>::value == bg::counterclockwise;
        bool const open =
            bg::closure<G1>::value == bg::open
            || bg::closure<G2>::value == bg::open;

        std::ostringstream filename;
        filename << "difference_"
            << caseid << "_"
            << string_from_type<coordinate_type>::name()
            << (ccw ? "_ccw" : "")
            << (open ? "_open" : "")
            << ".svg";

        std::ofstream svg(filename.str().c_str());

        bg::svg_mapper<point_type> mapper(svg, 500, 500);

        mapper.add(g1);
        mapper.add(g2);

        mapper.map(g1, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3");
        mapper.map(g2, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");


        for (auto const& item : output)
        {
            mapper.map(item,
                //sym ? "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,255,0);stroke:rgb(255,0,255);stroke-width:8" :
                "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);stroke:rgb(255,0,255);stroke-width:8");
        }
    }
#endif
}

template <typename OutputType, typename G1, typename G2>
std::string test_difference(std::string const& caseid, G1 const& g1, G2 const& g2,
        count_set const& expected_count,
        int expected_rings_count, int expected_point_count,
        expectation_limits const& expected_area,
        difference_type dtype,
        ut_settings const& settings)
{
    typedef typename bg::coordinate_type<G1>::type coordinate_type;
    boost::ignore_unused<coordinate_type>();
    boost::ignore_unused(expected_point_count);

    bg::model::multi_polygon<OutputType> result;

    if (dtype == difference_sym)
    {
        bg::sym_difference(g1, g2, result);
    }
    else
    {
        bg::difference(g1, g2, result);
    }

    if (settings.remove_spikes)
    {
        bg::remove_spikes(result);
    }

#if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
    {
        bg::model::multi_polygon<OutputType> result_s;
        typedef typename bg::strategy::relate::services::default_strategy
            <
                G1, G2
            >::type strategy_type;
        if (dtype == difference_sym)
        {
            bg::sym_difference(g1, g2, result_s, strategy_type());
        }
        else
        {
            bg::difference(g1, g2, result_s, strategy_type());
        }

        if (settings.remove_spikes)
        {
            bg::remove_spikes(result_s);
        }
        BOOST_CHECK_EQUAL(bg::num_points(result), bg::num_points(result_s));
    }
#endif


    std::ostringstream return_string;
    return_string << bg::wkt(result);

    typename bg::default_area_result<G1>::type const area = bg::area(result);

#if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)
    if (settings.test_validity_of_diff(dtype))
    {
        typedef bg::model::multi_polygon<OutputType> result_type;
        std::string message;
        bool const valid = check_validity<result_type>::apply(result, caseid, g1, g2, message);
        BOOST_CHECK_MESSAGE(valid,
            "difference: " << caseid << " not valid " << message
            << " type: " << (type_for_assert_message<G1, G2>()));
    }
#endif

    difference_output(caseid, g1, g2, result);

#if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
    {
        // Test inserter functionality
        // Test if inserter returns output-iterator (using Boost.Range copy)
        typename setop_output_type<OutputType>::type
            inserted, array_with_one_empty_geometry;
        array_with_one_empty_geometry.push_back(OutputType());
        if (dtype == difference_sym)
        {
            boost::copy(array_with_one_empty_geometry,
                bg::detail::sym_difference::sym_difference_insert<OutputType>
                    (g1, g2, std::back_inserter(inserted)));
        }
        else
        {
            boost::copy(array_with_one_empty_geometry,
                bg::detail::difference::difference_insert<OutputType>(
                    g1, g2, std::back_inserter(inserted)));
        }

        BOOST_CHECK_EQUAL(boost::size(result), boost::size(inserted) - 1);
    }
#endif



#if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)

    if (! expected_count.empty())
    {
        BOOST_CHECK_MESSAGE(expected_count.has(boost::size(result)),
                            "difference: " << caseid
                            << " #outputs expected: " << expected_count
                            << " detected: " << result.size()
                            << " type: " << (type_for_assert_message<G1, G2>())
                            );
    }

    if (expected_rings_count >= 0)
    {
        int nrings = int(boost::size(result) + bg::num_interior_rings(result));
        BOOST_CHECK_MESSAGE(nrings == expected_rings_count,
                "difference: " << caseid
                << " #rings expected: " << expected_rings_count
                << " detected: " << nrings
                << " type: " << (type_for_assert_message<G1, G2>())
                );
    }

    BOOST_CHECK_MESSAGE(expected_area.contains(area, settings.percentage),
            "difference: " << caseid << std::setprecision(20)
            << " #area expected: " << expected_area
            << " detected: " << area
            << " type: " << (type_for_assert_message<G1, G2>())
            );
#endif

    return return_string.str();
}

template <typename OutputType, typename G1, typename G2>
std::string test_difference(std::string const& caseid, G1 const& g1, G2 const& g2,
        count_set const&  expected_count, int expected_point_count,
        expectation_limits const& expected_area,
        difference_type dtype,
        ut_settings const& settings)
{
    return test_difference<OutputType>(caseid, g1, g2,
        expected_count, -1, expected_point_count, expected_area,
        dtype, settings);
}

#ifdef BOOST_GEOMETRY_CHECK_WITH_POSTGIS
static int counter = 0;
#endif


template <typename OutputType, typename G1, typename G2>
std::string test_one(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        count_set const& expected_count1,
        int expected_rings_count1,
        int expected_point_count1,
        expectation_limits const& expected_area1,
        count_set const& expected_count2,
        int expected_rings_count2,
        int expected_point_count2,
        expectation_limits const& expected_area2,
        count_set const&  expected_count_s,
        int expected_rings_count_s,
        int expected_point_count_s,
        expectation_limits const& expected_area_s,
        ut_settings const& settings = ut_settings())
{
    G1 g1;
    bg::read_wkt(wkt1, g1);

    G2 g2;
    bg::read_wkt(wkt2, g2);

    bg::correct(g1);
    bg::correct(g2);

    std::string result;

#if ! defined(BOOST_GEOMETRY_TEST_DIFFERENCE_ONLY_B)
    result = test_difference<OutputType>(caseid + "_a", g1, g2,
        expected_count1, expected_rings_count1, expected_point_count1,
        expected_area1, difference_a, settings);
#endif
#if defined(BOOST_GEOMETRY_TEST_DIFFERENCE_ONLY_A)
    return result;
#endif

    test_difference<OutputType>(caseid + "_b", g2, g1,
        expected_count2, expected_rings_count2, expected_point_count2,
        expected_area2, difference_b, settings);

#if defined(BOOST_GEOMETRY_TEST_DIFFERENCE_ONLY_B)
    return result;
#endif

#if ! defined(BOOST_GEOMETRY_TEST_ALWAYS_CHECK_SYMDIFFERENCE)
    if (settings.sym_difference)
#endif
    {
        test_difference<OutputType>(caseid + "_s", g1, g2,
            expected_count_s,
            expected_rings_count_s,
            expected_point_count_s,
            expected_area_s,
            difference_sym, settings);
    }
    return result;
}

template <typename OutputType, typename G1, typename G2>
std::string test_one(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        count_set const&  expected_count1,
        int expected_rings_count1,
        int expected_point_count1,
        expectation_limits const& expected_area1,
        count_set const&  expected_count2,
        int expected_rings_count2,
        int expected_point_count2,
        expectation_limits const& expected_area2,
        ut_settings const& settings = ut_settings())
{
    return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
        expected_count1, expected_rings_count1, expected_point_count1, expected_area1,
        expected_count2, expected_rings_count2, expected_point_count2, expected_area2,
        expected_count1 + expected_count2,
        expected_rings_count1 + expected_rings_count2,
        expected_point_count1 >= 0 && expected_point_count2 >= 0
            ? (expected_point_count1 + expected_point_count2) : -1,
        expected_area1 + expected_area2,
        settings);
}

// Version with expectations of symmetric: all specified
template <typename OutputType, typename G1, typename G2>
std::string test_one(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        count_set const&  expected_count1,
        int expected_point_count1,
        expectation_limits const& expected_area1,
        count_set const&  expected_count2,
        int expected_point_count2,
        expectation_limits const& expected_area2,
        count_set const&  expected_count_s,
        int expected_point_count_s,
        expectation_limits const& expected_area_s,
        ut_settings const& settings = ut_settings())
{
    return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
        expected_count1, -1, expected_point_count1, expected_area1,
        expected_count2, -1, expected_point_count2, expected_area2,
        expected_count_s, -1, expected_point_count_s, expected_area_s,
        settings);
}

// Version with expectations of symmetric: specify only count
template <typename OutputType, typename G1, typename G2>
std::string test_one(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        count_set const&  expected_count1,
        int expected_point_count1,
        expectation_limits const& expected_area1,
        count_set const&  expected_count2,
        int expected_point_count2,
        expectation_limits const& expected_area2,
        count_set const&  expected_count_s,
        ut_settings const& settings = ut_settings())
{
    return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
        expected_count1, -1, expected_point_count1, expected_area1,
        expected_count2, -1, expected_point_count2, expected_area2,
        expected_count_s, -1,
        expected_point_count1 >= 0 && expected_point_count2 >= 0
            ? (expected_point_count1 + expected_point_count2) : -1,
        expected_area1 + expected_area2,
        settings);
}

// Version with expectations of symmetric: all automatically
template <typename OutputType, typename G1, typename G2>
std::string test_one(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        count_set const&  expected_count1,
        int expected_point_count1,
        expectation_limits const& expected_area1,
        count_set const&  expected_count2,
        int expected_point_count2,
        expectation_limits const& expected_area2,
        ut_settings const& settings = ut_settings())
{
    return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
        expected_count1, expected_point_count1, expected_area1,
        expected_count2, expected_point_count2, expected_area2,
        expected_count1 + expected_count2,
        expected_point_count1 >= 0 && expected_point_count2 >= 0
            ? (expected_point_count1 + expected_point_count2) : -1,
        expected_area1 + expected_area2,
        settings);
}

template <typename OutputType, typename G1, typename G2>
void test_one_lp(std::string const& caseid,
        std::string const& wkt1, std::string const& wkt2,
        std::size_t expected_count,
        int expected_point_count,
        double expected_length)
{
    G1 g1;
    bg::read_wkt(wkt1, g1);

    G2 g2;
    bg::read_wkt(wkt2, g2);

    bg::correct(g1);
    bg::correct(g2);

    typedef typename setop_output_type<OutputType>::type result_type;
    result_type pieces;
    bg::difference(g1, g2, pieces);

    typename bg::default_length_result<G1>::type length = 0;
    std::size_t n = 0;
    std::size_t piece_count = 0;
    for (typename result_type::iterator it = pieces.begin();
            it != pieces.end();
            ++it)
    {
        if (expected_point_count >= 0)
        {
            n += bg::num_points(*it);
        }
        piece_count++;
        length += bg::length(*it);
    }

    BOOST_CHECK_MESSAGE(piece_count == expected_count,
            "difference: " << caseid
            << " #outputs expected: " << expected_count
            << " detected: " << pieces.size()
            );

    if (expected_point_count >= 0)
    {
        BOOST_CHECK_MESSAGE(n == std::size_t(expected_point_count),
                "difference: " << caseid
                << " #points expected: " << std::size_t(expected_point_count)
                << " detected: " << n
                << " type: " << (type_for_assert_message<G1, G2>())
                );
    }

    BOOST_CHECK_CLOSE(length, expected_length, 0.001);

    std::string lp = "lp_";
    difference_output(lp + caseid, g1, g2, pieces);
}


#endif