/src/geos/src/geomgraph/NodeMap.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | nodeFact(newNodeFact) |
43 | 0 | { |
44 | | #if GEOS_DEBUG |
45 | | std::cerr << "[" << this << "] NodeMap::NodeMap" << std::endl; |
46 | | #endif |
47 | 0 | } |
48 | | |
49 | 0 | NodeMap::~NodeMap() = default; |
50 | | |
51 | | Node* |
52 | | NodeMap::addNode(const Coordinate& coord) |
53 | 0 | { |
54 | | #if GEOS_DEBUG |
55 | | std::cerr << "[" << this << "] NodeMap::addNode(" << coord.toString() << ")"; |
56 | | #endif |
57 | 0 | Node* node = find(coord); |
58 | 0 | if(node == nullptr) { |
59 | | #if GEOS_DEBUG |
60 | | std::cerr << " is new" << std::endl; |
61 | | #endif |
62 | 0 | node = nodeFact.createNode(coord); |
63 | 0 | Coordinate* c = const_cast<Coordinate*>( |
64 | 0 | &(node->getCoordinate())); |
65 | 0 | nodeMap[c] = std::unique_ptr<Node>(node); |
66 | 0 | node = nodeMap[c].get(); |
67 | 0 | } |
68 | 0 | else { |
69 | | #if GEOS_DEBUG |
70 | | std::cerr << " already found (" << node->getCoordinate().toString() << ") - adding Z" << std::endl; |
71 | | #endif |
72 | 0 | node->addZ(coord.z); |
73 | 0 | } |
74 | 0 | return node; |
75 | 0 | } |
76 | | |
77 | | // first arg cannot be const because |
78 | | // it is liable to label-merging ... --strk; |
79 | | Node* |
80 | | NodeMap::addNode(Node* n) |
81 | 0 | { |
82 | 0 | assert(n); |
83 | |
|
84 | | #if GEOS_DEBUG |
85 | | std::cerr << "[" << this << "] NodeMap::addNode(" << n->print() << ")"; |
86 | | #endif |
87 | 0 | Coordinate* c = const_cast<Coordinate*>(&n->getCoordinate()); |
88 | 0 | Node* node = find(*c); |
89 | 0 | if(node == nullptr) { |
90 | | #if GEOS_DEBUG |
91 | | std::cerr << " is new" << std::endl; |
92 | | #endif |
93 | 0 | nodeMap[c] = std::unique_ptr<Node>(n); |
94 | 0 | return nodeMap[c].get(); |
95 | 0 | } |
96 | | #if GEOS_DEBUG |
97 | | else { |
98 | | std::cerr << " found already, merging label" << std::endl; |
99 | | const std::vector<double>& zvals = n->getZ(); |
100 | | for(unsigned int i = 0; i < zvals.size(); i++) { |
101 | | node->addZ(zvals[i]); |
102 | | } |
103 | | } |
104 | | #endif // GEOS_DEBUG |
105 | | |
106 | 0 | node->mergeLabel(*n); |
107 | 0 | return node; |
108 | 0 | } |
109 | | |
110 | | void |
111 | | NodeMap::add(EdgeEnd* e) |
112 | 0 | { |
113 | 0 | Coordinate& p = e->getCoordinate(); |
114 | 0 | Node* n = addNode(p); |
115 | 0 | n->add(e); |
116 | 0 | } |
117 | | |
118 | | void |
119 | | NodeMap::add(std::unique_ptr<EdgeEnd>&& e) |
120 | 0 | { |
121 | 0 | add(e.get()); |
122 | 0 | e.release(); |
123 | 0 | } |
124 | | |
125 | | /* |
126 | | * @return the node if found; null otherwise |
127 | | */ |
128 | | Node* |
129 | | NodeMap::find(const Coordinate& coord) const |
130 | 0 | { |
131 | 0 | Coordinate* c = const_cast<Coordinate*>(&coord); |
132 | |
|
133 | 0 | const auto& found = nodeMap.find(c); |
134 | |
|
135 | 0 | if(found == nodeMap.end()) { |
136 | 0 | return nullptr; |
137 | 0 | } |
138 | 0 | else { |
139 | 0 | return found->second.get(); |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | void |
144 | | NodeMap::getBoundaryNodes(uint8_t geomIndex, std::vector<Node*>& bdyNodes) const |
145 | 0 | { |
146 | 0 | for(const auto& it: nodeMap) { |
147 | 0 | Node* node = it.second.get(); |
148 | 0 | if(node->getLabel().getLocation(geomIndex) == Location::BOUNDARY) { |
149 | 0 | bdyNodes.push_back(node); |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | std::string |
155 | | NodeMap::print() const |
156 | 0 | { |
157 | 0 | std::string out = ""; |
158 | 0 | for(const auto& it: nodeMap) { |
159 | 0 | const Node* node = it.second.get(); |
160 | 0 | out += node->print(); |
161 | 0 | } |
162 | 0 | return out; |
163 | 0 | } |
164 | | |
165 | | } // namespace geos.geomgraph |
166 | | } // namespace geos |
167 | | |