Skip to content

Commit

Permalink
Make geotrellis.vector.triangulation Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
James McClain authored and echeipesh committed Oct 6, 2017
1 parent 747e0c3 commit 383b584
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
Expand Up @@ -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)))
}
}
Expand Down Expand Up @@ -322,15 +322,15 @@ object BoundaryDelaunay {
if (dt.isLinear) {
dt.numVertices match {
case 0 => -1
case 1 =>
case 1 =>
addPoint(dt.liveVertices.toSeq(0))
-1
case _ => copyConvertLinearBound
}
} else
copyConvertBoundingTris

BoundaryDelaunay(IndexedPointSet(verts.toMap), liveVertices.toSet, halfEdgeTable, triangles, boundary, isLinear)
BoundaryDelaunay(IndexedPointSet(verts.toMap), liveVertices.toSet, halfEdgeTable, triangles, boundary, isLinear)
}

}
Expand All @@ -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(
Expand Down
Expand Up @@ -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._
Expand Down
Expand Up @@ -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
*
Expand Down
Expand Up @@ -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)
))

Expand Down
Expand Up @@ -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) =>
Expand All @@ -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) }
Expand All @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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)
}}
Expand Down Expand Up @@ -211,17 +211,17 @@ 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))
}
(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))
Expand All @@ -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)
Expand Down Expand Up @@ -274,7 +274,7 @@ object StitchedDelaunay {
result
}}

val bound =
val bound =
if (joinedRows.isEmpty) {
scala.Left((-1, true))
} else {
Expand Down Expand Up @@ -313,7 +313,7 @@ object StitchedDelaunay {
result
}}
}

val boundary = bound match {
case scala.Left((bnd, _)) => bnd
case scala.Right(_) => -1
Expand All @@ -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) = {
Expand Down
Expand Up @@ -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

Expand Down
Expand Up @@ -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._

Expand Down

0 comments on commit 383b584

Please sign in to comment.