-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
203 lines (192 loc) · 6.39 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
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
var polygonIntersect = require('polygon-intersect-test')
var bbox = [0,0,0,0]
var irange = [0,0], jrange = [0,0]
module.exports = LabelEngine
function LabelEngine (opts) {
if (!(this instanceof LabelEngine)) return new LabelEngine(opts)
if (!opts) opts = {}
this._cellType = opts.cellType || 'u16'
this.types = opts.types || {}
this._outlines = opts.outlines !== undefined ? opts.outlines : false
this.data = {
positions: null,
cells: null,
labels: null,
bounds: null,
bbox: null,
outlines: null
}
this.count = {
positions: 0,
cells: 0,
bounds: 0,
bbox: 0,
outlines: 0
}
this._dst = {
positions: { offset: 0, data: null },
cells: { offset: 0, data: null },
bounds: { offset: 0, data: null }
}
this.visible = null
this.offsets = { bounds: null, positions: null }
this._size = { positions: 0, cells: 0, bounds: 0 }
this._maxIndex = opts.maxIndex === undefined ? 50 : opts.maxIndex
}
LabelEngine.prototype.update = function (features) {
this._features = features
var plen = 0, clen = 0, blen = 0
for (var i = 0; i < features.length; i++) {
var f = features[i]
var t = this.types[f.type]
if (!t) throw new Error('implementation not provided for type=' + f.type)
this._size.positions = 0
this._size.cells = 0
this._size.bounds = 0
t.size(this._size, features[i])
plen += this._size.positions
clen += this._size.cells
blen += this._size.bounds
}
if (!this.data.positions || plen > this.data.positions.length) {
this.data.positions = new Float32Array(plen)
this.data.labels = new Float32Array(plen/2)
}
if (!this.data.cells || clen > this.data.cells.length) {
var F = {
u16: Uint16Array,
u32: Uint32Array
}[this._cellType];
if (!F) {
throw new Error('cellType ' + this._cellType + ' not supported')
}
this.data.cells = new F(clen)
}
if (!this.data.bounds || blen > this.data.bounds.length) {
this.data.bounds = new Float32Array(blen)
}
if (this._outlines && (!this.data.outlines || blen*2 > this.data.outlines.length)) {
this.data.outlines = new Float32Array(blen*2)
}
if (!this.data.bbox || features.length * 4 > this.data.bbox.length) {
this.data.bbox = new Float32Array(features.length*4)
}
if (!this.visible || this.visible.length !== features.length) {
this.visible = new Float32Array(features.length)
} else {
this.visible.fill(0)
}
if (!this.offsets.bounds || features.length*2 > this.offsets.bounds.length) {
this.offsets.bounds = new Float32Array(features.length*2)
}
if (!this.offsets.positions || features.length*2 > this.offsets.positions.length) {
this.offsets.positions = new Float32Array(features.length*2)
}
this._step()
}
LabelEngine.prototype._step = function () {
var plen = 0, clen = 0, ooffset = 0
this._dst.positions.offset = 0
this._dst.positions.data = this.data.positions
this._dst.cells.offset = 0
this._dst.cells.data = this.data.cells
this._dst.bounds.offset = 0
this._dst.bounds.data = this.data.bounds
for (var i = 0; i < this._features.length; i++) {
var f = this._features[i]
var t = this.types[f.type]
if (!t) throw new Error('implementation not provided for type=' + f.type)
var pstart = this._dst.positions.offset
var cstart = this._dst.cells.offset
var bstart = this._dst.bounds.offset
this._dst.index = 0
var found = false
while (!found && this._dst.index < this._maxIndex) {
this._dst.positions.offset = pstart
this._dst.cells.offset = cstart
this._dst.bounds.offset = bstart
t.write(this._dst, this._features[i])
this._dst.index++
var pend = this._dst.positions.offset
var cend = this._dst.cells.offset
var bend = this._dst.bounds.offset
for (var j = cstart; j < cend; j++) {
this.data.cells[j] += pstart/2
}
this.offsets.bounds[i*2+0] = bstart
this.offsets.bounds[i*2+1] = bend
this.offsets.positions[i*2+0] = pstart
this.offsets.positions[i*2+1] = pend
bbox[0] = Infinity
bbox[1] = Infinity
bbox[2] = -Infinity
bbox[3] = -Infinity
for (var j = bstart; j < bend; j+=2) {
bbox[0] = Math.min(bbox[0], this.data.bounds[j+0])
bbox[1] = Math.min(bbox[1], this.data.bounds[j+1])
bbox[2] = Math.max(bbox[2], this.data.bounds[j+0])
bbox[3] = Math.max(bbox[3], this.data.bounds[j+1])
}
var visible = true
if (bstart === bend) {
bbox[0] = Infinity
bbox[1] = Infinity
bbox[2] = Infinity
bbox[3] = Infinity
visible = false
found = true // no solution
}
this.data.bbox[i*4+0] = bbox[0]
this.data.bbox[i*4+1] = bbox[1]
this.data.bbox[i*4+2] = bbox[2]
this.data.bbox[i*4+3] = bbox[3]
irange[0] = bstart
irange[1] = bend
if (visible) {
for (var j = 0; j < i; j++) {
if (j === i) continue
if (this.visible[j] < 0.5) continue
if (boxOverlap(i*4,this.data.bbox,j*4,this.data.bbox)) {
jrange[0] = this.offsets.bounds[j*2+0]
jrange[1] = this.offsets.bounds[j*2+1]
if (polygonIntersect(this.data.bounds,this.data.bounds,irange,jrange)) {
visible = false
break
}
}
}
}
this.visible[i] = visible
if (visible) {
found = true
}
}
if (this.visible[i] < 0.5) {
this._dst.positions.offset = pstart
this._dst.cells.offset = cstart
this._dst.bounds.offset = bstart
this.offsets.bounds[i*2+0] = bstart
this.offsets.bounds[i*2+1] = bstart
}
if (this._outlines && this.visible[i] > 0.5) {
var n = bend-bstart
for (var j = 0; j < n; j+=2) {
this.data.outlines[ooffset++] = this.data.bounds[bstart+j+0]
this.data.outlines[ooffset++] = this.data.bounds[bstart+j+1]
this.data.outlines[ooffset++] = this.data.bounds[bstart+(j+2)%n+0]
this.data.outlines[ooffset++] = this.data.bounds[bstart+(j+2)%n+1]
}
}
}
this.count.positions = this._dst.positions.offset/2
this.count.cells = this._dst.cells.offset
this.count.bounds = this._dst.bounds.offset/2
this.count.outlines = ooffset/2
}
LabelEngine.prototype.isVisible = function (i) {
return this.visible[i] > 0.5
}
function boxOverlap(ai, a, bi, b) {
return a[ai+2] >= b[bi+0] && a[ai+0] <= b[bi+2]
&& a[ai+3] >= b[bi+1] && a[ai+1] <= b[bi+3]
}