forked from d3/d3-force
-
Notifications
You must be signed in to change notification settings - Fork 54
/
manyBody.js
139 lines (118 loc) · 4.39 KB
/
manyBody.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {binarytree} from "d3-binarytree";
import {quadtree} from "d3-quadtree";
import {octree} from "d3-octree";
import constant from "./constant.js";
import jiggle from "./jiggle.js";
import {x, y, z} from "./simulation.js";
export default function() {
var nodes,
nDim,
node,
random,
alpha,
strength = constant(-30),
strengths,
distanceMin2 = 1,
distanceMax2 = Infinity,
theta2 = 0.81;
function force(_) {
var i,
n = nodes.length,
tree =
(nDim === 1 ? binarytree(nodes, x)
:(nDim === 2 ? quadtree(nodes, x, y)
:(nDim === 3 ? octree(nodes, x, y, z)
:null
))).visitAfter(accumulate);
for (alpha = _, i = 0; i < n; ++i) node = nodes[i], tree.visit(apply);
}
function initialize() {
if (!nodes) return;
var i, n = nodes.length, node;
strengths = new Array(n);
for (i = 0; i < n; ++i) node = nodes[i], strengths[node.index] = +strength(node, i, nodes);
}
function accumulate(treeNode) {
var strength = 0, q, c, weight = 0, x, y, z, i;
var numChildren = treeNode.length;
// For internal nodes, accumulate forces from children.
if (numChildren) {
for (x = y = z = i = 0; i < numChildren; ++i) {
if ((q = treeNode[i]) && (c = Math.abs(q.value))) {
strength += q.value, weight += c, x += c * (q.x || 0), y += c * (q.y || 0), z += c * (q.z || 0);
}
}
strength *= Math.sqrt(4 / numChildren); // scale accumulated strength according to number of dimensions
treeNode.x = x / weight;
if (nDim > 1) { treeNode.y = y / weight; }
if (nDim > 2) { treeNode.z = z / weight; }
}
// For leaf nodes, accumulate forces from coincident nodes.
else {
q = treeNode;
q.x = q.data.x;
if (nDim > 1) { q.y = q.data.y; }
if (nDim > 2) { q.z = q.data.z; }
do strength += strengths[q.data.index];
while (q = q.next);
}
treeNode.value = strength;
}
function apply(treeNode, x1, arg1, arg2, arg3) {
if (!treeNode.value) return true;
var x2 = [arg1, arg2, arg3][nDim-1];
var x = treeNode.x - node.x,
y = (nDim > 1 ? treeNode.y - node.y : 0),
z = (nDim > 2 ? treeNode.z - node.z : 0),
w = x2 - x1,
l = x * x + y * y + z * z;
// Apply the Barnes-Hut approximation if possible.
// Limit forces for very close nodes; randomize direction if coincident.
if (w * w / theta2 < l) {
if (l < distanceMax2) {
if (x === 0) x = jiggle(random), l += x * x;
if (nDim > 1 && y === 0) y = jiggle(random), l += y * y;
if (nDim > 2 && z === 0) z = jiggle(random), l += z * z;
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l);
node.vx += x * treeNode.value * alpha / l;
if (nDim > 1) { node.vy += y * treeNode.value * alpha / l; }
if (nDim > 2) { node.vz += z * treeNode.value * alpha / l; }
}
return true;
}
// Otherwise, process points directly.
else if (treeNode.length || l >= distanceMax2) return;
// Limit forces for very close nodes; randomize direction if coincident.
if (treeNode.data !== node || treeNode.next) {
if (x === 0) x = jiggle(random), l += x * x;
if (nDim > 1 && y === 0) y = jiggle(random), l += y * y;
if (nDim > 2 && z === 0) z = jiggle(random), l += z * z;
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l);
}
do if (treeNode.data !== node) {
w = strengths[treeNode.data.index] * alpha / l;
node.vx += x * w;
if (nDim > 1) { node.vy += y * w; }
if (nDim > 2) { node.vz += z * w; }
} while (treeNode = treeNode.next);
}
force.initialize = function(_nodes, ...args) {
nodes = _nodes;
random = args.find(arg => typeof arg === 'function') || Math.random;
nDim = args.find(arg => [1, 2, 3].includes(arg)) || 2;
initialize();
};
force.strength = function(_) {
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
};
force.distanceMin = function(_) {
return arguments.length ? (distanceMin2 = _ * _, force) : Math.sqrt(distanceMin2);
};
force.distanceMax = function(_) {
return arguments.length ? (distanceMax2 = _ * _, force) : Math.sqrt(distanceMax2);
};
force.theta = function(_) {
return arguments.length ? (theta2 = _ * _, force) : Math.sqrt(theta2);
};
return force;
}