forked from anvaka/allnpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout3d.js
48 lines (44 loc) · 1.35 KB
/
layout3d.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* This file will produce layout files from
* graph made by `convertToGraph.js` program
*
* Each 60th iteration is saved to a file
*/
var fs = require('fs');
var fileName = process.argv[2] || 'graph.out';
var content = fs.readFileSync(fileName, 'utf8');
var serializer = require('ngraph.serialization/json');
var graph = serializer.load(content);
var layout = require('ngraph.forcelayout3d')(graph);
if (process.argv[3]) {
console.log('initializing positions from ', process.argv[3]);
var positions = JSON.parse(fs.readFileSync(process.argv[3], 'utf8'));
positions.forEach(function (pos) {
layout.setNodePosition(pos.node, pos.pos.x, pos.pos.y, pos.pos.z);
});
} else {
graph.forEachNode(function (node) {
layout.setNodePosition(node.id, Math.random() * 10000, Math.random() * 10000, Math.random() * 10000);
});
}
var i = 0;
while (true) {
console.log('step' + i);
layout.step();
if (i % 5 === 0) {
saveIteration(Math.round(i/5));
}
++i;
}
function saveIteration(name) {
console.log("Saving: ", name + '.pos3d');
var positions = [];
graph.forEachNode(function (node) {
var pos = layout.getNodePosition(node.id);
pos.x = Math.round(pos.x);
pos.y = Math.round(pos.y);
pos.z = Math.round(pos.z);
positions.push({ node: node.id, pos: pos});
});
fs.writeFileSync(name + '.pos3d',JSON.stringify(positions));
}