From 383b5848edb212fef2ed2f7dff046f69e351f749 Mon Sep 17 00:00:00 2001 From: James McClain Date: Fri, 6 Oct 2017 11:35:12 -0400 Subject: [PATCH] Make geotrellis.vector.triangulation Serializable --- .../triangulation/BoundaryDelaunay.scala | 14 ++++----- .../triangulation/DelaunayStitcher.scala | 5 +++- .../triangulation/DelaunayTriangulation.scala | 8 +++-- .../vector/triangulation/QuadricError.scala | 4 +-- .../triangulation/StitchedDelaunay.scala | 30 +++++++++---------- .../vector/triangulation/TriangleMap.scala | 2 +- .../TriangulationPredicates.scala | 5 +++- 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/BoundaryDelaunay.scala b/vector/src/main/scala/geotrellis/vector/triangulation/BoundaryDelaunay.scala index cf7fef25f7..27e801f6cd 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/BoundaryDelaunay.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/BoundaryDelaunay.scala @@ -73,16 +73,16 @@ object BoundaryDelaunay { val (radius, center, _) = dt.predicates.circleCenter(i, j, k) val shortest = Seq(pi.distance(pj), pi.distance(pk), pj.distance(pk)).min - + if (radius / shortest > thresh) true else { val Extent(x0, y0, x1, y1) = extent def outside(x: Double): Boolean = x < 0.0 || x > 1.0 - x1 - radius < x0 + radius || - y1 - radius < y0 + radius || - outside((center.x - (x0 + radius)) / ((x1 - radius) - (x0 + radius))) || + x1 - radius < x0 + radius || + y1 - radius < y0 + radius || + outside((center.x - (x0 + radius)) / ((x1 - radius) - (x0 + radius))) || outside((center.y - (y0 + radius)) / ((y1 - radius) - (y0 + radius))) } } @@ -322,7 +322,7 @@ object BoundaryDelaunay { if (dt.isLinear) { dt.numVertices match { case 0 => -1 - case 1 => + case 1 => addPoint(dt.liveVertices.toSeq(0)) -1 case _ => copyConvertLinearBound @@ -330,7 +330,7 @@ object BoundaryDelaunay { } else copyConvertBoundingTris - BoundaryDelaunay(IndexedPointSet(verts.toMap), liveVertices.toSet, halfEdgeTable, triangles, boundary, isLinear) + BoundaryDelaunay(IndexedPointSet(verts.toMap), liveVertices.toSet, halfEdgeTable, triangles, boundary, isLinear) } } @@ -342,7 +342,7 @@ case class BoundaryDelaunay( triangleMap: TriangleMap, boundary: Int, isLinear: Boolean -) { +) extends Serializable { def trianglesFromVertices: MultiPolygon = { val indexToCoord = { i: Int => Point.jtsCoord2Point(pointSet.getCoordinate(i)) } geotrellis.vector.MultiPolygon( diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayStitcher.scala b/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayStitcher.scala index d38941a770..9def1c894e 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayStitcher.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayStitcher.scala @@ -5,7 +5,10 @@ import geotrellis.vector.{Line, MultiLine, Point} import geotrellis.vector.RobustPredicates.{LEFTOF, ON, RIGHTOF} import geotrellis.vector.mesh.{HalfEdgeTable, IndexedPointSet} -final class DelaunayStitcher(pointSet: IndexedPointSet, halfEdgeTable: HalfEdgeTable) { +final class DelaunayStitcher( + pointSet: IndexedPointSet, + halfEdgeTable: HalfEdgeTable +) extends Serializable { val predicates = new TriangulationPredicates(pointSet, halfEdgeTable) import predicates._ import pointSet._ diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayTriangulation.scala b/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayTriangulation.scala index 6bf375729b..ed1310e486 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayTriangulation.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/DelaunayTriangulation.scala @@ -32,8 +32,12 @@ import scala.collection.mutable.{ListBuffer, Map, PriorityQueue, Set} * reasonable value in the defaults of the DelaunayTriangulation object's apply() * method, but bear in mind that problems may arise in your particular case. */ -case class DelaunayTriangulation(pointSet: CompleteIndexedPointSet, halfEdgeTable: HalfEdgeTable, tolerance: Double, debug: Boolean) -{ +case class DelaunayTriangulation( + pointSet: CompleteIndexedPointSet, + halfEdgeTable: HalfEdgeTable, + tolerance: Double, + debug: Boolean +) extends Serializable { /** * Contains the triangles of a DelaunayTriangulation * diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/QuadricError.scala b/vector/src/main/scala/geotrellis/vector/triangulation/QuadricError.scala index ff62d81c50..20300faf2a 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/QuadricError.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/QuadricError.scala @@ -30,9 +30,9 @@ object QuadricError { def edgeMatrix(e0: HalfEdge[Int, Int], end: Int, trans: Int => Coordinate) = { var e = e0 var accum = MatrixUtils.createRealMatrix(Array( - Array[Double](0, 0, 0, 0), Array[Double](0, 0, 0, 0), - Array[Double](0, 0, 0, 0), + Array[Double](0, 0, 0, 0), + Array[Double](0, 0, 0, 0), Array[Double](0, 0, 0, 0) )) diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/StitchedDelaunay.scala b/vector/src/main/scala/geotrellis/vector/triangulation/StitchedDelaunay.scala index b169185c97..47d7686fe7 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/StitchedDelaunay.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/StitchedDelaunay.scala @@ -30,7 +30,7 @@ object StitchedDelaunay { } val vtrans = regions.map { dir => (dir, vertIndices(dir)) }.toMap - regions.foreach{ dir => + regions.foreach{ dir => val mapping = vtrans(dir) val pointSet = if (dir == Center) center.pointSet else neighbors(dir)._1.pointSet mapping.foreach { case (orig, newix) => @@ -55,7 +55,7 @@ object StitchedDelaunay { val vtrans = regions.map { dir => (dir, vertIndices(dir)) }.toMap - regions.foreach{ dir => + regions.foreach{ dir => val mapping = vtrans(dir) val pointSet = neighbors(dir)._1.pointSet mapping.foreach { case (orig, newix) => assert(points(newix) == null) ; points(newix) = pointSet(orig) } @@ -77,7 +77,7 @@ object StitchedDelaunay { val vtrans = regions.map { dir => (dir, vertIndices(dir)) }.toMap - regions.foreach{ dir => + regions.foreach{ dir => val mapping = vtrans(dir) val pointSet = neighbors(dir)._1.pointSet mapping.foreach { case (orig, newix) => assert(points(newix) == null) ; points(newix) = pointSet(orig) } @@ -98,7 +98,7 @@ object StitchedDelaunay { val boundaries = neighbors.map{ case (dir, (bdt, _)) => { val reindex = vtrans(dir)(_) val edgeoffset = allEdges.appendTable(bdt.halfEdgeTable, reindex) - val handle: Either[(Int, Boolean), Int] = + val handle: Either[(Int, Boolean), Int] = if (bdt.liveVertices.size == 1) scala.Right(reindex(bdt.liveVertices.toSeq(0))) else @@ -150,10 +150,10 @@ object StitchedDelaunay { val boundaries = neighbors.map{ case (dir, (dt, _)) => { val reindex = vtrans(dir).apply(_) val edgeoffset = allEdges.appendTable(dt.halfEdgeTable, reindex) - val handle: Either[(Int, Boolean), Int] = - if (dt.liveVertices.size == 1) + val handle: Either[(Int, Boolean), Int] = + if (dt.liveVertices.size == 1) scala.Right(reindex(dt.liveVertices.toSeq(0))) - else + else scala.Left((dt.boundary + edgeoffset, dt.isLinear)) (dir, handle) }} @@ -211,8 +211,8 @@ object StitchedDelaunay { val reindex = vtrans(dir)(_) if (dir == Center) { val edgeoffset = allEdges.appendTable(center.halfEdgeTable, reindex) - val handle: Either[(Int, Boolean), Int] = - if (center.liveVertices.size == 1) + val handle: Either[(Int, Boolean), Int] = + if (center.liveVertices.size == 1) scala.Right(reindex(center.liveVertices.head)) else { scala.Left((center.boundary + edgeoffset, center.isLinear)) @@ -220,8 +220,8 @@ object StitchedDelaunay { (dir, handle) } else { val edgeoffset = allEdges.appendTable(bdt.halfEdgeTable, reindex) - val handle: Either[(Int, Boolean), Int] = - if (bdt.liveVertices.size == 1) + val handle: Either[(Int, Boolean), Int] = + if (bdt.liveVertices.size == 1) scala.Right(reindex(bdt.liveVertices.head)) else { scala.Left((bdt.boundary + edgeoffset, bdt.isLinear)) @@ -240,7 +240,7 @@ object StitchedDelaunay { dirs .map{row => row.flatMap{ dir => boundaries.get(dir) }} .filter{ row => row.nonEmpty } - .map{row => row.reduce{ (l, r) => + .map{row => row.reduce{ (l, r) => val result: Either[(Int, Boolean), Int] = (l, r) match { case (scala.Left((left, isLeftLinear)), scala.Left((right, isRightLinear))) => val result = stitcher.merge(left, isLeftLinear, right, isRightLinear, overlayTris, debug) @@ -274,7 +274,7 @@ object StitchedDelaunay { result }} - val bound = + val bound = if (joinedRows.isEmpty) { scala.Left((-1, true)) } else { @@ -313,7 +313,7 @@ object StitchedDelaunay { result }} } - + val boundary = bound match { case scala.Left((bnd, _)) => bnd case scala.Right(_) => -1 @@ -330,7 +330,7 @@ case class StitchedDelaunay( boundary: Int, pointSet: IndexedPointSet, fillTriangles: TriangleMap -) { +) extends Serializable { def triangles(): Seq[(Int, Int, Int)] = fillTriangles.getTriangles.keys.toSeq def writeWKT(wktFile: String) = { diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/TriangleMap.scala b/vector/src/main/scala/geotrellis/vector/triangulation/TriangleMap.scala index 97947584a9..eb2e5629cf 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/TriangleMap.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/TriangleMap.scala @@ -2,7 +2,7 @@ package geotrellis.vector.triangulation import geotrellis.vector.mesh.HalfEdgeTable -class TriangleMap(halfEdgeTable: HalfEdgeTable) { +class TriangleMap(halfEdgeTable: HalfEdgeTable) extends Serializable { import halfEdgeTable._ import TriangleMap.regularizeIndex diff --git a/vector/src/main/scala/geotrellis/vector/triangulation/TriangulationPredicates.scala b/vector/src/main/scala/geotrellis/vector/triangulation/TriangulationPredicates.scala index bcb99c53f0..75ebf74343 100644 --- a/vector/src/main/scala/geotrellis/vector/triangulation/TriangulationPredicates.scala +++ b/vector/src/main/scala/geotrellis/vector/triangulation/TriangulationPredicates.scala @@ -8,7 +8,10 @@ import geotrellis.util.Constants.{DOUBLE_EPSILON => EPSILON} import geotrellis.vector.mesh.{HalfEdgeTable, IndexedPointSet} -final class TriangulationPredicates(pointSet: IndexedPointSet, halfEdgeTable: HalfEdgeTable) { +final class TriangulationPredicates( + pointSet: IndexedPointSet, + halfEdgeTable: HalfEdgeTable +) extends Serializable { import pointSet._ import halfEdgeTable._