From 6e4977ac0f59804bba2d55af04918e2e15118383 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Fri, 19 Aug 2016 08:04:18 -0700 Subject: [PATCH] added bounds function to rtree --- index/rtree/rtree.go | 14 ++++++++++++++ index/rtree/rtree_test.go | 20 ++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index/rtree/rtree.go b/index/rtree/rtree.go index 5310ff95..590165a7 100644 --- a/index/rtree/rtree.go +++ b/index/rtree/rtree.go @@ -163,6 +163,20 @@ func (tr *RTree) RemoveAll() { tr.root = nil } +func (tr *RTree) Bounds() (minX, minY, maxX, maxY float64) { + var rect rectT + if tr.root != nil { + if tr.root.count > 0 { + rect = tr.root.branch[0].rect + for i := 1; i < tr.root.count; i++ { + rect = combineRect(rect, tr.root.branch[i].rect) + } + } + } + minX, minY, maxX, maxY = rect.min[0], rect.min[1], rect.max[0], rect.max[1] + return +} + func countRec(node *nodeT, counter int) int { if node.isInternalNode() { // not a leaf node for index := 0; index < node.count; index++ { diff --git a/index/rtree/rtree_test.go b/index/rtree/rtree_test.go index 895c9389..a8c4a900 100644 --- a/index/rtree/rtree_test.go +++ b/index/rtree/rtree_test.go @@ -38,7 +38,12 @@ func wp(min, max []float64) *Rect { MaxY: max[1], } } - +func wpp(x, y float64) *Rect { + return &Rect{ + x, y, + x, y, + } +} func TestA(t *testing.T) { tr := New() item1 := wp([]float64{10, 10, 10, 10}, []float64{20, 20, 20, 20}) @@ -70,7 +75,18 @@ func TestMemory(t *testing.T) { runtime.ReadMemStats(&m) println(int(m.HeapAlloc)/tr.Count(), "bytes/rect") } - +func TestBounds(t *testing.T) { + tr := New() + tr.Insert(wpp(10, 10)) + tr.Insert(wpp(10, 20)) + tr.Insert(wpp(10, 30)) + tr.Insert(wpp(20, 10)) + tr.Insert(wpp(30, 10)) + minX, minY, maxX, maxY := tr.Bounds() + if minX != 10 || minY != 10 || maxX != 30 || maxY != 30 { + t.Fatalf("expected 10,10 30,30, got %v,%v %v,%v\n", minX, minY, maxX, maxY) + } +} func BenchmarkInsert(b *testing.B) { rand.Seed(0) tr := New()