plotboilerplate 1.29.0 → 1.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -13
- package/dist/index.esm.js +310 -57
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/plotboilerplate.browser.min.js +1 -1
- package/dist/plotboilerplate.browser.min.js.map +1 -1
- package/dist/plotboilerplate.js +266 -36
- package/dist/plotboilerplate.min.js +1 -1
- package/package.json +1 -1
- package/src/cjs/Circle.d.ts +17 -1
- package/src/cjs/Circle.js +21 -1
- package/src/cjs/Circle.js.map +1 -1
- package/src/cjs/CubicBezierCurve.d.ts +34 -3
- package/src/cjs/CubicBezierCurve.js +79 -12
- package/src/cjs/CubicBezierCurve.js.map +1 -1
- package/src/cjs/Line.d.ts +46 -1
- package/src/cjs/Line.js +80 -1
- package/src/cjs/Line.js.map +1 -1
- package/src/cjs/Polygon.d.ts +18 -4
- package/src/cjs/Polygon.js +56 -18
- package/src/cjs/Polygon.js.map +1 -1
- package/src/cjs/VertTuple.d.ts +24 -2
- package/src/cjs/VertTuple.js +28 -2
- package/src/cjs/VertTuple.js.map +1 -1
- package/src/cjs/Vertex.d.ts +2 -2
- package/src/cjs/Vertex.js +2 -2
- package/src/cjs/utils/algorithms/delaunay2voronoi.d.ts +2 -1
- package/src/cjs/utils/algorithms/delaunay2voronoi.js +8 -10
- package/src/cjs/utils/algorithms/delaunay2voronoi.js.map +1 -1
- package/src/cjs/utils/datastructures/PointSet.d.ts +63 -0
- package/src/cjs/utils/datastructures/PointSet.js +126 -0
- package/src/cjs/utils/datastructures/PointSet.js.map +1 -0
- package/src/cjs/utils/datastructures/VoronoiCell.d.ts +19 -2
- package/src/cjs/utils/datastructures/VoronoiCell.js +39 -12
- package/src/cjs/utils/datastructures/VoronoiCell.js.map +1 -1
- package/src/cjs/utils/renderers/VoronoiRenderer.d.ts +99 -0
- package/src/cjs/utils/renderers/VoronoiRenderer.js +147 -0
- package/src/cjs/utils/renderers/VoronoiRenderer.js.map +1 -0
- package/src/esm/Circle.d.ts +17 -1
- package/src/esm/Circle.js +21 -1
- package/src/esm/Circle.js.map +1 -1
- package/src/esm/CubicBezierCurve.d.ts +34 -3
- package/src/esm/CubicBezierCurve.js +79 -12
- package/src/esm/CubicBezierCurve.js.map +1 -1
- package/src/esm/Line.d.ts +46 -1
- package/src/esm/Line.js +80 -1
- package/src/esm/Line.js.map +1 -1
- package/src/esm/Polygon.d.ts +18 -4
- package/src/esm/Polygon.js +56 -18
- package/src/esm/Polygon.js.map +1 -1
- package/src/esm/VertTuple.d.ts +24 -2
- package/src/esm/VertTuple.js +28 -2
- package/src/esm/VertTuple.js.map +1 -1
- package/src/esm/Vertex.d.ts +2 -2
- package/src/esm/Vertex.js +2 -2
- package/src/esm/utils/algorithms/delaunay2voronoi.d.ts +2 -1
- package/src/esm/utils/algorithms/delaunay2voronoi.js +8 -10
- package/src/esm/utils/algorithms/delaunay2voronoi.js.map +1 -1
- package/src/esm/utils/datastructures/PointSet.d.ts +63 -0
- package/src/esm/utils/datastructures/PointSet.js +115 -0
- package/src/esm/utils/datastructures/PointSet.js.map +1 -0
- package/src/esm/utils/datastructures/VoronoiCell.d.ts +19 -2
- package/src/esm/utils/datastructures/VoronoiCell.js +39 -12
- package/src/esm/utils/datastructures/VoronoiCell.js.map +1 -1
- package/src/esm/utils/renderers/VoronoiRenderer.d.ts +99 -0
- package/src/esm/utils/renderers/VoronoiRenderer.js +142 -0
- package/src/esm/utils/renderers/VoronoiRenderer.js.map +1 -0
- package/src/index-gl.js +1 -2
- package/src/ts/Circle.ts +23 -1
- package/src/ts/CubicBezierCurve.ts +133 -48
- package/src/ts/Line.ts +84 -1
- package/src/ts/Polygon.ts +58 -18
- package/src/ts/VertTuple.ts +30 -2
- package/src/ts/Vertex.ts +2 -2
- package/src/ts/utils/_AppContext.ts_ +22 -0
- package/src/ts/utils/algorithms/delaunay2voronoi.ts +81 -88
- package/src/ts/utils/datastructures/PointSet.ts +135 -0
- package/src/ts/utils/datastructures/VoronoiCell.ts +198 -169
- package/src/ts/utils/renderers/VoronoiRenderer.ts +200 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @classdesc A simple voronoi cell (part of a voronoi diagram), stored as an array of
|
|
2
|
+
* @classdesc A simple voronoi cell (part of a voronoi diagram), stored as an array of
|
|
3
3
|
* adjacent triangles.
|
|
4
4
|
*
|
|
5
5
|
* @requires Line
|
|
@@ -10,13 +10,14 @@
|
|
|
10
10
|
* @author Ikaros Kappler
|
|
11
11
|
* @date 2018-04-11
|
|
12
12
|
* @modified 2018-05-04 Added the 'sharedVertex' param to the constructor. Extended open cells into 'infinity'.
|
|
13
|
-
* @modified 2019-10-25 Fixed a serious bug in the toPathArray function; cell with only one vertex (extreme cases) returned invalid arrays which broke the rendering.
|
|
13
|
+
* @modified 2019-10-25 Fixed a serious bug in the toPathArray function; cell with only one vertex (extreme cases) returned invalid arrays which broke the rendering.
|
|
14
14
|
* @modified 2019-12-09 Removed an unnecesary if-condition from the _calculateOpenEdgePoint(...) helper function.
|
|
15
15
|
* @modified 2020-05-18 Added function VoronoiCell.toPolygon().
|
|
16
16
|
* @modified 2020-08-12 Ported this class from vanilla JS to TypeScript.
|
|
17
17
|
* @modified 2020-08-17 Added some missing type declarations.
|
|
18
18
|
* @modified 2021-01-20 Members `triangles` and `sharedVertex` are now public.
|
|
19
|
-
* @
|
|
19
|
+
* @modified 2026-09-15 Added the `VoronoiCell.sharedVertexIndex` attribute.
|
|
20
|
+
* @version 1.2.0
|
|
20
21
|
*
|
|
21
22
|
* @file VoronoiCell
|
|
22
23
|
* @public
|
|
@@ -28,174 +29,202 @@ import { Triangle } from "../../Triangle";
|
|
|
28
29
|
import { Vertex } from "../../Vertex";
|
|
29
30
|
|
|
30
31
|
export class VoronoiCell {
|
|
32
|
+
/**
|
|
33
|
+
* @member {Array<Triangle>} triangles
|
|
34
|
+
* @memberof VoronoiCell
|
|
35
|
+
* @type {Array<Triangle>}
|
|
36
|
+
* @instance
|
|
37
|
+
*/
|
|
38
|
+
triangles: Array<Triangle>;
|
|
31
39
|
|
|
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
|
-
|
|
40
|
+
/**
|
|
41
|
+
* @member {Vertex} sharedVertex
|
|
42
|
+
* @memberof VoronoiCell
|
|
43
|
+
* @type {Vertex}
|
|
44
|
+
* @instance
|
|
45
|
+
*/
|
|
46
|
+
sharedVertex: Vertex;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
sharedVertexIndex: number;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The constructor.
|
|
55
|
+
*
|
|
56
|
+
* @constructor
|
|
57
|
+
* @name VoronoiCell
|
|
58
|
+
* @param {Triangle[]} triangles The passed triangle array must contain an ordered sequence of
|
|
59
|
+
* adjacent triangles.
|
|
60
|
+
* @param {Vertex} sharedVertex This is the 'center' of the voronoi cell; all triangles must share
|
|
61
|
+
* that vertex.
|
|
62
|
+
**/
|
|
63
|
+
constructor(triangles: Array<Triangle>, sharedVertex: Vertex, sharedVertexIndex: number) {
|
|
64
|
+
if (typeof triangles === "undefined") triangles = [];
|
|
65
|
+
if (typeof sharedVertex === "undefined") sharedVertex = new Vertex(0, 0);
|
|
66
|
+
this.triangles = triangles;
|
|
67
|
+
this.sharedVertex = sharedVertex;
|
|
68
|
+
this.sharedVertexIndex = sharedVertexIndex;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if the first and the last triangle in the path are NOT connected.
|
|
73
|
+
*
|
|
74
|
+
* @method isOpen
|
|
75
|
+
* @instance
|
|
76
|
+
* @memberof VoronoiCell
|
|
77
|
+
* @return {boolean}
|
|
78
|
+
**/
|
|
79
|
+
isOpen(): boolean {
|
|
80
|
+
// There must be at least three triangles
|
|
81
|
+
return this.triangles.length < 3 || !this.triangles[0].isAdjacent(this.triangles[this.triangles.length - 1]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Convert this Voronoi cell to a path polygon, consisting of all Voronoi cell corner points.
|
|
86
|
+
*
|
|
87
|
+
* Note that open Voronoi cell cannot properly converted to Polygons as they are considered
|
|
88
|
+
* infinite. 'Open' Voronoi edges will be cut off at their innermose vertices.
|
|
89
|
+
*
|
|
90
|
+
* @method toPolygon
|
|
91
|
+
* @instance
|
|
92
|
+
* @memberof VoronoiCell
|
|
93
|
+
* @return {Polygon}
|
|
94
|
+
**/
|
|
95
|
+
toPolygon(): Polygon {
|
|
96
|
+
return new Polygon(this.toPathArray(), this.isOpen());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get all 'umbrella' triangles for this Voronoi cell.
|
|
101
|
+
*
|
|
102
|
+
* The 'umbrella' is that sequence of triangles that covers the whole Voronoi cell and
|
|
103
|
+
* meet in the cell's centroid.
|
|
104
|
+
*
|
|
105
|
+
* @method getUmbrellaTriangles
|
|
106
|
+
* @instance
|
|
107
|
+
* @memberof VoronoiCell
|
|
108
|
+
* @return {Array<Triangle>}
|
|
109
|
+
**/
|
|
110
|
+
getUmbrellaTriangles(): Array<Triangle> {
|
|
111
|
+
var tris: Array<Triangle> = [];
|
|
112
|
+
// for (var i = 0; i < this.triangles.length; i++) {
|
|
113
|
+
// var delaunayTri = this.triangles[i];
|
|
114
|
+
// var tri = new Triangle(this.sharedVertex, delaunayTri.b, delaunayTri.c);
|
|
115
|
+
// tris.push(tri);
|
|
116
|
+
// }
|
|
117
|
+
var vertices = this.toPathArray();
|
|
118
|
+
var n = vertices.length;
|
|
119
|
+
for (var i = 0; i < n; i++) {
|
|
120
|
+
var tri = new Triangle(this.sharedVertex, vertices[i], vertices[(i + 1) % n]);
|
|
121
|
+
tris.push(tri);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return tris;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Convert the voronoi cell path data to an SVG polygon data string.
|
|
129
|
+
*
|
|
130
|
+
* "x0,y0 x1,y1 x2,y2 ..."
|
|
131
|
+
*
|
|
132
|
+
* @method toPathSVGString
|
|
133
|
+
* @instance
|
|
134
|
+
* @memberof VoronoiCell
|
|
135
|
+
* @return {string}
|
|
136
|
+
**/
|
|
137
|
+
toPathSVGString(): string {
|
|
138
|
+
if (this.triangles.length == 0) return "";
|
|
139
|
+
const arr: Array<Vertex> = this.toPathArray();
|
|
140
|
+
return arr
|
|
141
|
+
.map((vert: Vertex) => {
|
|
142
|
+
return "" + vert.x + "," + vert.y;
|
|
143
|
+
})
|
|
144
|
+
.join(" ");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Convert the voronoi cell path data to an array.
|
|
149
|
+
*
|
|
150
|
+
* [vertex0, vertex1, vertex2, ... ]
|
|
151
|
+
*
|
|
152
|
+
* @method toPathArray
|
|
153
|
+
* @instance
|
|
154
|
+
* @memberof VoronoiCell
|
|
155
|
+
* @return {Vertex[]}
|
|
156
|
+
**/
|
|
157
|
+
toPathArray(): Array<Vertex> {
|
|
158
|
+
if (this.triangles.length == 0) return [];
|
|
159
|
+
if (this.triangles.length == 1) return [this.triangles[0].getCircumcircle().center];
|
|
160
|
+
const arr: Array<Vertex> = [];
|
|
161
|
+
|
|
162
|
+
// Working for path begin
|
|
163
|
+
if (false && this.isOpen()) arr.push(VoronoiCell._calcOpenEdgePoint(this.triangles[0], this.triangles[1], this.sharedVertex));
|
|
164
|
+
|
|
165
|
+
for (var t = 0; t < this.triangles.length; t++) {
|
|
166
|
+
var cc = this.triangles[t].getCircumcircle();
|
|
167
|
+
arr.push(cc.center);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Urgh, this is not working right now.
|
|
171
|
+
/* if( false && this.isOpen() )
|
|
143
172
|
arr.push( VoronoiCell._calcOpenEdgePoint( this.triangles[ this.triangles.length-1 ], this.triangles[ this.triangles.length-2 ], this.sharedVertex ) );
|
|
144
173
|
*/
|
|
145
|
-
|
|
146
|
-
return arr;
|
|
147
|
-
}
|
|
148
174
|
|
|
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
|
-
|
|
175
|
+
return arr;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* A helper function.
|
|
180
|
+
*
|
|
181
|
+
* Calculate the 'infinite' open edge point based on the open path triangle
|
|
182
|
+
* 'tri' and its neighbour 'neigh'.
|
|
183
|
+
*
|
|
184
|
+
* This function is used to determine outer hull points.
|
|
185
|
+
*
|
|
186
|
+
* @method _calcOpenEdhePoint
|
|
187
|
+
* @private
|
|
188
|
+
* @static
|
|
189
|
+
* @instance
|
|
190
|
+
* @memberof VoronoiCell
|
|
191
|
+
* @return {Vertex}
|
|
192
|
+
**/
|
|
193
|
+
private static _calcOpenEdgePoint(tri: Triangle, neigh: Triangle, sharedVertex: Vertex): Vertex {
|
|
194
|
+
const center: Vertex = tri.getCircumcircle().center;
|
|
195
|
+
// Find non-adjacent edge (=outer edge)
|
|
196
|
+
const edgePoint: Vertex = VoronoiCell._findOuterEdgePoint(tri, neigh, sharedVertex);
|
|
197
|
+
// const perpendicular : Vertex = VoronoiCell._perpendicularLinePoint( sharedVertex, edgePoint, center );
|
|
198
|
+
const perpendicular: Vertex = new Line(sharedVertex, edgePoint).getClosestPoint(center);
|
|
199
|
+
// It is not necesary to make a difference on the determinant here
|
|
200
|
+
const openEdgePoint: Vertex = new Vertex(
|
|
201
|
+
perpendicular.x + (center.x - perpendicular.x) * 1000,
|
|
202
|
+
perpendicular.y + (center.y - perpendicular.y) * 1000
|
|
203
|
+
);
|
|
204
|
+
return openEdgePoint;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A helper function.
|
|
209
|
+
*
|
|
210
|
+
* Find the outer (not adjacent) vertex in triangle 'tri' which has triangle 'neighbour'.
|
|
211
|
+
*
|
|
212
|
+
* This function is used to determine outer hull points.
|
|
213
|
+
*
|
|
214
|
+
* @return {Vertex}
|
|
215
|
+
**/
|
|
216
|
+
private static _findOuterEdgePoint(tri: Triangle, neighbour: Triangle, sharedVertex: Vertex): Vertex {
|
|
217
|
+
if (tri.a.equals(sharedVertex)) {
|
|
218
|
+
if (neighbour.a.equals(tri.b) || neighbour.b.equals(tri.b) || neighbour.c.equals(tri.b)) return tri.c;
|
|
219
|
+
else return tri.b;
|
|
220
|
+
}
|
|
221
|
+
if (tri.b.equals(sharedVertex)) {
|
|
222
|
+
if (neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a)) return tri.c;
|
|
223
|
+
else return tri.a;
|
|
224
|
+
}
|
|
225
|
+
// Here:
|
|
226
|
+
// tri.c.equals(sharedVertex)
|
|
227
|
+
if (neighbour.a.equals(tri.a) || neighbour.b.equals(tri.a) || neighbour.c.equals(tri.a)) return tri.b;
|
|
228
|
+
else return tri.a;
|
|
229
|
+
}
|
|
201
230
|
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refactored all Voronoi helper functions for drawing the diagram (demo 07) into this class.
|
|
3
|
+
*
|
|
4
|
+
* @author Ikaros Kappler
|
|
5
|
+
* @date 2026-09-13
|
|
6
|
+
* @modified 2026-09-15 Ported to Typescript.
|
|
7
|
+
* @version 1.0.1
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Bounds } from "../../Bounds";
|
|
11
|
+
import { Polygon } from "../../Polygon";
|
|
12
|
+
import { Triangle } from "../../Triangle";
|
|
13
|
+
import { DrawLib } from "../../interfaces";
|
|
14
|
+
import { convexPolygonIncircle } from "../algorithms/convexPolygonIncircle";
|
|
15
|
+
import { sutherlandHodgman } from "../algorithms/sutherlandHodgman";
|
|
16
|
+
import { cloneVertexArray } from "../cloneVertexArray";
|
|
17
|
+
import { PointSet } from "../datastructures/PointSet";
|
|
18
|
+
import { VoronoiCell } from "../datastructures/VoronoiCell";
|
|
19
|
+
|
|
20
|
+
export interface IVoronoiConfig {
|
|
21
|
+
makeVoronoiDiagram: boolean;
|
|
22
|
+
drawPoints: boolean;
|
|
23
|
+
drawTriangles: boolean;
|
|
24
|
+
drawCircumCircles: boolean;
|
|
25
|
+
drawCubicCurves: boolean;
|
|
26
|
+
fillVoronoiCells: boolean;
|
|
27
|
+
voronoiOutlineColor: string;
|
|
28
|
+
voronoiCellColor: string;
|
|
29
|
+
voronoiCubicThreshold: number;
|
|
30
|
+
voronoiCellScale: number;
|
|
31
|
+
clipVoronoiCells: boolean;
|
|
32
|
+
drawClipBox: boolean;
|
|
33
|
+
drawUnclippedVoronoiCells: boolean;
|
|
34
|
+
drawVoronoiIncircles: boolean;
|
|
35
|
+
drawVoronoiOutlines: boolean;
|
|
36
|
+
pointCount: number;
|
|
37
|
+
horizontalSafeArea: number;
|
|
38
|
+
verticalSafeArea: number;
|
|
39
|
+
voronoiCellLineWidth: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IVoronoiContext {
|
|
43
|
+
pointSet: PointSet;
|
|
44
|
+
triangles: Array<Triangle>;
|
|
45
|
+
voronoiCells: Array<VoronoiCell>;
|
|
46
|
+
// cells: Array<VoronoiCell>;
|
|
47
|
+
config: IVoronoiConfig;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class VoronoiRenderer {
|
|
51
|
+
voronoiContext: IVoronoiContext;
|
|
52
|
+
|
|
53
|
+
constructor(voronoiContext: IVoronoiContext) {
|
|
54
|
+
this.voronoiContext = voronoiContext;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Draw the given triangle with the specified (CSS-) color.
|
|
59
|
+
*
|
|
60
|
+
* @name draw
|
|
61
|
+
* @instance
|
|
62
|
+
* @memberof VoronoiRenderer
|
|
63
|
+
* @param {DrawLib} draw
|
|
64
|
+
* @param {Triangle} t
|
|
65
|
+
* @param {string} color
|
|
66
|
+
* @return {void}
|
|
67
|
+
*/
|
|
68
|
+
draw(draw: DrawLib<any>, fill: DrawLib<any>) {
|
|
69
|
+
// Draw circumcircles
|
|
70
|
+
if (this.voronoiContext.config.drawCircumCircles) {
|
|
71
|
+
VoronoiRenderer.drawCircumCircles(draw, this.voronoiContext.triangles);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// An array of VoronoiCells.
|
|
75
|
+
var clipBoxPolygon = Bounds.computeFromVertices(this.voronoiContext.pointSet.points).toPolygon();
|
|
76
|
+
if (this.voronoiContext.config.drawClipBox) {
|
|
77
|
+
draw.polygon(clipBoxPolygon, "rgba(192,192,192,0.25)");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for (var v in this.voronoiContext.voronoiCells) {
|
|
81
|
+
var cell = this.voronoiContext.voronoiCells[v];
|
|
82
|
+
this._drawCell(draw, fill, clipBoxPolygon, cell);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Draw the given triangle with the specified (CSS-) color.
|
|
88
|
+
*
|
|
89
|
+
* @name _drawCell
|
|
90
|
+
* @instance
|
|
91
|
+
* @private
|
|
92
|
+
* @memberof VoronoiRenderer
|
|
93
|
+
* @param {DrawLib} draw
|
|
94
|
+
* @param {Polygon} clipBoxPolygon
|
|
95
|
+
* @param {VoronoiCell} cell
|
|
96
|
+
* @return {void}
|
|
97
|
+
*/
|
|
98
|
+
private _drawCell(draw: DrawLib<any>, fill: DrawLib<any>, clipBoxPolygon: Polygon, cell: VoronoiCell) {
|
|
99
|
+
var cellPolygon = cell.toPolygon();
|
|
100
|
+
cellPolygon.scale(this.voronoiContext.config.voronoiCellScale, cell.sharedVertex);
|
|
101
|
+
|
|
102
|
+
// Draw large (unclipped) Voronoi cell
|
|
103
|
+
if (
|
|
104
|
+
this.voronoiContext.config.drawVoronoiOutlines &&
|
|
105
|
+
(!this.voronoiContext.config.clipVoronoiCells || this.voronoiContext.config.drawUnclippedVoronoiCells)
|
|
106
|
+
) {
|
|
107
|
+
draw.polyline(
|
|
108
|
+
cellPolygon.vertices,
|
|
109
|
+
false,
|
|
110
|
+
this.voronoiContext.config.clipVoronoiCells ? "rgba(128,128,128,0.333)" : this.voronoiContext.config.voronoiOutlineColor,
|
|
111
|
+
this.voronoiContext.config.voronoiCellLineWidth ?? 2.0
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Apply clipping?
|
|
116
|
+
if (this.voronoiContext.config.clipVoronoiCells) {
|
|
117
|
+
// Clone the array here: convert Array<XYCoords> to Array<Vertex>
|
|
118
|
+
// polygon = new Polygon(cloneVertexArray(sutherlandHodgman(polygon.vertices, clipBoxPolygon.vertices)), false);
|
|
119
|
+
cellPolygon = VoronoiRenderer.clipVoronoiPolygon(cellPolygon, clipBoxPolygon);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (this.voronoiContext.config.drawVoronoiOutlines && this.voronoiContext.config.clipVoronoiCells) {
|
|
123
|
+
draw.polygon(
|
|
124
|
+
cellPolygon,
|
|
125
|
+
this.voronoiContext.config.voronoiOutlineColor,
|
|
126
|
+
this.voronoiContext.config.voronoiCellLineWidth ?? 2.0
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if ((!cell.isOpen() || this.voronoiContext.config.clipVoronoiCells) && cell.triangles.length >= 3) {
|
|
131
|
+
if (this.voronoiContext.config.drawCubicCurves) {
|
|
132
|
+
var cbezier = cellPolygon.toCubicBezierData(this.voronoiContext.config.voronoiCubicThreshold);
|
|
133
|
+
if (this.voronoiContext.config.fillVoronoiCells) {
|
|
134
|
+
fill.cubicBezierPath(cbezier, this.voronoiContext.config.voronoiCellColor);
|
|
135
|
+
} else {
|
|
136
|
+
draw.cubicBezierPath(cbezier, this.voronoiContext.config.voronoiCellColor);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (this.voronoiContext.config.drawVoronoiIncircles) {
|
|
140
|
+
var result = convexPolygonIncircle(cellPolygon);
|
|
141
|
+
var circle = result.circle;
|
|
142
|
+
// var triangle = result.triangle;
|
|
143
|
+
// Here we should have found the best inlying circle (and the corresponding triangle)
|
|
144
|
+
// inside the Voronoi cell.
|
|
145
|
+
draw.circle(circle.center, circle.radius, "rgba(255,192,0,1.0)", 2);
|
|
146
|
+
}
|
|
147
|
+
} // END cell is not open
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A function for drawing the triangles.
|
|
152
|
+
*
|
|
153
|
+
* @static
|
|
154
|
+
*/
|
|
155
|
+
drawTriangles(draw: DrawLib<any>) {
|
|
156
|
+
for (var i in this.voronoiContext.triangles) {
|
|
157
|
+
var t = this.voronoiContext.triangles[i];
|
|
158
|
+
VoronoiRenderer.drawTriangle(draw, t, this.voronoiContext.config.makeVoronoiDiagram ? "rgba(0,128,224,0.33)" : "#0088d8");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Draw the given triangle with the specified (CSS-) color.
|
|
164
|
+
*
|
|
165
|
+
* @static
|
|
166
|
+
* @name drawTriangle
|
|
167
|
+
* @memberof VoronoiRenderer
|
|
168
|
+
* @param {DrawLib} draw
|
|
169
|
+
* @param {Triangle} tri
|
|
170
|
+
* @param {string} color
|
|
171
|
+
* @return {void}
|
|
172
|
+
*/
|
|
173
|
+
static drawTriangle(draw: DrawLib<any>, tri: Triangle, color: string) {
|
|
174
|
+
// draw.line(t.a, t.b, color);
|
|
175
|
+
// draw.line(t.b, t.c, color);
|
|
176
|
+
// draw.line(t.c, t.a, color);
|
|
177
|
+
draw.polyline([tri.a, tri.b, tri.c], false, color);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Draw the circumcircles of all triangles.
|
|
182
|
+
*/
|
|
183
|
+
static drawCircumCircles(draw: DrawLib<any>, triangles: Array<Triangle>) {
|
|
184
|
+
for (var t in triangles) {
|
|
185
|
+
var cc = triangles[t].getCircumcircle();
|
|
186
|
+
draw.circle(cc.center, cc.radius, "#e86800");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Clip the convex (!) cell polygon by the convex (!) clipping polygon.
|
|
192
|
+
*
|
|
193
|
+
* @param cellPolygon
|
|
194
|
+
* @param clipBoxPolygon
|
|
195
|
+
* @returns
|
|
196
|
+
*/
|
|
197
|
+
static clipVoronoiPolygon(cellPolygon: Polygon, clipBoxPolygon: Polygon) {
|
|
198
|
+
return new Polygon(cloneVertexArray(sutherlandHodgman(cellPolygon.vertices, clipBoxPolygon.vertices)), false);
|
|
199
|
+
}
|
|
200
|
+
}
|