tile38/index/rtree/rtree.go

74 lines
1.9 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package rtree
// Item is an rtree item
type Item interface {
2016-10-03 21:37:16 +03:00
Rect() (minX, minY, minZ, maxX, maxY, maxZ float64)
2016-03-05 02:08:16 +03:00
}
// Rect is a rectangle
type Rect struct {
2016-10-03 21:37:16 +03:00
MinX, MinY, MinZ, MaxX, MaxY, MaxZ float64
2016-03-05 02:08:16 +03:00
}
// Rect returns the rectangle
2016-10-03 21:37:16 +03:00
func (item *Rect) Rect() (minX, minY, minZ, maxX, maxY, maxZ float64) {
return item.MinX, item.MinY, item.MinZ, item.MaxX, item.MaxY, item.MaxZ
2016-03-05 02:08:16 +03:00
}
// RTree is an implementation of an rtree
type RTree struct {
2016-10-03 21:37:16 +03:00
tr *d3RTree
2016-03-05 02:08:16 +03:00
}
// New creates a new RTree
func New() *RTree {
2016-10-03 03:28:23 +03:00
return &RTree{
2016-10-03 21:37:16 +03:00
tr: d3New(),
2016-10-03 03:28:23 +03:00
}
2016-03-05 02:08:16 +03:00
}
// Insert inserts item into rtree
func (tr *RTree) Insert(item Item) {
2016-10-03 21:37:16 +03:00
minX, minY, minZ, maxX, maxY, maxZ := item.Rect()
tr.tr.Insert([3]float64{minX, minY, minZ}, [3]float64{maxX, maxY, maxZ}, item)
2016-03-05 02:08:16 +03:00
}
// Remove removes item from rtree
func (tr *RTree) Remove(item Item) {
2016-10-03 21:37:16 +03:00
minX, minY, minZ, maxX, maxY, maxZ := item.Rect()
tr.tr.Remove([3]float64{minX, minY, minZ}, [3]float64{maxX, maxY, maxZ}, item)
2016-03-05 02:08:16 +03:00
}
// Search finds all items in bounding box.
2016-10-03 21:37:16 +03:00
func (tr *RTree) Search(minX, minY, minZ, maxX, maxY, maxZ float64, iterator func(item Item) bool) {
tr.tr.Search([3]float64{minX, minY, minZ}, [3]float64{maxX, maxY, maxZ}, func(data interface{}) bool {
2016-10-03 03:28:23 +03:00
return iterator(data.(Item))
})
2016-03-05 02:08:16 +03:00
}
// Count return the number of items in rtree.
func (tr *RTree) Count() int {
2016-10-03 03:28:23 +03:00
return tr.tr.Count()
2016-03-05 02:08:16 +03:00
}
// RemoveAll removes all items from rtree.
func (tr *RTree) RemoveAll() {
2016-10-03 03:28:23 +03:00
tr.tr.RemoveAll()
2016-03-05 02:08:16 +03:00
}
2016-10-03 21:37:16 +03:00
func (tr *RTree) Bounds() (minX, minY, minZ, maxX, maxY, maxZ float64) {
var rect d3rectT
2016-10-03 03:28:23 +03:00
if tr.tr.root != nil {
if tr.tr.root.count > 0 {
rect = tr.tr.root.branch[0].rect
for i := 1; i < tr.tr.root.count; i++ {
rect2 := tr.tr.root.branch[i].rect
2016-10-03 21:37:16 +03:00
rect = d3combineRect(&rect, &rect2)
2016-08-19 18:04:18 +03:00
}
}
}
minX, minY, minZ = float64(rect.min[0]), float64(rect.min[1]), float64(rect.min[2])
maxX, maxY, maxZ = float64(rect.max[0]), float64(rect.max[1]), float64(rect.max[2])
2016-08-19 18:04:18 +03:00
return
}