Coverage Report

Created: 2026-02-14 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/geos/include/geos/operation/overlayng/RingClipper.h
Line
Count
Source
1
/**********************************************************************
2
 *
3
 * GEOS - Geometry Engine Open Source
4
 * http://geos.osgeo.org
5
 *
6
 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7
 *
8
 * This is free software; you can redistribute and/or modify it under
9
 * the terms of the GNU Lesser General Public Licence as published
10
 * by the Free Software Foundation.
11
 * See the COPYING file for more information.
12
 *
13
 **********************************************************************/
14
15
#pragma once
16
17
#include <geos/export.h>
18
19
#include <geos/geom/Envelope.h>
20
#include <geos/geom/CoordinateSequence.h>
21
22
// Forward declarations
23
namespace geos {
24
namespace geom {
25
class Coordinate;
26
class CoordinateSequence;
27
}
28
}
29
30
namespace geos {      // geos.
31
namespace operation { // geos.operation
32
namespace overlayng { // geos.operation.overlayng
33
34
/**
35
 * Clips rings of points to a rectangle.
36
 * Uses a variant of Cohen-Sutherland clipping.
37
 *
38
 * In general the output is not topologically valid.
39
 * In particular, the output may contain coincident non-noded line segments
40
 * along the clip rectangle sides.
41
 * However, the output is sufficiently well-structured
42
 * that it can be used as input to the {@link OverlayNG} algorithm
43
 * (which is able to process coincident linework due
44
 * to the need to handle topology collapse under precision reduction).
45
 *
46
 * Because of the likelihood of creating
47
 * extraneous line segments along the clipping rectangle sides,
48
 * this class is not suitable for clipping linestrings.
49
 *
50
 * The clipping envelope should be generated using {@link RobustClipEnvelopeComputer},
51
 * to ensure that intersecting line segments are not perturbed
52
 * by clipping.
53
 * This is required to ensure that the overlay of the
54
 * clipped geometry is robust and correct (i.e. the same as
55
 * if clipping was not used).
56
 *
57
 * @see LineLimiter
58
 *
59
 * @author Martin Davis
60
 *
61
 */
62
class GEOS_DLL RingClipper {
63
    using Coordinate = geos::geom::Coordinate;
64
    using CoordinateSequence = geos::geom::CoordinateSequence;
65
    using Envelope = geos::geom::Envelope;
66
67
private:
68
69
    // Constants
70
    static constexpr int BOX_LEFT = 3;
71
    static constexpr int BOX_TOP = 2;
72
    static constexpr int BOX_RIGHT = 1;
73
    static constexpr int BOX_BOTTOM = 0;
74
75
    // Members
76
    const Envelope clipEnv;
77
78
    // Methods
79
80
    /**
81
    * Clips line to the axis-parallel line defined by a single box edge.
82
    */
83
    std::unique_ptr<CoordinateSequence> clipToBoxEdge(const CoordinateSequence* pts, int edgeIndex, bool closeRing) const;
84
85
    /**
86
    * Computes the intersection point of a segment
87
    * with an edge of the clip box.
88
    * The segment must be known to intersect the edge.
89
    */
90
    void intersection(const Coordinate& a, const Coordinate& b, int edgeIndex, Coordinate& rsltPt) const;
91
    double intersectionLineY(const Coordinate& a, const Coordinate& b, double y) const;
92
    double intersectionLineX(const Coordinate& a, const Coordinate& b, double x) const;
93
    bool isInsideEdge(const Coordinate& p, int edgeIndex) const;
94
95
96
public:
97
98
    RingClipper(const Envelope* env)
99
23.8k
        : clipEnv(*env)
100
23.8k
        {};
101
102
    /**
103
    * Clips a list of points to the clipping rectangle box.
104
    */
105
    std::unique_ptr<CoordinateSequence> clip(const CoordinateSequence* cs) const;
106
107
};
108
109
110
} // namespace geos.operation.overlayng
111
} // namespace geos.operation
112
} // namespace geos
113