-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnap.js
145 lines (122 loc) · 3.16 KB
/
snap.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
'use strict'
var getBounds = require('array-bounds')
var sort = require('./sort')
module.exports = snapPoints
function snapPoints(srcPoints, bounds) {
var n = srcPoints.length >>> 1
if(n < 1) {
return {levels: [], ids: null, weights: null, points: srcPoints}
}
var points = new Float64Array(n * 2)
if (!bounds) bounds = []
var ids = new Uint32Array(n)
var weights = new Uint32Array(n)
var levels = new Uint8Array(n)
for(var i=0; i < n; ++i) {
ids[i] = i
}
// empty bounds or invalid bounds are considered as undefined and require recalc
if (!bounds.length || bounds.length < 4 || bounds[0] >= bounds[2] || bounds[1] >= bounds[3]) {
var b = getBounds(srcPoints, 2)
if(b[0] === b[2]) {
b[2] += 1
}
if(b[1] === b[3]) {
b[3] += 1
}
bounds[0] = b[0]
bounds[1] = b[1]
bounds[2] = b[2]
bounds[3] = b[3]
}
var lox = bounds[0]
var loy = bounds[1]
var hix = bounds[2]
var hiy = bounds[3]
// Calculate diameter
var scaleX = 1.0 / (hix - lox)
var scaleY = 1.0 / (hiy - loy)
var diam = Math.max(hix - lox, hiy - loy)
// normalize values
for (var i = 0; i < n; i++) {
points[2*i] = (srcPoints[2*i] - lox) * scaleX
points[2*i+1] = (srcPoints[2*i+1] - loy) * scaleY
}
// Rearrange in quadtree order
var ptr = 0
snapRec(0, 0, 1, 0, n, 0)
function snapRec(x, y, diam, start, end, level) {
var diam_2 = diam * 0.5
var offset = start + 1
var count = end - start
weights[ptr] = count
levels[ptr++] = level
for(var i=0; i<2; ++i) {
for(var j=0; j<2; ++j) {
var nx = x+i*diam_2
var ny = y+j*diam_2
var nextOffset = partition(
points
, ids
, offset
, end
, nx, ny
, nx+diam_2, ny+diam_2)
if(nextOffset === offset) {
continue
}
snapRec(nx, ny, diam_2, offset, nextOffset, level+1)
offset = nextOffset
}
}
}
function partition(points, ids, start, end, lox, loy, hix, hiy) {
var mid = start
for(var i=start; i < end; ++i) {
var x = points[2*i]
var y = points[2*i+1]
var s = ids[i]
if(lox <= x && x <= hix &&
loy <= y && y <= hiy) {
if(i === mid) {
mid += 1
} else {
points[2*i] = points[2*mid]
points[2*i+1] = points[2*mid+1]
ids[i] = ids[mid]
points[2*mid] = x
points[2*mid+1] = y
ids[mid] = s
mid += 1
}
}
}
return mid
}
// sort by levels with accordance to x-coordinate
var result = sort(levels, points, ids, weights, n)
// form levels of details
var lod = []
var lastLevel = 0
var prevOffset = n
for(var ptr=n-1; ptr>=0; --ptr) {
var level = result.levels[ptr]
if(level === lastLevel) {
continue
}
lod.push({
pixelSize: diam * Math.pow(0.5, level),
offset: ptr+1,
count: prevOffset - (ptr+1)
})
prevOffset = ptr+1
lastLevel = level
}
lod.push({
pixelSize: diam * Math.pow(0.5, level+1),
offset: 0,
count: prevOffset
})
result.levels = lod
return result
}