forked from mapbox/martini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (136 loc) · 6.1 KB
/
index.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
export default class Martini {
constructor(gridSize = 257) {
this.gridSize = gridSize;
const tileSize = gridSize - 1;
if (tileSize & (tileSize - 1)) throw new Error(
`Expected grid size to be 2^n+1, got ${gridSize}.`);
this.numTriangles = tileSize * tileSize * 2 - 2;
this.numParentTriangles = this.numTriangles - tileSize * tileSize;
this.indices = new Uint32Array(this.gridSize * this.gridSize);
// coordinates for all possible triangles in an RTIN tile
this.coords = new Uint16Array(this.numTriangles * 4);
// get triangle coordinates from its index in an implicit binary tree
for (let i = 0; i < this.numTriangles; i++) {
let id = i + 2;
let ax = 0, ay = 0, bx = 0, by = 0, cx = 0, cy = 0;
if (id & 1) {
bx = by = cx = tileSize; // bottom-left triangle
} else {
ax = ay = cy = tileSize; // top-right triangle
}
while ((id >>= 1) > 1) {
const mx = (ax + bx) >> 1;
const my = (ay + by) >> 1;
if (id & 1) { // left half
bx = ax; by = ay;
ax = cx; ay = cy;
} else { // right half
ax = bx; ay = by;
bx = cx; by = cy;
}
cx = mx; cy = my;
}
const k = i * 4;
this.coords[k + 0] = ax;
this.coords[k + 1] = ay;
this.coords[k + 2] = bx;
this.coords[k + 3] = by;
}
}
createTile(terrain) {
return new Tile(terrain, this);
}
}
class Tile {
constructor(terrain, martini) {
const size = martini.gridSize;
if (terrain.length !== size * size) throw new Error(
`Expected terrain data of length ${size * size} (${size} x ${size}), got ${terrain.length}.`);
this.terrain = terrain;
this.martini = martini;
this.errors = new Float32Array(terrain.length);
this.update();
}
update() {
const {numTriangles, numParentTriangles, coords, gridSize: size} = this.martini;
const {terrain, errors} = this;
// iterate over all possible triangles, starting from the smallest level
for (let i = numTriangles - 1; i >= 0; i--) {
const k = i * 4;
const ax = coords[k + 0];
const ay = coords[k + 1];
const bx = coords[k + 2];
const by = coords[k + 3];
const mx = (ax + bx) >> 1;
const my = (ay + by) >> 1;
const cx = mx + my - ay;
const cy = my + ax - mx;
// calculate error in the middle of the long edge of the triangle
const interpolatedHeight = (terrain[ay * size + ax] + terrain[by * size + bx]) / 2;
const middleIndex = my * size + mx;
const middleError = Math.abs(interpolatedHeight - terrain[middleIndex]);
errors[middleIndex] = Math.max(errors[middleIndex], middleError);
if (i < numParentTriangles) { // bigger triangles; accumulate error with children
const leftChildIndex = ((ay + cy) >> 1) * size + ((ax + cx) >> 1);
const rightChildIndex = ((by + cy) >> 1) * size + ((bx + cx) >> 1);
errors[middleIndex] = Math.max(errors[middleIndex], errors[leftChildIndex], errors[rightChildIndex]);
}
}
}
getMesh(maxError = 0) {
const {gridSize: size, indices} = this.martini;
const {errors} = this;
let numVertices = 0;
let numTriangles = 0;
const max = size - 1;
// use an index grid to keep track of vertices that were already used to avoid duplication
indices.fill(0);
// retrieve mesh in two stages that both traverse the error map:
// - countElements: find used vertices (and assign each an index), and count triangles (for minimum allocation)
// - processTriangle: fill the allocated vertices & triangles typed arrays
function countElements(ax, ay, bx, by, cx, cy) {
const mx = (ax + bx) >> 1;
const my = (ay + by) >> 1;
if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
countElements(cx, cy, ax, ay, mx, my);
countElements(bx, by, cx, cy, mx, my);
} else {
indices[ay * size + ax] = indices[ay * size + ax] || ++numVertices;
indices[by * size + bx] = indices[by * size + bx] || ++numVertices;
indices[cy * size + cx] = indices[cy * size + cx] || ++numVertices;
numTriangles++;
}
}
countElements(0, 0, max, max, max, 0);
countElements(max, max, 0, 0, 0, max);
const vertices = new Uint16Array(numVertices * 2);
const triangles = new Uint32Array(numTriangles * 3);
let triIndex = 0;
function processTriangle(ax, ay, bx, by, cx, cy) {
const mx = (ax + bx) >> 1;
const my = (ay + by) >> 1;
if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && errors[my * size + mx] > maxError) {
// triangle doesn't approximate the surface well enough; drill down further
processTriangle(cx, cy, ax, ay, mx, my);
processTriangle(bx, by, cx, cy, mx, my);
} else {
// add a triangle
const a = indices[ay * size + ax] - 1;
const b = indices[by * size + bx] - 1;
const c = indices[cy * size + cx] - 1;
vertices[2 * a] = ax;
vertices[2 * a + 1] = ay;
vertices[2 * b] = bx;
vertices[2 * b + 1] = by;
vertices[2 * c] = cx;
vertices[2 * c + 1] = cy;
triangles[triIndex++] = a;
triangles[triIndex++] = b;
triangles[triIndex++] = c;
}
}
processTriangle(0, 0, max, max, max, 0);
processTriangle(max, max, 0, 0, 0, max);
return {vertices, triangles};
}
}