-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
OverlayOp.js
312 lines (307 loc) · 12 KB
/
OverlayOp.js
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
import PolygonBuilder from './PolygonBuilder.js'
import Position from '../../geomgraph/Position.js'
import IllegalArgumentException from '../../../../../java/lang/IllegalArgumentException.js'
import LineBuilder from './LineBuilder.js'
import PointBuilder from './PointBuilder.js'
import SnapIfNeededOverlayOp from './snap/SnapIfNeededOverlayOp.js'
import Label from '../../geomgraph/Label.js'
import ArrayList from '../../../../../java/util/ArrayList.js'
import Assert from '../../util/Assert.js'
import PlanarGraph from '../../geomgraph/PlanarGraph.js'
import PointLocator from '../../algorithm/PointLocator.js'
import Location from '../../geom/Location.js'
import EdgeNodingValidator from '../../geomgraph/EdgeNodingValidator.js'
import GeometryCollectionMapper from '../../geom/util/GeometryCollectionMapper.js'
import OverlayNodeFactory from './OverlayNodeFactory.js'
import GeometryGraphOperation from '../GeometryGraphOperation.js'
import EdgeList from '../../geomgraph/EdgeList.js'
export default class OverlayOp extends GeometryGraphOperation {
constructor() {
super()
OverlayOp.constructor_.apply(this, arguments)
}
static constructor_() {
this._ptLocator = new PointLocator()
this._geomFact = null
this._resultGeom = null
this._graph = null
this._edgeList = new EdgeList()
this._resultPolyList = new ArrayList()
this._resultLineList = new ArrayList()
this._resultPointList = new ArrayList()
const g0 = arguments[0], g1 = arguments[1]
GeometryGraphOperation.constructor_.call(this, g0, g1)
this._graph = new PlanarGraph(new OverlayNodeFactory())
this._geomFact = g0.getFactory()
}
static overlayOp(geom0, geom1, opCode) {
const gov = new OverlayOp(geom0, geom1)
const geomOv = gov.getResultGeometry(opCode)
return geomOv
}
static union(geom, other) {
if (geom.isEmpty() || other.isEmpty()) {
if (geom.isEmpty() && other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.UNION, geom, other, geom.getFactory())
if (geom.isEmpty()) return other.copy()
if (other.isEmpty()) return geom.copy()
}
if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments')
return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.UNION)
}
static intersection(geom, other) {
if (geom.isEmpty() || other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.INTERSECTION, geom, other, geom.getFactory())
if (geom.isGeometryCollection()) {
const g2 = other
return GeometryCollectionMapper.map(geom, new (class {
get interfaces_() {
return [MapOp]
}
map(g) {
return OverlayOp.intersection(g, g2)
}
})())
}
return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.INTERSECTION)
}
static symDifference(geom, other) {
if (geom.isEmpty() || other.isEmpty()) {
if (geom.isEmpty() && other.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.SYMDIFFERENCE, geom, other, geom.getFactory())
if (geom.isEmpty()) return other.copy()
if (other.isEmpty()) return geom.copy()
}
if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments')
return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.SYMDIFFERENCE)
}
static resultDimension(opCode, g0, g1) {
const dim0 = g0.getDimension()
const dim1 = g1.getDimension()
let resultDimension = -1
switch (opCode) {
case OverlayOp.INTERSECTION:
resultDimension = Math.min(dim0, dim1)
break
case OverlayOp.UNION:
resultDimension = Math.max(dim0, dim1)
break
case OverlayOp.DIFFERENCE:
resultDimension = dim0
break
case OverlayOp.SYMDIFFERENCE:
resultDimension = Math.max(dim0, dim1)
break
}
return resultDimension
}
static createEmptyResult(overlayOpCode, a, b, geomFact) {
let result = null
const resultDim = OverlayOp.resultDimension(overlayOpCode, a, b)
return result = geomFact.createEmpty(resultDim)
}
static difference(geom, other) {
if (geom.isEmpty()) return OverlayOp.createEmptyResult(OverlayOp.DIFFERENCE, geom, other, geom.getFactory())
if (other.isEmpty()) return geom.copy()
if (geom.isGeometryCollection() || other.isGeometryCollection()) throw new IllegalArgumentException('This method does not support GeometryCollection arguments')
return SnapIfNeededOverlayOp.overlayOp(geom, other, OverlayOp.DIFFERENCE)
}
static isResultOfOp() {
if (arguments.length === 2) {
const label = arguments[0], opCode = arguments[1]
const loc0 = label.getLocation(0)
const loc1 = label.getLocation(1)
return OverlayOp.isResultOfOp(loc0, loc1, opCode)
} else if (arguments.length === 3) {
let loc0 = arguments[0], loc1 = arguments[1], overlayOpCode = arguments[2]
if (loc0 === Location.BOUNDARY) loc0 = Location.INTERIOR
if (loc1 === Location.BOUNDARY) loc1 = Location.INTERIOR
switch (overlayOpCode) {
case OverlayOp.INTERSECTION:
return loc0 === Location.INTERIOR && loc1 === Location.INTERIOR
case OverlayOp.UNION:
return loc0 === Location.INTERIOR || loc1 === Location.INTERIOR
case OverlayOp.DIFFERENCE:
return loc0 === Location.INTERIOR && loc1 !== Location.INTERIOR
case OverlayOp.SYMDIFFERENCE:
return loc0 === Location.INTERIOR && loc1 !== Location.INTERIOR || loc0 !== Location.INTERIOR && loc1 === Location.INTERIOR
}
return false
}
}
insertUniqueEdge(e) {
const existingEdge = this._edgeList.findEqualEdge(e)
if (existingEdge !== null) {
const existingLabel = existingEdge.getLabel()
let labelToMerge = e.getLabel()
if (!existingEdge.isPointwiseEqual(e)) {
labelToMerge = new Label(e.getLabel())
labelToMerge.flip()
}
const depth = existingEdge.getDepth()
if (depth.isNull())
depth.add(existingLabel)
depth.add(labelToMerge)
existingLabel.merge(labelToMerge)
} else {
this._edgeList.add(e)
}
}
getGraph() {
return this._graph
}
cancelDuplicateResultEdges() {
for (let it = this._graph.getEdgeEnds().iterator(); it.hasNext(); ) {
const de = it.next()
const sym = de.getSym()
if (de.isInResult() && sym.isInResult()) {
de.setInResult(false)
sym.setInResult(false)
}
}
}
mergeSymLabels() {
for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext(); ) {
const node = nodeit.next()
node.getEdges().mergeSymLabels()
}
}
computeOverlay(opCode) {
this.copyPoints(0)
this.copyPoints(1)
this._arg[0].computeSelfNodes(this._li, false)
this._arg[1].computeSelfNodes(this._li, false)
this._arg[0].computeEdgeIntersections(this._arg[1], this._li, true)
const baseSplitEdges = new ArrayList()
this._arg[0].computeSplitEdges(baseSplitEdges)
this._arg[1].computeSplitEdges(baseSplitEdges)
const splitEdges = baseSplitEdges
this.insertUniqueEdges(baseSplitEdges)
this.computeLabelsFromDepths()
this.replaceCollapsedEdges()
EdgeNodingValidator.checkValid(this._edgeList.getEdges())
this._graph.addEdges(this._edgeList.getEdges())
this.computeLabelling()
this.labelIncompleteNodes()
this.findResultAreaEdges(opCode)
this.cancelDuplicateResultEdges()
const polyBuilder = new PolygonBuilder(this._geomFact)
polyBuilder.add(this._graph)
this._resultPolyList = polyBuilder.getPolygons()
const lineBuilder = new LineBuilder(this, this._geomFact, this._ptLocator)
this._resultLineList = lineBuilder.build(opCode)
const pointBuilder = new PointBuilder(this, this._geomFact, this._ptLocator)
this._resultPointList = pointBuilder.build(opCode)
this._resultGeom = this.computeGeometry(this._resultPointList, this._resultLineList, this._resultPolyList, opCode)
}
findResultAreaEdges(opCode) {
for (let it = this._graph.getEdgeEnds().iterator(); it.hasNext(); ) {
const de = it.next()
const label = de.getLabel()
if (label.isArea() && !de.isInteriorAreaEdge() && OverlayOp.isResultOfOp(label.getLocation(0, Position.RIGHT), label.getLocation(1, Position.RIGHT), opCode))
de.setInResult(true)
}
}
computeLabelsFromDepths() {
for (let it = this._edgeList.iterator(); it.hasNext(); ) {
const e = it.next()
const lbl = e.getLabel()
const depth = e.getDepth()
if (!depth.isNull()) {
depth.normalize()
for (let i = 0; i < 2; i++)
if (!lbl.isNull(i) && lbl.isArea() && !depth.isNull(i))
if (depth.getDelta(i) === 0) {
lbl.toLine(i)
} else {
Assert.isTrue(!depth.isNull(i, Position.LEFT), 'depth of LEFT side has not been initialized')
lbl.setLocation(i, Position.LEFT, depth.getLocation(i, Position.LEFT))
Assert.isTrue(!depth.isNull(i, Position.RIGHT), 'depth of RIGHT side has not been initialized')
lbl.setLocation(i, Position.RIGHT, depth.getLocation(i, Position.RIGHT))
}
}
}
}
isCoveredByA(coord) {
if (this.isCovered(coord, this._resultPolyList)) return true
return false
}
isCoveredByLA(coord) {
if (this.isCovered(coord, this._resultLineList)) return true
if (this.isCovered(coord, this._resultPolyList)) return true
return false
}
computeGeometry(resultPointList, resultLineList, resultPolyList, opcode) {
const geomList = new ArrayList()
geomList.addAll(resultPointList)
geomList.addAll(resultLineList)
geomList.addAll(resultPolyList)
if (geomList.isEmpty()) return OverlayOp.createEmptyResult(opcode, this._arg[0].getGeometry(), this._arg[1].getGeometry(), this._geomFact)
return this._geomFact.buildGeometry(geomList)
}
isCovered(coord, geomList) {
for (let it = geomList.iterator(); it.hasNext(); ) {
const geom = it.next()
const loc = this._ptLocator.locate(coord, geom)
if (loc !== Location.EXTERIOR) return true
}
return false
}
replaceCollapsedEdges() {
const newEdges = new ArrayList()
for (let it = this._edgeList.iterator(); it.hasNext(); ) {
const e = it.next()
if (e.isCollapsed()) {
it.remove()
newEdges.add(e.getCollapsedEdge())
}
}
this._edgeList.addAll(newEdges)
}
updateNodeLabelling() {
for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext(); ) {
const node = nodeit.next()
const lbl = node.getEdges().getLabel()
node.getLabel().merge(lbl)
}
}
getResultGeometry(overlayOpCode) {
this.computeOverlay(overlayOpCode)
return this._resultGeom
}
insertUniqueEdges(edges) {
for (let i = edges.iterator(); i.hasNext(); ) {
const e = i.next()
this.insertUniqueEdge(e)
}
}
labelIncompleteNode(n, targetIndex) {
const loc = this._ptLocator.locate(n.getCoordinate(), this._arg[targetIndex].getGeometry())
n.getLabel().setLocation(targetIndex, loc)
}
copyPoints(argIndex) {
for (let i = this._arg[argIndex].getNodeIterator(); i.hasNext(); ) {
const graphNode = i.next()
const newNode = this._graph.addNode(graphNode.getCoordinate())
newNode.setLabel(argIndex, graphNode.getLabel().getLocation(argIndex))
}
}
computeLabelling() {
for (let nodeit = this._graph.getNodes().iterator(); nodeit.hasNext(); ) {
const node = nodeit.next()
node.getEdges().computeLabelling(this._arg)
}
this.mergeSymLabels()
this.updateNodeLabelling()
}
labelIncompleteNodes() {
for (let ni = this._graph.getNodes().iterator(); ni.hasNext(); ) {
const n = ni.next()
const label = n.getLabel()
if (n.isIsolated())
if (label.isNull(0)) this.labelIncompleteNode(n, 0); else this.labelIncompleteNode(n, 1)
n.getEdges().updateLabelling(label)
}
}
}
OverlayOp.INTERSECTION = 1
OverlayOp.UNION = 2
OverlayOp.DIFFERENCE = 3
OverlayOp.SYMDIFFERENCE = 4