-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.js
183 lines (158 loc) · 3.77 KB
/
select.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
'use strict'
module.exports = createSelectBuffer
var createFBO = require('gl-fbo')
var pool = require('typedarray-pool')
var ndarray = require('ndarray')
var nextPow2 = require('bit-twiddle').nextPow2
var selectRange = function(arr, x, y) {
var closestD2 = 1e8
var closestX = -1
var closestY = -1
var ni = arr.shape[0]
var nj = arr.shape[1]
for(var i = 0; i < ni; i++) {
for(var j = 0; j < nj; j++) {
var r = arr.get(i, j, 0)
var g = arr.get(i, j, 1)
var b = arr.get(i, j, 2)
var a = arr.get(i, j, 3)
if(r < 255 || g < 255 || b < 255 || a < 255) {
var dx = x - i
var dy = y - j
var d2 = dx*dx + dy*dy
if(d2 < closestD2) {
closestD2 = d2
closestX = i
closestY = j
}
}
}
}
return [closestX, closestY, closestD2]
}
function SelectResult(x, y, id, value, distance) {
this.coord = [x, y]
this.id = id
this.value = value
this.distance = distance
}
function SelectBuffer(gl, fbo, buffer) {
this.gl = gl
this.fbo = fbo
this.buffer = buffer
this._readTimeout = null
var self = this
this._readCallback = function() {
if(!self.gl) {
return
}
fbo.bind()
gl.readPixels(0,0,fbo.shape[0],fbo.shape[1],gl.RGBA,gl.UNSIGNED_BYTE,self.buffer)
self._readTimeout = null
}
}
var proto = SelectBuffer.prototype
Object.defineProperty(proto, 'shape', {
get: function() {
if(!this.gl) {
return [0,0]
}
return this.fbo.shape.slice()
},
set: function(v) {
if(!this.gl) {
return
}
this.fbo.shape = v
var c = this.fbo.shape[0]
var r = this.fbo.shape[1]
if(r*c*4 > this.buffer.length) {
pool.free(this.buffer)
var buffer = this.buffer = pool.mallocUint8(nextPow2(r*c*4))
for(var i=0; i<r*c*4; ++i) {
buffer[i] = 0xff
}
}
return v
}
})
proto.begin = function() {
var gl = this.gl
var shape = this.shape
if(!gl) {
return
}
this.fbo.bind()
gl.clearColor(1,1,1,1)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}
proto.end = function() {
var gl = this.gl
if(!gl) {
return
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
if(!this._readTimeout) {
clearTimeout(this._readTimeout)
}
this._readTimeout = setTimeout(this._readCallback, 1)
}
proto.query = function(x, y, radius) {
if(!this.gl) {
return null
}
var shape = this.fbo.shape.slice()
x = x|0
y = y|0
if(typeof radius !== 'number') {
radius = 1.0
}
var x0 = Math.min(Math.max(x - radius, 0), shape[0])|0
var x1 = Math.min(Math.max(x + radius, 0), shape[0])|0
var y0 = Math.min(Math.max(y - radius, 0), shape[1])|0
var y1 = Math.min(Math.max(y + radius, 0), shape[1])|0
if(x1 <= x0 || y1 <= y0) {
return null
}
var dims = [x1-x0,y1-y0]
var region = ndarray(
this.buffer,
[dims[0], dims[1], 4],
[4, shape[0]*4, 1],
4*(x0 + shape[0]*y0));
var closest = selectRange(region.hi(dims[0],dims[1],1), radius, radius)
var dx = closest[0]
var dy = closest[1]
if(dx < 0 || Math.pow(this.radius, 2) < closest[2]) {
return null
}
var c0 = region.get(dx, dy, 0)
var c1 = region.get(dx, dy, 1)
var c2 = region.get(dx, dy, 2)
var c3 = region.get(dx, dy, 3)
return new SelectResult(
(dx + x0)|0,
(dy + y0)|0,
c0,
[c1, c2, c3],
Math.sqrt(closest[2]))
}
proto.dispose = function() {
if(!this.gl) {
return
}
this.fbo.dispose()
pool.free(this.buffer)
this.gl = null
if(this._readTimeout) {
clearTimeout(this._readTimeout)
}
}
function createSelectBuffer(gl, shape) {
var width = shape[0]
var height = shape[1]
var options = {}
var fbo = createFBO(gl, width, height, options)
var buffer = pool.mallocUint8(width*height*4)
return new SelectBuffer(gl, fbo, buffer)
}