-
Notifications
You must be signed in to change notification settings - Fork 5
/
scatter2d.js
286 lines (231 loc) · 8.45 KB
/
scatter2d.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
'use strict'
var createShader = require('gl-shader')
var createBuffer = require('gl-buffer')
var search = require('binary-search-bounds')
var snapPoints = require('snap-points-2d')
var pool = require('typedarray-pool')
var SHADERS = require('./lib/shader')
var normalize = require('array-normalize')
var getBounds = require('array-bounds')
module.exports = createScatter2D
function Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader) {
this.plot = plot
this.positionBufferHi = positionBufferHi
this.positionBufferLo = positionBufferLo
this.pickBuffer = pickBuffer
this.weightBuffer = weightBuffer
this.shader = shader
this.pickShader = pickShader
this.scales = []
this.size = 12.0
this.borderSize = 1.0
this.pointCount = 0
this.color = [1, 0, 0, 1]
this.borderColor = [0, 0, 0, 1]
this.bounds = [Infinity, Infinity, -Infinity, -Infinity]
this.pickOffset = 0
this.points = null
this.xCoords = null
this.snapPoints = true
}
var proto = Scatter2D.prototype
var scaleHi = new Float32Array(2)
var scaleLo = new Float32Array(2)
var translateHi = new Float32Array(2)
var translateLo = new Float32Array(2)
var PICK_VEC4 = [0, 0, 0, 0]
proto.dispose = function() {
this.shader.dispose()
this.pickShader.dispose()
this.positionBufferHi.dispose()
this.positionBufferLo.dispose()
this.pickBuffer.dispose()
if(this.xCoords) pool.free(this.xCoords)
this.plot.removeObject(this)
}
proto.update = function(options) {
options = options || {}
function dflt(opt, value) {
return opt in options ? options[opt] : value
}
this.size = dflt('size', 12)
this.color = dflt('color', [1, 0, 0, 1]).slice()
this.borderSize = dflt('borderSize', 1)
this.borderColor = dflt('borderColor', [0, 0, 0, 1]).slice()
this.snapPoints = dflt('snapPoints', true)
//do not recalc points if there is no positions
if (options.positions != null) {
if(this.xCoords) pool.free(this.xCoords)
this.points = options.positions
var pointCount = this.points.length >>> 1
var packedId = pool.mallocInt32(pointCount)
var packedW = pool.mallocFloat32(pointCount)
var packed = pool.mallocFloat64(2 * pointCount)
packed.set(this.points)
if (this.snapPoints) {
this.scales = snapPoints(packed, packedId, packedW, this.bounds)
}
else {
//get bounds
this.bounds = getBounds(packed, 2)
// rescale packed to unit box
normalize(packed, 2, this.bounds)
// generate fake ids
for (var i = 0; i < pointCount; i++) {
packedId[i] = i
packedW[i] = 1
}
}
var xCoords = pool.mallocFloat64(pointCount)
var packedHi = pool.mallocFloat32(2 * pointCount)
var packedLo = pool.mallocFloat32(2 * pointCount)
packedHi.set(packed)
for(var i = 0, j = 0; i < pointCount; i++, j += 2) {
packedLo[j] = packed[j] - packedHi[j]
packedLo[j + 1] = packed[j + 1] - packedHi[j + 1]
xCoords[i] = packed[j]
}
this.positionBufferHi.update(packedHi)
this.positionBufferLo.update(packedLo)
this.pickBuffer.update(packedId)
this.weightBuffer.update(packedW)
pool.free(packedHi)
pool.free(packedLo)
pool.free(packedW)
pool.free(packed)
pool.free(packedId)
this.xCoords = xCoords
this.pointCount = pointCount
this.pickOffset = 0
}
}
proto.draw = function(pickOffset) {
var pick = pickOffset !== void(0)
var plot = this.plot
var shader = pick ? this.pickShader : this.shader
var scales = this.scales
var positionBufferHi = this.positionBufferHi
var positionBufferLo = this.positionBufferLo
var pickBuffer = this.pickBuffer
var bounds = this.bounds
var size = this.size
var borderSize = this.borderSize
var gl = plot.gl
var pixelRatio = pick ? plot.pickPixelRatio : plot.pixelRatio
var viewBox = plot.viewBox
var dataBox = plot.dataBox
if(this.pointCount === 0)
return pickOffset
var boundX = bounds[2] - bounds[0]
var boundY = bounds[3] - bounds[1]
var dataX = dataBox[2] - dataBox[0]
var dataY = dataBox[3] - dataBox[1]
var screenX = (viewBox[2] - viewBox[0]) * pixelRatio / plot.pixelRatio
var screenY = (viewBox[3] - viewBox[1]) * pixelRatio / plot.pixelRatio
var pixelSize = this.pixelSize = Math.min(dataX / screenX, dataY / screenY)
var scaleX = 2 * boundX / dataX
var scaleY = 2 * boundY / dataY
scaleHi[0] = scaleX
scaleHi[1] = scaleY
scaleLo[0] = scaleX - scaleHi[0]
scaleLo[1] = scaleY - scaleHi[1]
var translateX = (bounds[0] - dataBox[0] - 0.5 * dataX) / boundX
var translateY = (bounds[1] - dataBox[1] - 0.5 * dataY) / boundY
translateHi[0] = translateX
translateHi[1] = translateY
translateLo[0] = translateX - translateHi[0]
translateLo[1] = translateY - translateHi[1]
shader.bind()
shader.uniforms.scaleHi = scaleHi
shader.uniforms.scaleLo = scaleLo
shader.uniforms.translateHi = translateHi
shader.uniforms.translateLo = translateLo
shader.uniforms.color = this.color
shader.uniforms.borderColor = this.borderColor
shader.uniforms.pointSize = pixelRatio * (size + borderSize)
shader.uniforms.centerFraction = this.borderSize === 0 ? 2 : size / (size + borderSize + 1.25)
positionBufferHi.bind()
shader.attributes.positionHi.pointer()
positionBufferLo.bind()
shader.attributes.positionLo.pointer()
if(pick) {
this.pickOffset = pickOffset
PICK_VEC4[0] = ( pickOffset & 0xff)
PICK_VEC4[1] = ((pickOffset >> 8) & 0xff)
PICK_VEC4[2] = ((pickOffset >> 16) & 0xff)
PICK_VEC4[3] = ((pickOffset >> 24) & 0xff)
shader.uniforms.pickOffset = PICK_VEC4
pickBuffer.bind()
shader.attributes.pickId.pointer(gl.UNSIGNED_BYTE)
} else {
shader.uniforms.useWeight = 1
this.weightBuffer.bind()
shader.attributes.weight.pointer()
}
var firstLevel = true
if (this.snapPoints) {
for(var scaleNum = scales.length - 1; scaleNum >= 0; scaleNum--) {
var lod = scales[scaleNum]
if(lod.pixelSize < pixelSize && scaleNum > 1)
continue
var range = this.getVisibleRange(lod)
var startOffset = range[0], endOffset = range[1]
if(endOffset > startOffset)
gl.drawArrays(gl.POINTS, startOffset, endOffset - startOffset)
if(!pick && firstLevel) {
firstLevel = false
shader.uniforms.useWeight = 0
}
}
}
else {
gl.drawArrays(gl.POINTS, 0, this.pointCount)
}
return pickOffset + this.pointCount
}
proto.getVisibleRange = function (lod) {
var dataBox = this.plot.dataBox,
bounds = this.bounds,
pixelSize = this.pixelSize,
size = this.size,
pixelRatio = this.plot.pixelRatio,
boundX = bounds[2] - bounds[0],
boundY = bounds[3] - bounds[1]
if (!lod) {
for(var scaleNum = this.scales.length - 1, lod; scaleNum >= 0; scaleNum--) {
lod = this.scales[scaleNum];
if(!(lod.pixelSize < pixelSize && scaleNum > 1)) break;
}
}
var xCoords = this.xCoords
var xStart = (dataBox[0] - bounds[0] - pixelSize * size * pixelRatio) / boundX
var xEnd = (dataBox[2] - bounds[0] + pixelSize * size * pixelRatio) / boundX
var intervalStart = lod.offset
var intervalEnd = lod.count + intervalStart
var startOffset = search.ge(xCoords, xStart, intervalStart, intervalEnd - 1)
var endOffset = search.lt(xCoords, xEnd, startOffset, intervalEnd - 1) + 1
return [startOffset, endOffset]
}
proto.drawPick = proto.draw
proto.pick = function(x, y, value) {
var pointId = value - this.pickOffset
return pointId < 0 || pointId >= this.pointCount
? null : {
object: this,
pointId: pointId,
dataCoord: [ this.points[2 * pointId], this.points[2 * pointId + 1] ]
}
}
function createScatter2D(plot, options) {
var gl = plot.gl
var positionBufferHi = createBuffer(gl)
var positionBufferLo = createBuffer(gl)
var pickBuffer = createBuffer(gl)
var weightBuffer = createBuffer(gl)
var shader = createShader(gl, SHADERS.pointVertex, SHADERS.pointFragment)
var pickShader = createShader(gl, SHADERS.pickVertex, SHADERS.pickFragment)
var result = new Scatter2D(plot, positionBufferHi, positionBufferLo, pickBuffer, weightBuffer, shader, pickShader)
result.update(options)
plot.addObject(result) // register with plot
return result
}