summaryrefslogtreecommitdiff
path: root/boost/geometry/algorithms/detail/overlay/traversal_intersection_patterns.hpp
blob: a8abea230efc68266989cc095e3a2307775f774f (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
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2007-2017 Barend Gehrels, 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)

#ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_INTERSECTION_PATTERNS_HPP
#define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_INTERSECTION_PATTERNS_HPP

#include <cstddef>
#include <vector>

#include <boost/geometry/algorithms/detail/overlay/aggregate_operations.hpp>
#include <boost/geometry/algorithms/detail/overlay/sort_by_side.hpp>

namespace boost { namespace geometry
{

#ifndef DOXYGEN_NO_DETAIL
namespace detail { namespace overlay
{

inline bool check_pairs(std::vector<sort_by_side::rank_with_rings> const& aggregation,
                        signed_size_type incoming_region_id,
                        std::size_t first, std::size_t last)
{
    // Check if pairs 1,2 (and possibly 3,4 and 5,6 etc) satisfy

    for (std::size_t i = first; i <= last; i += 2)
    {
        sort_by_side::rank_with_rings const& curr = aggregation[i];
        sort_by_side::rank_with_rings const& next = aggregation[i + 1];
        signed_size_type const curr_id = curr.region_id();
        signed_size_type const next_id = next.region_id();

        bool const possible =
                curr.rings.size() == 2
                && curr.is_isolated()
                && curr.has_unique_region_id()
                && next.rings.size() == 2
                && next.is_isolated()
                && next.has_unique_region_id()
                && curr_id == next_id
                && curr_id != incoming_region_id;

        if (! possible)
        {
            return false;
        }
    }

    return true;
}

inline bool intersection_pattern_common_interior1(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: coming from exterior ring, encountering an isolated
    // parallel interior ring, which should be skipped, and the first
    // left (normally intersection takes first right) should be taken.
    // Solves cases #case_133_multi
    // and #case_recursive_boxes_49

    std::size_t const n = aggregation.size();
    if (n < 4)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& outgoing = aggregation.back();

    bool const incoming_ok =
        incoming.all_from()
        && incoming.rings.size() == 1
        && incoming.has_only(operation_intersection);

    if (! incoming_ok)
    {
        return false;
    }

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.rings.size() == 1
        && outgoing.has_only(operation_intersection)
        && outgoing.region_id() == incoming.region_id();

    if (! outgoing_ok)
    {
        return false;
    }

    if (check_pairs(aggregation, incoming.region_id(), 1, n - 2))
    {
        selected_rank = n - 1;
        return true;
    }
    return false;
}

inline bool intersection_pattern_common_interior2(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: coming from two exterior rings, encountering two isolated
    // equal interior rings

    // See (for example, for ii) #case_recursive_boxes_53:

    // INCOMING:
    // Rank 0  {11[0] (s:0, m:0) i F rgn: 1 ISO}             {13[1] (s:1, m:0) i F rgn: 1 ISO}

    // PAIR:
    // Rank 1  {13[0] (s:0, r:1, m:0) i T rgn: 3 ISO ->16}   {11[1] (s:1, r:5, m:0) i T rgn: 3 ISO ->16}
    // Rank 2  {13[0] (s:0, r:1, m:0) i F rgn: 3 ISO}        {11[1] (s:1, r:5, m:0) i F rgn: 3 ISO}

    // LEAVING (in the same direction, take last one)
    // Rank 3  {11[0] (s:0, m:0) i T rgn: 1 ISO ->10}        {13[1] (s:1, m:0) i T rgn: 1 ISO ->10}


    std::size_t const n = aggregation.size();
    if (n < 4)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& outgoing = aggregation.back();

    bool const incoming_ok =
        incoming.all_from()
        && incoming.rings.size() == 2
        && incoming.has_unique_region_id();

    if (! incoming_ok)
    {
        return false;
    }

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.rings.size() == 2
        && outgoing.has_unique_region_id()
        && outgoing.region_id() == incoming.region_id();

    if (! outgoing_ok)
    {
        return false;
    }

    bool const operation_ok =
            (incoming.has_only(operation_continue) && outgoing.has_only(operation_continue))
             || (incoming.has_only(operation_intersection) && outgoing.has_only(operation_intersection));

    if (! operation_ok)
    {
        return false;
    }

    // Check if pairs 1,2 (and possibly 3,4 and 5,6 etc) satisfy
    if (check_pairs(aggregation, incoming.region_id(), 1, n - 2))
    {
        selected_rank = n - 1;
        return true;
    }
    return false;
}

inline bool intersection_pattern_common_interior3(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: approaches colocated turn (exterior+interior) from two
    // different directions, and both leaves in the same direction

    // See #case_136_multi:
    // INCOMING:
    //Rank 0  {10[0] (s:0, m:0) c F rgn: 1 ISO}

    // PAIR:
    //Rank 1  {14[0] (s:0, r:0, m:0) i T rgn: 2 ISO ->16} {11[1] (s:1, r:1, m:0) i T rgn: 2 ISO ->16}
    //Rank 2  {14[0] (s:0, r:0, m:0) i F rgn: 2 ISO}      {11[1] (s:1, r:1, m:0) i F rgn: 2 ISO}

    // LEAVING (select this one):
    //Rank 3  {10[0] (s:0, m:0) c T rgn: 1 ISO ->12}      {10[1] (s:1, m:0) c T rgn: 1 ISO ->12}

    // ADDITIONALLY: (other polygon coming in)
    //Rank 4  {10[1] (s:1, m:0) c F rgn: 1 ISO}

    std::size_t const n = aggregation.size();
    if (n < 4)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& outgoing = aggregation[n - 2];
    sort_by_side::rank_with_rings const& last = aggregation.back();

    bool const incoming_ok =
        incoming.all_from()
        && incoming.rings.size() == 1
        && incoming.has_only(operation_continue);

    if (! incoming_ok)
    {
        return false;
    }

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.rings.size() == 2
        && outgoing.has_only(operation_continue)
        && outgoing.has_unique_region_id()
        && outgoing.region_id() == incoming.region_id()
        && last.all_from()
        && last.rings.size() == 1
        && last.region_id() == incoming.region_id()
        && last.all_from();

    if (! outgoing_ok)
    {
        return false;
    }

    // Check if pairs 1,2 (and possibly 3,4 and 5,6 etc) satisfy
    if (check_pairs(aggregation, incoming.region_id(), 1, n - 3))
    {
        selected_rank = n - 2;
        return true;
    }
    return false;
}


inline bool intersection_pattern_common_interior4(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: approaches colocated turn (exterior+interior) from same
    // direction, but leaves in two different directions

    // See #case_137_multi:

    // INCOMING:
    //Rank 0  {11[0] (s:0, m:0) i F rgn: 1 ISO}            {10[1] (s:1, m:0) i F rgn: 1 ISO}

    // PAIR:
    //Rank 1  {13[0] (s:0, r:0, m:0) i T rgn: 2 ISO ->15}  {11[1] (s:1, r:1, m:0) i T rgn: 2 ISO ->15}
    //Rank 2  {13[0] (s:0, r:0, m:0) i F rgn: 2 ISO}       {11[1] (s:1, r:1, m:0) i F rgn: 2 ISO}

    // LEAVING (in two different directions, take penultimate one)
    //Rank 3  {10[1] (s:1, m:0) i T rgn: 1 ISO ->0}
    //Rank 4  {11[0] (s:0, m:0) i T rgn: 1 ISO ->12}

    std::size_t const n = aggregation.size();
    if (n < 4)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& extra = aggregation[n - 2];
    sort_by_side::rank_with_rings const& outgoing = aggregation.back();

    bool const incoming_ok =
        incoming.all_from()
        && incoming.rings.size() == 2
        && incoming.has_unique_region_id()
        && incoming.has_only(operation_intersection);

    if (! incoming_ok)
    {
        return false;
    }

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.rings.size() == 1
        && outgoing.has_only(operation_intersection)
        && outgoing.region_id() == incoming.region_id()
        && extra.all_to()
        && extra.rings.size() == 1
        && extra.has_only(operation_intersection)
        && extra.region_id() == incoming.region_id();

    if (! outgoing_ok)
    {
        return false;
    }

    // Check if pairs 1,2 (and possibly 3,4 and 5,6 etc) satisfy
    if (check_pairs(aggregation, incoming.region_id(), 1, n - 3))
    {
        selected_rank = n - 2;
        return true;
    }
    return false;
}

inline bool intersection_pattern_common_interior5(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: isolated regions

    // See #case_recursive_boxes_65

    // INCOMING:
    //    Rank 0  {19[0] (s:0, r:2, m:0) i F rgn: 4 ISO}

    //    Rank 1  {19[1] (s:1, m:0) i T rgn: 1 ISO FIN ->18 (2.5)}
    //    Rank 2  {21[1] (s:1, m:2) i F rgn: 1 ISO}
    //    Rank 3  {21[1] (s:1, m:2) i T rgn: 1 ISO ->17 (2)}
    //    Rank 4  {19[1] (s:1, m:0) i F rgn: 1 ISO FIN}

    // LEAVING (take this one):
    //    Rank 5  {19[0] (s:0, r:2, m:0) i T rgn: 4 ISO ->22 (1)}

    std::size_t const n = aggregation.size();
    if (n < 3)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& outgoing = aggregation.back();

    bool const incoming_ok =
        incoming.all_from()
        && incoming.has_unique_region_id()
        && incoming.is_isolated();

    if (! incoming_ok)
    {
        return false;
    }

    signed_size_type const incoming_region_id = incoming.region_id();

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.has_unique_region_id()
        && outgoing.is_isolated()
        && outgoing.region_id() == incoming_region_id;

    if (! outgoing_ok)
    {
        return false;
    }

    selected_rank = n - 1;
    bool other_region = true;

    // Assumed is that other regions go (T) and come back (F)
    for (std::size_t i = 1; i < n - 1; i++)
    {
        sort_by_side::rank_with_rings const& rwr = aggregation[i];
        if (! rwr.has_unique_region_id() || ! rwr.is_isolated())
        {
            return false;
        }
        signed_size_type const region_id = rwr.region_id();
        if (other_region && region_id != incoming_region_id)
        {
            // OK
        }
        else if (other_region && region_id == incoming_region_id)
        {
            // OK, next phase (same region as incoming region)
            selected_rank = i;
            other_region = false;
        }
        else if (! other_region && region_id != incoming_region_id)
        {
            // After that the region is the same is incoming, it should
            // stay like that
            return false;
        }
    }

    return true;
}

inline bool intersection_pattern_common_interior6(std::size_t& selected_rank,
           std::vector<sort_by_side::rank_with_rings> const& aggregation)
{
    // Pattern: isolated regions in between

    // See #case_recursive_boxes_75

    // Incoming: one region
    // In between: several rings having isolated region, all the same
    // Outging == incoming

    std::size_t const n = aggregation.size();
    if (n < 3)
    {
        return false;
    }

    sort_by_side::rank_with_rings const& incoming = aggregation.front();
    sort_by_side::rank_with_rings const& outgoing = aggregation.back();
    sort_by_side::rank_with_rings const& first_isolated = aggregation[2];

    bool const incoming_ok =
        incoming.all_from()
        && incoming.has_unique_region_id()
        && ! incoming.is_isolated();

    if (! incoming_ok)
    {
        return false;
    }

    signed_size_type const incoming_region_id = incoming.region_id();

    bool const outgoing_ok =
        outgoing.all_to()
        && outgoing.has_unique_region_id()
        && ! outgoing.is_isolated()
        && outgoing.region_id() == incoming_region_id;

    if (! outgoing_ok)
    {
        return false;
    }

    const signed_size_type isolated_region_id = first_isolated.region_id();

    for (std::size_t i = 1; i < n - 1; i++)
    {
        sort_by_side::rank_with_rings const& rwr = aggregation[i];
        if (! rwr.has_unique_region_id()
                || ! rwr.is_isolated()
                || rwr.region_id() != isolated_region_id)
        {
            return false;
        }
    }

    selected_rank = n - 1;

    return true;
}

}} // namespace detail::overlay
#endif // DOXYGEN_NO_DETAIL

}} // namespace boost::geometry

#endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_INTERSECTION_PATTERNS_HPP