mirror of
https://github.com/knight0zh/aoi.git
synced 2026-02-03 23:55:09 +08:00
优化
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,2 +1,2 @@
|
|||||||
bench:
|
bench:
|
||||||
go test -bench=. -benchmem -count=3
|
go test -bench=. -benchmem
|
||||||
24
aoi.go
24
aoi.go
@@ -1,12 +1,28 @@
|
|||||||
package aoi
|
package aoi
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
type AOI interface {
|
type AOI interface {
|
||||||
Add(entity *Entity) // 添加实体
|
Add(x, y float64, name string) // 添加实体
|
||||||
Delete(entity *Entity) // 移除实体
|
Delete(x, y float64, name string) // 移除实体
|
||||||
Search(entity *Entity) (result []*Entity) // 范围查询
|
Search(x, y float64) (result []string) // 范围查询
|
||||||
}
|
}
|
||||||
|
|
||||||
type Entity struct {
|
type Entity struct {
|
||||||
X, Y float64
|
X, Y float64
|
||||||
Name string
|
Key string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
resultPool sync.Pool
|
||||||
|
entityPool sync.Pool
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
resultPool.New = func() interface{} {
|
||||||
|
return make([]string, 0, 500)
|
||||||
|
}
|
||||||
|
entityPool.New = func() interface{} {
|
||||||
|
return &Entity{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
90
grid.go
90
grid.go
@@ -2,6 +2,12 @@ package aoi
|
|||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
|
var (
|
||||||
|
// 分别将这8个方向的方向向量按顺序写入x, y的分量数组
|
||||||
|
dx = []int{-1, -1, -1, 0, 0, 1, 1, 1}
|
||||||
|
dy = []int{-1, 0, 1, -1, 1, -1, 0, 1}
|
||||||
|
)
|
||||||
|
|
||||||
// Grid 格子
|
// Grid 格子
|
||||||
type Grid struct {
|
type Grid struct {
|
||||||
GID int //格子ID
|
GID int //格子ID
|
||||||
@@ -15,6 +21,7 @@ type GridManger struct {
|
|||||||
AreaWidth int // 格子宽度(长=宽)
|
AreaWidth int // 格子宽度(长=宽)
|
||||||
GridCount int // 格子数量
|
GridCount int // 格子数量
|
||||||
grids map[int]*Grid
|
grids map[int]*Grid
|
||||||
|
pool sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGrid(gid int) *Grid {
|
func NewGrid(gid int) *Grid {
|
||||||
@@ -31,6 +38,9 @@ func NewGridManger(startX, startY, areaWidth, gridCount int) AOI {
|
|||||||
GridCount: gridCount,
|
GridCount: gridCount,
|
||||||
grids: make(map[int]*Grid),
|
grids: make(map[int]*Grid),
|
||||||
}
|
}
|
||||||
|
manager.pool.New = func() interface{} {
|
||||||
|
return make([]*Grid, 0, 9)
|
||||||
|
}
|
||||||
|
|
||||||
for y := 0; y < gridCount; y++ {
|
for y := 0; y < gridCount; y++ {
|
||||||
for x := 0; x < gridCount; x++ {
|
for x := 0; x < gridCount; x++ {
|
||||||
@@ -47,66 +57,76 @@ func (g *GridManger) gridWidth() int {
|
|||||||
return g.AreaWidth / g.GridCount
|
return g.AreaWidth / g.GridCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGIDByPos 通过横纵坐标获取对应的格子ID
|
// getGIDByPos 通过横纵坐标获取对应的格子ID
|
||||||
func (g *GridManger) GetGIDByPos(entity *Entity) int {
|
func (g *GridManger) getGIDByPos(x, y float64) int {
|
||||||
gx := (int(entity.X) - g.StartX) / g.gridWidth()
|
gx := (int(x) - g.StartX) / g.gridWidth()
|
||||||
gy := (int(entity.Y) - g.StartY) / g.gridWidth()
|
gy := (int(y) - g.StartY) / g.gridWidth()
|
||||||
|
|
||||||
return gy*g.GridCount + gx
|
return gy*g.GridCount + gx
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSurroundGrids 根据格子的gID得到当前周边的九宫格信息
|
// getSurroundGrids 根据格子的gID得到当前周边的九宫格信息
|
||||||
func (g *GridManger) GetSurroundGrids(gID int) (grids []*Grid) {
|
func (g *GridManger) getSurroundGrids(gID int) []*Grid {
|
||||||
|
grids := g.pool.Get().([]*Grid)
|
||||||
|
defer func() {
|
||||||
|
grids = grids[:0]
|
||||||
|
g.pool.Put(grids)
|
||||||
|
}()
|
||||||
|
|
||||||
if _, ok := g.grids[gID]; !ok {
|
if _, ok := g.grids[gID]; !ok {
|
||||||
return
|
return grids
|
||||||
}
|
}
|
||||||
grids = append(grids, g.grids[gID])
|
grids = append(grids, g.grids[gID])
|
||||||
|
|
||||||
// 根据gID, 得到格子所在的坐标
|
// 根据gID, 得到格子所在的坐标
|
||||||
x, y := gID%g.GridCount, gID/g.GridCount
|
x, y := gID%g.GridCount, gID/g.GridCount
|
||||||
|
|
||||||
// 分别将这8个方向的方向向量按顺序写入x, y的分量数组
|
|
||||||
dx := []int{-1, -1, -1, 0, 0, 1, 1, 1}
|
|
||||||
dy := []int{-1, 0, 1, -1, 1, -1, 0, 1}
|
|
||||||
|
|
||||||
surroundGID := make([]int, 0)
|
|
||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
newX := x + dx[i]
|
newX := x + dx[i]
|
||||||
newY := y + dy[i]
|
newY := y + dy[i]
|
||||||
|
|
||||||
if newX >= 0 && newX < g.GridCount && newY >= 0 && newY < g.GridCount {
|
if newX >= 0 && newX < g.GridCount && newY >= 0 && newY < g.GridCount {
|
||||||
surroundGID = append(surroundGID, newY*g.GridCount+newX)
|
grids = append(grids, g.grids[newY*g.GridCount+newX])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, gID := range surroundGID {
|
return grids
|
||||||
grids = append(grids, g.grids[gID])
|
}
|
||||||
|
|
||||||
|
func (g *GridManger) Add(x, y float64, key string) {
|
||||||
|
entity := entityPool.Get().(*Entity)
|
||||||
|
entity.X = x
|
||||||
|
entity.Y = y
|
||||||
|
entity.Key = key
|
||||||
|
|
||||||
|
ID := g.getGIDByPos(x, y)
|
||||||
|
grid := g.grids[ID]
|
||||||
|
grid.Entities.Store(key, entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GridManger) Delete(x, y float64, key string) {
|
||||||
|
ID := g.getGIDByPos(x, y)
|
||||||
|
grid := g.grids[ID]
|
||||||
|
|
||||||
|
if entity, ok := grid.Entities.Load(key); ok {
|
||||||
|
grid.Entities.Delete(key)
|
||||||
|
entityPool.Put(entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GridManger) Add(entity *Entity) {
|
func (g *GridManger) Search(x, y float64) []string {
|
||||||
ID := g.GetGIDByPos(entity)
|
result := resultPool.Get().([]string)
|
||||||
grid := g.grids[ID]
|
defer func() {
|
||||||
grid.Entities.Store(entity.Name, entity)
|
result = result[:0]
|
||||||
}
|
resultPool.Put(result)
|
||||||
|
}()
|
||||||
func (g *GridManger) Delete(entity *Entity) {
|
ID := g.getGIDByPos(x, y)
|
||||||
ID := g.GetGIDByPos(entity)
|
grids := g.getSurroundGrids(ID)
|
||||||
grid := g.grids[ID]
|
|
||||||
grid.Entities.Delete(entity.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GridManger) Search(entity *Entity) (result []*Entity) {
|
|
||||||
ID := g.GetGIDByPos(entity)
|
|
||||||
grids := g.GetSurroundGrids(ID)
|
|
||||||
for _, grid := range grids {
|
for _, grid := range grids {
|
||||||
grid.Entities.Range(func(_, value interface{}) bool {
|
grid.Entities.Range(func(_, value interface{}) bool {
|
||||||
result = append(result, value.(*Entity))
|
result = append(result, value.(*Entity).Key)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
88
grid_test.go
88
grid_test.go
@@ -24,44 +24,34 @@ func TestGridManger_GetSurroundGrids(t *testing.T) {
|
|||||||
aol := NewGridManger(0, 0, 250, 5)
|
aol := NewGridManger(0, 0, 250, 5)
|
||||||
manger := aol.(*GridManger)
|
manger := aol.(*GridManger)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
entity *Entity
|
x, y float64
|
||||||
want []int
|
want []int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
entity: &Entity{
|
x: 0, y: 0,
|
||||||
X: 0, Y: 0,
|
|
||||||
},
|
|
||||||
want: []int{0, 1, 5, 6},
|
want: []int{0, 1, 5, 6},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
entity: &Entity{
|
x: 150, y: 0,
|
||||||
X: 150, Y: 0,
|
|
||||||
},
|
|
||||||
want: []int{2, 3, 4, 7, 8, 9},
|
want: []int{2, 3, 4, 7, 8, 9},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
entity: &Entity{
|
x: 50, y: 50,
|
||||||
X: 50, Y: 50,
|
|
||||||
},
|
|
||||||
want: []int{0, 1, 2, 5, 6, 7, 10, 11, 12},
|
want: []int{0, 1, 2, 5, 6, 7, 10, 11, 12},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
entity: &Entity{
|
x: 200, y: 100,
|
||||||
X: 200, Y: 100,
|
|
||||||
},
|
|
||||||
want: []int{8, 9, 13, 14, 18, 19},
|
want: []int{8, 9, 13, 14, 18, 19},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
entity: &Entity{
|
x: 200, y: 200,
|
||||||
X: 200, Y: 200,
|
|
||||||
},
|
|
||||||
want: []int{18, 19, 23, 24},
|
want: []int{18, 19, 23, 24},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
ID := manger.GetGIDByPos(tt.entity)
|
ID := manger.getGIDByPos(tt.x, tt.y)
|
||||||
grids := manger.GetSurroundGrids(ID)
|
grids := manger.getSurroundGrids(ID)
|
||||||
gID := make([]int, 0)
|
gID := make([]int, 0)
|
||||||
for _, grid := range grids {
|
for _, grid := range grids {
|
||||||
gID = append(gID, grid.GID)
|
gID = append(gID, grid.GID)
|
||||||
@@ -76,51 +66,51 @@ func TestNewGridManger(t *testing.T) {
|
|||||||
manger := aol.(*GridManger)
|
manger := aol.(*GridManger)
|
||||||
entities := []*Entity{
|
entities := []*Entity{
|
||||||
{
|
{
|
||||||
X: 0, Y: 0, Name: "a",
|
X: 0, Y: 0, Key: "a",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 50, Y: 0, Name: "b",
|
X: 50, Y: 0, Key: "b",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 100, Y: 0, Name: "c",
|
X: 100, Y: 0, Key: "c",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 50, Y: 0, Name: "d",
|
X: 50, Y: 0, Key: "d",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 50, Y: 50, Name: "e",
|
X: 50, Y: 50, Key: "e",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 50, Y: 100, Name: "f",
|
X: 50, Y: 100, Key: "f",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 100, Y: 0, Name: "g",
|
X: 100, Y: 0, Key: "g",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 100, Y: 50, Name: "h",
|
X: 100, Y: 50, Key: "h",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
X: 100, Y: 100, Name: "i",
|
X: 100, Y: 100, Key: "i",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entity := range entities {
|
for _, entity := range entities {
|
||||||
manger.Add(entity)
|
manger.Add(entity.X, entity.Y, entity.Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
search := manger.Search(&Entity{X: 50, Y: 50})
|
search := manger.Search(50, 50)
|
||||||
result := make([]string, 0)
|
result := make([]string, 0)
|
||||||
for _, entity := range search {
|
for _, entity := range search {
|
||||||
result = append(result, entity.Name)
|
result = append(result, entity)
|
||||||
}
|
}
|
||||||
sort.Strings(result)
|
sort.Strings(result)
|
||||||
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, result)
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, result)
|
||||||
|
|
||||||
manger.Delete(&Entity{X: 100, Y: 100, Name: "i"})
|
manger.Delete(100, 100, "i")
|
||||||
search2 := manger.Search(&Entity{X: 50, Y: 50})
|
search2 := manger.Search(50, 50)
|
||||||
result2 := make([]string, 0)
|
result2 := make([]string, 0)
|
||||||
for _, entity := range search2 {
|
for _, entity := range search2 {
|
||||||
result2 = append(result2, entity.Name)
|
result2 = append(result2, entity)
|
||||||
}
|
}
|
||||||
sort.Strings(result2)
|
sort.Strings(result2)
|
||||||
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f", "g", "h"}, result2)
|
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f", "g", "h"}, result2)
|
||||||
@@ -128,7 +118,7 @@ func TestNewGridManger(t *testing.T) {
|
|||||||
|
|
||||||
func BenchmarkGridManger(b *testing.B) {
|
func BenchmarkGridManger(b *testing.B) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
aol := NewGridManger(0, 0, 256, 16)
|
aol := NewGridManger(0, 0, 1024, 16)
|
||||||
manger := aol.(*GridManger)
|
manger := aol.(*GridManger)
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
@@ -136,28 +126,28 @@ func BenchmarkGridManger(b *testing.B) {
|
|||||||
wg.Add(30000)
|
wg.Add(30000)
|
||||||
for j := 0; j < 10000; j++ {
|
for j := 0; j < 10000; j++ {
|
||||||
go func() {
|
go func() {
|
||||||
manger.Add(&Entity{
|
manger.Add(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Name: fmt.Sprintf("player%d", rand.Intn(50)),
|
fmt.Sprintf("player%d", rand.Intn(100)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
manger.Delete(&Entity{
|
manger.Delete(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Name: fmt.Sprintf("player%d", rand.Intn(50)),
|
fmt.Sprintf("player%d", rand.Intn(100)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
manger.Search(&Entity{
|
manger.Search(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
64
quadtree.go
64
quadtree.go
@@ -9,19 +9,16 @@ const (
|
|||||||
rightDown
|
rightDown
|
||||||
|
|
||||||
maxCap = 500 // 节点最大容量
|
maxCap = 500 // 节点最大容量
|
||||||
maxDeep = 3 // 节点最大深度
|
maxDeep = 4 // 节点最大深度
|
||||||
radius = 16 // 视野半径
|
radius = 16 // 视野半径
|
||||||
)
|
)
|
||||||
|
|
||||||
type QuadOption func(*QuadTree)
|
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
Leaf bool // 是否为叶子节点
|
Leaf bool // 是否为叶子节点
|
||||||
Deep int // 深度
|
Deep int // 深度
|
||||||
AreaWidth float64 // 格子宽度(长=宽)
|
AreaWidth float64 // 格子宽度(长=宽)
|
||||||
XStart float64 // 起始范围
|
XStart float64 // 起始范围
|
||||||
YStart float64 // 起始范围
|
YStart float64 // 起始范围
|
||||||
Parent *Node // 父节点
|
|
||||||
Tree *QuadTree // 树指针
|
Tree *QuadTree // 树指针
|
||||||
Child [4]*Node // 子节点
|
Child [4]*Node // 子节点
|
||||||
Entities *sync.Map // 实体
|
Entities *sync.Map // 实体
|
||||||
@@ -30,6 +27,7 @@ type Node struct {
|
|||||||
type QuadTree struct {
|
type QuadTree struct {
|
||||||
maxCap, maxDeep int
|
maxCap, maxDeep int
|
||||||
radius float64
|
radius float64
|
||||||
|
mPool sync.Pool
|
||||||
*Node
|
*Node
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,9 +38,8 @@ func NewSonNode(xStart, yStart float64, parent *Node) *Node {
|
|||||||
AreaWidth: parent.AreaWidth / 2,
|
AreaWidth: parent.AreaWidth / 2,
|
||||||
XStart: xStart,
|
XStart: xStart,
|
||||||
YStart: yStart,
|
YStart: yStart,
|
||||||
Parent: parent,
|
|
||||||
Tree: parent.Tree,
|
Tree: parent.Tree,
|
||||||
Entities: &sync.Map{},
|
Entities: parent.Tree.mPool.Get().(*sync.Map),
|
||||||
}
|
}
|
||||||
|
|
||||||
return son
|
return son
|
||||||
@@ -99,21 +96,22 @@ func (n *Node) cutNode() {
|
|||||||
n.Child[rightDown] = NewSonNode(n.XStart+half, n.YStart+half, n)
|
n.Child[rightDown] = NewSonNode(n.XStart+half, n.YStart+half, n)
|
||||||
|
|
||||||
// 将实体迁移到对应子节点
|
// 将实体迁移到对应子节点
|
||||||
n.Entities.Range(func(_, v interface{}) bool {
|
n.Entities.Range(func(k, v interface{}) bool {
|
||||||
entity := v.(*Entity)
|
entity := v.(*Entity)
|
||||||
for _, node := range n.Child {
|
for _, node := range n.Child {
|
||||||
if node.intersects(entity.X, entity.Y) {
|
if node.intersects(entity.X, entity.Y) {
|
||||||
node.Entities.Store(entity.Name, entity)
|
node.Entities.Store(entity.Key, entity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
n.Entities.Delete(k)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
// 清空容器
|
n.Tree.mPool.Put(n.Entities)
|
||||||
n.Entities = nil
|
n.Entities = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewQuadTree(xStart, yStart, width float64, opts ...QuadOption) AOI {
|
func NewQuadTree(xStart, yStart, width float64) AOI {
|
||||||
basicNode := &Node{
|
basicNode := &Node{
|
||||||
Leaf: true,
|
Leaf: true,
|
||||||
Deep: 1,
|
Deep: 1,
|
||||||
@@ -121,7 +119,6 @@ func NewQuadTree(xStart, yStart, width float64, opts ...QuadOption) AOI {
|
|||||||
XStart: xStart,
|
XStart: xStart,
|
||||||
YStart: yStart,
|
YStart: yStart,
|
||||||
Child: [4]*Node{},
|
Child: [4]*Node{},
|
||||||
Entities: &sync.Map{},
|
|
||||||
}
|
}
|
||||||
tree := &QuadTree{
|
tree := &QuadTree{
|
||||||
maxDeep: maxDeep,
|
maxDeep: maxDeep,
|
||||||
@@ -129,12 +126,15 @@ func NewQuadTree(xStart, yStart, width float64, opts ...QuadOption) AOI {
|
|||||||
radius: radius,
|
radius: radius,
|
||||||
Node: basicNode,
|
Node: basicNode,
|
||||||
}
|
}
|
||||||
|
tree.mPool.New = func() interface{} {
|
||||||
|
return &sync.Map{}
|
||||||
|
}
|
||||||
basicNode.Tree = tree
|
basicNode.Tree = tree
|
||||||
|
basicNode.Entities = tree.mPool.Get().(*sync.Map)
|
||||||
return tree
|
return tree
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Add(entity *Entity) {
|
func (n *Node) Add(x, y float64, name string) {
|
||||||
// 判断是否需要分割
|
// 判断是否需要分割
|
||||||
if n.Leaf && n.needCut() {
|
if n.Leaf && n.needCut() {
|
||||||
n.cutNode()
|
n.cutNode()
|
||||||
@@ -142,39 +142,57 @@ func (n *Node) Add(entity *Entity) {
|
|||||||
|
|
||||||
// 非叶子节点往下递归
|
// 非叶子节点往下递归
|
||||||
if !n.Leaf {
|
if !n.Leaf {
|
||||||
n.Child[n.findSonQuadrant(entity.X, entity.Y)].Add(entity)
|
n.Child[n.findSonQuadrant(x, y)].Add(x, y, name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entity := entityPool.Get().(*Entity)
|
||||||
|
entity.X = x
|
||||||
|
entity.Y = y
|
||||||
|
entity.Key = name
|
||||||
|
|
||||||
// 叶子节点进行存储
|
// 叶子节点进行存储
|
||||||
n.Entities.Store(entity.Name, entity)
|
n.Entities.Store(entity.Key, entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Delete(entity *Entity) {
|
func (n *Node) Delete(x, y float64, name string) {
|
||||||
if !n.Leaf {
|
if !n.Leaf {
|
||||||
n.Child[n.findSonQuadrant(entity.X, entity.Y)].Delete(entity)
|
n.Child[n.findSonQuadrant(x, y)].Delete(x, y, name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Entities.Delete(entity.Name)
|
if entity, ok := n.Entities.Load(name); ok {
|
||||||
|
n.Entities.Delete(name)
|
||||||
|
entityPool.Put(entity)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) Search(entity *Entity) (result []*Entity) {
|
func (n *Node) Search(x, y float64) []string {
|
||||||
|
result := resultPool.Get().([]string)
|
||||||
|
defer func() {
|
||||||
|
result = result[:0]
|
||||||
|
resultPool.Put(result)
|
||||||
|
}()
|
||||||
|
n.search(x, y, &result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node) search(x, y float64, result *[]string) {
|
||||||
if !n.Leaf {
|
if !n.Leaf {
|
||||||
minX, maxX := entity.X-n.Tree.radius, entity.X+n.Tree.radius
|
minX, maxX := x-n.Tree.radius, x+n.Tree.radius
|
||||||
minY, maxY := entity.Y-n.Tree.radius, entity.Y+n.Tree.radius
|
minY, maxY := y-n.Tree.radius, y+n.Tree.radius
|
||||||
|
|
||||||
for _, son := range n.Child {
|
for _, son := range n.Child {
|
||||||
if son.intersects(minX, minY) || son.intersects(maxX, minY) ||
|
if son.intersects(minX, minY) || son.intersects(maxX, minY) ||
|
||||||
son.intersects(minX, maxY) || son.intersects(maxX, maxY) {
|
son.intersects(minX, maxY) || son.intersects(maxX, maxY) {
|
||||||
result = append(result, son.Search(entity)...)
|
son.search(x, y, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Entities.Range(func(key, value interface{}) bool {
|
n.Entities.Range(func(key, value interface{}) bool {
|
||||||
result = append(result, value.(*Entity))
|
*result = append(*result, value.(*Entity).Key)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
|||||||
105
quadtree_test.go
105
quadtree_test.go
@@ -68,20 +68,10 @@ func Test_NeedCut(t *testing.T) {
|
|||||||
|
|
||||||
tree.maxCap = 2 // 超过两人节点分裂
|
tree.maxCap = 2 // 超过两人节点分裂
|
||||||
assert.Equal(t, false, tree.needCut())
|
assert.Equal(t, false, tree.needCut())
|
||||||
player1 := &Entity{
|
tree.Add(60.9, 24.9, "player1")
|
||||||
X: 60.9,
|
|
||||||
Y: 24.9,
|
|
||||||
Name: "player1",
|
|
||||||
}
|
|
||||||
tree.Add(player1)
|
|
||||||
|
|
||||||
assert.Equal(t, false, tree.needCut())
|
assert.Equal(t, false, tree.needCut())
|
||||||
player2 := &Entity{
|
tree.Add(25, 25, "player2")
|
||||||
X: 25,
|
|
||||||
Y: 25,
|
|
||||||
Name: "player2",
|
|
||||||
}
|
|
||||||
tree.Add(player2)
|
|
||||||
|
|
||||||
assert.Equal(t, true, tree.needCut())
|
assert.Equal(t, true, tree.needCut())
|
||||||
}
|
}
|
||||||
@@ -91,63 +81,50 @@ func TestNode_Search(t *testing.T) {
|
|||||||
tree := aoi.(*QuadTree)
|
tree := aoi.(*QuadTree)
|
||||||
tree.maxCap = 2 // 超过两人节点分裂
|
tree.maxCap = 2 // 超过两人节点分裂
|
||||||
tree.radius = 5
|
tree.radius = 5
|
||||||
player1 := &Entity{
|
|
||||||
X: 60.9,
|
|
||||||
Y: 24.9,
|
|
||||||
Name: "player1",
|
|
||||||
}
|
|
||||||
tree.Add(player1)
|
|
||||||
|
|
||||||
player2 := &Entity{
|
tree.Add(60.9, 24.9, "player1")
|
||||||
X: 25,
|
tree.Add(25, 25, "player2")
|
||||||
Y: 25,
|
|
||||||
Name: "player2",
|
// 查询player1附近
|
||||||
}
|
entities := tree.Search(60.9, 24.9)
|
||||||
tree.Add(player2)
|
|
||||||
entities := tree.Search(player1)
|
|
||||||
assert.Equal(t, 2, len(entities), "player1 player2")
|
assert.Equal(t, 2, len(entities), "player1 player2")
|
||||||
|
|
||||||
// 当出现第三个玩家超过节点最大容量产生分裂
|
// 当出现第三个玩家超过节点最大容量产生分裂
|
||||||
player3 := &Entity{
|
tree.Add(99, 24, "player3")
|
||||||
X: 99,
|
|
||||||
Y: 24,
|
// 查询player1附近
|
||||||
Name: "player3",
|
entities = tree.Search(60.9, 24.9)
|
||||||
}
|
|
||||||
tree.Add(player3)
|
|
||||||
entities = tree.Search(player1)
|
|
||||||
assert.Equal(t, 2, len(entities), "player1 player3")
|
assert.Equal(t, 2, len(entities), "player1 player3")
|
||||||
|
|
||||||
// 添加第四个玩家
|
// 添加第四个玩家
|
||||||
player4 := &Entity{
|
tree.Add(72, 23, "player4")
|
||||||
X: 72,
|
|
||||||
Y: 23,
|
// 查询player1附近
|
||||||
Name: "player4",
|
entities = tree.Search(60.9, 24.9)
|
||||||
}
|
|
||||||
tree.Add(player4)
|
|
||||||
entities = tree.Search(player1)
|
|
||||||
assert.Equal(t, 2, len(entities), "player1 player4")
|
assert.Equal(t, 2, len(entities), "player1 player4")
|
||||||
|
|
||||||
entities = tree.Search(player2)
|
// 查询player2附近
|
||||||
|
entities = tree.Search(25, 25)
|
||||||
assert.Equal(t, 1, len(entities), "player2")
|
assert.Equal(t, 1, len(entities), "player2")
|
||||||
|
|
||||||
// 添加第五个玩家
|
// 添加第五个玩家
|
||||||
player5 := &Entity{
|
tree.Add(49.9, 49.9, "player5")
|
||||||
X: 49.9,
|
|
||||||
Y: 49.9,
|
// 查询player2附近
|
||||||
Name: "player5",
|
entities = tree.Search(25, 25)
|
||||||
}
|
|
||||||
tree.Add(player5)
|
|
||||||
entities = tree.Search(player2)
|
|
||||||
assert.Equal(t, 2, len(entities), "player2 player5")
|
assert.Equal(t, 2, len(entities), "player2 player5")
|
||||||
|
|
||||||
tree.Delete(player5)
|
// 移除player5
|
||||||
entities = tree.Search(player2)
|
tree.Delete(49.9, 49.9, "player5")
|
||||||
|
|
||||||
|
// 查询player2附近
|
||||||
|
entities = tree.Search(25, 25)
|
||||||
assert.Equal(t, 1, len(entities), "player2")
|
assert.Equal(t, 1, len(entities), "player2")
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkQuadtree(b *testing.B) {
|
func BenchmarkQuadtree(b *testing.B) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
aoi := NewQuadTree(0, 0, 100)
|
aoi := NewQuadTree(0, 0, 1024)
|
||||||
tree := aoi.(*QuadTree)
|
tree := aoi.(*QuadTree)
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
@@ -155,28 +132,28 @@ func BenchmarkQuadtree(b *testing.B) {
|
|||||||
wg.Add(30000)
|
wg.Add(30000)
|
||||||
for j := 0; j < 10000; j++ {
|
for j := 0; j < 10000; j++ {
|
||||||
go func() {
|
go func() {
|
||||||
tree.Add(&Entity{
|
tree.Add(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Name: fmt.Sprintf("player%d", rand.Intn(50)),
|
fmt.Sprintf("player%d", rand.Intn(100)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
tree.Delete(&Entity{
|
tree.Delete(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Name: fmt.Sprintf("player%d", rand.Intn(50)),
|
fmt.Sprintf("player%d", rand.Intn(100)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
tree.Search(&Entity{
|
tree.Search(
|
||||||
X: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
Y: float64(rand.Intn(5) * 10),
|
float64(rand.Intn(10)*10+rand.Intn(10)),
|
||||||
})
|
)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user