tidwall 6257ddba78 Faster point in polygon / GeoJSON updates
The big change is that the GeoJSON package has been completely
rewritten to fix a few of geometry calculation bugs, increase
performance, and to better follow the GeoJSON spec RFC 7946.

GeoJSON updates

- A LineString now requires at least two points.
- All json members, even foreign, now persist with the object.
- The bbox member persists too but is no longer used for geometry
  calculations. This is change in behavior. Previously Tile38 would
  treat the bbox as the object's physical rectangle.
- Corrections to geometry intersects and within calculations.

Faster spatial queries

- The performance of Point-in-polygon and object intersect operations
  are greatly improved for complex polygons and line strings. It went
  from O(n) to roughly O(log n).
- The same for all collection types with many children, including
  FeatureCollection, GeometryCollection, MultiPoint, MultiLineString,
  and MultiPolygon.

Codebase changes

- The pkg directory has been renamed to internal
- The GeoJSON internal package has been moved to a seperate repo at
  https://github.com/tidwall/geojson. It's now vendored.

Please look out for higher memory usage for datasets using complex
shapes. A complex shape is one that has 64 or more points. For these
shapes it's expected that there will be increase of least 54 bytes per
point.
2018-10-13 04:30:48 -07:00

89 lines
2.9 KiB
Go

// Copyright 2018 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package geometry
import (
"math/rand"
"os"
"testing"
"time"
)
func init() {
seed := time.Now().UnixNano()
println(seed)
rand.Seed(seed)
if os.Getenv("PIPBENCH") != "1" {
println("use PIPBENCH=1 for point-in-polygon benchmarks")
}
}
func S(ax, ay, bx, by float64) Segment {
return Segment{Point{ax, ay}, Point{bx, by}}
}
func R(minX, minY, maxX, maxY float64) Rect {
return Rect{Point{minX, minY}, Point{maxX, maxY}}
}
func P(x, y float64) Point {
return Point{x, y}
}
func L(points ...Point) *Line {
return NewLine(points, DefaultIndex)
}
var (
// rings
rectangle = []Point{{0, 0}, {10, 0}, {10, 10}, {0, 10}, {0, 0}}
pentagon = []Point{{2, 2}, {8, 0}, {10, 6}, {5, 10}, {0, 6}, {2, 2}}
triangle = []Point{{0, 0}, {10, 0}, {5, 10}, {0, 0}}
trapezoid = []Point{{0, 0}, {10, 0}, {8, 10}, {2, 10}, {0, 0}}
octagon = []Point{
{3, 0}, {7, 0}, {10, 3}, {10, 7},
{7, 10}, {3, 10}, {0, 7}, {0, 3}, {3, 0},
}
concave1 = []Point{{5, 0}, {10, 0}, {10, 10}, {0, 10}, {0, 5}, {5, 5}, {5, 0}}
concave2 = []Point{{0, 0}, {5, 0}, {5, 5}, {10, 5}, {10, 10}, {0, 10}, {0, 0}}
concave3 = []Point{{0, 0}, {10, 0}, {10, 5}, {5, 5}, {5, 10}, {0, 10}, {0, 0}}
concave4 = []Point{{0, 0}, {10, 0}, {10, 10}, {5, 10}, {5, 5}, {0, 5}, {0, 0}}
bowtie = []Point{{0, 0}, {5, 4}, {10, 0}, {10, 10}, {5, 6}, {0, 10}, {0, 0}}
notClosed = []Point{{0, 0}, {10, 0}, {10, 10}, {0, 10}}
// lines
u1 = []Point{{0, 10}, {0, 0}, {10, 0}, {10, 10}}
u2 = []Point{{0, 0}, {10, 0}, {10, 10}, {0, 10}}
u3 = []Point{{10, 0}, {10, 10}, {0, 10}, {0, 0}}
u4 = []Point{{10, 10}, {0, 10}, {0, 0}, {10, 0}}
v1 = []Point{{0, 10}, {5, 0}, {10, 10}}
v2 = []Point{{0, 0}, {10, 5}, {0, 10}}
v3 = []Point{{10, 0}, {5, 10}, {0, 0}}
v4 = []Point{{10, 10}, {0, 5}, {10, 0}}
)
func expect(t testing.TB, what bool) {
t.Helper()
if !what {
t.Fatal("expection failure")
}
}
// func TestRectVarious(t *testing.T) {
// expect(t, R(0, 0, 10, 10).ContainsRing(newRingSimple2(octagon)))
// expect(t, !R(5, 0, 15, 10).ContainsRing(newRingSimple2(octagon)))
// expect(t, R(5, 0, 15, 10).IntersectsRing(newRingSimple2(octagon)))
// expect(t, R(0, 0, 10, 10).Center() == P(5, 5))
// }
func TestRaycastBounds(t *testing.T) {
expect(t, S(0, 0, 10, 10).Raycast(P(20, -1)) == RaycastResult{false, false})
expect(t, S(10, 10, 0, 0).Raycast(P(-1, 20)) == RaycastResult{false, false})
expect(t, S(0, 0, 0, 0).Raycast(P(0, 0)) == RaycastResult{false, true})
expect(t, S(0, 0, 0, 0).Raycast(P(0, 1)) == RaycastResult{false, false})
expect(t, S(0, 0, 1, 0).Raycast(P(1, 0)) == RaycastResult{false, true})
expect(t, S(1, 0, 0, 0).Raycast(P(1, 0)) == RaycastResult{false, true})
expect(t, S(0, 1, 0, 0).Raycast(P(0, 1)) == RaycastResult{false, true})
expect(t, S(0, 0, 0, 1).Raycast(P(0, 1)) == RaycastResult{false, true})
}