-
Notifications
You must be signed in to change notification settings - Fork 3
/
contour.js
421 lines (342 loc) · 10.4 KB
/
contour.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
'use strict'
module.exports = createContour2D
var iota = require('iota-array')
var createShader = require('gl-shader')
var createBuffer = require('gl-buffer')
var ndarray = require('ndarray')
var surfaceNets = require('surface-nets')
var cdt2d = require('cdt2d')
var cleanPSLG = require('clean-pslg')
var bsearch = require('binary-search-bounds')
var shaders = require('./lib/shaders')
function GLContour2D (
plot,
shader,
fillShader,
positionBuffer,
colorBuffer,
idBuffer,
fillPositionBuffer,
fillColorBuffer) {
this.plot = plot
this.shader = shader
this.fillShader = fillShader
this.positionBuffer = positionBuffer
this.colorBuffer = colorBuffer
this.idBuffer = idBuffer
this.fillPositionBuffer = fillPositionBuffer
this.fillColorBuffer = fillColorBuffer
this.fillVerts = 0
this.shape = [0, 0]
this.bounds = [Infinity, Infinity, -Infinity, -Infinity]
this.numVertices = 0
this.lineWidth = 1
}
var proto = GLContour2D.prototype
var WEIGHTS = [
1, 0,
0, 0,
0, 1,
1, 0,
1, 1,
0, 1
]
proto.draw = (function () {
var MATRIX = [
1, 0, 0,
0, 1, 0,
0, 0, 1
]
var SCREEN_SHAPE = [0, 0]
return function () {
var plot = this.plot
var shader = this.shader
var fillShader = this.fillShader
var bounds = this.bounds
var numVertices = this.numVertices
var fillVerts = this.fillVerts
var uniforms, attributes
var gl = plot.gl
var viewBox = plot.viewBox
var dataBox = plot.dataBox
var boundX = bounds[2] - bounds[0]
var boundY = bounds[3] - bounds[1]
var dataX = dataBox[2] - dataBox[0]
var dataY = dataBox[3] - dataBox[1]
MATRIX[0] = 2.0 * boundX / dataX
MATRIX[4] = 2.0 * boundY / dataY
MATRIX[6] = 2.0 * (bounds[0] - dataBox[0]) / dataX - 1.0
MATRIX[7] = 2.0 * (bounds[1] - dataBox[1]) / dataY - 1.0
SCREEN_SHAPE[0] = viewBox[2] - viewBox[0]
SCREEN_SHAPE[1] = viewBox[3] - viewBox[1]
if (fillVerts > 0) {
fillShader.bind()
uniforms = fillShader.uniforms
uniforms.viewTransform = MATRIX
uniforms.screenShape = SCREEN_SHAPE
attributes = shader.attributes
this.fillPositionBuffer.bind()
attributes.position.pointer()
this.fillColorBuffer.bind()
attributes.color.pointer(gl.UNSIGNED_BYTE, true)
gl.drawArrays(gl.TRIANGLES, 0, fillVerts)
}
if (numVertices > 0) {
shader.bind()
var lineWidth = this.lineWidth * plot.pixelRatio
uniforms = shader.uniforms
uniforms.viewTransform = MATRIX
uniforms.screenShape = SCREEN_SHAPE
uniforms.lineWidth = lineWidth
uniforms.pointSize = 1000
attributes = shader.attributes
// Draw lines
this.positionBuffer.bind()
attributes.position.pointer(gl.FLOAT, false, 16, 0)
attributes.tangent.pointer(gl.FLOAT, false, 16, 8)
this.colorBuffer.bind()
attributes.color.pointer(gl.UNSIGNED_BYTE, true)
gl.drawArrays(gl.TRIANGLES, 0, numVertices)
// Draw end caps
uniforms.lineWidth = 0
uniforms.pointSize = lineWidth
this.positionBuffer.bind()
attributes.position.pointer(gl.FLOAT, false, 16 * 3, 0)
attributes.tangent.pointer(gl.FLOAT, false, 16 * 3, 8)
this.colorBuffer.bind()
attributes.color.pointer(gl.UNSIGNED_BYTE, true, 4 * 3, 0)
gl.drawArrays(gl.POINTS, 0, numVertices / 3)
}
}
})()
proto.drawPick = (function () {
return function (pickOffset) {
return pickOffset
}
})()
proto.pick = function (x, y, value) {
return null
}
function interpolate (array, point) {
var idx = Math.floor(point)
if (idx < 0) {
return array[0]
} else if (idx >= array.length - 1) {
return array[array.length - 1]
}
var t = point - idx
return (1.0 - t) * array[idx] + t * array[idx + 1]
}
proto.update = function (options) {
options = options || {}
var shape = options.shape || [0, 0]
var x = options.x || iota(shape[0])
var y = options.y || iota(shape[1])
var z = options.z || new Float32Array(shape[0] * shape[1])
var levels = options.levels || []
var levelColors = options.levelColors || []
var bounds = this.bounds
var lox = bounds[0] = x[0]
var loy = bounds[1] = y[0]
var hix = bounds[2] = x[x.length - 1]
var hiy = bounds[3] = y[y.length - 1]
if (lox === hix) {
bounds[2] += 1
hix += 1
}
if (loy === hiy) {
bounds[3] += 1
hiy += 1
}
var xs = 1.0 / (hix - lox)
var ys = 1.0 / (hiy - loy)
this.lineWidth = options.lineWidth || 1
var zarray = ndarray(z, shape)
var positions = []
var colors = []
var ids = []
var fillCells = []
var fillPositions = [
[0, 0],
[shape[0] - 1, 0],
[0, shape[1] - 1],
[shape[0] - 1, shape[1] - 1]
]
function intersect (level, x, a, b) {
var d = (b - a)
if (Math.abs(d) < 1e-6) {
return x
}
return Math.floor(x) + Math.max(0.001, Math.min(0.999, (level - a) / d))
}
for (var i = 0; i < levels.length; ++i) {
var level = levels[i]
if (i > 0 && level === levels[i - 1]) {
continue
}
var contour = surfaceNets(zarray, level)
var c_r = (255 * levelColors[4 * i]) | 0
var c_g = (255 * levelColors[4 * i + 1]) | 0
var c_b = (255 * levelColors[4 * i + 2]) | 0
var c_a = (255 * levelColors[4 * i + 3]) | 0
var c_cells = contour.cells
var c_positions = contour.positions
// Fix boundaries
var in_degree = Array(c_positions.length)
for (var j = 0; j < in_degree.length; ++j) {
in_degree[j] = 0
}
for (j = 0; j < c_cells.length; ++j) {
var edge = c_cells[j]
in_degree[edge[0]] += 1
in_degree[edge[1]] += 1
}
for (j = 0; j < in_degree.length; ++j) {
var deg = in_degree[j]
if (deg === 0) {
continue
}
var pp = c_positions[j]
in_degree[j] = fillPositions.length
fillPositions.push(pp)
if (deg > 1) {
continue
}
var ppx = pp[0]
var ppy = pp[1]
var z00 = zarray.get(Math.floor(ppx), Math.floor(ppy))
var z01 = zarray.get(Math.floor(ppx), Math.ceil(ppy))
var z10 = zarray.get(Math.ceil(ppx), Math.floor(ppy))
var z11 = zarray.get(Math.ceil(ppx), Math.ceil(ppy))
var intercept
if (Math.floor(pp[0]) === 0 &&
((z00 <= level) !== (z01 < level))) {
intercept = [0, intersect(level, pp[1], z00, z01)]
} else if (Math.ceil(pp[0]) === shape[0] - 1 &&
((z10 <= level) !== (z11 < level))) {
intercept = [shape[0] - 1, intersect(level, pp[1], z10, z11)]
} else if (Math.floor(pp[1]) === 0 &&
((z00 <= level) !== (z10 < level))) {
intercept = [intersect(level, pp[0], z00, z10), 0]
} else if (Math.ceil(pp[1]) === shape[1] - 1 &&
((z01 <= level) !== (z11 < level))) {
intercept = [intersect(level, pp[0], z01, z11), shape[1] - 1]
}
if (intercept) {
c_cells.push([j, c_positions.length])
in_degree.push(fillPositions.length)
c_positions.push(intercept)
}
}
for (j = 0; j < c_cells.length; ++j) {
var e = c_cells[j]
var a = c_positions[e[0]]
var b = c_positions[e[1]]
fillCells.push([in_degree[e[0]], in_degree[e[1]]])
var pointId = Math.round(a[0]) + shape[0] * Math.round(a[1])
var ax = interpolate(x, a[0])
var ay = interpolate(y, a[1])
var bx = interpolate(x, b[0])
var by = interpolate(y, b[1])
ax = xs * (ax - lox)
ay = ys * (ay - loy)
bx = xs * (bx - lox)
by = ys * (by - loy)
var dx = ax - bx
var dy = ay - by
for (var k = 0; k < WEIGHTS.length; k += 2) {
var wx = WEIGHTS[k]
var wix = 1.0 - wx
var wy = 2.0 * WEIGHTS[k + 1] - 1.0
positions.push(
wix * ax + wx * bx, wix * ay + wx * by,
wy * dx, wy * dy)
colors.push(c_r, c_g, c_b, c_a)
ids.push(pointId)
}
}
}
this.positionBuffer.update(new Float32Array(positions))
this.colorBuffer.update(new Uint8Array(colors))
this.idBuffer.update(new Uint32Array(ids))
this.numVertices = ids.length
var fillColors = options.fillColors
var fillCellColors = []
var fillCellPositions = []
var fillVerts = 0
if (fillColors) {
cleanPSLG(fillPositions, fillCells)
var fillMesh = cdt2d(fillPositions, fillCells, {
delaunay: false
})
for (i = 0; i < fillMesh.length; ++i) {
var cell = fillMesh[i]
var cx = 0
var cy = 0
for (j = 0; j < 3; ++j) {
var p = fillPositions[cell[j]]
var px = interpolate(x, p[0])
var py = interpolate(y, p[1])
cx += p[0]
cy += p[1]
fillCellPositions.push(
xs * (px - lox),
ys * (py - loy))
}
// Compute centroid of triangle
cx /= 3
cy /= 3
// Sample height field at triangle centroid
var cxi = Math.floor(cx)
var cyi = Math.floor(cy)
var cxf = cx - cxi
var cyf = cy - cyi
var c00 = zarray.get(cxi, cyi)
var c01 = zarray.get(cxi, cyi + 1)
var c10 = zarray.get(cxi + 1, cyi)
var c11 = zarray.get(cxi + 1, cyi + 1)
var zlevel =
(1 - cyf) * ((1 - cxf) * c00 + cxf * c10) +
cyf * ((1 - cxf) * c01 + cxf * c11)
// Color triangle using centroid data
var l = bsearch.le(levels, zlevel) + 1
var cr = (255 * fillColors[4 * l + 0]) | 0
var cg = (255 * fillColors[4 * l + 1]) | 0
var cb = (255 * fillColors[4 * l + 2]) | 0
var ca = (255 * fillColors[4 * l + 3]) | 0
fillCellColors.push(
cr, cg, cb, ca,
cr, cg, cb, ca,
cr, cg, cb, ca)
fillVerts += 3
}
this.fillPositionBuffer.update(new Float32Array(fillCellPositions))
this.fillColorBuffer.update(new Uint8Array(fillCellColors))
this.fillVerts = fillVerts
}
}
proto.dispose = function () {
this.plot.removeObject(this)
}
function createContour2D (plot, options) {
var gl = plot.gl
var shader = createShader(gl, shaders.vertex, shaders.fragment)
var fillShader = createShader(gl, shaders.fillVertex, shaders.fragment)
var positionBuffer = createBuffer(gl)
var colorBuffer = createBuffer(gl)
var idBuffer = createBuffer(gl)
var fillPositionBuffer = createBuffer(gl)
var fillColorBuffer = createBuffer(gl)
var contours = new GLContour2D(
plot,
shader,
fillShader,
positionBuffer,
colorBuffer,
idBuffer,
fillPositionBuffer,
fillColorBuffer)
contours.update(options)
plot.addObject(contours)
return contours
}