Coverage Report

Created: 2026-06-10 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/geos/src/simplify/LineSegmentIndex.cpp
Line
Count
Source
1
/**********************************************************************
2
 *
3
 * GEOS - Geometry Engine Open Source
4
 * http://geos.osgeo.org
5
 *
6
 * Copyright (C) 2006 Refractions Research Inc.
7
 *
8
 * This is free software; you can redistribute and/or modify it under
9
 * the terms of the GNU Lesser General Licence as published
10
 * by the Free Software Foundation.
11
 * See the COPYING file for more information.
12
 *
13
 **********************************************************************
14
 *
15
 * Last port: simplify/LineSegmentIndex.java rev. 1.1 (JTS-1.7.1)
16
 *
17
 **********************************************************************/
18
19
#include <geos/simplify/LineSegmentIndex.h>
20
#include <geos/simplify/TaggedLineSegment.h>
21
#include <geos/simplify/TaggedLineString.h>
22
#include <geos/index/quadtree/Quadtree.h>
23
#include <geos/index/ItemVisitor.h>
24
#include <geos/geom/LineSegment.h>
25
#include <geos/geom/Envelope.h>
26
27
#include <vector>
28
#include <memory> // for unique_ptr
29
#include <cassert>
30
31
#ifndef GEOS_DEBUG
32
#define GEOS_DEBUG 0
33
#endif
34
35
#if GEOS_DEBUG
36
#include <iostream>
37
#endif
38
39
40
using namespace geos::geom;
41
using namespace geos::index::quadtree;
42
43
namespace geos {
44
namespace simplify { // geos::simplify
45
46
/**
47
 * ItemVisitor subclass to reduce volume of query results.
48
 */
49
class LineSegmentVisitor: public index::ItemVisitor {
50
51
// MD - only seems to make about a 10% difference in overall time.
52
53
private:
54
55
    const LineSegment* querySeg;
56
57
    std::vector<const LineSegment*> items;
58
59
public:
60
61
    LineSegmentVisitor(const LineSegment* s)
62
        :
63
0
        ItemVisitor(),
64
0
        querySeg(s)
65
0
    {}
66
67
    ~LineSegmentVisitor() override
68
0
    {
69
        // nothing to do, LineSegments are not owned by us
70
0
    }
71
72
    void
73
    visitItem(void* item) override
74
0
    {
75
0
        const LineSegment* seg = static_cast<const LineSegment*>(item);
76
0
        if(Envelope::intersects(seg->p0, seg->p1,
77
0
                                querySeg->p0, querySeg->p1)) {
78
0
            items.push_back(seg);
79
0
        }
80
0
    }
81
82
    std::vector<const LineSegment*>
83
    getItems()
84
0
    {
85
        // NOTE: Apparently, this is 'source' method giving up the object resource.
86
0
        return std::move(items);
87
0
    }
88
89
};
90
91
92
/*public*/
93
void
94
LineSegmentIndex::add(const TaggedLineString& line)
95
0
{
96
0
    for(const LineSegment* seg : line.getSegments()) {
97
0
        add(seg);
98
0
    }
99
0
}
100
101
/*public*/
102
void
103
LineSegmentIndex::add(const LineSegment* seg)
104
0
{
105
0
    std::unique_ptr<Envelope> env{new Envelope(seg->p0, seg->p1)};
106
107
    // We need a cast because index wants a non-const,
108
    // although it won't change the argument
109
0
    index.insert(env.get(), const_cast<LineSegment*>(seg));
110
111
0
    newEnvelopes.push_back(std::move(env));
112
0
}
113
114
/*public*/
115
void
116
LineSegmentIndex::remove(const LineSegment* seg)
117
0
{
118
0
    Envelope env(seg->p0, seg->p1);
119
120
    // We need a cast because index wants a non-const
121
    // although it won't change the argument
122
0
    index.remove(&env, const_cast<LineSegment*>(seg));
123
0
}
124
125
/*public*/
126
std::vector<const LineSegment*>
127
LineSegmentIndex::query(const LineSegment* querySeg)
128
0
{
129
0
    Envelope env(querySeg->p0, querySeg->p1);
130
131
0
    LineSegmentVisitor visitor(querySeg);
132
0
    index.query(&env, visitor);
133
134
0
    auto itemsFound = visitor.getItems();
135
136
0
    return itemsFound;
137
0
}
138
139
} // namespace geos::simplify
140
} // namespace geos