index.v43.d.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. declare module "ngraph.forcelayout" {
  2. import { Graph, NodeId, LinkId, Node, Link } from "ngraph.graph";
  3. export type ForceFunction = (iterationNumber: number) => void;
  4. export interface Vector {
  5. x: number;
  6. y: number;
  7. z?: number;
  8. [coord: string]: number | undefined;
  9. }
  10. export interface Body {
  11. isPinned: boolean;
  12. pos: Vector;
  13. force: Vector;
  14. velocity: Vector;
  15. mass: number;
  16. springCount: number;
  17. springLength: number;
  18. reset(): void;
  19. setPosition(x: number, y: number, z?: number, ...c: number[]): void;
  20. }
  21. export interface Spring {
  22. from: Body;
  23. to: Body;
  24. length: number;
  25. coefficient: number;
  26. }
  27. export interface QuadNode {
  28. body: Body | null;
  29. mass: number;
  30. mass_x: number;
  31. mass_y: number;
  32. mass_z?: number;
  33. }
  34. export interface QuadTree {
  35. insertBodies(bodies: Body[]): void;
  36. getRoot(): QuadNode & Record<string, number | null>;
  37. updateBodyForce(sourceBody: Body): void;
  38. options(newOptions: { gravity: number; theta: number }): { gravity: number; theta: number };
  39. }
  40. export interface BoundingBox {
  41. min_x: number;
  42. max_x: number;
  43. min_y: number;
  44. max_y: number;
  45. min_z?: number;
  46. max_z?: number;
  47. [min_max: string]: number | undefined;
  48. }
  49. /**
  50. * Settings for a PhysicsSimulator
  51. */
  52. export interface PhysicsSettings {
  53. /**
  54. * Ideal length for links (springs in physical model).
  55. */
  56. springLength: number;
  57. /**
  58. * Hook's law coefficient. 1 - solid spring.
  59. */
  60. springCoefficient: number;
  61. /**
  62. * Coulomb's law coefficient. It's used to repel nodes thus should be negative
  63. * if you make it positive nodes start attract each other :).
  64. */
  65. gravity: number;
  66. /**
  67. * Theta coefficient from Barnes Hut simulation. Ranged between (0, 1).
  68. * The closer it's to 1 the more nodes algorithm will have to go through.
  69. * Setting it to one makes Barnes Hut simulation no different from
  70. * brute-force forces calculation (each node is considered).
  71. */
  72. theta: number;
  73. /**
  74. * Drag force coefficient. Used to slow down system, thus should be less than 1.
  75. * The closer it is to 0 the less tight system will be.
  76. */
  77. dragCoefficient: number;
  78. /**
  79. * Default time step (dt) for forces integration
  80. */
  81. timeStep: number;
  82. /**
  83. * Adaptive time step uses average spring length to compute actual time step:
  84. * See: https://twitter.com/anvaka/status/1293067160755957760
  85. */
  86. adaptiveTimeStepWeight: number;
  87. /**
  88. * This parameter defines number of dimensions of the space where simulation
  89. * is performed.
  90. */
  91. dimensions: number;
  92. /**
  93. * In debug mode more checks are performed, this will help you catch errors
  94. * quickly, however for production build it is recommended to turn off this flag
  95. * to speed up computation.
  96. */
  97. debug: boolean;
  98. }
  99. /**
  100. * Manages a simulation of physical forces acting on bodies and springs.
  101. */
  102. export interface PhysicsSimulator {
  103. /**
  104. * Array of bodies, registered with current simulator
  105. *
  106. * Note: To add new body, use addBody() method. This property is only
  107. * exposed for testing/performance purposes.
  108. */
  109. bodies: Body[];
  110. quadTree: QuadTree;
  111. /**
  112. * Array of springs, registered with current simulator
  113. *
  114. * Note: To add new spring, use addSpring() method. This property is only
  115. * exposed for testing/performance purposes.
  116. */
  117. springs: Spring[];
  118. /**
  119. * Returns settings with which current simulator was initialized
  120. */
  121. settings: PhysicsSettings;
  122. /**
  123. * Adds a new force to simulation
  124. * @param forceName force identifier
  125. * @param forceFunction the function to apply
  126. */
  127. addForce(forceName: string, forceFunction: ForceFunction): void;
  128. /**
  129. * Removes a force from the simulation
  130. * @param forceName force identifier
  131. */
  132. removeForce(forceName: string): void;
  133. /**
  134. * Returns a map of all registered forces
  135. */
  136. getForces(): Map<string, ForceFunction>;
  137. /**
  138. * Performs one step of force simulation.
  139. *
  140. * @returns true if system is considered stable; False otherwise.
  141. */
  142. step(): boolean;
  143. /**
  144. * Adds body to the system
  145. * @param body physical body
  146. * @returns added body
  147. */
  148. addBody(body: Body): Body;
  149. /**
  150. * Adds body to the system at given position
  151. * @param pos position of a body
  152. * @returns added body
  153. */
  154. addBodyAt(pos: Vector): Body;
  155. /**
  156. * Removes body from the system
  157. * @param body to remove
  158. * @returns true if body found and removed. falsy otherwise;
  159. */
  160. removeBody(body: Body): boolean;
  161. /**
  162. * Adds a spring to this simulation
  163. * @param body1 first body
  164. * @param body2 second body
  165. * @param springLength Ideal length for links
  166. * @param springCoefficient Hook's law coefficient. 1 - solid spring
  167. * @returns a handle for a spring. If you want to later remove
  168. * spring pass it to removeSpring() method.
  169. */
  170. addSpring(body1: Body, body2: Body, springLength: number, springCoefficient: number): Spring;
  171. /**
  172. * Returns amount of movement performed on last step() call
  173. */
  174. getTotalMovement(): number;
  175. /**
  176. * Removes spring from the system
  177. * @param spring to remove. Spring is an object returned by addSpring
  178. * @returns true if spring found and removed. falsy otherwise;
  179. */
  180. removeSpring(spring: Spring): boolean;
  181. getBestNewBodyPosition(neighbors: Body[]): Vector;
  182. /**
  183. * Returns bounding box which covers all bodies
  184. */
  185. getBBox(): BoundingBox;
  186. /**
  187. * Returns bounding box which covers all bodies
  188. */
  189. getBoundingBox(): BoundingBox;
  190. /** @deprecated invalidateBBox() is deprecated, bounds always recomputed on `getBBox()` call */
  191. invalidateBBox(): void;
  192. /**
  193. * Changes the gravity for the system
  194. * @param value Coulomb's law coefficient
  195. */
  196. gravity(value: number): number;
  197. /**
  198. * Changes the theta coeffitient for the system
  199. * @param value Theta coefficient from Barnes Hut simulation
  200. */
  201. theta(value: number): number;
  202. // TODO: create types declaration file for ngraph.random
  203. /**
  204. * Returns pseudo-random number generator instance
  205. */
  206. random: any;
  207. }
  208. /**
  209. * Force based layout for a given graph.
  210. */
  211. export interface Layout<T extends Graph> {
  212. /**
  213. * Performs one step of iterative layout algorithm
  214. * @returns true if the system should be considered stable; False otherwise.
  215. * The system is stable if no further call to `step()` can improve the layout.
  216. */
  217. step(): boolean;
  218. /**
  219. * For a given `nodeId` returns position
  220. * @param nodeId node identifier
  221. */
  222. getNodePosition(nodeId: NodeId): Vector;
  223. /**
  224. * Sets position of a node to a given coordinates
  225. * @param nodeId node identifier
  226. * @param x position of a node
  227. * @param y position of a node
  228. * @param z position of node (only if applicable to body)
  229. */
  230. setNodePosition(nodeId: NodeId, x: number, y: number, z?: number, ...c: number[]): void;
  231. /**
  232. * Gets Link position by link id
  233. * @param linkId link identifier
  234. * @returns from: {x, y} coordinates of link start
  235. * @returns to: {x, y} coordinates of link end
  236. */
  237. getLinkPosition(linkId: LinkId): { from: Vector; to: Vector };
  238. /**
  239. * @returns area required to fit in the graph. Object contains
  240. * `x1`, `y1` - top left coordinates
  241. * `x2`, `y2` - bottom right coordinates
  242. */
  243. getGraphRect(): { x1: number; y1: number; x2: number; y2: number };
  244. /**
  245. * Iterates over each body in the layout simulator and performs a callback(body, nodeId)
  246. * @param callbackfn the callback function
  247. */
  248. forEachBody(callbackfn: (value: Body, key: NodeId, map: Map<NodeId, Body>) => void): void;
  249. /**
  250. * Requests layout algorithm to pin/unpin node to its current position
  251. * Pinned nodes should not be affected by layout algorithm and always
  252. * remain at their position
  253. * @param node the node to pin/unpin
  254. * @param isPinned true to pin, false to unpin
  255. */
  256. pinNode(node: Node, isPinned: boolean): void;
  257. /**
  258. * Checks whether given graph's node is currently pinned
  259. * @param node the node to check
  260. */
  261. isNodePinned(node: Node): boolean;
  262. /**
  263. * Request to release all resources
  264. */
  265. dispose(): void;
  266. /**
  267. * Gets physical body for a given node id. If node is not found undefined
  268. * value is returned.
  269. * @param nodeId node identifier
  270. */
  271. getBody(nodeId: NodeId): Body | undefined;
  272. /**
  273. * Gets spring for a given edge.
  274. *
  275. * @param linkId link identifer.
  276. */
  277. getSpring(linkId: LinkId | Link): Spring;
  278. /**
  279. * Gets spring for a given edge.
  280. *
  281. * @param fromId node identifer - tail of the link
  282. * @param toId head of the link - head of the link
  283. */
  284. getSpring(fromId: NodeId, toId: NodeId): Spring | undefined;
  285. /**
  286. * Returns length of cumulative force vector. The closer this to zero - the more stable the system is
  287. */
  288. getForceVectorLength(): number;
  289. /**
  290. * @readonly Gets current physics simulator
  291. */
  292. readonly simulator: PhysicsSimulator;
  293. /**
  294. * Gets the graph that was used for layout
  295. */
  296. graph: T;
  297. /**
  298. * Gets amount of movement performed during last step operation
  299. */
  300. lastMove: number;
  301. }
  302. /**
  303. * Creates force based layout for a given graph.
  304. *
  305. * @param graph which needs to be laid out
  306. * @param physicsSettings if you need custom settings
  307. * for physics simulator you can pass your own settings here. If it's not passed
  308. * a default one will be created.
  309. */
  310. export default function createLayout<T extends Graph>(
  311. graph: T,
  312. physicsSettings?: Partial<PhysicsSettings>
  313. ): Layout<T>;
  314. }