forked from d3/d3-force
-
Notifications
You must be signed in to change notification settings - Fork 54
/
collide.js
132 lines (113 loc) · 3.58 KB
/
collide.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
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";
function x(d) {
return d.x + d.vx;
}
function y(d) {
return d.y + d.vy;
}
function z(d) {
return d.z + d.vz;
}
export default function(radius) {
var nodes,
nDim,
radii,
random,
strength = 1,
iterations = 1;
if (typeof radius !== "function") radius = constant(radius == null ? 1 : +radius);
function force() {
var i, n = nodes.length,
tree,
node,
xi,
yi,
zi,
ri,
ri2;
for (var k = 0; k < iterations; ++k) {
tree =
(nDim === 1 ? binarytree(nodes, x)
:(nDim === 2 ? quadtree(nodes, x, y)
:(nDim === 3 ? octree(nodes, x, y, z)
:null
))).visitAfter(prepare);
for (i = 0; i < n; ++i) {
node = nodes[i];
ri = radii[node.index], ri2 = ri * ri;
xi = node.x + node.vx;
if (nDim > 1) { yi = node.y + node.vy; }
if (nDim > 2) { zi = node.z + node.vz; }
tree.visit(apply);
}
}
function apply(treeNode, arg1, arg2, arg3, arg4, arg5, arg6) {
var args = [arg1, arg2, arg3, arg4, arg5, arg6];
var x0 = args[0],
y0 = args[1],
z0 = args[2],
x1 = args[nDim],
y1 = args[nDim+1],
z1 = args[nDim+2];
var data = treeNode.data, rj = treeNode.r, r = ri + rj;
if (data) {
if (data.index > node.index) {
var x = xi - data.x - data.vx,
y = (nDim > 1 ? yi - data.y - data.vy : 0),
z = (nDim > 2 ? zi - data.z - data.vz : 0),
l = x * x + y * y + z * z;
if (l < r * r) {
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;
l = (r - (l = Math.sqrt(l))) / l * strength;
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));
if (nDim > 1) { node.vy += (y *= l) * r; }
if (nDim > 2) { node.vz += (z *= l) * r; }
data.vx -= x * (r = 1 - r);
if (nDim > 1) { data.vy -= y * r; }
if (nDim > 2) { data.vz -= z * r; }
}
}
return;
}
return x0 > xi + r || x1 < xi - r
|| (nDim > 1 && (y0 > yi + r || y1 < yi - r))
|| (nDim > 2 && (z0 > zi + r || z1 < zi - r));
}
}
function prepare(treeNode) {
if (treeNode.data) return treeNode.r = radii[treeNode.data.index];
for (var i = treeNode.r = 0; i < Math.pow(2, nDim); ++i) {
if (treeNode[i] && treeNode[i].r > treeNode.r) {
treeNode.r = treeNode[i].r;
}
}
}
function initialize() {
if (!nodes) return;
var i, n = nodes.length, node;
radii = new Array(n);
for (i = 0; i < n; ++i) node = nodes[i], radii[node.index] = +radius(node, i, nodes);
}
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.iterations = function(_) {
return arguments.length ? (iterations = +_, force) : iterations;
};
force.strength = function(_) {
return arguments.length ? (strength = +_, force) : strength;
};
force.radius = function(_) {
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius;
};
return force;
}