-
Notifications
You must be signed in to change notification settings - Fork 31
/
Delaunay.js
363 lines (269 loc) · 8.02 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
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
// Based on http://www.travellermap.com/tmp/delaunay.htm
var vertices = [];
var rasters;
var group;
function onMouseDown(event) {
rasters = document.getItems({
type: [Raster, PlacedFile],
selected: true
});
if(!rasters[0]) {
Dialog.alert("Select an image first!");
return
}
if(event.modifiers.shift) {
vertices = [];
group = null;
return;
}
AddAt(event.point.x, event.point.y);
}
function AddAt( x, y ) {
vertices.push( new Vertex( x, y ) );
Render();
}
function Render()
{
if (group) group.remove();
group = new Group();
group.moveBelow(rasters[0]);
var triangles = Triangulate( vertices );
for( i in triangles ) {
var triangle = triangles[i];
var shape = createTriangle(triangle);
if (shape) group.appendTop(shape);
}
}
function createTriangle(triangle) {
//if (!validateTriangle(triangle)) return null;
var shape = new Path();
shape.add( new Point(triangle.v0.x, triangle.v0.y ));
shape.add( new Point(triangle.v1.x, triangle.v1.y ));
shape.add( new Point(triangle.v2.x, triangle.v2.y ));
// COLOR GRADIENTS
//var c1 = rasters[0].getAverageColor(new Point(triangle.v0.x, triangle.v0.y));
//var c2 = rasters[0].getAverageColor(new Point(triangle.v1.x, triangle.v1.y));
// Jonathan Puckey's shading style
mxx = max3(triangle.v0.x, triangle.v1.x, triangle.v2.x);
mxy = max3(triangle.v0.y, triangle.v1.y, triangle.v2.y);
mnx = min3(triangle.v0.x, triangle.v1.x, triangle.v2.x);
mny = min3(triangle.v0.y, triangle.v1.y, triangle.v2.y);
var c1 = rasters[0].getAverageColor(shape);
var c2 = c1.clone();
c1.red *=1.1;
c1.green *=1.1;
c1.blue *=1.1;
c2.red *=0.7;
c2.green *=0.7;
c2.blue *=0.7;
var gr = new Gradient() { type: 'linear', stops: [ new GradientStop(c1, 0.0), new GradientStop(c2, 1.0)]};
var or = new Point(mxx, mxy);
var ds = new Point(mnx, mny);
shape.fillColor = new GradientColor(gr, or, ds);
shape.closed = true;
return shape;
}
function validateTriangle(triangle) {
var l1 = len(triangle.v0, triangle.v1);
var l2 = len(triangle.v1, triangle.v2);
var l3 = len(triangle.v2, triangle.v0);
var max = max3(l1,l2,l3);
var min = max3(l1,l2,l3);
l1 /= max;
l2 /= max;
l3 /= max;
var min = min3(l1,l2,l3);
if (1/min>5) return false;
return true;
}
function max3( a, b, c ) { return ( a >= b && a >= c ) ? a : ( b >= a && b >= c ) ? b : c; }
function min3( a, b, c ) { return ( a <= b && a <= c ) ? a : ( b <= a && b <= c ) ? b : c; }
function len ( p1, p2) { return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); }
// DELAUNAY //
var EPSILON = 1.0e-6;
//------------------------------------------------------------
// Vertex class
//------------------------------------------------------------
function Vertex( x, y )
{
this.x = x;
this.y = y;
} // Vertex
//------------------------------------------------------------
// Triangle class
//------------------------------------------------------------
function Triangle( v0, v1, v2 )
{
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.CalcCircumcircle();
} // Triangle
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 = min3( this.v0.x, this.v1.x, this.v2.x );
var miny = min3( this.v0.y, this.v1.y, this.v2.y );
var maxx = max3( this.v0.x, this.v1.x, this.v2.x );
var maxy = max3( 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 );
}; // CalcCircumcircle
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 );
}; // InCircumcircle
//------------------------------------------------------------
// Edge class
//------------------------------------------------------------
function Edge( v0, v1 )
{
this.v0 = v0;
this.v1 = v1;
} // Edge
//------------------------------------------------------------
// 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
//
var i;
for( i in vertices )
{
// 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
var vertex = vertices[i];
AddVertex( vertex, triangles );
}
//
// Remove triangles that shared edges with "supertriangle"
//
for( i in triangles )
{
var triangle = triangles[i];
if( 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 )
{
delete triangles[i];
}
}
return triangles;
} // Triangulate
// 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;
for( var i in vertices )
{
var vertex = vertices[i];
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 );
} // CreateBoundingTriangle
// Internal: update triangulation with a vertex
function AddVertex( vertex, triangles )
{
var edges = [];
// Remove triangles with circumcircles containing the vertex
var i;
for( i in triangles )
{
var triangle = triangles[i];
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 ) );
delete triangles[i];
}
}
edges = UniqueEdges( edges );
// Create new triangles from the unique edges and new vertex
for( i in edges )
{
var edge = edges[i];
triangles.push( new Triangle( edge.v0, edge.v1, vertex ) );
}
} // AddVertex
// 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 in edges )
{
var edge1 = edges[i];
var unique = true;
for( var j in edges )
{
if( i != j )
{
var edge2 = edges[j];
if( ( edge1.v0 == edge2.v0 && edge1.v1 == edge2.v1 ) ||
( edge1.v0 == edge2.v1 && edge1.v1 == edge2.v0 ) )
{
unique = false;
break;
}
}
}
if( unique )
{
uniqueEdges.push( edge1 );
}
}
return uniqueEdges;
} // UniqueEdges