forked from mirror/rtred
point tests
This commit is contained in:
parent
a2f2655468
commit
442f9f0d8e
|
@ -74,7 +74,6 @@ func init() {
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
root *Node ///< Root of tree
|
root *Node ///< Root of tree
|
||||||
unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal bounding rectangle (n-dimensional)
|
/// Minimal bounding rectangle (n-dimensional)
|
||||||
|
@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) {
|
||||||
} //_DEBUG
|
} //_DEBUG
|
||||||
var branch Branch
|
var branch Branch
|
||||||
branch.data = dataId
|
branch.data = dataId
|
||||||
branch.child = nil
|
|
||||||
for axis := 0; axis < NUMDIMS; axis++ {
|
for axis := 0; axis < NUMDIMS; axis++ {
|
||||||
branch.rect.min[axis] = min[axis]
|
branch.rect.min[axis] = min[axis]
|
||||||
branch.rect.max[axis] = max[axis]
|
branch.rect.max[axis] = max[axis]
|
||||||
|
@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) {
|
||||||
// Remove element by swapping with the last element to prevent gaps in array
|
// Remove element by swapping with the last element to prevent gaps in array
|
||||||
node.branch[index] = node.branch[node.count-1]
|
node.branch[index] = node.branch[node.count-1]
|
||||||
node.branch[node.count-1].data = nil
|
node.branch[node.count-1].data = nil
|
||||||
|
node.branch[node.count-1].child = nil
|
||||||
node.count--
|
node.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,14 +52,14 @@ func tRandPoint() *tPoint {
|
||||||
}
|
}
|
||||||
func TestRTree(t *testing.T) {
|
func TestRTree(t *testing.T) {
|
||||||
tr := New()
|
tr := New()
|
||||||
|
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})
|
||||||
tr.Insert(&tRect{10, 10, 20, 20})
|
tr.Insert(&tRect{10, 10, 20, 20})
|
||||||
tr.Insert(&tRect{10, 20})
|
tr.Insert(&tRect{10, 20})
|
||||||
|
tr.Insert(zeroPoint)
|
||||||
if tr.Count() != 4 {
|
if tr.Count() != 5 {
|
||||||
t.Fatalf("expecting %v, got %v", 4, tr.Count())
|
t.Fatalf("expecting %v, got %v", 5, tr.Count())
|
||||||
}
|
}
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
|
@ -68,6 +68,15 @@ func TestRTree(t *testing.T) {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if count != 3 {
|
||||||
|
t.Fatalf("expecting %v, got %v", 3, count)
|
||||||
|
}
|
||||||
|
tr.Remove(zeroPoint)
|
||||||
|
count = 0
|
||||||
|
tr.Search(&tRect{0, 0, 0, 100, 100, 5}, func(item Item) bool {
|
||||||
|
count++
|
||||||
|
return true
|
||||||
|
})
|
||||||
if count != 2 {
|
if count != 2 {
|
||||||
t.Fatalf("expecting %v, got %v", 2, count)
|
t.Fatalf("expecting %v, got %v", 2, count)
|
||||||
}
|
}
|
||||||
|
@ -100,8 +109,8 @@ func TestInsertDelete(t *testing.T) {
|
||||||
if p < .23 || p > .27 {
|
if p < .23 || p > .27 {
|
||||||
t.Fatalf("bad random range, expected between 0.24-0.26, got %v", p)
|
t.Fatalf("bad random range, expected between 0.24-0.26, got %v", p)
|
||||||
}
|
}
|
||||||
for _, r := range r2arr {
|
for _, i := range rand.Perm(len(r2arr)) {
|
||||||
tr.Remove(r)
|
tr.Remove(r2arr[i])
|
||||||
}
|
}
|
||||||
total := tr.Count() + count
|
total := tr.Count() + count
|
||||||
if total != n {
|
if total != n {
|
||||||
|
@ -110,7 +119,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 := 100000
|
n := 25000
|
||||||
tr := New()
|
tr := New()
|
||||||
var points []*tPoint
|
var points []*tPoint
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
|
@ -130,11 +139,31 @@ func TestPoints(t *testing.T) {
|
||||||
if count != n {
|
if count != n {
|
||||||
t.Fatalf("expecting %v, got %v", n, count)
|
t.Fatalf("expecting %v, got %v", n, count)
|
||||||
}
|
}
|
||||||
for _, p := range points {
|
for _, i := range rand.Perm(len(points)) {
|
||||||
tr.Remove(p)
|
tr.Remove(points[i])
|
||||||
}
|
}
|
||||||
total := tr.Count() + count
|
total := tr.Count() + count
|
||||||
if total != n {
|
if total != n {
|
||||||
t.Fatalf("expected %v, got %v", n, total)
|
t.Fatalf("expected %v, got %v", n, total)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func BenchmarkInsert(t *testing.B) {
|
||||||
|
t.StopTimer()
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
tr := New()
|
||||||
|
var points []*tPoint
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
points = append(points, tRandPoint())
|
||||||
|
}
|
||||||
|
t.StartTimer()
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
tr.Insert(points[i])
|
||||||
|
}
|
||||||
|
t.StopTimer()
|
||||||
|
count := tr.Count()
|
||||||
|
if count != t.N {
|
||||||
|
t.Fatalf("expected %v, got %v", t.N, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.StartTimer()
|
||||||
|
}
|
||||||
|
|
|
@ -74,7 +74,6 @@ func init() {
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
root *Node ///< Root of tree
|
root *Node ///< Root of tree
|
||||||
unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal bounding rectangle (n-dimensional)
|
/// Minimal bounding rectangle (n-dimensional)
|
||||||
|
@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) {
|
||||||
} //_DEBUG
|
} //_DEBUG
|
||||||
var branch Branch
|
var branch Branch
|
||||||
branch.data = dataId
|
branch.data = dataId
|
||||||
branch.child = nil
|
|
||||||
for axis := 0; axis < NUMDIMS; axis++ {
|
for axis := 0; axis < NUMDIMS; axis++ {
|
||||||
branch.rect.min[axis] = min[axis]
|
branch.rect.min[axis] = min[axis]
|
||||||
branch.rect.max[axis] = max[axis]
|
branch.rect.max[axis] = max[axis]
|
||||||
|
@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) {
|
||||||
// Remove element by swapping with the last element to prevent gaps in array
|
// Remove element by swapping with the last element to prevent gaps in array
|
||||||
node.branch[index] = node.branch[node.count-1]
|
node.branch[index] = node.branch[node.count-1]
|
||||||
node.branch[node.count-1].data = nil
|
node.branch[node.count-1].data = nil
|
||||||
|
node.branch[node.count-1].child = nil
|
||||||
node.count--
|
node.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,6 @@ func init() {
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
root *Node ///< Root of tree
|
root *Node ///< Root of tree
|
||||||
unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal bounding rectangle (n-dimensional)
|
/// Minimal bounding rectangle (n-dimensional)
|
||||||
|
@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) {
|
||||||
} //_DEBUG
|
} //_DEBUG
|
||||||
var branch Branch
|
var branch Branch
|
||||||
branch.data = dataId
|
branch.data = dataId
|
||||||
branch.child = nil
|
|
||||||
for axis := 0; axis < NUMDIMS; axis++ {
|
for axis := 0; axis < NUMDIMS; axis++ {
|
||||||
branch.rect.min[axis] = min[axis]
|
branch.rect.min[axis] = min[axis]
|
||||||
branch.rect.max[axis] = max[axis]
|
branch.rect.max[axis] = max[axis]
|
||||||
|
@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) {
|
||||||
// Remove element by swapping with the last element to prevent gaps in array
|
// Remove element by swapping with the last element to prevent gaps in array
|
||||||
node.branch[index] = node.branch[node.count-1]
|
node.branch[index] = node.branch[node.count-1]
|
||||||
node.branch[node.count-1].data = nil
|
node.branch[node.count-1].data = nil
|
||||||
|
node.branch[node.count-1].child = nil
|
||||||
node.count--
|
node.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,6 @@ func init() {
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
root *Node ///< Root of tree
|
root *Node ///< Root of tree
|
||||||
unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal bounding rectangle (n-dimensional)
|
/// Minimal bounding rectangle (n-dimensional)
|
||||||
|
@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) {
|
||||||
} //_DEBUG
|
} //_DEBUG
|
||||||
var branch Branch
|
var branch Branch
|
||||||
branch.data = dataId
|
branch.data = dataId
|
||||||
branch.child = nil
|
|
||||||
for axis := 0; axis < NUMDIMS; axis++ {
|
for axis := 0; axis < NUMDIMS; axis++ {
|
||||||
branch.rect.min[axis] = min[axis]
|
branch.rect.min[axis] = min[axis]
|
||||||
branch.rect.max[axis] = max[axis]
|
branch.rect.max[axis] = max[axis]
|
||||||
|
@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) {
|
||||||
// Remove element by swapping with the last element to prevent gaps in array
|
// Remove element by swapping with the last element to prevent gaps in array
|
||||||
node.branch[index] = node.branch[node.count-1]
|
node.branch[index] = node.branch[node.count-1]
|
||||||
node.branch[node.count-1].data = nil
|
node.branch[node.count-1].data = nil
|
||||||
|
node.branch[node.count-1].child = nil
|
||||||
node.count--
|
node.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,6 @@ func init() {
|
||||||
|
|
||||||
type RTree struct {
|
type RTree struct {
|
||||||
root *Node ///< Root of tree
|
root *Node ///< Root of tree
|
||||||
unitSphereVolume float64 ///< Unit sphere constant for required number of dimensions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal bounding rectangle (n-dimensional)
|
/// Minimal bounding rectangle (n-dimensional)
|
||||||
|
@ -154,7 +153,6 @@ func (tr *RTree) Insert(min, max [NUMDIMS]float64, dataId interface{}) {
|
||||||
} //_DEBUG
|
} //_DEBUG
|
||||||
var branch Branch
|
var branch Branch
|
||||||
branch.data = dataId
|
branch.data = dataId
|
||||||
branch.child = nil
|
|
||||||
for axis := 0; axis < NUMDIMS; axis++ {
|
for axis := 0; axis < NUMDIMS; axis++ {
|
||||||
branch.rect.min[axis] = min[axis]
|
branch.rect.min[axis] = min[axis]
|
||||||
branch.rect.max[axis] = max[axis]
|
branch.rect.max[axis] = max[axis]
|
||||||
|
@ -379,6 +377,7 @@ func DisconnectBranch(node *Node, index int) {
|
||||||
// Remove element by swapping with the last element to prevent gaps in array
|
// Remove element by swapping with the last element to prevent gaps in array
|
||||||
node.branch[index] = node.branch[node.count-1]
|
node.branch[index] = node.branch[node.count-1]
|
||||||
node.branch[node.count-1].data = nil
|
node.branch[node.count-1].data = nil
|
||||||
|
node.branch[node.count-1].child = nil
|
||||||
node.count--
|
node.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue