added bounds function to rtree

This commit is contained in:
Josh Baker 2016-08-19 08:04:18 -07:00
parent f69153efb0
commit 6e4977ac0f
2 changed files with 32 additions and 2 deletions

View File

@ -163,6 +163,20 @@ func (tr *RTree) RemoveAll() {
tr.root = nil 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 { func countRec(node *nodeT, counter int) int {
if node.isInternalNode() { // not a leaf node if node.isInternalNode() { // not a leaf node
for index := 0; index < node.count; index++ { for index := 0; index < node.count; index++ {

View File

@ -38,7 +38,12 @@ func wp(min, max []float64) *Rect {
MaxY: max[1], MaxY: max[1],
} }
} }
func wpp(x, y float64) *Rect {
return &Rect{
x, y,
x, y,
}
}
func TestA(t *testing.T) { func TestA(t *testing.T) {
tr := New() tr := New()
item1 := wp([]float64{10, 10, 10, 10}, []float64{20, 20, 20, 20}) item1 := wp([]float64{10, 10, 10, 10}, []float64{20, 20, 20, 20})
@ -70,7 +75,18 @@ func TestMemory(t *testing.T) {
runtime.ReadMemStats(&m) runtime.ReadMemStats(&m)
println(int(m.HeapAlloc)/tr.Count(), "bytes/rect") 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) { func BenchmarkInsert(b *testing.B) {
rand.Seed(0) rand.Seed(0)
tr := New() tr := New()