-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelaunay.js
227 lines (178 loc) · 6.56 KB
/
delaunay.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
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
////////////////////////////////////////////////////////////////////////////////
//
// Delaunay Triangulation Code, by Joshua Bell
//
// Inspired by: http://www.codeguru.com/cpp/data/mfc_database/misc/article.php/c8901/
//
// This work is hereby released into the Public Domain. To view a copy of the public
// domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or send
// a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,
// California, 94105, USA.
//
////////////////////////////////////////////////////////////////////////////////
(function(global) {
var EPSILON = 1.0e-6;
//------------------------------------------------------------
// Vertex class
//------------------------------------------------------------
function Vertex(x, y) {
this.x = x;
this.y = y;
}
//------------------------------------------------------------
// Triangle class
//------------------------------------------------------------
function Triangle(v0, v1, v2) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.calcCircumcircle();
}
Triangle.prototype.calcCircumcircle = function() {
// From: http://www.exaflop.org/docs/cgafaq/cga1.html
var A = this.v1.x - this.v0.x;
var B = this.v1.y - this.v0.y;
var C = this.v2.x - this.v0.x;
var D = this.v2.y - this.v0.y;
var E = A * (this.v0.x + this.v1.x) + B * (this.v0.y + this.v1.y);
var F = C * (this.v0.x + this.v2.x) + D * (this.v0.y + this.v2.y);
var G = 2.0 * (A * (this.v2.y - this.v1.y) - B * (this.v2.x - this.v1.x));
var dx, dy;
if (Math.abs(G) < EPSILON) {
// Collinear - find extremes and use the midpoint
var minx = Math.min(this.v0.x, this.v1.x, this.v2.x);
var miny = Math.min(this.v0.y, this.v1.y, this.v2.y);
var maxx = Math.max(this.v0.x, this.v1.x, this.v2.x);
var maxy = Math.max(this.v0.y, this.v1.y, this.v2.y);
this.center = new Vertex((minx + maxx) / 2, (miny + maxy) / 2);
dx = this.center.x - minx;
dy = this.center.y - miny;
} else {
var cx = (D * E - B * F) / G;
var cy = (A * F - C * E) / G;
this.center = new Vertex(cx, cy);
dx = this.center.x - this.v0.x;
dy = this.center.y - this.v0.y;
}
this.radius_squared = dx * dx + dy * dy;
this.radius = Math.sqrt(this.radius_squared);
};
Triangle.prototype.inCircumcircle = function(v) {
var dx = this.center.x - v.x;
var dy = this.center.y - v.y;
var dist_squared = dx * dx + dy * dy;
return (dist_squared <= this.radius_squared);
};
//------------------------------------------------------------
// Edge class
//------------------------------------------------------------
function Edge(v0, v1) {
this.v0 = v0;
this.v1 = v1;
}
Edge.prototype.equals = function(other) {
return (this.v0 === other.v0 && this.v1 === other.v1);
};
Edge.prototype.inverse = function() {
return new Edge(this.v1, this.v0);
};
//------------------------------------------------------------
// triangulate
//
// Perform the Delaunay Triangulation of a set of vertices.
//
// vertices: Array of Vertex objects
//
// returns: Array of Triangles
//------------------------------------------------------------
function triangulate(vertices) {
var triangles = [];
//
// First, create a "supertriangle" that bounds all vertices
//
var st = createBoundingTriangle(vertices);
triangles.push(st);
//
// Next, begin the triangulation one vertex at a time
//
vertices.forEach(function(vertex) {
// NOTE: This is O(n^2) - can be optimized by sorting vertices
// along the x-axis and only considering triangles that have
// potentially overlapping circumcircles
triangles = addVertex(vertex, triangles);
});
//
// Remove triangles that shared edges with "supertriangle"
//
triangles = triangles.filter(function(triangle) {
return !(triangle.v0 == st.v0 || triangle.v0 == st.v1 || triangle.v0 == st.v2 ||
triangle.v1 == st.v0 || triangle.v1 == st.v1 || triangle.v1 == st.v2 ||
triangle.v2 == st.v0 || triangle.v2 == st.v1 || triangle.v2 == st.v2);
});
return triangles;
}
// Internal: create a triangle that bounds the given vertices, with room to spare
function createBoundingTriangle(vertices) {
// NOTE: There's a bit of a heuristic here. If the bounding triangle
// is too large and you see overflow/underflow errors. If it is too small
// you end up with a non-convex hull.
var minx, miny, maxx, maxy;
vertices.forEach(function(vertex) {
if (minx === undefined || vertex.x < minx) { minx = vertex.x; }
if (miny === undefined || vertex.y < miny) { miny = vertex.y; }
if (maxx === undefined || vertex.x > maxx) { maxx = vertex.x; }
if (maxy === undefined || vertex.y > maxy) { maxy = vertex.y; }
});
var dx = (maxx - minx) * 10;
var dy = (maxy - miny) * 10;
var stv0 = new Vertex(minx - dx, miny - dy * 3);
var stv1 = new Vertex(minx - dx, maxy + dy);
var stv2 = new Vertex(maxx + dx * 3, maxy + dy);
return new Triangle(stv0, stv1, stv2);
}
// Internal: update triangulation with a vertex
function addVertex(vertex, triangles) {
var edges = [];
// Remove triangles with circumcircles containing the vertex
triangles = triangles.filter(function(triangle) {
if (triangle.inCircumcircle(vertex)) {
edges.push(new Edge(triangle.v0, triangle.v1));
edges.push(new Edge(triangle.v1, triangle.v2));
edges.push(new Edge(triangle.v2, triangle.v0));
return false;
}
return true;
});
edges = uniqueEdges(edges);
// Create new triangles from the unique edges and new vertex
edges.forEach(function(edge) {
triangles.push(new Triangle(edge.v0, edge.v1, vertex));
});
return triangles;
}
// Internal: remove duplicate edges from an array
function uniqueEdges(edges) {
// TODO: This is O(n^2), make it O(n) with a hash or some such
var uniqueEdges = [];
for (var i = 0; i < edges.length; ++i) {
var edge1 = edges[i];
var unique = true;
for (var j = 0; j < edges.length; ++j) {
if (i === j)
continue;
var edge2 = edges[j];
if (edge1.equals(edge2) || edge1.inverse().equals(edge2)) {
unique = false;
break;
}
}
if (unique)
uniqueEdges.push(edge1);
}
return uniqueEdges;
}
global.Vertex = Vertex;
global.Triangle = Triangle;
global.Edge = Edge;
global.triangulate = triangulate;
}(self));