Skip to content

Commit

Permalink
very basic update function + updated viz
Browse files Browse the repository at this point in the history
  • Loading branch information
thisredone committed Mar 28, 2020
1 parent 8c48d9d commit 90d2a51
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 61 deletions.
8 changes: 6 additions & 2 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export default class RBush

return false

update: (item) ->
if contains(item.parent.bbox, item.bbox)
return
@remove(item)
@insert(item)

insert: (item) ->
@_insert(item, @data.height - 1) if item
this
Expand Down Expand Up @@ -224,7 +230,6 @@ export default class RBush

_condense: (node) ->
# go upward, removing empty
node = path
while node
if node.children.length is 0
if node.parent?
Expand All @@ -238,7 +243,6 @@ export default class RBush
null



# calculate node's bbox from bboxes of its children
calcBBox = (node) ->
distBBox(node, 0, node.children.length, null, node)
Expand Down
37 changes: 0 additions & 37 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,6 @@ pry = require 'pry'
assert = require 'assert'


# data = [
# [0,0,0,0],[10,10,10,10],[20,20,20,20],[25,0,25,0],[35,10,35,10],[45,20,45,20],[0,25,0,25],[10,35,10,35],
# [20,45,20,45],[25,25,25,25],[35,35,35,35],[45,45,45,45],[50,0,50,0],[60,10,60,10],[70,20,70,20],[75,0,75,0],
# [85,10,85,10],[95,20,95,20],[50,25,50,25],[60,35,60,35],[70,45,70,45],[75,25,75,25],[85,35,85,35],[95,45,95,45],
# [0,50,0,50],[10,60,10,60],[20,70,20,70],[25,50,25,50],[35,60,35,60],[45,70,45,70],[0,75,0,75],[10,85,10,85],
# [20,95,20,95],[25,75,25,75],[35,85,35,85],[45,95,45,95],[50,50,50,50],[60,60,60,60],[70,70,70,70],[75,50,75,50],
# [85,60,85,60],[95,70,95,70],[50,75,50,75],[60,85,60,85],[70,95,70,95],[75,75,75,75],[85,85,85,85],[95,95,95,95]
# ]


# OriginalRbush = require '../rbush.js'
# if process.argv.includes 'o'
# Orig = true
# RBush = OriginalRbush
# data = data.map ([minX, minY, maxX, maxY]) -> { minX, minY, maxX, maxY, bbox: [minX, minY, maxX, maxY] }

# else
# data = data.map (bbox) -> { bbox }


# tree = new RBush(4)

# for item, i in data
# # break if i > 4
# tree.insert item

# for i in [6..15]
# tree.remove data[i]

# if Orig
# log tree.all().map ({ minX, minY, maxX, maxY }) -> [minX, minY, maxX, maxY]
# else
# log tree.all().map (n) -> n.bbox

# eval pry.it

global.pp = (node) ->
if node.bbox?
node.bbox.join(', ')
Expand All @@ -48,7 +12,6 @@ global.pp = (node) ->
[minX, minY, maxX, maxY].join(', ')



data = [
[0,0,0,0],[10,10,10,10],[20,20,20,20],[25,0,25,0],[35,10,35,10],[45,20,45,20],[0,25,0,25],[10,35,10,35],
[20,45,20,45],[25,25,25,25],[35,35,35,35],[45,45,45,45],[50,0,50,0],[60,10,60,10],[70,20,70,20],[75,0,75,0],
Expand Down
27 changes: 9 additions & 18 deletions viz/viz-cluster.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<br>
<button id="insert1">Insert 50000</button>
<button id="insert2">Insert 1000</button>
<button id="insert3">Bulk-insert 50000</button>
<button id="insert4">Bulk-insert 1000</button>
<button id="remove">Remove leftmost 10000</button>
<button id="remove2">Remove every other</button>

<button id="move">start moving</button>
<button id="stop">stop moving</button>

<script src="../rbush.js"></script>
<script src="viz.js"></script>
Expand All @@ -33,7 +35,7 @@
var tree = new RBush(16);
var data = [];

genBulkInsert(N, M)();
genInsertOneByOne(N, M)();

function genInsertOneByOne(K, M) {
return function () {
Expand All @@ -51,25 +53,14 @@
};
}

function genBulkInsert(K, M) {
return function () {
var data2 = genData(K, M, R);

console.time('bulk-insert ' + K + ' items');
tree.load(data2);
console.timeEnd('bulk-insert ' + K + ' items');

data = data.concat(data2);

draw();
};
}

document.getElementById('insert1').onclick = genInsertOneByOne(50000, M);
document.getElementById('insert2').onclick = genInsertOneByOne(1000, 1);
document.getElementById('insert3').onclick = genBulkInsert(50000, M);
document.getElementById('insert4').onclick = genBulkInsert(1000, 1);
document.getElementById('canvas').onclick = search;
document.getElementById('remove').onclick = remove;
document.getElementById('remove2').onclick = removeHalf;

document.getElementById('move').onclick = startMoving;
document.getElementById('stop').onclick = stopMoving;

</script>
6 changes: 6 additions & 0 deletions viz/viz-uniform.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<button id="remove">Remove leftmost 10000</button>
<button id="remove2">Remove every other</button>

<button id="move">start moving</button>
<button id="stop">stop moving</button>

<script src="../rbush.js"></script>
<script src="viz.js"></script>
<script>
Expand Down Expand Up @@ -49,4 +52,7 @@
document.getElementById('remove').onclick = remove;
document.getElementById('remove2').onclick = removeHalf;

document.getElementById('move').onclick = startMoving();
document.getElementById('stop').onclick = stopMoving();

</script>
30 changes: 26 additions & 4 deletions viz/viz.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,32 @@ function removeHalf() {
};


function move() {
console.time('move all');
let interval;

function startMoving() {
console.log('started moving')
const items = data.slice(0, 2000);
interval = setInterval(() => {
// console.time('movement update')
for (let item of items) {
if (!item.dir || Math.random() < 0.005) {
item.dir = [Math.random() * 3 - 1.5, Math.random() * 3 - 1.5]
}
var bb = item.bbox;
bb[0] += item.dir[0];
bb[1] += item.dir[1];
bb[2] += item.dir[0];
bb[3] += item.dir[1];
if (bb[0] < 0 || bb[2] > 700) item.dir[0] *= -1
if (bb[1] < 0 || bb[3] > 700) item.dir[1] *= -1
tree.update(item);
}
// console.timeEnd('movement update')
draw();
}, 25);
}


console.timeEnd('move all');
draw();
function stopMoving() {
clearInterval(interval);
}

0 comments on commit 90d2a51

Please sign in to comment.