diff --git a/index.html b/index.html index b905071..f9051d1 100644 --- a/index.html +++ b/index.html @@ -4,9 +4,10 @@ BLE-mesh SIM - - - + + + + diff --git a/CompleteMoveOnEdge.js b/js/CompleteMoveOnEdge.js similarity index 100% rename from CompleteMoveOnEdge.js rename to js/CompleteMoveOnEdge.js diff --git a/js/GridIndex.js b/js/GridIndex.js new file mode 100644 index 0000000..e377f44 --- /dev/null +++ b/js/GridIndex.js @@ -0,0 +1,60 @@ +/* + Grid index for quickly looking up neighbor candidates. +*/ +class GridIndex { + constructor(cellSize) { + this.cellSize = cellSize; + this.cells = new Map(); + this.nodeCell = new Map(); + } + keyFor(x, y) { + const cx = Math.floor(x / this.cellSize); + const cy = Math.floor(y / this.cellSize); + return `${cx},${cy}`; + } + add(nodeId, x, y) { + const key = this.keyFor(x, y); + if (!this.cells.has(key)) { + this.cells.set(key, new Set()); + } + this.cells.get(key).add(nodeId); + this.nodeCell.set(nodeId, key); + } + update(nodeId, x, y) { + const newKey = this.keyFor(x, y); + const oldKey = this.nodeCell.get(nodeId); + if (oldKey == newKey) { + return; + } + if (oldKey && this.cells.has(oldKey)) { + this.cells.get(oldKey).delete(nodeId); + } + if (!this.cells.has(newKey)) { + this.cells.set(newKey, new Set()); + } + this.cells.get(newKey).add(nodeId); + this.nodeCell.set(nodeId, newKey); + } + remove(nodeId) { + const key = this.nodeCell.get(nodeId); + if (key && this.cells.has(key)) { + this.cells.get(key).delete(nodeId); + } + this.nodeCell.delete(nodeId); + } + getNeighborCandidates(x, y) { + const cx = Math.floor(x / this.cellSize); + const cy = Math.floor(y / this.cellSize); + const output = []; + for (let dx = -1; dx <= 1; dx++) { + for (let dy = -1; dy <= 1; dy++) { + const key = `${cx + dx},${cy + dy}`; + const bucket = this.cells.get(key); + if (bucket) { + output.push(...bucket); + } + } + } + return output; + } +} \ No newline at end of file diff --git a/simhelper.js b/js/simhelper.js similarity index 100% rename from simhelper.js rename to js/simhelper.js diff --git a/vis-network.min.js b/js/vis-network.min.js similarity index 100% rename from vis-network.min.js rename to js/vis-network.min.js