| 1 |
package base |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"math" |
| 7 |
"math/rand" |
| 8 |
"reflect" |
| 9 |
"runtime" |
| 10 |
"sort" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
const D = 2 |
| 16 |
const M = 13 |
| 17 |
|
| 18 |
type Rect struct { |
| 19 |
min, max []float64 |
| 20 |
item interface{} |
| 21 |
} |
| 22 |
|
| 23 |
func (r *Rect) equals(r2 Rect) bool { |
| 24 |
if len(r.min) != len(r2.min) || len(r.max) != len(r2.max) || r.item != r2.item { |
| 25 |
return false |
| 26 |
} |
| 27 |
for i := 0; i < len(r.min); i++ { |
| 28 |
if r.min[i] != r2.min[i] { |
| 29 |
return false |
| 30 |
} |
| 31 |
} |
| 32 |
for i := 0; i < len(r.max); i++ { |
| 33 |
if r.max[i] != r2.max[i] { |
| 34 |
return false |
| 35 |
} |
| 36 |
} |
| 37 |
return true |
| 38 |
} |
| 39 |
|
| 40 |
func ptrMakePoint(vals ...float64) *Rect { |
| 41 |
var r Rect |
| 42 |
r.min = make([]float64, D) |
| 43 |
r.max = make([]float64, D) |
| 44 |
for i := 0; i < D && i < len(vals); i++ { |
| 45 |
r.min[i] = vals[i] |
| 46 |
r.max[i] = vals[i] |
| 47 |
} |
| 48 |
r.item = &r |
| 49 |
return &r |
| 50 |
} |
| 51 |
|
| 52 |
func ptrMakeRect(vals ...float64) *Rect { |
| 53 |
var r Rect |
| 54 |
r.min = make([]float64, D) |
| 55 |
r.max = make([]float64, D) |
| 56 |
for i := 0; i < D && i < len(vals); i++ { |
| 57 |
r.min[i] = vals[i] |
| 58 |
r.max[i] = vals[i+D] |
| 59 |
} |
| 60 |
r.item = &r |
| 61 |
return &r |
| 62 |
} |
| 63 |
|
| 64 |
func TestRTree(t *testing.T) { |
| 65 |
tr := New(D, M) |
| 66 |
p := ptrMakePoint(10, 10) |
| 67 |
tr.Insert(p.min, p.max, p.item) |
| 68 |
} |
| 69 |
|
| 70 |
func TestPtrBasic2D(t *testing.T) { |
| 71 |
if D != 2 { |
| 72 |
return |
| 73 |
} |
| 74 |
tr := New(D, M) |
| 75 |
p1 := ptrMakePoint(-115, 33) |
| 76 |
p2 := ptrMakePoint(-113, 35) |
| 77 |
tr.Insert(p1.min, p1.max, p1.item) |
| 78 |
tr.Insert(p2.min, p2.max, p2.item) |
| 79 |
assertEqual(t, 2, tr.Count()) |
| 80 |
|
| 81 |
var points []*Rect |
| 82 |
bbox := ptrMakeRect(-116, 32, -114, 34) |
| 83 |
tr.Search(bbox.min, bbox.max, func(item interface{}) bool { |
| 84 |
points = append(points, item.(*Rect)) |
| 85 |
return true |
| 86 |
}) |
| 87 |
assertEqual(t, 1, len(points)) |
| 88 |
tr.Remove(p1.min, p1.max, p1.item) |
| 89 |
assertEqual(t, 1, tr.Count()) |
| 90 |
|
| 91 |
points = nil |
| 92 |
bbox = ptrMakeRect(-116, 33, -114, 34) |
| 93 |
tr.Search(bbox.min, bbox.max, func(item interface{}) bool { |
| 94 |
points = append(points, item.(*Rect)) |
| 95 |
return true |
| 96 |
}) |
| 97 |
assertEqual(t, 0, len(points)) |
| 98 |
tr.Remove(p2.min, p2.max, p2.item) |
| 99 |
assertEqual(t, 0, tr.Count()) |
| 100 |
} |
| 101 |
|
| 102 |
func getMemStats() runtime.MemStats { |
| 103 |
runtime.GC() |
| 104 |
time.Sleep(time.Millisecond) |
| 105 |
runtime.GC() |
| 106 |
var ms runtime.MemStats |
| 107 |
runtime.ReadMemStats(&ms) |
| 108 |
return ms |
| 109 |
} |
| 110 |
|
| 111 |
func ptrMakeRandom(what string) *Rect { |
| 112 |
if what == "point" { |
| 113 |
vals := make([]float64, D) |
| 114 |
for i := 0; i < D; i++ { |
| 115 |
if i == 0 { |
| 116 |
vals[i] = rand.Float64()*360 - 180 |
| 117 |
} else if i == 1 { |
| 118 |
vals[i] = rand.Float64()*180 - 90 |
| 119 |
} else { |
| 120 |
vals[i] = rand.Float64()*100 - 50 |
| 121 |
} |
| 122 |
} |
| 123 |
return ptrMakePoint(vals...) |
| 124 |
} else if what == "rect" { |
| 125 |
vals := make([]float64, D) |
| 126 |
for i := 0; i < D; i++ { |
| 127 |
if i == 0 { |
| 128 |
vals[i] = rand.Float64()*340 - 170 |
| 129 |
} else if i == 1 { |
| 130 |
vals[i] = rand.Float64()*160 - 80 |
| 131 |
} else { |
| 132 |
vals[i] = rand.Float64()*80 - 30 |
| 133 |
} |
| 134 |
} |
| 135 |
rvals := make([]float64, D*2) |
| 136 |
for i := 0; i < D; i++ { |
| 137 |
rvals[i] = vals[i] - rand.Float64()*10 |
| 138 |
rvals[D+i] = vals[i] + rand.Float64()*10 |
| 139 |
} |
| 140 |
return ptrMakeRect(rvals...) |
| 141 |
} |
| 142 |
panic("??") |
| 143 |
} |
| 144 |
|
| 145 |
func TestPtrRandom(t *testing.T) { |
| 146 |
t.Run(fmt.Sprintf("%dD", D), func(t *testing.T) { |
| 147 |
t.Run("point", func(t *testing.T) { ptrTestRandom(t, "point", 10000) }) |
| 148 |
t.Run("rect", func(t *testing.T) { ptrTestRandom(t, "rect", 10000) }) |
| 149 |
}) |
| 150 |
} |
| 151 |
|
| 152 |
func ptrTestRandom(t *testing.T, which string, n int) { |
| 153 |
fmt.Println("-------------------------------------------------") |
| 154 |
fmt.Printf("Testing Random %dD %ss\n", D, which) |
| 155 |
fmt.Println("-------------------------------------------------") |
| 156 |
rand.Seed(time.Now().UnixNano()) |
| 157 |
tr := New(D, M) |
| 158 |
min, max := tr.Bounds() |
| 159 |
assertEqual(t, make([]float64, D), min[:]) |
| 160 |
assertEqual(t, make([]float64, D), max[:]) |
| 161 |
|
| 162 |
|
| 163 |
m1 := getMemStats() |
| 164 |
objs := make([]*Rect, n) |
| 165 |
for i := 0; i < n; i++ { |
| 166 |
objs[i] = ptrMakeRandom(which) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
m2 := getMemStats() |
| 171 |
start := time.Now() |
| 172 |
for _, r := range objs { |
| 173 |
tr.Insert(r.min, r.max, r.item) |
| 174 |
} |
| 175 |
durInsert := time.Since(start) |
| 176 |
m3 := getMemStats() |
| 177 |
assertEqual(t, len(objs), tr.Count()) |
| 178 |
fmt.Printf("Inserted %d random %ss in %dms -- %d ops/sec\n", |
| 179 |
len(objs), which, int(durInsert.Seconds()*1000), |
| 180 |
int(float64(len(objs))/durInsert.Seconds())) |
| 181 |
fmt.Printf(" total cost is %d bytes/%s\n", int(m3.HeapAlloc-m1.HeapAlloc)/len(objs), which) |
| 182 |
fmt.Printf(" tree cost is %d bytes/%s\n", int(m3.HeapAlloc-m2.HeapAlloc)/len(objs), which) |
| 183 |
fmt.Printf(" tree overhead %d%%\n", int((float64(m3.HeapAlloc-m2.HeapAlloc)/float64(len(objs)))/(float64(m3.HeapAlloc-m1.HeapAlloc)/float64(len(objs)))*100)) |
| 184 |
fmt.Printf(" complexity %f\n", tr.Complexity()) |
| 185 |
|
| 186 |
start = time.Now() |
| 187 |
|
| 188 |
var nodes int |
| 189 |
var leaves int |
| 190 |
var maxLevel int |
| 191 |
tr.Traverse(func(min, max []float64, level int, item interface{}) bool { |
| 192 |
if level != 0 { |
| 193 |
nodes++ |
| 194 |
} |
| 195 |
if level == 1 { |
| 196 |
leaves++ |
| 197 |
} |
| 198 |
if level > maxLevel { |
| 199 |
maxLevel = level |
| 200 |
} |
| 201 |
return true |
| 202 |
}) |
| 203 |
fmt.Printf(" nodes: %d, leaves: %d, level: %d\n", nodes, leaves, maxLevel) |
| 204 |
|
| 205 |
|
| 206 |
for i := 0; i < D; i++ { |
| 207 |
min[i] = math.Inf(+1) |
| 208 |
max[i] = math.Inf(-1) |
| 209 |
} |
| 210 |
for _, o := range objs { |
| 211 |
for i := 0; i < D; i++ { |
| 212 |
if o.min[i] < min[i] { |
| 213 |
min[i] = o.min[i] |
| 214 |
} |
| 215 |
if o.max[i] > max[i] { |
| 216 |
max[i] = o.max[i] |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
minb, maxb := tr.Bounds() |
| 221 |
assertEqual(t, min, minb) |
| 222 |
assertEqual(t, max, maxb) |
| 223 |
|
| 224 |
|
| 225 |
var arr []*Rect |
| 226 |
tr.Scan(func(item interface{}) bool { |
| 227 |
arr = append(arr, item.(*Rect)) |
| 228 |
return true |
| 229 |
}) |
| 230 |
assertEqual(t, true, ptrTestHasSameItems(objs, arr)) |
| 231 |
|
| 232 |
|
| 233 |
ptrTestSearch(t, tr, objs, 0.10, true) |
| 234 |
ptrTestSearch(t, tr, objs, 0.50, true) |
| 235 |
ptrTestSearch(t, tr, objs, 1.00, true) |
| 236 |
|
| 237 |
|
| 238 |
ptrTestKNN(t, tr, objs, int(float64(len(objs))*0.01), true) |
| 239 |
ptrTestKNN(t, tr, objs, int(float64(len(objs))*0.50), true) |
| 240 |
ptrTestKNN(t, tr, objs, int(float64(len(objs))*1.00), true) |
| 241 |
|
| 242 |
|
| 243 |
indexes := rand.Perm(len(objs)) |
| 244 |
start = time.Now() |
| 245 |
for _, i := range indexes { |
| 246 |
tr.Remove(objs[i].min, objs[i].max, objs[i].item) |
| 247 |
} |
| 248 |
durRemove := time.Since(start) |
| 249 |
assertEqual(t, 0, tr.Count()) |
| 250 |
fmt.Printf("Removed %d random %ss in %dms -- %d ops/sec\n", |
| 251 |
len(objs), which, int(durRemove.Seconds()*1000), |
| 252 |
int(float64(len(objs))/durRemove.Seconds())) |
| 253 |
|
| 254 |
min, max = tr.Bounds() |
| 255 |
assertEqual(t, make([]float64, D), min[:]) |
| 256 |
assertEqual(t, make([]float64, D), max[:]) |
| 257 |
} |
| 258 |
|
| 259 |
func ptrTestHasSameItems(a1, a2 []*Rect) bool { |
| 260 |
if len(a1) != len(a2) { |
| 261 |
return false |
| 262 |
} |
| 263 |
for _, p1 := range a1 { |
| 264 |
var found bool |
| 265 |
for _, p2 := range a2 { |
| 266 |
if p1.equals(*p2) { |
| 267 |
found = true |
| 268 |
break |
| 269 |
} |
| 270 |
} |
| 271 |
if !found { |
| 272 |
return false |
| 273 |
} |
| 274 |
} |
| 275 |
return true |
| 276 |
} |
| 277 |
|
| 278 |
func ptrTestSearch(t *testing.T, tr *RTree, objs []*Rect, percent float64, check bool) { |
| 279 |
var found int |
| 280 |
var start time.Time |
| 281 |
var stop time.Time |
| 282 |
defer func() { |
| 283 |
dur := stop.Sub(start) |
| 284 |
fmt.Printf("Searched %.0f%% (%d/%d items) in %dms -- %d ops/sec\n", |
| 285 |
percent*100, found, len(objs), int(dur.Seconds()*1000), |
| 286 |
int(float64(1)/dur.Seconds()), |
| 287 |
) |
| 288 |
}() |
| 289 |
min, max := tr.Bounds() |
| 290 |
vals := make([]float64, D*2) |
| 291 |
for i := 0; i < D; i++ { |
| 292 |
vals[i] = ((max[i]+min[i])/2 - ((max[i]-min[i])*percent)/2) |
| 293 |
vals[D+i] = ((max[i]+min[i])/2 + ((max[i]-min[i])*percent)/2) |
| 294 |
} |
| 295 |
var arr1 []*Rect |
| 296 |
var box *Rect |
| 297 |
if percent == 1 { |
| 298 |
box = ptrMakeRect(append(append([]float64{}, min[:]...), max[:]...)...) |
| 299 |
} else { |
| 300 |
box = ptrMakeRect(vals...) |
| 301 |
} |
| 302 |
start = time.Now() |
| 303 |
tr.Search(box.min, box.max, func(item interface{}) bool { |
| 304 |
if check { |
| 305 |
arr1 = append(arr1, item.(*Rect)) |
| 306 |
} |
| 307 |
found++ |
| 308 |
return true |
| 309 |
}) |
| 310 |
stop = time.Now() |
| 311 |
if !check { |
| 312 |
return |
| 313 |
} |
| 314 |
var arr2 []*Rect |
| 315 |
for _, obj := range objs { |
| 316 |
if ptrTestIntersects(obj, box) { |
| 317 |
arr2 = append(arr2, obj) |
| 318 |
} |
| 319 |
} |
| 320 |
assertEqual(t, len(arr1), len(arr2)) |
| 321 |
for _, o1 := range arr1 { |
| 322 |
var found bool |
| 323 |
for _, o2 := range arr2 { |
| 324 |
if o2.equals(*o1) { |
| 325 |
found = true |
| 326 |
break |
| 327 |
} |
| 328 |
} |
| 329 |
if !found { |
| 330 |
t.Fatalf("not found") |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func ptrTestKNN(t *testing.T, tr *RTree, objs []*Rect, n int, check bool) { |
| 336 |
var start time.Time |
| 337 |
var stop time.Time |
| 338 |
defer func() { |
| 339 |
dur := stop.Sub(start) |
| 340 |
fmt.Printf("KNN %d items in %dms -- %d ops/sec\n", |
| 341 |
n, int(dur.Seconds()*1000), |
| 342 |
int(float64(1)/dur.Seconds()), |
| 343 |
) |
| 344 |
}() |
| 345 |
min, max := tr.Bounds() |
| 346 |
pvals := make([]float64, D) |
| 347 |
for i := 0; i < D; i++ { |
| 348 |
pvals[i] = (max[i] + min[i]) / 2 |
| 349 |
} |
| 350 |
point := ptrMakePoint(pvals...) |
| 351 |
|
| 352 |
|
| 353 |
var arr1 []Rect |
| 354 |
var dists1 []float64 |
| 355 |
pdist := math.Inf(-1) |
| 356 |
start = time.Now() |
| 357 |
tr.KNN(point.min, point.max, false, func(item interface{}, dist float64) bool { |
| 358 |
if len(arr1) == n { |
| 359 |
return false |
| 360 |
} |
| 361 |
arr1 = append(arr1, Rect{min: min, max: max, item: item}) |
| 362 |
dists1 = append(dists1, dist) |
| 363 |
if dist < pdist { |
| 364 |
panic("dist out of order") |
| 365 |
} |
| 366 |
pdist = dist |
| 367 |
return true |
| 368 |
}) |
| 369 |
stop = time.Now() |
| 370 |
assertEqual(t, true, n > len(objs) || n == len(arr1)) |
| 371 |
|
| 372 |
|
| 373 |
nobjs := make([]*Rect, len(objs)) |
| 374 |
copy(nobjs, objs) |
| 375 |
sort.Slice(nobjs, func(i, j int) bool { |
| 376 |
idist := ptrTestBoxDist(pvals, nobjs[i].min, nobjs[i].max) |
| 377 |
jdist := ptrTestBoxDist(pvals, nobjs[j].min, nobjs[j].max) |
| 378 |
return idist < jdist |
| 379 |
}) |
| 380 |
arr2 := nobjs[:len(arr1)] |
| 381 |
var dists2 []float64 |
| 382 |
for i := 0; i < len(arr2); i++ { |
| 383 |
dist := ptrTestBoxDist(pvals, arr2[i].min, arr2[i].max) |
| 384 |
dists2 = append(dists2, dist) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
assertEqual(t, dists1, dists2) |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
func ptrTestBoxDist(point []float64, min, max []float64) float64 { |
| 393 |
var dist float64 |
| 394 |
for i := 0; i < len(point); i++ { |
| 395 |
d := ptrTestAxisDist(point[i], min[i], max[i]) |
| 396 |
dist += d * d |
| 397 |
} |
| 398 |
return dist |
| 399 |
} |
| 400 |
func ptrTestAxisDist(k, min, max float64) float64 { |
| 401 |
if k < min { |
| 402 |
return min - k |
| 403 |
} |
| 404 |
if k <= max { |
| 405 |
return 0 |
| 406 |
} |
| 407 |
return k - max |
| 408 |
} |
| 409 |
func ptrTestIntersects(obj, box *Rect) bool { |
| 410 |
for i := 0; i < D; i++ { |
| 411 |
if box.min[i] > obj.max[i] || box.max[i] < obj.min[i] { |
| 412 |
return false |
| 413 |
} |
| 414 |
} |
| 415 |
return true |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
func TestBenchmarks(t *testing.T) { |
| 505 |
var points []*Rect |
| 506 |
for i := 0; i < 2000000; i++ { |
| 507 |
x := rand.Float64()*360 - 180 |
| 508 |
y := rand.Float64()*180 - 90 |
| 509 |
points = append(points, ptrMakePoint(x, y)) |
| 510 |
} |
| 511 |
tr := New(D, M) |
| 512 |
start := time.Now() |
| 513 |
for i := len(points) / 2; i < len(points); i++ { |
| 514 |
tr.Insert(points[i].min, points[i].max, points[i].item) |
| 515 |
} |
| 516 |
dur := time.Since(start) |
| 517 |
log.Printf("insert 1M items one by one: %.3fs", dur.Seconds()) |
| 518 |
|
| 519 |
rarr := rand.Perm(len(points) / 2) |
| 520 |
start = time.Now() |
| 521 |
for i := 0; i < len(points)/2; i++ { |
| 522 |
a := points[rarr[i]+len(points)/2] |
| 523 |
b := points[rarr[i]] |
| 524 |
tr.Remove(a.min, a.max, a.item) |
| 525 |
tr.Insert(b.min, b.max, b.item) |
| 526 |
} |
| 527 |
dur = time.Since(start) |
| 528 |
log.Printf("replaced 1M items one by one: %.3fs", dur.Seconds()) |
| 529 |
points = points[:len(points)/2] |
| 530 |
|
| 531 |
start = time.Now() |
| 532 |
for i := 0; i < 1000; i++ { |
| 533 |
tr.Remove(points[i].min, points[i].max, points[i].item) |
| 534 |
} |
| 535 |
dur = time.Since(start) |
| 536 |
log.Printf("remove 100 items one by one: %.3fs", dur.Seconds()) |
| 537 |
|
| 538 |
bbox := ptrMakeRect(0, 0, 0+(360*0.0001), 0+(180*0.0001)) |
| 539 |
start = time.Now() |
| 540 |
for i := 0; i < 1000; i++ { |
| 541 |
tr.Search(bbox.min, bbox.max, func(_ interface{}) bool { return true }) |
| 542 |
} |
| 543 |
dur = time.Since(start) |
| 544 |
log.Printf("1000 searches of 0.01%% area: %.3fs", dur.Seconds()) |
| 545 |
|
| 546 |
bbox = ptrMakeRect(0, 0, 0+(360*0.01), 0+(180*0.01)) |
| 547 |
start = time.Now() |
| 548 |
for i := 0; i < 1000; i++ { |
| 549 |
tr.Search(bbox.min, bbox.max, func(_ interface{}) bool { return true }) |
| 550 |
} |
| 551 |
dur = time.Since(start) |
| 552 |
log.Printf("1000 searches of 1%% area: %.3fs", dur.Seconds()) |
| 553 |
|
| 554 |
bbox = ptrMakeRect(0, 0, 0+(360*0.10), 0+(180*0.10)) |
| 555 |
start = time.Now() |
| 556 |
for i := 0; i < 1000; i++ { |
| 557 |
tr.Search(bbox.min, bbox.max, func(_ interface{}) bool { return true }) |
| 558 |
} |
| 559 |
dur = time.Since(start) |
| 560 |
log.Printf("1000 searches of 10%% area: %.3fs", dur.Seconds()) |
| 561 |
|
| 562 |
|
| 563 |
var mins [][]float64 |
| 564 |
var maxs [][]float64 |
| 565 |
var items []interface{} |
| 566 |
for i := 0; i < len(points); i++ { |
| 567 |
mins = append(mins, points[i].min) |
| 568 |
maxs = append(maxs, points[i].max) |
| 569 |
items = append(items, points[i].item) |
| 570 |
} |
| 571 |
|
| 572 |
tr = New(D, M) |
| 573 |
start = time.Now() |
| 574 |
tr.Load(mins, maxs, items) |
| 575 |
dur = time.Since(start) |
| 576 |
log.Printf("bulk-insert 1M items: %.3fs", dur.Seconds()) |
| 577 |
} |
| 578 |
|
| 579 |
func assertEqual(t *testing.T, expected, actual interface{}) { |
| 580 |
t.Helper() |
| 581 |
if !reflect.DeepEqual(expected, actual) { |
| 582 |
t.Fatalf("expected '%v', got '%v'", expected, actual) |
| 583 |
} |
| 584 |
} |