-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrebuild.js
More file actions
67 lines (65 loc) · 1.8 KB
/
rebuild.js
File metadata and controls
67 lines (65 loc) · 1.8 KB
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
const Bezier = require("bezier-js");
const keyofz = require("./toPoly").keyofz;
function rebuildShape(polys, seghash, termhash, RESOLUTION) {
let ans = [];
for (let c = 0; c < polys.length; c++) {
let cx = rebuildContour(polys[c], seghash, termhash, RESOLUTION);
if (cx.length) {
ans.push(cx);
}
}
return ans;
}
function split(s, t1, t2) {
if (t1 > t2) {
let b = s.split(t2, t1);
return new Bezier(...b.points.reverse());
} else {
return s.split(t1, t2);
}
}
function bezpt(Z, RESOLUTION) {
return { x: Z.X / RESOLUTION, y: Z.Y / RESOLUTION };
}
function rebuildContour(_poly, seghash, termhash, RESOLUTION) {
let j0 = 0;
for (; j0 < _poly.length && !termhash.has(keyofz(_poly[j0])); j0++);
const poly = _poly.slice(j0).concat(_poly.slice(0, j0 + 1));
let primSegments = [];
for (let j = 0; j < poly.length - 1; j++) {
const segkey = keyofz(poly[j]) + "-" + keyofz(poly[j + 1]);
if (seghash.has(segkey)) {
primSegments.push(seghash.get(segkey));
} else {
primSegments.push([
new Bezier(
bezpt(poly[j], RESOLUTION),
bezpt(poly[j], RESOLUTION),
bezpt(poly[j + 1], RESOLUTION),
bezpt(poly[j + 1], RESOLUTION)
),
0,
1
]);
}
}
let ans = [primSegments[0]];
for (let j = 1; j < primSegments.length; j++) {
const last = ans[ans.length - 1];
if (primSegments[j][0] === last[0]) {
// annex
if (last[1] < last[2]) {
last[1] = Math.min(last[1], primSegments[j][1]);
last[2] = Math.max(last[2], primSegments[j][2]);
} else {
last[1] = Math.max(last[1], primSegments[j][1]);
last[2] = Math.min(last[2], primSegments[j][2]);
}
} else {
// push
ans.push(primSegments[j]);
}
}
return ans.map(([s, t1, t2]) => split(s, t1, t2));
}
exports.rebuildShape = rebuildShape;