-
Notifications
You must be signed in to change notification settings - Fork 362
/
StitchedDelaunay.scala
223 lines (197 loc) · 8.34 KB
/
StitchedDelaunay.scala
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
package geotrellis.vector.triangulation
import com.vividsolutions.jts.geom.Coordinate
import geotrellis.util.Direction
import geotrellis.util.Direction._
import geotrellis.vector._
import geotrellis.vector.io.wkt.WKT
object StitchedDelaunay {
def directionToVertexOffset(d: Direction) = {
val increment = Int.MaxValue / 9
d match {
case Center => 0
case Left => increment
case BottomLeft => 2 * increment
case Bottom => 3 * increment
case BottomRight => 4 * increment
case Right => 5 * increment
case TopRight => 6 * increment
case Top => 7 * increment
case TopLeft => 8 * increment
}
}
def indexToVertex(neighbors: Map[Direction, (BoundaryDelaunay, Extent)]): Int => Coordinate =
{ i =>
val increment = Int.MaxValue / 9
val group = i / increment
val index = i % increment
val dir = group match {
case 0 => Center
case 1 => Left
case 2 => BottomLeft
case 3 => Bottom
case 4 => BottomRight
case 5 => Right
case 6 => TopRight
case 7 => Top
case 8 => TopLeft
}
neighbors(dir)._1.pointSet.getCoordinate(index)
}
def indexToVertex(neighbors: Map[Direction, (DelaunayTriangulation, Extent)])(implicit dummy: DummyImplicit): Int => Coordinate =
{ i =>
val increment = Int.MaxValue / 9
val group = i / increment
val index = i % increment
val dir = group match {
case 0 => Center
case 1 => Left
case 2 => BottomLeft
case 3 => Bottom
case 4 => BottomRight
case 5 => Right
case 6 => TopRight
case 7 => Top
case 8 => TopLeft
}
neighbors(dir)._1.pointSet.getCoordinate(index)
}
def indexToVertex(center: DelaunayTriangulation, neighbors: Map[Direction, (BoundaryDelaunay, Extent)])(implicit dummy: DummyImplicit): Int => Coordinate =
{ i =>
val increment = Int.MaxValue / 9
val group = i / increment
val index = i % increment
if (group == 0) {
center.pointSet.getCoordinate(index)
} else {
val dir = group match {
case 1 => Left
case 2 => BottomLeft
case 3 => Bottom
case 4 => BottomRight
case 5 => Right
case 6 => TopRight
case 7 => Top
case 8 => TopLeft
}
neighbors(dir)._1.pointSet.getCoordinate(index)
}
}
/**
* Given a set of BoundaryDelaunay objects and their non-overlapping boundary
* extents, each pair associated with a cardinal direction, this function
* creates a merged representation
*/
def apply(neighbors: Map[Direction, (BoundaryDelaunay, Extent)], debug: Boolean = false): StitchedDelaunay = {
val vertCount = neighbors.map{ case (_, (bdt, _)) => bdt.pointSet.length }.reduce(_ + _)
val allEdges = new HalfEdgeTable(2 * (3 * vertCount - 6))
val pointMap = indexToVertex(neighbors)
val allPoints = IndexedPointSet(pointMap, vertCount)
val boundaries = neighbors.map{ case (dir, (bdt, _)) => {
val offset = directionToVertexOffset(dir)
val reindex = {x: Int => x + offset}
val edgeoffset = allEdges.appendTable(bdt.halfEdgeTable, reindex)
(dir, (bdt.boundary + edgeoffset, bdt.isLinear))
}}
val dirs = Seq(Seq(TopLeft, Top, TopRight), Seq(Left, Center, Right), Seq(BottomLeft, Bottom, BottomRight))
val overlayTris = new TriangleMap(allEdges)
val stitcher = new DelaunayStitcher(allPoints, allEdges)
dirs
.map{row => row.flatMap{ dir => boundaries.get(dir) }}
.filter{ row => !row.isEmpty }
.map{row => row.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
val result = stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
result
}}}
.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
}}
new StitchedDelaunay(pointMap, allEdges, allPoints, overlayTris)
}
def apply(neighbors: Map[Direction, (DelaunayTriangulation, Extent)], debug: Boolean)(implicit dummy: DummyImplicit): StitchedDelaunay = {
val vertCount = neighbors.map{ case (_, (bdt, _)) => bdt.pointSet.length }.reduce(_ + _)
val allEdges = new HalfEdgeTable(2 * (3 * vertCount - 6))
val pointMap = indexToVertex(neighbors)
val allPoints = IndexedPointSet(pointMap, vertCount)
val boundaries = neighbors.map{ case (dir, (bdt, _)) => {
val offset = directionToVertexOffset(dir)
val reindex = {x: Int => x + offset}
val edgeoffset = allEdges.appendTable(bdt.halfEdgeTable, reindex)
(dir, (bdt.boundary + edgeoffset, bdt.isLinear))
}}
val dirs = Seq(Seq(TopLeft, Top, TopRight), Seq(Left, Center, Right), Seq(BottomLeft, Bottom, BottomRight))
val overlayTris = new TriangleMap(allEdges)
val stitcher = new DelaunayStitcher(allPoints, allEdges)
dirs
.map{row => row.flatMap{ dir => boundaries.get(dir) }}
.filter{ row => !row.isEmpty }
.map{row => row.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
val result = stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
result
}}}
.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
}}
new StitchedDelaunay(pointMap, allEdges, allPoints, overlayTris)
}
def apply(center: DelaunayTriangulation, neighbors: Map[Direction, (BoundaryDelaunay, Extent)], debug: Boolean): StitchedDelaunay = {
val vertCount = center.pointSet.length + neighbors.map{ case (dir, (bdt, _)) => if (dir == Center) 0 else bdt.pointSet.length }.reduce(_ + _)
val allEdges = new HalfEdgeTable(2 * (3 * vertCount - 6))
val pointMap = indexToVertex(center, neighbors)
val allPoints = IndexedPointSet(pointMap, vertCount)
val boundaries = neighbors.map{ case (dir, (bdt, _)) => {
val offset = directionToVertexOffset(dir)
val reindex = {x: Int => x + offset}
if (dir == Center) {
val edgeoffset = allEdges.appendTable(center.halfEdgeTable, reindex)
// println(s"Center boundary loop (before): ${center.halfEdgeTable.showLoop(center.boundary)}")
// println(s"Center boundary loop (after): ${allEdges.showLoop(center.boundary + edgeoffset)}")
(dir, (center.boundary + edgeoffset, center.isLinear))
} else {
val edgeoffset = allEdges.appendTable(bdt.halfEdgeTable, reindex)
// println(s"$dir boundary loop (before): ${bdt.halfEdgeTable.mapOverLoop(bdt.boundary) { e => reindex(bdt.halfEdgeTable.getSrc(e))}} ")
// println(s"$dir boundary loop (after): ${allEdges.mapOverLoop(bdt.boundary + edgeoffset) { e => allEdges.getSrc(e) }}")
(dir, (bdt.boundary + edgeoffset, bdt.isLinear))
}
}}
val dirs = Seq(Seq(TopLeft, Top, TopRight), Seq(Left, Center, Right), Seq(BottomLeft, Bottom, BottomRight))
val overlayTris = new TriangleMap(allEdges)
val stitcher = new DelaunayStitcher(allPoints, allEdges)
dirs
.map{row => row.flatMap{ dir => boundaries.get(dir) }}
.filter{ row => !row.isEmpty }
.map{row => row.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
val result = stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
result
}}}
.reduce{ (l, r) => {
val (left, isLeftLinear) = l
val (right, isRightLinear) = r
stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug)
}}
// println("Done merging")
new StitchedDelaunay(pointMap, allEdges, allPoints, overlayTris)
}
}
case class StitchedDelaunay(
indexToCoord: Int => Coordinate,
halfEdgeTable: HalfEdgeTable,
private val pointSet: IndexedPointSet,
private val fillTriangles: TriangleMap
) {
def triangles(): Seq[(Int, Int, Int)] = fillTriangles.getTriangles.keys.toSeq
def writeWKT(wktFile: String) = {
val mp = MultiPolygon(triangles.map{ case (i,j,k) => Polygon(indexToCoord(i), indexToCoord(j), indexToCoord(k), indexToCoord(i)) })
val wktString = WKT.write(mp)
new java.io.PrintWriter(wktFile) { write(wktString); close }
}
}