-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.html
206 lines (173 loc) · 6.3 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Delatin, a fast JavaScript terrain mesh tool</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
font: 16px/1.2 "Helvetica Neue", Helvetica, Arial, sans-serif;
padding: 0 10px;
}
#container {
max-width: 640px;
margin: 0 auto;
}
/*body {
font: 16px/1.3 -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
*/h1 {
font-size: 32px;
font-weight: normal;
margin-bottom: 15px;
}
p {
margin: 0 0 10px;
}
#info {
margin: 5px;
color: #666;
}
#info code {
color: black;
}
#controls {
margin-top: 20px;
}
</style>
</head>
<body><div id="container">
<h1>Delatin demo</h1>
<p>A fast JavaScript terrain mesh generation tool. Approximates a height field with a Delaunay triangulation, minimizing the amount of points and triangles for a given maximum error. <strong>GitHub:</strong> <a href="https://github.com/mapbox/delatin">mapbox/delatin</a></p>
<p id="controls">
<select id="location">
<option value="11/2014/1267">Mount Taranaki, New Zealand</option>
<option value="10/906/404">Mount Fuji, Japan</option>
<option value="10/188/402">Grand Canyon, USA</option>
<option value="10/171/395">Yosemite, USA</option>
<option value="10/163/395">Bay Area, USA</option>
<option value="14/2625/6369">Bay Area Close, USA</option>
</select>
<select id="speed">
<option value="1">1x</option>
<option value="10" selected>10x</option>
<option value="100">100x</option>
<option value="1000">1000x</option>
</select>
<button id="replay">Replay</button>
</p>
<p id="info"></p>
<canvas id="canvas"></canvas>
<p>Terrain source: <a href="https://docs.mapbox.com/help/troubleshooting/access-elevation-data/#mapbox-terrain-rgb">Mapbox Terrain-RGB</a></p>
<script type="module">
import Delatin from './index.js';
const info = document.getElementById('info');
const speedEl = document.getElementById('speed');
const canvas = document.getElementById('canvas');
const ctx = window.ctx = canvas.getContext('2d');
const size = 640;
const ratio = 2 * size / 511;
canvas.width = 2 * size;
canvas.height = 2 * size;
canvas.style.width = `${size}px`;
canvas.style.height = `${size}px`;
ctx.scale(ratio, ratio);
let metersPerPixel;
const tile = new Image();
tile.crossOrigin = true;
const token = 'pk.eyJ1IjoibW91cm5lciIsImEiOiJjbG83ZDF0ZGgwNTJ1MmtsajF1ZDN0c3ZvIn0.56k4reJ2cZAGNVklIGCXnQ';
let frameId;
document.getElementById('replay').onclick = start;
const select = document.getElementById('location');
function onSelect() {
const tileCoord = select.value;
const [tz, tx, ty] = tileCoord.split('/').map(Number);
const numTiles = Math.pow(2, tz);
const lat = 2 * Math.atan(Math.exp((1 - 2 * (ty + 0.5) / numTiles) * Math.PI)) - Math.PI / 2;
metersPerPixel = 40075016.7 * Math.cos(lat) / numTiles / 512;
cancelAnimationFrame(frameId);
ctx.fillStyle = '#ccc';
ctx.fillRect(0, 0, size, size);
info.innerHTML = 'Loading...';
tile.src = `https://a.tiles.mapbox.com/v4/mapbox.terrain-rgb/${tileCoord}@2x.png?access_token=${token}`;
}
select.onchange = onSelect;
onSelect();
let tin;
const tileCoord = '14/2625/6369';
function start() {
cancelAnimationFrame(frameId);
const data = getImageData(tile);
const values = new Float64Array(tile.width * tile.height);
for (let i = 0; i < values.length; i++) {
const r = data[4 * i];
const g = data[4 * i + 1];
const b = data[4 * i + 2];
values[i] = Math.round(((r * 256 * 256 + g * 256.0 + b) / 10.0 - 10000.0) * 10) / 10;
}
tin = window.tin = new Delatin(values, tile.width, tile.height);
frame();
}
tile.onload = start;
function getImageData(img) {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(tile, 0, 0);
return ctx.getImageData(0, 0, img.width, img.height).data;
}
const minError = 0.1;
function frame() {
const scale = 3 / metersPerPixel;
const speed = +speedEl.value;
const iterations = speed - (tin.coords.length >> 1) % speed;
const pending = [];
for (let k = 0; k < iterations; k++) {
// use private methods for incremental drawing of updated triangles
tin._step();
for (let i = 0; i < tin._pendingLen; i++) {
const t = tin._pending[i];
const p0 = 2 * tin.triangles[3 * t + 0];
const p1 = 2 * tin.triangles[3 * t + 1];
const p2 = 2 * tin.triangles[3 * t + 2];
const x0 = tin.coords[p0], y0 = tin.coords[p0 + 1], z0 = tin.heightAt(x0, y0) * scale;
const x1 = tin.coords[p1], y1 = tin.coords[p1 + 1], z1 = tin.heightAt(x1, y1) * scale;
const x2 = tin.coords[p2], y2 = tin.coords[p2 + 1], z2 = tin.heightAt(x2, y2) * scale;
pending.push({x0, y0, z0, x1, y1, z1, x2, y2, z2});
}
tin._flush();
if (tin.getMaxError() <= minError) break;
}
for (const {x0, y0, z0, x1, y1, z1, x2, y2, z2} of pending) {
const nx = -((y1 - y0) * (z2 - z0) - (y2 - y0) * (z1 - z0));
const ny = (z1 - z0) * (x2 - x0) - (x1 - x0) * (z2 - z0);
const nz = -((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0));
const d = 2 * Math.hypot(nx, ny, nz);
const r = Math.round(255 * (nx / d + 0.5));
const g = Math.round(255 * (ny / d + 0.5));
const b = Math.round(255 * (nz / d + 0.5));
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.closePath();
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
ctx.fill();
}
const numPoints = tin.coords.length >> 1;
const error = tin.getMaxError();
const rmse = tin.getRMSD();
const numTriangles = tin.triangles.length / 3;
const percentTriangles = 100 * numTriangles / tin.data.length / 2;
info.innerHTML = `
<code>${numPoints}</code> points,
<code>${numTriangles}</code> triangles (<code>${percentTriangles.toFixed(2)}</code>%),
<code>${rmse.toFixed(2)}</code>m RMSD,
<code>${error.toFixed(2)}</code>m max error`;
if (error > minError) frameId = requestAnimationFrame(frame);
};
</script>
</div></body>
</html>