forked from mirror/rtred
added context to rectangle
This commit is contained in:
parent
8904718ccc
commit
4ab2859944
12
rtree.go
12
rtree.go
|
@ -10,18 +10,20 @@ import (
|
||||||
|
|
||||||
type Iterator func(item Item) bool
|
type Iterator func(item Item) bool
|
||||||
type Item interface {
|
type Item interface {
|
||||||
Rect() (min []float64, max []float64)
|
Rect(ctx interface{}) (min []float64, max []float64)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
|
ctx interface{}
|
||||||
tr1 *d1.RTree
|
tr1 *d1.RTree
|
||||||
tr2 *d2.RTree
|
tr2 *d2.RTree
|
||||||
tr3 *d3.RTree
|
tr3 *d3.RTree
|
||||||
tr4 *d4.RTree
|
tr4 *d4.RTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *RTree {
|
func New(ctx interface{}) *RTree {
|
||||||
return &RTree{
|
return &RTree{
|
||||||
|
ctx: ctx,
|
||||||
tr1: d1.NewRTree(),
|
tr1: d1.NewRTree(),
|
||||||
tr2: d2.NewRTree(),
|
tr2: d2.NewRTree(),
|
||||||
tr3: d3.NewRTree(),
|
tr3: d3.NewRTree(),
|
||||||
|
@ -33,7 +35,7 @@ func (tr *RTree) Insert(item Item) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
panic("nil item being added to RTree")
|
panic("nil item being added to RTree")
|
||||||
}
|
}
|
||||||
min, max := item.Rect()
|
min, max := item.Rect(tr.ctx)
|
||||||
if len(min) != len(max) {
|
if len(min) != len(max) {
|
||||||
panic("invalid item rectangle")
|
panic("invalid item rectangle")
|
||||||
}
|
}
|
||||||
|
@ -71,7 +73,7 @@ func (tr *RTree) Remove(item Item) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
panic("nil item being added to RTree")
|
panic("nil item being added to RTree")
|
||||||
}
|
}
|
||||||
min, max := item.Rect()
|
min, max := item.Rect(tr.ctx)
|
||||||
if len(min) != len(max) {
|
if len(min) != len(max) {
|
||||||
panic("invalid item rectangle")
|
panic("invalid item rectangle")
|
||||||
}
|
}
|
||||||
|
@ -117,7 +119,7 @@ func (tr *RTree) Search(bounds Item, iter Iterator) {
|
||||||
if bounds == nil {
|
if bounds == nil {
|
||||||
panic("nil item being added to RTree")
|
panic("nil item being added to RTree")
|
||||||
}
|
}
|
||||||
min, max := bounds.Rect()
|
min, max := bounds.Rect(tr.ctx)
|
||||||
if len(min) != len(max) {
|
if len(min) != len(max) {
|
||||||
panic("invalid item rectangle")
|
panic("invalid item rectangle")
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,12 @@ type tRect []float64
|
||||||
func (r *tRect) Arr() []float64 {
|
func (r *tRect) Arr() []float64 {
|
||||||
return []float64(*r)
|
return []float64(*r)
|
||||||
}
|
}
|
||||||
func (r *tRect) Rect() (min, max []float64) {
|
func (r *tRect) Rect(ctx interface{}) (min, max []float64) {
|
||||||
return r.Arr()[:len(r.Arr())/2], r.Arr()[len(r.Arr())/2:]
|
return r.Arr()[:len(r.Arr())/2], r.Arr()[len(r.Arr())/2:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tRect) String() string {
|
func (r *tRect) String() string {
|
||||||
min, max := r.Rect()
|
min, max := r.Rect(nil)
|
||||||
return fmt.Sprintf("%v,%v", min, max)
|
return fmt.Sprintf("%v,%v", min, max)
|
||||||
}
|
}
|
||||||
func tRandRect(dims int) *tRect {
|
func tRandRect(dims int) *tRect {
|
||||||
|
@ -41,7 +41,7 @@ type tPoint struct {
|
||||||
x, y float64
|
x, y float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *tPoint) Rect() (min, max []float64) {
|
func (r *tPoint) Rect(ctx interface{}) (min, max []float64) {
|
||||||
return []float64{r.x, r.y}, []float64{r.x, r.y}
|
return []float64{r.x, r.y}, []float64{r.x, r.y}
|
||||||
}
|
}
|
||||||
func tRandPoint() *tPoint {
|
func tRandPoint() *tPoint {
|
||||||
|
@ -51,7 +51,7 @@ func tRandPoint() *tPoint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func TestRTree(t *testing.T) {
|
func TestRTree(t *testing.T) {
|
||||||
tr := New()
|
tr := New("hello")
|
||||||
zeroPoint := &tRect{0, 0, 0, 0}
|
zeroPoint := &tRect{0, 0, 0, 0}
|
||||||
tr.Insert(&tRect{10, 10, 10, 10, 20, 20, 20, 20})
|
tr.Insert(&tRect{10, 10, 10, 10, 20, 20, 20, 20})
|
||||||
tr.Insert(&tRect{10, 10, 10, 20, 20, 20})
|
tr.Insert(&tRect{10, 10, 10, 20, 20, 20})
|
||||||
|
@ -85,7 +85,7 @@ func TestRTree(t *testing.T) {
|
||||||
func TestInsertDelete(t *testing.T) {
|
func TestInsertDelete(t *testing.T) {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
n := 50000
|
n := 50000
|
||||||
tr := New()
|
tr := New(nil)
|
||||||
var r2arr []*tRect
|
var r2arr []*tRect
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
r := tRandRect(-1)
|
r := tRandRect(-1)
|
||||||
|
@ -120,7 +120,7 @@ func TestInsertDelete(t *testing.T) {
|
||||||
func TestPoints(t *testing.T) {
|
func TestPoints(t *testing.T) {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
n := 25000
|
n := 25000
|
||||||
tr := New()
|
tr := New(nil)
|
||||||
var points []*tPoint
|
var points []*tPoint
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
r := tRandPoint()
|
r := tRandPoint()
|
||||||
|
@ -150,7 +150,7 @@ func TestPoints(t *testing.T) {
|
||||||
func BenchmarkInsert(t *testing.B) {
|
func BenchmarkInsert(t *testing.B) {
|
||||||
t.StopTimer()
|
t.StopTimer()
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
tr := New()
|
tr := New(nil)
|
||||||
var points []*tPoint
|
var points []*tPoint
|
||||||
for i := 0; i < t.N; i++ {
|
for i := 0; i < t.N; i++ {
|
||||||
points = append(points, tRandPoint())
|
points = append(points, tRandPoint())
|
||||||
|
|
Loading…
Reference in New Issue