UNPKG

10.9 kBTypeScriptView Raw
1// Last module patch version validated against: 1.1.4
2
3// --------------------------------------------------------------------------
4// Shared Type Definitions and Interfaces
5// --------------------------------------------------------------------------
6
7/**
8 * The VoronoiPoint interface is defined as a cue that the array is strictly of type [number, number] with two elements
9 * for x and y coordinates. However, it is used as a base for interface definitions, and [number, number]
10 * cannot be extended.
11 */
12export interface VoronoiPoint extends Array<number> {
13 0: number;
14 1: number;
15}
16
17/**
18 * The VoronoiPointPair interface is defined as a cue that the array is strictly of type [[number, number], [number, number]] with two elements, one
19 * for each point containing the respective x and y coordinates. However, it is used as a base for interface definitions, and
20 * [[number, number], [number, number]] cannot be extended.
21 */
22export interface VoronoiPointPair extends Array<[number, number]> {
23 0: [number, number];
24 1: [number, number];
25}
26
27/**
28 * A Voronoi Polygon is represented as an array of points [x, y] where x and y are the point coordinates, and a data field that refers to the corresponding element in data.
29 * Polygons are open: they do not contain a closing point that duplicates the first point; a triangle, for example, is an array of three points.
30 * Polygons are also counterclockwise, assuming the origin ⟨0,0⟩ is in the top-left corner.
31 *
32 * The generic refers to the type of the data for the corresponding element.
33 */
34export interface VoronoiPolygon<T> extends Array<[number, number]> {
35 /**
36 * The input data corresponding to this Voronoi polygon.
37 */
38 data: T;
39}
40
41/**
42 * Voronoi Triangle is a three-element array of elements from data.
43 *
44 * The generic refers to the type of the data for the corresponding element.
45 */
46export type VoronoiTriangle<T> = [T, T, T];
47
48/**
49 * A Voronoi Site in the diagram is an array [x, y] with two additional properties:
50 * index and data.
51 *
52 * The generic refers to the type of the data for the corresponding element.
53 */
54export interface VoronoiSite<T> extends VoronoiPoint {
55 /**
56 * The Voronoi Site’s index, corresponding to the associated input point.
57 */
58 index: number;
59
60 /**
61 * The input data corresponding to this site.
62 */
63 data: T;
64}
65
66/**
67 * A Voronoi Cell in the diagram is an object with the following properties:
68 * site and halfedges
69 *
70 * The generic refers to the type of the data for the corresponding element.
71 */
72export interface VoronoiCell<T> {
73 /**
74 * The Voronoi Site of the cell’s associated input point.
75 */
76 site: VoronoiSite<T>;
77
78 /**
79 * An array of indexes into diagram.edges representing the cell’s polygon.
80 */
81 halfedges: number[];
82}
83
84/**
85 * Voronoi Edge in the diagram is an array [[x0, y0], [x1, y1]] with two additional properties:
86 * left and right.
87 *
88 * The generic refers to the type of the data for the corresponding element.
89 */
90export interface VoronoiEdge<T> extends VoronoiPointPair {
91 /**
92 * The Voronoi site on the left side of the edge.
93 */
94 left: VoronoiSite<T>;
95
96 /**
97 * The Voronoi site on the right side of the edge; `null` for a clipped border edge.
98 */
99 right: VoronoiSite<T> | null;
100}
101
102/**
103 * Voronoi Link for an edge in the mesh created by the Delaunay triangulation of the specified data array.
104 * Each link has the following attributes: source and target.
105 *
106 * The generic refers to the type of the data for the corresponding element.
107 */
108export interface VoronoiLink<T> {
109 /**
110 * The source node, an element in data.
111 */
112 source: T;
113
114 /**
115 * The target node, an element in data.
116 */
117 target: T;
118}
119
120/**
121 * A Voronoi Layout.
122 *
123 * The generic refers to the type of the data for the corresponding element.
124 */
125export interface VoronoiLayout<T> {
126 /**
127 * Computes the Voronoi diagram for the specified data points.
128 * @param data Array of data elements
129 */
130 (data: T[]): VoronoiDiagram<T>;
131
132 /**
133 * Return the current x-coordinate accessor,
134 * which defaults to accessing the first element of an array (i.e. at index 0).
135 */
136 x(): (d: T) => number;
137 /**
138 * Set the x-coordinate accessor and return the layout.
139 *
140 * @param x An accessor function which takes a data element as input and return a
141 * numeric value for the x-coordinate.
142 */
143 x(x: (d: T) => number): this;
144
145 /**
146 * Return the current y-coordinate accessor,
147 * which defaults to accessing the second element of an array (i.e. at index 1).
148 */
149 y(): (d: T) => number;
150 /**
151 * Set the y-coordinate accessor and return the layout.
152 *
153 * @param y An accessor function which takes a data element as input and return a
154 * numeric value for the y-coordinate.
155 */
156 y(y: (d: T) => number): this;
157
158 /**
159 * Returns the current clip extent which defaults to null.
160 *
161 * The extent bounds are specified as an array [[x0, y0], [x1, y1]],
162 * where x0 is the left side of the extent, y0 is the top,
163 * x1 is the right and y1 is the bottom.
164 *
165 * A clip extent is required when using voronoi.polygons.
166 */
167 extent(): [[number, number], [number, number]] | null;
168 /**
169 * Set the clip extent of the Voronoi layout to the specified bounds and return the layout.
170 *
171 * A clip extent is required when using voronoi.polygons.
172 *
173 * @param extent The extent bounds are specified as an array [[x0, y0], [x1, y1]],
174 * where x0 is the left side of the extent, y0 is the top, x1 is the right and y1 is the bottom.
175 */
176 extent(extent: [[number, number], [number, number]]): this;
177
178 /**
179 * Get the clip size of the Voronoi layout. Size is an alias for voronoi.extent
180 * where the minimum x and y of the extent are ⟨0,0⟩.
181 */
182 size(): [number, number] | null;
183 /**
184 * Set the clip size and return the layout.
185 *
186 * Size is an alias for voronoi.extent where the minimum x and y of the extent are ⟨0,0⟩.
187 *
188 * @param size An array representing the x- and y-size of the clip extent,
189 * where the minimum x and y of the extent are ⟨0,0⟩.
190 */
191 size(size: [number, number]): this;
192
193 /**
194 * Return an array of polygons clipped to the extent, one for each input point in the specified data points,
195 * corresponding to the cells in the computed Voronoi diagram.
196 *
197 * Each polygon is represented as an array of points [x, y] where x and y are the point coordinates,
198 * and a data field that refers to the corresponding element in data.
199 * Polygons are open: they do not contain a closing point that duplicates the first point;
200 * a triangle, for example, is an array of three points. Polygons are also counterclockwise,
201 * assuming the origin ⟨0,0⟩ is in the top-left corner.
202 *
203 * If the cell’s site is coincident with an earlier site, the associated polygon is null.
204 *
205 * Important: Using polygon requires the extent to be set for the layout.
206 *
207 * @param data Array of data points.
208 */
209 polygons(data: T[]): Array<VoronoiPolygon<T>>;
210
211 /**
212 * Return the Delaunay triangulation of the specified data array as an array of triangles.
213 * Each triangle is a three-element array of elements from data.
214 *
215 * @param data Array of data points.
216 */
217 triangles(data: T[]): Array<VoronoiTriangle<T>>;
218
219 /**
220 * Return the Delaunay triangulation of the specified data array as an array of links.
221 * Each link has source and target attributes referring to elements in data.
222 *
223 * @param data Array of data points.
224 */
225 links(data: T[]): Array<VoronoiLink<T>>;
226}
227
228/**
229 * Computed Voronoi diagram
230 *
231 * The generic refers to the type of the data for the corresponding element.
232 */
233export interface VoronoiDiagram<T> {
234 /**
235 * Array of Voronoi Edges
236 */
237 edges: Array<VoronoiEdge<T>>;
238
239 /**
240 * Array of Voronoi Cells, one per input point; a cell may be null for a coincident point.
241 */
242 cells: Array<VoronoiCell<T> | null>;
243
244 /**
245 * Return an array of polygons clipped to the extent, one for each cell in the diagram.
246 * Each polygon is represented as an array of points [x, y] where x and y are the point coordinates,
247 * and a data field that refers to the corresponding element in data.
248 * Polygons are open: they do not contain a closing point that duplicates the first point;
249 * a triangle, for example, is an array of three points. Polygons are also counterclockwise,
250 * assuming the origin ⟨0,0⟩ is in the top-left corner.
251 *
252 * If the cell’s site is coincident with an earlier site, the associated polygon is null.
253 */
254 polygons(): Array<VoronoiPolygon<T>>;
255
256 /**
257 * Returns the Delaunay triangulation of the specified data array as an array of triangles.
258 * Each triangle is a three-element array of elements from data.
259 * Since the triangulation is computed as the dual of the Voronoi diagram, and the Voronoi diagram is clipped by the extent,
260 * a subset of the Delaunay triangulation is returned.
261 */
262 triangles(): Array<VoronoiTriangle<T>>;
263
264 /**
265 * Returns the Delaunay triangulation of the specified data array as an array of links, one for each edge in the mesh.
266 * Each link has the following attributes:
267 * - source (the source node, an element in data)
268 * - target (the target node, an element in data)
269 *
270 * Since the triangulation is computed as the dual of the Voronoi diagram, and the Voronoi diagram is clipped by the extent, a subset of the Delaunay links is returned.
271 */
272 links(): Array<VoronoiLink<T>>;
273
274 /**
275 * Return the nearest Voronoi Site to point [x, y]. If radius is specified, only sites within radius distance are considered.
276 * If no Voronoi Site can be found (within the specified radius), null is returned.
277 *
278 * @param x x-coordinate
279 * @param y y-coordinate
280 * @param radius Optional parameter for search radius around [x, y]
281 */
282 find(x: number, y: number, radius?: number): VoronoiSite<T> | null;
283}
284
285// --------------------------------------------------------------------------
286// voronoi Export
287// --------------------------------------------------------------------------
288
289/**
290 * Creates a new Voronoi layout with default x- and y- accessors and a null extent.
291 * x- and y-accessors may have to be set to correspond to the data type provided by the
292 * generic.
293 *
294 * The generic refers to the type of the data for the corresponding element.
295 * Without specifying a generic the layout is assumed to be based on data represented
296 * by a two-dimensional coordinate `[number, number]` for x- and y-coordinate, respectively.
297 */
298export function voronoi<T = [number, number]>(): VoronoiLayout<T>;