Coverage Report

Created: 2026-07-16 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/geos/src/geomgraph/NodeMap.cpp
Line
Count
Source
1
/**********************************************************************
2
 *
3
 * GEOS - Geometry Engine Open Source
4
 * http://geos.osgeo.org
5
 *
6
 * Copyright (C) 2005-2006 Refractions Research Inc.
7
 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8
 *
9
 * This is free software; you can redistribute and/or modify it under
10
 * the terms of the GNU Lesser General Public Licence as published
11
 * by the Free Software Foundation.
12
 * See the COPYING file for more information.
13
 *
14
 **********************************************************************
15
 *
16
 * Last port: geomgraph/NodeMap.java rev. 1.3 (JTS-1.10)
17
 *
18
 **********************************************************************/
19
20
#include <geos/geomgraph/NodeMap.h>
21
#include <geos/geomgraph/Node.h>
22
#include <geos/geomgraph/NodeFactory.h>
23
#include <geos/geomgraph/EdgeEnd.h>
24
#include <geos/geomgraph/Label.h>
25
#include <geos/geom/Location.h>
26
#include <geos/geom/Coordinate.h>
27
28
#include <vector>
29
#include <cassert>
30
31
#ifndef GEOS_DEBUG
32
#define GEOS_DEBUG 0
33
#endif
34
35
using namespace geos::geom;
36
37
namespace geos {
38
namespace geomgraph { // geos.geomgraph
39
40
NodeMap::NodeMap(const NodeFactory& newNodeFact)
41
    :
42
37.6k
    nodeFact(newNodeFact)
43
37.6k
{
44
#if GEOS_DEBUG
45
    std::cerr << "[" << this << "] NodeMap::NodeMap" << std::endl;
46
#endif
47
37.6k
}
48
49
Node*
50
NodeMap::addNode(const Coordinate& coord)
51
37.7M
{
52
#if GEOS_DEBUG
53
    std::cerr << "[" << this << "] NodeMap::addNode(" << coord.toString() << ")";
54
#endif
55
37.7M
    Node* node = find(coord);
56
37.7M
    if(node == nullptr) {
57
#if GEOS_DEBUG
58
        std::cerr << " is new" << std::endl;
59
#endif
60
9.07M
        node = nodeFact.createNode(coord);
61
9.07M
        Coordinate* c = const_cast<Coordinate*>(
62
9.07M
                            &(node->getCoordinate()));
63
9.07M
        nodeMap[c] = std::unique_ptr<Node>(node);
64
9.07M
        node = nodeMap[c].get();
65
9.07M
    }
66
28.6M
    else {
67
#if GEOS_DEBUG
68
        std::cerr << " already found (" << node->getCoordinate().toString() << ") - adding Z" << std::endl;
69
#endif
70
28.6M
        node->addZ(coord.z);
71
28.6M
    }
72
37.7M
    return node;
73
37.7M
}
74
75
// first arg cannot be const because
76
// it is liable to label-merging ... --strk;
77
Node*
78
NodeMap::addNode(Node* n)
79
0
{
80
0
    assert(n);
81
82
#if GEOS_DEBUG
83
    std::cerr << "[" << this << "] NodeMap::addNode(" << n->print() << ")";
84
#endif
85
0
    Coordinate* c = const_cast<Coordinate*>(&n->getCoordinate());
86
0
    Node* node = find(*c);
87
0
    if(node == nullptr) {
88
#if GEOS_DEBUG
89
        std::cerr << " is new" << std::endl;
90
#endif
91
0
        nodeMap[c] = std::unique_ptr<Node>(n);
92
0
        return nodeMap[c].get();
93
0
    }
94
#if GEOS_DEBUG
95
    else {
96
        std::cerr << " found already, merging label" << std::endl;
97
        const std::vector<double>& zvals = n->getZ();
98
        for(unsigned int i = 0; i < zvals.size(); i++) {
99
            node->addZ(zvals[i]);
100
        }
101
    }
102
#endif // GEOS_DEBUG
103
104
0
    node->mergeLabel(*n);
105
0
    return node;
106
0
}
107
108
void
109
NodeMap::add(EdgeEnd* e)
110
37.7M
{
111
37.7M
    Coordinate& p = e->getCoordinate();
112
37.7M
    Node* n = addNode(p);
113
37.7M
    n->add(e);
114
37.7M
}
115
116
void
117
NodeMap::add(std::unique_ptr<EdgeEnd>&& e)
118
0
{
119
0
    add(e.get());
120
0
    e.release();
121
0
}
122
123
/*
124
 * @return the node if found; null otherwise
125
 */
126
Node*
127
NodeMap::find(const Coordinate& coord) const
128
37.7M
{
129
37.7M
    Coordinate* c = const_cast<Coordinate*>(&coord);
130
131
37.7M
    const auto& found = nodeMap.find(c);
132
133
37.7M
    if(found == nodeMap.end()) {
134
9.07M
        return nullptr;
135
9.07M
    }
136
28.6M
    else {
137
28.6M
        return found->second.get();
138
28.6M
    }
139
37.7M
}
140
141
void
142
NodeMap::getBoundaryNodes(uint8_t geomIndex, std::vector<Node*>& bdyNodes) const
143
0
{
144
0
    for(const auto& it: nodeMap) {
145
0
        Node* node = it.second.get();
146
0
        if(node->getLabel().getLocation(geomIndex) == Location::BOUNDARY) {
147
0
            bdyNodes.push_back(node);
148
0
        }
149
0
    }
150
0
}
151
152
std::string
153
NodeMap::print() const
154
0
{
155
0
    std::string out = "";
156
0
    for(const auto& it: nodeMap) {
157
0
        const Node* node = it.second.get();
158
0
        out += node->print();
159
0
    }
160
0
    return out;
161
0
}
162
163
} // namespace geos.geomgraph
164
} // namespace geos
165