-
Notifications
You must be signed in to change notification settings - Fork 1
/
toPoly.js
98 lines (95 loc) · 2.37 KB
/
toPoly.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
"use strict";
function keyofz(z) {
return "X" + z.X + "Y" + z.Y;
}
function by_t(a, b) {
return a.t - b.t;
}
function filterKnots(knots) {
knots = knots.sort(by_t);
let ans = [knots[0]];
for (let z of knots) {
const last = ans[ans.length - 1];
if (z.X !== last.X || z.Y !== last.Y) {
ans.push(z);
} else {
last.t = z.t < 1 / 2 ? Math.min(z.t, last.t) : Math.max(z.t, last.t);
}
}
return ans;
}
function setSegHash(seghash, key, entry) {
if (seghash.has(key)) {
const [seg, t1, t2, sindex, j, k] = entry;
const [seg1, t11, t21, sindex1, j1, k1] = seghash.get(key);
if (
sindex < sindex1 ||
(sindex === sindex1 && j < j1) ||
(sindex === sindex1 && j === j1 && k < k1)
) {
seghash.set(key, entry);
}
} else {
seghash.set(key, entry);
}
}
function toPoly(shape, sindex, splats, seghash, termhash, RESOLUTION) {
let ans = [];
for (let j = 0; j < shape.length; j++) {
let points = [];
const contour = shape[j];
const splat = splats[j];
for (let k = 0; k < contour.length; k++) {
let knots = [];
const nStaticBreaks = contour[k]._linear
? 1
: Math.max(5, Math.ceil(contour[k].length() / 5));
for (let j = 0; j <= nStaticBreaks; j++) {
const z = contour[k].compute(j / nStaticBreaks);
const knot = {
X: Math.round(z.x * RESOLUTION),
Y: Math.round(z.y * RESOLUTION),
t: j / nStaticBreaks
};
knots.push(knot);
if (j === 0 || (j === nStaticBreaks && k === contour.length - 1)) {
termhash.add(keyofz(knot));
}
}
for (let s of splat) {
if (s.t <= k || s.t >= k + 1) continue;
const knot = {
X: Math.round(s.x * RESOLUTION),
Y: Math.round(s.y * RESOLUTION),
t: s.t - k
};
knots.push(knot);
termhash.add(keyofz(knot));
}
knots = filterKnots(knots);
for (let j = 0; j < knots.length - 1; j++) {
setSegHash(seghash, keyofz(knots[j]) + "-" + keyofz(knots[j + 1]), [
contour[k],
knots[j].t,
knots[j + 1].t,
sindex,
j,
k
]);
setSegHash(seghash, keyofz(knots[j + 1]) + "-" + keyofz(knots[j]), [
contour[k],
knots[j + 1].t,
knots[j].t,
sindex,
j,
k
]);
}
points = points.concat(knots.slice(k > 0 ? 1 : 0));
}
ans.push(points);
}
return ans;
}
exports.keyofz = keyofz;
exports.toPoly = toPoly;