Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unneeeded allocation in computeBoundingBox #43

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,3 @@ func boundingBox(r1, r2 Rect) (bb Rect) {
}
return
}

// boundingBoxN constructs the smallest rectangle containing all of r...
func boundingBoxN(rects ...Rect) (bb Rect) {
if len(rects) == 1 {
bb = rects[0]
return
}
bb = boundingBox(rects[0], rects[1])
for _, rect := range rects[2:] {
bb = boundingBox(bb, rect)
}
return
}
14 changes: 0 additions & 14 deletions geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,20 +336,6 @@ func TestBoundingBoxContains(t *testing.T) {
}
}

func TestBoundingBoxN(t *testing.T) {
rect1, _ := NewRect(Point{0, 0}, []float64{1, 1})
rect2, _ := NewRect(Point{0, 1}, []float64{1, 1})
rect3, _ := NewRect(Point{1, 0}, []float64{1, 1})

exp, _ := NewRect(Point{0, 0}, []float64{2, 2})
bb := boundingBoxN(rect1, rect2, rect3)
d1 := bb.p.dist(exp.p)
d2 := bb.q.dist(exp.q)
if d1 > EPS || d2 > EPS {
t.Errorf("boundingBoxN(%v, %v, %v) != %v, got %v", rect1, rect2, rect3, exp, bb)
}
}

func TestMinDistZero(t *testing.T) {
p := Point{1, 2, 3}
r := p.ToRect(1)
Expand Down
12 changes: 8 additions & 4 deletions rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,15 @@ func (n *node) getEntry() *entry {

// computeBoundingBox finds the MBR of the children of n.
func (n *node) computeBoundingBox() (bb Rect) {
childBoxes := make([]Rect, len(n.entries))
for i, e := range n.entries {
childBoxes[i] = e.bb
if len(n.entries) == 1 {
bb = n.entries[0].bb
return
}

bb = boundingBox(n.entries[0].bb, n.entries[1].bb)
for _, e := range n.entries[2:] {
bb = boundingBox(bb, e.bb)
}
bb = boundingBoxN(childBoxes...)
return
}

Expand Down
18 changes: 18 additions & 0 deletions rtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,24 @@ func TestNearestNeighbor(t *testing.T) {
}
}

func TestComputeBoundingBox(t *testing.T) {
rect1, _ := NewRect(Point{0, 0}, []float64{1, 1})
rect2, _ := NewRect(Point{0, 1}, []float64{1, 1})
rect3, _ := NewRect(Point{1, 0}, []float64{1, 1})
n := &node{}
n.entries = append(n.entries, entry{bb: rect1})
n.entries = append(n.entries, entry{bb: rect2})
n.entries = append(n.entries, entry{bb: rect3})

exp, _ := NewRect(Point{0, 0}, []float64{2, 2})
bb := n.computeBoundingBox()
d1 := bb.p.dist(exp.p)
d2 := bb.q.dist(exp.q)
if d1 > EPS || d2 > EPS {
t.Errorf("boundingBoxN(%v, %v, %v) != %v, got %v", rect1, rect2, rect3, exp, bb)
}
}

func TestGetAllBoundingBoxes(t *testing.T) {
rt1 := NewTree(2, 3, 3)
rt2 := NewTree(2, 2, 4)
Expand Down