-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.v43.d.ts
369 lines (315 loc) · 9.89 KB
/
index.v43.d.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
declare module "ngraph.forcelayout" {
import { Graph, NodeId, LinkId, Node, Link } from "ngraph.graph";
import { EventedType } from "ngraph.events";
export type ForceFunction = (iterationNumber: number) => void;
export interface Vector {
x: number;
y: number;
z?: number;
[coord: string]: number | undefined;
}
export interface Body {
isPinned: boolean;
pos: Vector;
force: Vector;
velocity: Vector;
mass: number;
springCount: number;
springLength: number;
reset(): void;
setPosition(x: number, y: number, z?: number, ...c: number[]): void;
}
export interface Spring {
from: Body;
to: Body;
length: number;
coefficient: number;
}
export interface QuadNode {
body: Body | null;
mass: number;
mass_x: number;
mass_y: number;
mass_z?: number;
}
export interface QuadTree {
insertBodies(bodies: Body[]): void;
getRoot(): QuadNode & Record<string, number | null>;
updateBodyForce(sourceBody: Body): void;
options(newOptions: { gravity: number; theta: number }): { gravity: number; theta: number };
}
export interface BoundingBox {
min_x: number;
max_x: number;
min_y: number;
max_y: number;
min_z?: number;
max_z?: number;
[min_max: string]: number | undefined;
}
/**
* Settings for a PhysicsSimulator
*/
export interface PhysicsSettings {
/**
* Ideal length for links (springs in physical model).
*/
springLength: number;
/**
* Hook's law coefficient. 1 - solid spring.
*/
springCoefficient: number;
/**
* Coulomb's law coefficient. It's used to repel nodes thus should be negative
* if you make it positive nodes start attract each other :).
*/
gravity: number;
/**
* Theta coefficient from Barnes Hut simulation. Ranged between (0, 1).
* The closer it's to 1 the more nodes algorithm will have to go through.
* Setting it to one makes Barnes Hut simulation no different from
* brute-force forces calculation (each node is considered).
*/
theta: number;
/**
* Drag force coefficient. Used to slow down system, thus should be less than 1.
* The closer it is to 0 the less tight system will be.
*/
dragCoefficient: number;
/**
* Default time step (dt) for forces integration
*/
timeStep: number;
/**
* Adaptive time step uses average spring length to compute actual time step:
* See: https://twitter.com/anvaka/status/1293067160755957760
*/
adaptiveTimeStepWeight: number;
/**
* This parameter defines number of dimensions of the space where simulation
* is performed.
*/
dimensions: number;
/**
* In debug mode more checks are performed, this will help you catch errors
* quickly, however for production build it is recommended to turn off this flag
* to speed up computation.
*/
debug: boolean;
}
/**
* Manages a simulation of physical forces acting on bodies and springs.
*/
export interface PhysicsSimulator {
/**
* Array of bodies, registered with current simulator
*
* Note: To add new body, use addBody() method. This property is only
* exposed for testing/performance purposes.
*/
bodies: Body[];
quadTree: QuadTree;
/**
* Array of springs, registered with current simulator
*
* Note: To add new spring, use addSpring() method. This property is only
* exposed for testing/performance purposes.
*/
springs: Spring[];
/**
* Returns settings with which current simulator was initialized
*/
settings: PhysicsSettings;
/**
* Adds a new force to simulation
* @param forceName force identifier
* @param forceFunction the function to apply
*/
addForce(forceName: string, forceFunction: ForceFunction): void;
/**
* Removes a force from the simulation
* @param forceName force identifier
*/
removeForce(forceName: string): void;
/**
* Returns a map of all registered forces
*/
getForces(): Map<string, ForceFunction>;
/**
* Performs one step of force simulation.
*
* @returns true if system is considered stable; False otherwise.
*/
step(): boolean;
/**
* Adds body to the system
* @param body physical body
* @returns added body
*/
addBody(body: Body): Body;
/**
* Adds body to the system at given position
* @param pos position of a body
* @returns added body
*/
addBodyAt(pos: Vector): Body;
/**
* Removes body from the system
* @param body to remove
* @returns true if body found and removed. falsy otherwise;
*/
removeBody(body: Body): boolean;
/**
* Adds a spring to this simulation
* @param body1 first body
* @param body2 second body
* @param springLength Ideal length for links
* @param springCoefficient Hook's law coefficient. 1 - solid spring
* @returns a handle for a spring. If you want to later remove
* spring pass it to removeSpring() method.
*/
addSpring(body1: Body, body2: Body, springLength: number, springCoefficient: number): Spring;
/**
* Returns amount of movement performed on last step() call
*/
getTotalMovement(): number;
/**
* Removes spring from the system
* @param spring to remove. Spring is an object returned by addSpring
* @returns true if spring found and removed. falsy otherwise;
*/
removeSpring(spring: Spring): boolean;
getBestNewBodyPosition(neighbors: Body[]): Vector;
/**
* Returns bounding box which covers all bodies
*/
getBBox(): BoundingBox;
/**
* Returns bounding box which covers all bodies
*/
getBoundingBox(): BoundingBox;
/** @deprecated invalidateBBox() is deprecated, bounds always recomputed on `getBBox()` call */
invalidateBBox(): void;
/**
* Changes the gravity for the system
* @param value Coulomb's law coefficient
*/
gravity(value: number): number;
/**
* Changes the theta coeffitient for the system
* @param value Theta coefficient from Barnes Hut simulation
*/
theta(value: number): number;
// TODO: create types declaration file for ngraph.random
/**
* Returns pseudo-random number generator instance
*/
random: any;
}
/**
* Force based layout for a given graph.
*/
export interface Layout<T extends Graph> {
/**
* Performs one step of iterative layout algorithm
* @returns true if the system should be considered stable; False otherwise.
* The system is stable if no further call to `step()` can improve the layout.
*/
step(): boolean;
/**
* For a given `nodeId` returns position
* @param nodeId node identifier
*/
getNodePosition(nodeId: NodeId): Vector;
/**
* Sets position of a node to a given coordinates
* @param nodeId node identifier
* @param x position of a node
* @param y position of a node
* @param z position of node (only if applicable to body)
*/
setNodePosition(nodeId: NodeId, x: number, y: number, z?: number, ...c: number[]): void;
/**
* Gets Link position by link id
* @param linkId link identifier
* @returns from: {x, y} coordinates of link start
* @returns to: {x, y} coordinates of link end
*/
getLinkPosition(linkId: LinkId): { from: Vector; to: Vector };
/**
* @returns area required to fit in the graph. Object contains
* `x1`, `y1` - top left coordinates
* `x2`, `y2` - bottom right coordinates
*/
getGraphRect(): { x1: number; y1: number; x2: number; y2: number };
/**
* Iterates over each body in the layout simulator and performs a callback(body, nodeId)
* @param callbackfn the callback function
*/
forEachBody(callbackfn: (value: Body, key: NodeId, map: Map<NodeId, Body>) => void): void;
/**
* Requests layout algorithm to pin/unpin node to its current position
* Pinned nodes should not be affected by layout algorithm and always
* remain at their position
* @param node the node to pin/unpin
* @param isPinned true to pin, false to unpin
*/
pinNode(node: Node, isPinned: boolean): void;
/**
* Checks whether given graph's node is currently pinned
* @param node the node to check
*/
isNodePinned(node: Node): boolean;
/**
* Request to release all resources
*/
dispose(): void;
/**
* Gets physical body for a given node id. If node is not found undefined
* value is returned.
* @param nodeId node identifier
*/
getBody(nodeId: NodeId): Body | undefined;
/**
* Gets spring for a given edge.
*
* @param linkId link identifer.
*/
getSpring(linkId: LinkId | Link): Spring;
/**
* Gets spring for a given edge.
*
* @param fromId node identifer - tail of the link
* @param toId head of the link - head of the link
*/
getSpring(fromId: NodeId, toId: NodeId): Spring | undefined;
/**
* Returns length of cumulative force vector. The closer this to zero - the more stable the system is
*/
getForceVectorLength(): number;
/**
* @readonly Gets current physics simulator
*/
readonly simulator: PhysicsSimulator;
/**
* Gets the graph that was used for layout
*/
graph: T;
/**
* Gets amount of movement performed during last step operation
*/
lastMove: number;
}
/**
* Creates force based layout for a given graph.
*
* @param graph which needs to be laid out
* @param physicsSettings if you need custom settings
* for physics simulator you can pass your own settings here. If it's not passed
* a default one will be created.
*/
export default function createLayout<T extends Graph>(
graph: T,
physicsSettings?: Partial<PhysicsSettings>
): Layout<T> & EventedType;
}