Bulk network generation is significantly faster

This commit is contained in:
2025-09-10 14:52:26 -05:00
parent 0b1c16c124
commit d4ed5524b1
+15 -9
View File
@@ -705,14 +705,16 @@ function dropConnection(nodeA, nodeB) {
} }
} }
function onNodeAdd(nodeId, defer=false) { function onNodeAdd(nodeId, defer=false, nodeData=null) {
// update grid // update grid
const position = network.getPosition(nodeId); if (!defer) {
grid.add(nodeId, position.x, position.y); const position = network.getPosition(nodeId);
grid.add(nodeId, position.x, position.y);
if (!defer) {
recomputeNodeConnections(nodeId); recomputeNodeConnections(nodeId);
} } else {
const position = nodeData;
grid.add(nodeId, nodeData.x, nodeData.y);
}
var nodeLength = Object.keys(nodeTable).length; var nodeLength = Object.keys(nodeTable).length;
document.getElementById('nodeCount').innerText = 'Node Count: ' + nodeLength; document.getElementById('nodeCount').innerText = 'Node Count: ' + nodeLength;
@@ -908,6 +910,7 @@ document.getElementById('createRandomGraphConfirm').addEventListener('click', fu
nodeTable = {}; nodeTable = {};
grid = new GridIndex(CONNECTION_DISTANCE); grid = new GridIndex(CONNECTION_DISTANCE);
selectedNode = null; selectedNode = null;
let nodesToAdd = [];
const cellSize = CONNECTION_DISTANCE; const cellSize = CONNECTION_DISTANCE;
const cellsNeeded = Math.max(1, Math.ceil(nodeCount / density)); const cellsNeeded = Math.max(1, Math.ceil(nodeCount / density));
@@ -932,7 +935,7 @@ document.getElementById('createRandomGraphConfirm').addEventListener('click', fu
// Jitter within each cell so nodes are spread but remain near the cell center // Jitter within each cell so nodes are spread but remain near the cell center
const jitter = cellSize * 0.45; const jitter = cellSize * 0.45;
let cellsPerFrame = 1; let cellsPerFrame = cols;
let runId; let runId;
let nodesAdded = 0; let nodesAdded = 0;
let cellsFilled = 0; let cellsFilled = 0;
@@ -950,9 +953,10 @@ document.getElementById('createRandomGraphConfirm').addEventListener('click', fu
const y = cy + (Math.random() * 2 - 1) * jitter; const y = cy + (Math.random() * 2 - 1) * jitter;
const nodeId = generateRealisticLabel(); const nodeId = generateRealisticLabel();
nodes.add({ id: nodeId, label: nodeId, x, y }); const nodeData = { id: nodeId, label: nodeId, x: x, y: y };
nodesToAdd.push(nodeData);
nodeTable[nodeId] = { 'connections': [], 'packetCache': new Set(), 'routingTable': {} }; nodeTable[nodeId] = { 'connections': [], 'packetCache': new Set(), 'routingTable': {} };
onNodeAdd(nodeId, true); onNodeAdd(nodeId, true, nodeData);
lastNodeAdded = nodeId; lastNodeAdded = nodeId;
nodesAdded++; nodesAdded++;
} }
@@ -964,6 +968,8 @@ document.getElementById('createRandomGraphConfirm').addEventListener('click', fu
runId = requestAnimationFrame(addCells); runId = requestAnimationFrame(addCells);
} else { } else {
cancelAnimationFrame(runId); cancelAnimationFrame(runId);
console.log('done')
nodes.add(nodesToAdd);
computeAllConnectionsFast(); computeAllConnectionsFast();
} }
} }