From e5407d36236a1d813ddce15091f5f8b021902d1f Mon Sep 17 00:00:00 2001 From: Roel Arents Date: Fri, 10 Nov 2023 11:54:07 +0100 Subject: [PATCH] add tests for edge and corner cases --- snap/pointindex.go | 2 -- snap/pointindex_test.go | 61 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/snap/pointindex.go b/snap/pointindex.go index 889309e..b2dbce1 100644 --- a/snap/pointindex.go +++ b/snap/pointindex.go @@ -273,8 +273,6 @@ type quadrantToCheck struct { i int // quadrant number certain bool // whether the line certainly intersects this quadrant (true) or needs to be checked (false) mutex bool // if the line intersects this one, the other cannot be intersected - // TODO account for edge cases: lines on, or stopping at the exclusive extent edges - // TODO not only exclusive lines but also last/first point on a line } // getInfiniteQuadrant determines in which infinite quadrant the point lies diff --git a/snap/pointindex_test.go b/snap/pointindex_test.go index 1ddbe90..c8389ee 100644 --- a/snap/pointindex_test.go +++ b/snap/pointindex_test.go @@ -357,14 +357,71 @@ func TestPointIndex_SnapClosestPoints(t *testing.T) { {110907.6453125, 504428.8065625}, // horizontal line still here }, }, + { + name: "corner case topleft", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{0.0, 4.0}, {1.0, 3.0}}, + want: [][2]float64{}, + }, + { + name: "corner case topright", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{4.0, 4.0}, {3.0, 3.0}}, + want: [][2]float64{}, + }, + { + name: "corner case bottomright", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{4.0, 0.0}, {3.0, 1.0}}, + want: [][2]float64{}, + }, + { + name: "corner case bottomleft", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{0.0, 0.0}, {1.0, 1.0}}, + want: [][2]float64{{1.5, 1.5}}, + }, + { + name: "edge case top", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{0.0, 3.0}, {4.0, 3.0}}, + want: [][2]float64{}, + }, + { + name: "edge case right", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{3.0, 4.0}, {3.0, 0.0}}, + want: [][2]float64{}, + }, + { + name: "edge case bottom", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{0.0, 1.0}, {4.0, 1.0}}, + want: [][2]float64{{1.5, 1.5}, {2.5, 1.5}}, + }, + { + name: "edge case left", + matrix: newSimpleTileMatrix(4.0, 2, 1.0), + poly: geom.Polygon{{{1.5, 1.5}, {2.5, 1.5}, {2.5, 2.5}, {1.5, 2.5}}}, + line: geom.Line{{1.0, 0.0}, {1.0, 4.0}}, + want: [][2]float64{{1.5, 1.5}, {1.5, 2.5}}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { ix := NewPointIndexFromTileMatrix(tt.matrix) - ix.InsertPolygon(&tt.poly) - ix.toWkt(os.Stdout) + poly := tt.poly + ix.InsertPolygon(&poly) got := ix.SnapClosestPoints(tt.line) if !assert.EqualValues(t, tt.want, got) { + ix.toWkt(os.Stdout) t.Errorf("SnapClosestPoints() = %v, want %v", got, tt.want) } })