summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/2geom/intersection-graph.cpp
blob: a294c72387c456aae2dd954cf40c322e9557e75c (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
/**
 * \file
 * \brief Intersection graph for Boolean operations
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 * 
 * Copyright 2015 Authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include <2geom/intersection-graph.h>
#include <2geom/path.h>
#include <2geom/pathvector.h>
#include <2geom/utils.h>
#include <iostream>
#include <iterator>

namespace Geom {

struct PathIntersectionGraph::IntersectionVertexLess {
    bool operator()(IntersectionVertex const &a, IntersectionVertex const &b) const {
        return a.pos < b.pos;
    }
};

/** @class PathIntersectionGraph
 * @brief Intermediate data for computing Boolean operations on paths.
 *
 * This class implements the Greiner-Hormann clipping algorithm,
 * with improvements inspired by Foster and Overfelt as well as some
 * original contributions.
 *
 * @ingroup Paths
 */

PathIntersectionGraph::PathIntersectionGraph(PathVector const &a, PathVector const &b, Coord precision)
    : _graph_valid(true)
{
    if (a.empty() || b.empty()) return;

    _pv[0] = a;
    _pv[1] = b;

    _prepareArguments();
    bool has_intersections = _prepareIntersectionLists(precision);
    if (!has_intersections) return;

    _assignEdgeWindingParities(precision);
    _assignComponentStatusFromDegenerateIntersections();
    _removeDegenerateIntersections();
    if (_graph_valid) {
        _verify();
    }
}

void PathIntersectionGraph::_prepareArguments()
{
    // all paths must be closed, otherwise we will miss some intersections
    for (auto & w : _pv) {
        for (auto & i : w) {
            i.close();
        }
    }
    // remove degenerate segments
    for (auto & w : _pv) {
        for (std::size_t i = w.size(); i > 0; --i) {
            if (w[i-1].empty()) {
                w.erase(w.begin() + (i-1));
                continue;
            }
            for (std::size_t j = w[i-1].size(); j > 0; --j) {
                if (w[i-1][j-1].isDegenerate()) {
                    w[i-1].erase(w[i-1].begin() + (j-1));
                }
            }
        }
    }
}

bool PathIntersectionGraph::_prepareIntersectionLists(Coord precision)
{
    std::vector<PVIntersection> pxs = _pv[0].intersect(_pv[1], precision);
    // NOTE: this early return means that the path data structures will not be created
    // if there are no intersections at all!
    if (pxs.empty()) return false;

    // prepare intersection lists for each path component
    for (unsigned w = 0; w < 2; ++w) {
        for (std::size_t i = 0; i < _pv[w].size(); ++i) {
            _components[w].push_back(new PathData(w, i));
        }
    }

    // create intersection vertices
    for (auto & px : pxs) {
        IntersectionVertex *xa, *xb;
        xa = new IntersectionVertex();
        xb = new IntersectionVertex();
        //xa->processed = xb->processed = false;
        xa->which = 0; xb->which = 1;
        xa->pos = px.first;
        xb->pos = px.second;
        xa->p = xb->p = px.point();
        xa->neighbor = xb;
        xb->neighbor = xa;
        xa->next_edge = xb->next_edge = OUTSIDE;
        xa->defective = xb->defective = false;
        _xs.push_back(xa);
        _xs.push_back(xb);
        _components[0][xa->pos.path_index].xlist.push_back(*xa);
        _components[1][xb->pos.path_index].xlist.push_back(*xb);
    }

    // sort components according to time value of intersections
    for (auto & _component : _components) {
        for (std::size_t i = 0; i < _component.size(); ++i) {
            _component[i].xlist.sort(IntersectionVertexLess());
        }
    }

    return true;
}

void PathIntersectionGraph::_assignEdgeWindingParities(Coord precision)
{
    // determine the winding numbers of path portions between intersections
    for (unsigned w = 0; w < 2; ++w) {
        unsigned ow = (w+1) % 2;

        for (unsigned li = 0; li < _components[w].size(); ++li) {
            IntersectionList &xl = _components[w][li].xlist;
            for (ILIter i = xl.begin(); i != xl.end(); ++i) {
                ILIter n = cyclic_next(i, xl);
                std::size_t pi = i->pos.path_index;

                PathInterval ival = forward_interval(i->pos, n->pos, _pv[w][pi].size());
                PathTime mid = ival.inside(precision);

                Point wpoint = _pv[w][pi].pointAt(mid);
                _winding_points.push_back(wpoint);
                int wdg = _pv[ow].winding(wpoint);
                if (wdg % 2) {
                    i->next_edge = INSIDE;
                } else {
                    i->next_edge = OUTSIDE;
                }
            }
        }
    }
}

void PathIntersectionGraph::_assignComponentStatusFromDegenerateIntersections()
{
    // If a path has only degenerate intersections, assign its status now.
    // This protects against later accidentally picking a point for winding
    // determination that is exactly at a removed intersection.
    for (auto & _component : _components) {
        for (unsigned li = 0; li < _component.size(); ++li) {
            IntersectionList &xl = _component[li].xlist;
            bool has_in = false;
            bool has_out = false;
            for (auto & i : xl) {
                has_in |= (i.next_edge == INSIDE);
                has_out |= (i.next_edge == OUTSIDE);
            }
            if (has_in && !has_out) {
                _component[li].status = INSIDE;
            }
            if (!has_in && has_out) {
                _component[li].status = OUTSIDE;
            }
        }
    }
}

void PathIntersectionGraph::_removeDegenerateIntersections()
{
    // remove intersections that don't change between in/out
    for (auto & _component : _components) {
        for (unsigned li = 0; li < _component.size(); ++li) {
            IntersectionList &xl = _component[li].xlist;
            for (ILIter i = xl.begin(); i != xl.end();) {
                ILIter n = cyclic_next(i, xl);
                if (i->next_edge == n->next_edge) {
                    bool last_node = (i == n);
                    ILIter nn = _getNeighbor(n);
                    IntersectionList &oxl = _getPathData(nn).xlist;

                    // When exactly 3 out of 4 edges adjacent to an intersection
                    // have the same winding, we have a defective intersection,
                    // which is neither degenerate nor normal. Those can occur in paths
                    // that contain overlapping segments. We cannot handle that case
                    // for now, so throw an exception.
                    if (cyclic_prior(nn, oxl)->next_edge != nn->next_edge) {
                        _graph_valid = false;
                        n->defective = true;
                        nn->defective = true;
                        ++i;
                        continue;
                    }

                    oxl.erase(nn);
                    xl.erase(n);
                    if (last_node) break;
                } else {
                    ++i;
                }
            }
        }
    }
}

void PathIntersectionGraph::_verify()
{
#ifndef NDEBUG
    for (auto & _component : _components) {
        for (unsigned li = 0; li < _component.size(); ++li) {
            IntersectionList &xl = _component[li].xlist;
            assert(xl.size() % 2 == 0);
            for (ILIter i = xl.begin(); i != xl.end(); ++i) {
                ILIter j = cyclic_next(i, xl);
                assert(i->next_edge != j->next_edge);
            }
        }
    }
#endif
}

PathVector PathIntersectionGraph::getUnion()
{
    PathVector result = _getResult(false, false);
    _handleNonintersectingPaths(result, 0, false);
    _handleNonintersectingPaths(result, 1, false);
    return result;
}

PathVector PathIntersectionGraph::getIntersection()
{
    PathVector result = _getResult(true, true);
    _handleNonintersectingPaths(result, 0, true);
    _handleNonintersectingPaths(result, 1, true);
    return result;
}

PathVector PathIntersectionGraph::getAminusB()
{
    PathVector result = _getResult(false, true);
    _handleNonintersectingPaths(result, 0, false);
    _handleNonintersectingPaths(result, 1, true);
    return result;
}

PathVector PathIntersectionGraph::getBminusA()
{
    PathVector result = _getResult(true, false);
    _handleNonintersectingPaths(result, 1, false);
    _handleNonintersectingPaths(result, 0, true);
    return result;
}

PathVector PathIntersectionGraph::getXOR()
{
    PathVector r1, r2;
    r1 = getAminusB();
    r2 = getBminusA();
    std::copy(r2.begin(), r2.end(), std::back_inserter(r1));
    return r1;
}

std::size_t PathIntersectionGraph::size() const
{
    std::size_t result = 0;
    for (std::size_t i = 0; i < _components[0].size(); ++i) {
        result += _components[0][i].xlist.size();
    }
    return result;
}

std::vector<Point> PathIntersectionGraph::intersectionPoints(bool defective) const
{
    std::vector<Point> result;

    for (std::size_t i = 0; i < _components[0].size(); ++i) {
        for (const auto & j : _components[0][i].xlist) {
            if (j.defective == defective) {
                result.push_back(j.p);
            }
        }
    }
    return result;
}

void PathIntersectionGraph::fragments(PathVector &in, PathVector &out) const
{
    typedef boost::ptr_vector<PathData>::const_iterator PIter;
    for (unsigned w = 0; w < 2; ++w) {
        for (PIter li = _components[w].begin(); li != _components[w].end(); ++li) {
            for (CILIter k = li->xlist.begin(); k != li->xlist.end(); ++k) {
                CILIter n = cyclic_next(k, li->xlist);
                // TODO: investigate why non-contiguous paths are sometimes generated here
                Path frag(k->p);
                frag.setStitching(true);
                PathInterval ival = forward_interval(k->pos, n->pos, _pv[w][k->pos.path_index].size());
                _pv[w][k->pos.path_index].appendPortionTo(frag, ival, k->p, n->p);
                if (k->next_edge == INSIDE) {
                    in.push_back(frag);
                } else {
                    out.push_back(frag);
                }
            }
        }
    }
}

PathVector PathIntersectionGraph::_getResult(bool enter_a, bool enter_b)
{
    PathVector result;
    if (_xs.empty()) return result;

    // reset processed status
    _ulist.clear();
    for (auto & _component : _components) {
        for (auto & li : _component) {
            for (auto & k : li.xlist) {
                _ulist.push_back(k);
            }
        }
    }

    unsigned n_processed = 0;

    while (true) {
        // get unprocessed intersection
        if (_ulist.empty()) break;
        IntersectionVertex &iv = _ulist.front();
        unsigned w = iv.which;
        ILIter i = _components[w][iv.pos.path_index].xlist.iterator_to(iv);

        result.push_back(Path(i->p));
        result.back().setStitching(true);
        bool reverse = false;
        while (i->_proc_hook.is_linked()) {
            ILIter prev = i;
            std::size_t pi = i->pos.path_index;
            // determine which direction to go
            // union: always go outside
            // intersection: always go inside
            // a minus b: go inside in b, outside in a
            // b minus a: go inside in a, outside in b
            reverse = false;
            if (w == 0) {
                reverse = (i->next_edge == INSIDE) ^ enter_a;
            } else {
                reverse = (i->next_edge == INSIDE) ^ enter_b;
            }

            // get next intersection
            if (reverse) {
                i = cyclic_prior(i, _components[w][pi].xlist);
            } else {
                i = cyclic_next(i, _components[w][pi].xlist);
            }

            // append portion of path
            PathInterval ival = PathInterval::from_direction(
                prev->pos.asPathTime(), i->pos.asPathTime(),
                reverse, _pv[i->which][pi].size());

            _pv[i->which][pi].appendPortionTo(result.back(), ival, prev->p, i->p);

            // mark both vertices as processed
            //prev->processed = true;
            //i->processed = true;
            n_processed += 2;
            if (prev->_proc_hook.is_linked()) {
                _ulist.erase(_ulist.iterator_to(*prev));
            }
            if (i->_proc_hook.is_linked()) {
                _ulist.erase(_ulist.iterator_to(*i));
            }

            // switch to the other path
            i = _getNeighbor(i);
            w = i->which;
        }
        result.back().close(true);
        if (reverse){
            result.back() = result.back().reversed();
        }
        if (result.back().empty()) {
            // std::cerr << "Path is empty" << std::endl;
            throw GEOM_ERR_INTERSECGRAPH;
        }
    }
    
    if (n_processed != size() * 2) {
        // std::cerr << "Processed " << n_processed << " intersections, expected " << (size() * 2) << std::endl;
        throw GEOM_ERR_INTERSECGRAPH;
    }

    return result;
}

void PathIntersectionGraph::_handleNonintersectingPaths(PathVector &result, unsigned which, bool inside)
{
    /* Every component that has any intersections will be processed by _getResult.
     * Here we take care of paths that don't have any intersections. They are either
     * completely inside or completely outside the other pathvector. We test this by
     * evaluating the winding rule at the initial point. If inside is true and
     * the path is inside, we add it to the result.
     */
    unsigned w = which;
    unsigned ow = (w+1) % 2;

    for (std::size_t i = 0; i < _pv[w].size(); ++i) {
        // the path data vector might have been left empty if there were no intersections at all
        bool has_path_data = !_components[w].empty();
        // Skip if the path has intersections
        if (has_path_data && !_components[w][i].xlist.empty()) continue;
        bool path_inside = false;

        // Use the in/out determination from constructor, if available
        if (has_path_data && _components[w][i].status == INSIDE) {
            path_inside = true;
        } else if (has_path_data && _components[w][i].status == OUTSIDE) {
            path_inside = false;
        } else {
            int wdg = _pv[ow].winding(_pv[w][i].initialPoint());
            path_inside = wdg % 2 != 0;
        }

        if (path_inside == inside) {
            result.push_back(_pv[w][i]);
        }
    }
}

PathIntersectionGraph::ILIter PathIntersectionGraph::_getNeighbor(ILIter iter)
{
    unsigned ow = (iter->which + 1) % 2;
    return _components[ow][iter->neighbor->pos.path_index].xlist.iterator_to(*iter->neighbor);
}

PathIntersectionGraph::PathData &
PathIntersectionGraph::_getPathData(ILIter iter)
{
    return _components[iter->which][iter->pos.path_index];
}

std::ostream &operator<<(std::ostream &os, PathIntersectionGraph const &pig)
{
    os << "Intersection graph:\n"
       << pig._xs.size()/2 << " total intersections\n"
       << pig.size() << " considered intersections\n";
    for (std::size_t i = 0; i < pig._components[0].size(); ++i) {
        PathIntersectionGraph::IntersectionList const &xl = pig._components[0][i].xlist;
        for (const auto & j : xl) {
            os << j.pos << " - " << j.neighbor->pos << " @ " << j.p << "\n";
        }
    }
    return os;
}

} // namespace Geom

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :