forked from mapbox/linematch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (162 loc) · 5.45 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
204
205
206
207
208
'use strict';
var Flatbush = require('flatbush');
module.exports = linematch;
module.exports.default = linematch;
function linematch(lines1, lines2, threshold) {
var segments = linesToSegments(lines1);
var segments2 = linesToSegments(lines2);
var index = new Flatbush(segments2.length / 4);
for (var i = 0; i < segments2.length; i += 4) {
index.add(
Math.min(segments2[i + 0], segments2[i + 2]),
Math.min(segments2[i + 1], segments2[i + 3]),
Math.max(segments2[i + 0], segments2[i + 2]),
Math.max(segments2[i + 1], segments2[i + 3])
);
}
index.finish();
var diff = [];
var last;
while (segments.length) {
var by = segments.pop();
var bx = segments.pop();
var ay = segments.pop();
var ax = segments.pop();
var other = index.search(
Math.min(ax, bx) - threshold, // minX
Math.min(ay, by) - threshold, // minY
Math.max(ax, bx) + threshold, // maxX
Math.max(ay, by) + threshold // maxY
);
var overlap = false;
// loop through segments close to the current one, looking for matches;
// if a match found, unmatched parts of the segment will be added to the queue
for (var j = 0; j < other.length; j++) {
var k = other[j] * 4;
var matched = matchSegment(
ax, ay, bx, by,
segments2[k + 0], segments2[k + 1],
segments2[k + 2], segments2[k + 3],
threshold, segments
);
if (matched) {
overlap = true;
break;
}
}
// if segment didn't match any other segments, add it to the diff
if (!overlap) {
// join segment with previous one if possible
var p = last && last[last.length - 1];
if (p && p[0] === ax && p[1] === ay) {
last.push([bx, by]);
} else {
last = [[ax, ay], [bx, by]];
diff.push(last);
}
}
}
return diff;
}
function linesToSegments(lines) {
var segments = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
for (var j = line.length - 1; j > 0; j--) {
var a = line[j - 1];
var b = line[j];
if (a[0] !== b[0] || a[1] !== b[1]) {
addSegment(segments, a[0], a[1], b[0], b[1]);
}
}
}
return segments;
}
// subtract segment [c, d] from [a, b] within threshold r
function matchSegment(ax, ay, bx, by, cx, cy, dx, dy, r, result) {
var len = result.length;
var ap = closePoint(ax, ay, cx, cy, dx, dy, r);
var bp = closePoint(bx, by, cx, cy, dx, dy, r);
// a----b
// c---ap---bp---d
if (ap !== null && bp !== null) return true; // fully covered
var cp = closePoint(cx, cy, ax, ay, bx, by, r);
var dp = closePoint(dx, dy, ax, ay, bx, by, r);
if (cp !== null && cp === dp) return false; // degenerate case, no overlap
var cpx, cpy, dpx, dpy;
if (cp !== null) {
cpx = interp(ax, bx, cp);
cpy = interp(ay, by, cp);
}
if (dp !== null) {
dpx = interp(ax, bx, dp);
dpy = interp(ay, by, dp);
}
if (cp !== null && dp !== null) {
if (cpx === dpx && cpy === dpy) return false; // degenerate case
// a---cp---dp---b
// c----d
if (cp < dp) {
if (!equals(ax, ay, cpx, cpy)) addSegment(result, ax, ay, cpx, cpy);
if (!equals(dpx, dpy, bx, by)) addSegment(result, dpx, dpy, bx, by);
// a---dp---cp---b
// d----c
} else {
if (!equals(ax, ay, dpx, dpy)) addSegment(result, ax, ay, dpx, dpy);
if (!equals(cpx, cpy, bx, by)) addSegment(result, cpx, cpy, bx, by);
}
} else if (cp !== null) {
// a----cp---b
// d---ap---c
if (ap !== null && !equals(ax, ay, cpx, cpy)) addSegment(result, cpx, cpy, bx, by);
// a---cp---b
// c----bp---d
else if (bp !== null && !equals(cpx, cpy, bx, by)) addSegment(result, ax, ay, cpx, cpy);
} else if (dp !== null) {
// a---dp---b
// d----bp---c
if (bp !== null && !equals(dpx, dpy, bx, by)) addSegment(result, ax, ay, dpx, dpy);
// a----dp---b
// c---ap---d
else if (ap !== null && !equals(ax, ay, dpx, dpy)) addSegment(result, dpx, dpy, bx, by);
}
return result.length !== len; // segment processed
}
function addSegment(arr, ax, ay, bx, by) {
arr.push(ax);
arr.push(ay);
arr.push(bx);
arr.push(by);
}
function interp(a, b, t) {
return a + (b - a) * t;
}
// find a closest point from a given point p to a segment [a, b]
// if it's within given square distance r
function closePoint(px, py, ax, ay, bx, by, r) {
var x = ax,
y = ay,
dx = bx - x,
dy = by - y;
if (dx !== 0 || dy !== 0) {
var t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
if (t >= 1) {
x = bx;
y = by;
t = 1;
} else if (t > 0) {
x += dx * t;
y += dy * t;
} else {
t = 0;
}
}
dx = px - x;
dy = py - y;
return dx * dx + dy * dy < r * r ? t : null;
}
function equals(ax, ay, bx, by) {
var dx = Math.abs(ax - bx);
var dy = Math.abs(ay - by);
return dx < 1e-12 && dy < 1e-12;
}