@ac-kit/math-grid 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +84 -0
- package/README.md +97 -3
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_coverage-contour2.d.ts +8 -1
- package/dist/_coverage-contour2.d.ts.map +1 -1
- package/dist/_coverage-contour2.js +477 -79
- package/dist/_coverage-contour2.js.map +1 -1
- package/dist/_grid-offsets2.d.ts +15 -0
- package/dist/_grid-offsets2.d.ts.map +1 -0
- package/dist/_grid-offsets2.js +53 -0
- package/dist/_grid-offsets2.js.map +1 -0
- package/dist/_nearest-site-propagation2.d.ts +1 -1
- package/dist/_nearest-site-propagation2.d.ts.map +1 -1
- package/dist/_nearest-site-propagation2.js +215 -59
- package/dist/_nearest-site-propagation2.js.map +1 -1
- package/dist/breadth-first-grid2.d.ts +65 -0
- package/dist/breadth-first-grid2.d.ts.map +1 -0
- package/dist/breadth-first-grid2.js +116 -0
- package/dist/breadth-first-grid2.js.map +1 -0
- package/dist/flood-fill2.d.ts +39 -0
- package/dist/flood-fill2.d.ts.map +1 -0
- package/dist/flood-fill2.js +76 -0
- package/dist/flood-fill2.js.map +1 -0
- package/dist/grid-neighbors2.d.ts +45 -0
- package/dist/grid-neighbors2.d.ts.map +1 -0
- package/dist/grid-neighbors2.js +45 -0
- package/dist/grid-neighbors2.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/max-rects-pack.d.ts +90 -0
- package/dist/max-rects-pack.d.ts.map +1 -0
- package/dist/max-rects-pack.js +310 -0
- package/dist/max-rects-pack.js.map +1 -0
- package/dist/rasterize-line2.d.ts +33 -0
- package/dist/rasterize-line2.d.ts.map +1 -0
- package/dist/rasterize-line2.js +103 -0
- package/dist/rasterize-line2.js.map +1 -0
- package/dist/signed-distance-from-coverage2.d.ts.map +1 -1
- package/dist/signed-distance-from-coverage2.js +147 -59
- package/dist/signed-distance-from-coverage2.js.map +1 -1
- package/dist/tile/_tile-matrix.d.ts +33 -0
- package/dist/tile/_tile-matrix.d.ts.map +1 -0
- package/dist/tile/_tile-matrix.js +186 -0
- package/dist/tile/_tile-matrix.js.map +1 -0
- package/dist/tile/index.d.ts +8 -0
- package/dist/tile/index.d.ts.map +1 -0
- package/dist/tile/index.js +8 -0
- package/dist/tile/index.js.map +1 -0
- package/dist/tile/quadkey.d.ts +30 -0
- package/dist/tile/quadkey.d.ts.map +1 -0
- package/dist/tile/quadkey.js +91 -0
- package/dist/tile/quadkey.js.map +1 -0
- package/dist/tile/tile-address.d.ts +91 -0
- package/dist/tile/tile-address.d.ts.map +1 -0
- package/dist/tile/tile-address.js +228 -0
- package/dist/tile/tile-address.js.map +1 -0
- package/dist/tile/tile-index.d.ts +26 -0
- package/dist/tile/tile-index.d.ts.map +1 -0
- package/dist/tile/tile-index.js +2 -0
- package/dist/tile/tile-index.js.map +1 -0
- package/dist/tile/tile-level.d.ts +24 -0
- package/dist/tile/tile-level.d.ts.map +1 -0
- package/dist/tile/tile-level.js +53 -0
- package/dist/tile/tile-level.js.map +1 -0
- package/dist/tile/tile-matrix-set-json.d.ts +67 -0
- package/dist/tile/tile-matrix-set-json.d.ts.map +1 -0
- package/dist/tile/tile-matrix-set-json.js +247 -0
- package/dist/tile/tile-matrix-set-json.js.map +1 -0
- package/dist/tile/tile-matrix-set.d.ts +174 -0
- package/dist/tile/tile-matrix-set.d.ts.map +1 -0
- package/dist/tile/tile-matrix-set.js +244 -0
- package/dist/tile/tile-matrix-set.js.map +1 -0
- package/dist/tile/tile-range.d.ts +57 -0
- package/dist/tile/tile-range.d.ts.map +1 -0
- package/dist/tile/tile-range.js +140 -0
- package/dist/tile/tile-range.js.map +1 -0
- package/package.json +10 -9
|
@@ -85,6 +85,201 @@ const SPREAD_TOLERANCE_ = 0.25;
|
|
|
85
85
|
const UNASSIGNED_ = -1;
|
|
86
86
|
/** A straight piece that belongs to no run. */
|
|
87
87
|
const LONE_ = -2;
|
|
88
|
+
/**
|
|
89
|
+
* Writes the straight edge of antialiased texel `(x, y)`, of coverage `value`,
|
|
90
|
+
* at `sites[at]` in the `COVERAGE_STRIDE_` layout, and its straight-edge
|
|
91
|
+
* residual over the 3 × 3 neighbourhood into `residuals[cell]`. Interior texels
|
|
92
|
+
* reuse the nine coverage samples already loaded for the Sobel gradient.
|
|
93
|
+
*/
|
|
94
|
+
export function locateEdgeAndResidual_(sites, at, coverage, value, x, y, width, height, residuals, cell) {
|
|
95
|
+
sites[at + 3] = Number.NaN;
|
|
96
|
+
sites[at + 4] = 0;
|
|
97
|
+
const border = x === 0 || y === 0 || x === width - 1 || y === height - 1;
|
|
98
|
+
if (border) {
|
|
99
|
+
if (borderStripe_(sites, at, coverage, value, x, y, width, height)) {
|
|
100
|
+
residuals[cell] = Number.NaN;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const topLeft = borderSample_(coverage, x, y, -1, -1, width, height, true);
|
|
104
|
+
const top = borderSample_(coverage, x, y, 0, -1, width, height, true);
|
|
105
|
+
const topRight = borderSample_(coverage, x, y, 1, -1, width, height, true);
|
|
106
|
+
const middleLeft = borderSample_(coverage, x, y, -1, 0, width, height, true);
|
|
107
|
+
const middleRight = borderSample_(coverage, x, y, 1, 0, width, height, true);
|
|
108
|
+
const bottomLeft = borderSample_(coverage, x, y, -1, 1, width, height, true);
|
|
109
|
+
const bottom = borderSample_(coverage, x, y, 0, 1, width, height, true);
|
|
110
|
+
const bottomRight = borderSample_(coverage, x, y, 1, 1, width, height, true);
|
|
111
|
+
const gradientX = topRight +
|
|
112
|
+
2 * middleRight +
|
|
113
|
+
bottomRight -
|
|
114
|
+
topLeft -
|
|
115
|
+
2 * middleLeft -
|
|
116
|
+
bottomLeft;
|
|
117
|
+
const gradientY = bottomLeft + 2 * bottom + bottomRight - topLeft - 2 * top - topRight;
|
|
118
|
+
const length = Math.sqrt(gradientX * gradientX + gradientY * gradientY);
|
|
119
|
+
if (length === 0) {
|
|
120
|
+
locateStripe_(sites, at, coverage, value, x, y, width, height, middleLeft + middleRight - 2 * value, top + bottom - 2 * value, topLeft + bottomRight - topRight - bottomLeft);
|
|
121
|
+
residuals[cell] = Number.NaN;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const normalX = -gradientX / length;
|
|
125
|
+
const normalY = -gradientY / length;
|
|
126
|
+
sites[at] = normalX;
|
|
127
|
+
sites[at + 1] = normalY;
|
|
128
|
+
EDGE_[0] = normalX;
|
|
129
|
+
EDGE_[1] = normalY;
|
|
130
|
+
EDGE_[2] = value / 255;
|
|
131
|
+
edgeOffset_();
|
|
132
|
+
sites[at + 2] = EDGE_[3];
|
|
133
|
+
edgeResidual_(sites, at, coverage, x, y, width, height, residuals, cell);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const above = cell - width;
|
|
137
|
+
const below = cell + width;
|
|
138
|
+
const topLeft = coverage[above - 1];
|
|
139
|
+
const top = coverage[above];
|
|
140
|
+
const topRight = coverage[above + 1];
|
|
141
|
+
const middleLeft = coverage[cell - 1];
|
|
142
|
+
const middleRight = coverage[cell + 1];
|
|
143
|
+
const bottomLeft = coverage[below - 1];
|
|
144
|
+
const bottom = coverage[below];
|
|
145
|
+
const bottomRight = coverage[below + 1];
|
|
146
|
+
const gradientX = topRight +
|
|
147
|
+
2 * middleRight +
|
|
148
|
+
bottomRight -
|
|
149
|
+
topLeft -
|
|
150
|
+
2 * middleLeft -
|
|
151
|
+
bottomLeft;
|
|
152
|
+
const gradientY = bottomLeft + 2 * bottom + bottomRight - topLeft - 2 * top - topRight;
|
|
153
|
+
const length = Math.sqrt(gradientX * gradientX + gradientY * gradientY);
|
|
154
|
+
if (length === 0) {
|
|
155
|
+
locateStripe_(sites, at, coverage, value, x, y, width, height, middleLeft + middleRight - 2 * value, top + bottom - 2 * value, topLeft + bottomRight - topRight - bottomLeft);
|
|
156
|
+
residuals[cell] = Number.NaN;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const normalX = -gradientX / length;
|
|
160
|
+
const normalY = -gradientY / length;
|
|
161
|
+
const absX = normalX < 0 ? -normalX : normalX;
|
|
162
|
+
const absY = normalY < 0 ? -normalY : normalY;
|
|
163
|
+
const a = absX >= absY ? absX : absY;
|
|
164
|
+
const b = absX >= absY ? absY : absX;
|
|
165
|
+
const alpha = value * INV_255_;
|
|
166
|
+
const half = (a + b) * 0.5;
|
|
167
|
+
let offset;
|
|
168
|
+
if (b < 1e-6) {
|
|
169
|
+
offset = a * (alpha - 0.5);
|
|
170
|
+
}
|
|
171
|
+
else if (alpha <= b / (2 * a)) {
|
|
172
|
+
offset = -half + Math.sqrt(2 * a * b * alpha);
|
|
173
|
+
}
|
|
174
|
+
else if (alpha >= 1 - b / (2 * a)) {
|
|
175
|
+
offset = half - Math.sqrt(2 * a * b * (1 - alpha));
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
offset = a * (alpha - 0.5);
|
|
179
|
+
}
|
|
180
|
+
sites[at] = normalX;
|
|
181
|
+
sites[at + 1] = normalY;
|
|
182
|
+
sites[at + 2] = offset;
|
|
183
|
+
// Residual over the 3 × 3 window using the eight samples already in registers
|
|
184
|
+
// (the centre texel's offset was inverted from `alpha`, so its error is 0).
|
|
185
|
+
const mid = (a - b) * 0.5;
|
|
186
|
+
const invA = 1 / a;
|
|
187
|
+
const inv2ab = b >= 1e-6 ? 0.5 / (a * b) : 0;
|
|
188
|
+
let residual = 0;
|
|
189
|
+
let o = offset + normalX + normalY;
|
|
190
|
+
let pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
191
|
+
let diff = pred - topLeft * INV_255_;
|
|
192
|
+
if (diff < 0) {
|
|
193
|
+
diff = -diff;
|
|
194
|
+
}
|
|
195
|
+
if (diff > residual) {
|
|
196
|
+
residual = diff;
|
|
197
|
+
}
|
|
198
|
+
o = offset + normalY;
|
|
199
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
200
|
+
diff = pred - top * INV_255_;
|
|
201
|
+
if (diff < 0) {
|
|
202
|
+
diff = -diff;
|
|
203
|
+
}
|
|
204
|
+
if (diff > residual) {
|
|
205
|
+
residual = diff;
|
|
206
|
+
}
|
|
207
|
+
o = offset - normalX + normalY;
|
|
208
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
209
|
+
diff = pred - topRight * INV_255_;
|
|
210
|
+
if (diff < 0) {
|
|
211
|
+
diff = -diff;
|
|
212
|
+
}
|
|
213
|
+
if (diff > residual) {
|
|
214
|
+
residual = diff;
|
|
215
|
+
}
|
|
216
|
+
o = offset + normalX;
|
|
217
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
218
|
+
diff = pred - middleLeft * INV_255_;
|
|
219
|
+
if (diff < 0) {
|
|
220
|
+
diff = -diff;
|
|
221
|
+
}
|
|
222
|
+
if (diff > residual) {
|
|
223
|
+
residual = diff;
|
|
224
|
+
}
|
|
225
|
+
o = offset - normalX;
|
|
226
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
227
|
+
diff = pred - middleRight * INV_255_;
|
|
228
|
+
if (diff < 0) {
|
|
229
|
+
diff = -diff;
|
|
230
|
+
}
|
|
231
|
+
if (diff > residual) {
|
|
232
|
+
residual = diff;
|
|
233
|
+
}
|
|
234
|
+
o = offset + normalX - normalY;
|
|
235
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
236
|
+
diff = pred - bottomLeft * INV_255_;
|
|
237
|
+
if (diff < 0) {
|
|
238
|
+
diff = -diff;
|
|
239
|
+
}
|
|
240
|
+
if (diff > residual) {
|
|
241
|
+
residual = diff;
|
|
242
|
+
}
|
|
243
|
+
o = offset - normalY;
|
|
244
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
245
|
+
diff = pred - bottom * INV_255_;
|
|
246
|
+
if (diff < 0) {
|
|
247
|
+
diff = -diff;
|
|
248
|
+
}
|
|
249
|
+
if (diff > residual) {
|
|
250
|
+
residual = diff;
|
|
251
|
+
}
|
|
252
|
+
o = offset - normalX - normalY;
|
|
253
|
+
pred = edgeCovFast_(o, half, mid, invA, inv2ab);
|
|
254
|
+
diff = pred - bottomRight * INV_255_;
|
|
255
|
+
if (diff < 0) {
|
|
256
|
+
diff = -diff;
|
|
257
|
+
}
|
|
258
|
+
if (diff > residual) {
|
|
259
|
+
residual = diff;
|
|
260
|
+
}
|
|
261
|
+
residuals[cell] = residual;
|
|
262
|
+
}
|
|
263
|
+
const INV_255_ = 1 / 255;
|
|
264
|
+
function edgeCovFast_(offset, half, mid, invA, inv2ab) {
|
|
265
|
+
if (offset <= -half) {
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
if (offset >= half) {
|
|
269
|
+
return 1;
|
|
270
|
+
}
|
|
271
|
+
if (inv2ab !== 0) {
|
|
272
|
+
if (offset < -mid) {
|
|
273
|
+
const t = offset + half;
|
|
274
|
+
return t * t * inv2ab;
|
|
275
|
+
}
|
|
276
|
+
if (offset > mid) {
|
|
277
|
+
const t = half - offset;
|
|
278
|
+
return 1 - t * t * inv2ab;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return 0.5 + offset * invA;
|
|
282
|
+
}
|
|
88
283
|
/**
|
|
89
284
|
* Writes the straight edge of antialiased texel `(x, y)`, of coverage `value`,
|
|
90
285
|
* at `sites[at]` in the `COVERAGE_STRIDE_` layout.
|
|
@@ -427,13 +622,13 @@ const GRID_ = {
|
|
|
427
622
|
* texel. Returns how many corners there are, their holders listed from
|
|
428
623
|
* `scratch[count]`.
|
|
429
624
|
*/
|
|
430
|
-
export function findContour_(scratch, residuals, coverage, width, height) {
|
|
625
|
+
export function findContour_(scratch, residuals, coverage, width, height, siteCells, siteCount) {
|
|
431
626
|
const count = width * height;
|
|
432
627
|
GRID_.width = width;
|
|
433
628
|
GRID_.height = height;
|
|
434
629
|
GRID_.base = 2 * count;
|
|
435
630
|
GRID_.coverage = coverage;
|
|
436
|
-
const runs = buildRuns_(scratch, residuals);
|
|
631
|
+
const runs = buildRuns_(scratch, residuals, siteCells, siteCount);
|
|
437
632
|
const corners = joinRuns_(scratch, residuals, runs);
|
|
438
633
|
spreadCorners_(scratch, corners);
|
|
439
634
|
GRID_.coverage = NO_COVERAGE_;
|
|
@@ -502,11 +697,12 @@ function straightPiece_(scratch, residuals, cell, piece) {
|
|
|
502
697
|
* are listed downward from `scratch[2 · count − 1]`, and each run's pieces are
|
|
503
698
|
* labelled with its holder, the others `LONE_`.
|
|
504
699
|
*/
|
|
505
|
-
function buildRuns_(scratch, residuals) {
|
|
700
|
+
function buildRuns_(scratch, residuals, siteCells, siteCount) {
|
|
506
701
|
const { width, height } = GRID_;
|
|
507
702
|
const count = width * height;
|
|
508
703
|
let runs = 0;
|
|
509
|
-
for (let
|
|
704
|
+
for (let siteIndex = 0; siteIndex < siteCount; siteIndex += 1) {
|
|
705
|
+
const seed = siteCells[siteIndex];
|
|
510
706
|
if (scratch[seed] !== UNASSIGNED_ ||
|
|
511
707
|
!straightPiece_(scratch, residuals, seed, SEED_PIECE_)) {
|
|
512
708
|
continue;
|
|
@@ -572,6 +768,8 @@ const MAX_RUN_PIECES_ = 1024;
|
|
|
572
768
|
* `-1` for a binary one: every step of the fit reads them again.
|
|
573
769
|
*/
|
|
574
770
|
const RUN_PIECES_ = new Float64Array(5 * MAX_RUN_PIECES_);
|
|
771
|
+
const RUN_KEPT_ = new Uint8Array(MAX_RUN_PIECES_);
|
|
772
|
+
const RUN_ALONG_ = new Float64Array(MAX_RUN_PIECES_);
|
|
575
773
|
/** Stores `piece`, of cell `cell`, as the `index`-th of `RUN_PIECES_`. */
|
|
576
774
|
function cachePiece_(index, cell, piece) {
|
|
577
775
|
const value = GRID_.coverage[cell];
|
|
@@ -617,8 +815,11 @@ function fitRun_(scratch, members) {
|
|
|
617
815
|
if (fitted) {
|
|
618
816
|
for (let index = 0; index < members; index += 1) {
|
|
619
817
|
const member = scratch[count + index];
|
|
620
|
-
|
|
621
|
-
|
|
818
|
+
const isKept = runMember_(scratch, index, RUN_PASS_);
|
|
819
|
+
const along = RUN_[2] * (PIECE_[0] - RUN_[0]) + RUN_[3] * (PIECE_[1] - RUN_[1]);
|
|
820
|
+
RUN_KEPT_[index] = isKept ? 1 : 0;
|
|
821
|
+
RUN_ALONG_[index] = along;
|
|
822
|
+
if (isKept) {
|
|
622
823
|
kept += 1;
|
|
623
824
|
if (along < low) {
|
|
624
825
|
low = along;
|
|
@@ -641,10 +842,10 @@ function fitRun_(scratch, members) {
|
|
|
641
842
|
// from the line there (an arc whose two flanks are near one chord): not a
|
|
642
843
|
// straight run
|
|
643
844
|
for (let index = 0; index < members; index += 1) {
|
|
644
|
-
if (
|
|
845
|
+
if (RUN_KEPT_[index] === 1) {
|
|
645
846
|
continue;
|
|
646
847
|
}
|
|
647
|
-
const along =
|
|
848
|
+
const along = RUN_ALONG_[index];
|
|
648
849
|
if (along > low && along < high) {
|
|
649
850
|
for (let other = 0; other < members; other += 1) {
|
|
650
851
|
scratch[scratch[count + other]] = LONE_;
|
|
@@ -661,14 +862,10 @@ function fitRun_(scratch, members) {
|
|
|
661
862
|
let holderGap = Infinity;
|
|
662
863
|
for (let index = 0; index < members; index += 1) {
|
|
663
864
|
const member = scratch[count + index];
|
|
664
|
-
const inner = member !== lowMember &&
|
|
665
|
-
member !== highMember &&
|
|
666
|
-
runMember_(scratch, index, RUN_PASS_);
|
|
865
|
+
const inner = member !== lowMember && member !== highMember && RUN_KEPT_[index] === 1;
|
|
667
866
|
scratch[member] = inner ? INNER_ : LONE_;
|
|
668
867
|
if (inner) {
|
|
669
|
-
const gap = Math.abs(
|
|
670
|
-
RUN_[3] * (PIECE_[1] - RUN_[1]) -
|
|
671
|
-
middle);
|
|
868
|
+
const gap = Math.abs(RUN_ALONG_[index] - middle);
|
|
672
869
|
if (gap < holderGap) {
|
|
673
870
|
holderGap = gap;
|
|
674
871
|
holder = member;
|
|
@@ -786,23 +983,76 @@ function fitRunPass_(scratch, members, pass) {
|
|
|
786
983
|
// moments about the run's current point keep the sums small
|
|
787
984
|
const originX = RUN_[0];
|
|
788
985
|
const originY = RUN_[1];
|
|
986
|
+
const dirX = RUN_[2];
|
|
987
|
+
const dirY = RUN_[3];
|
|
988
|
+
const { width, height } = GRID_;
|
|
989
|
+
const count = width * height;
|
|
990
|
+
const tol = TOLERANCES_[pass];
|
|
991
|
+
let runNX = -dirY;
|
|
992
|
+
let runNY = dirX;
|
|
993
|
+
if (runNX * SEED_PIECE_[2] + runNY * SEED_PIECE_[3] < 0) {
|
|
994
|
+
runNX = -runNX;
|
|
995
|
+
runNY = -runNY;
|
|
996
|
+
}
|
|
997
|
+
const absNX = runNX < 0 ? -runNX : runNX;
|
|
998
|
+
const absNY = runNY < 0 ? -runNY : runNY;
|
|
999
|
+
const a = absNX >= absNY ? absNX : absNY;
|
|
1000
|
+
const b = absNX >= absNY ? absNY : absNX;
|
|
1001
|
+
const half = (a + b) * 0.5;
|
|
1002
|
+
const bOver2a = b >= 1e-6 ? b / (2 * a) : -1;
|
|
1003
|
+
const twoAB = 2 * a * b;
|
|
789
1004
|
for (let index = 0; index < members; index += 1) {
|
|
790
|
-
|
|
791
|
-
|
|
1005
|
+
const at = 5 * index;
|
|
1006
|
+
let px = RUN_PIECES_[at];
|
|
1007
|
+
let py = RUN_PIECES_[at + 1];
|
|
1008
|
+
const pnx = RUN_PIECES_[at + 2];
|
|
1009
|
+
const pny = RUN_PIECES_[at + 3];
|
|
1010
|
+
if (pass !== ALL_PIECES_) {
|
|
1011
|
+
if (runNX * pnx + runNY * pny < SAME_RUN_COSINE_) {
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
const alpha = RUN_PIECES_[at + 4];
|
|
1015
|
+
if (alpha >= 0) {
|
|
1016
|
+
let offset;
|
|
1017
|
+
if (bOver2a < 0) {
|
|
1018
|
+
offset = a * (alpha - 0.5);
|
|
1019
|
+
}
|
|
1020
|
+
else if (alpha <= bOver2a) {
|
|
1021
|
+
offset = -half + Math.sqrt(twoAB * alpha);
|
|
1022
|
+
}
|
|
1023
|
+
else if (alpha >= 1 - bOver2a) {
|
|
1024
|
+
offset = half - Math.sqrt(twoAB * (1 - alpha));
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
offset = a * (alpha - 0.5);
|
|
1028
|
+
}
|
|
1029
|
+
const member = scratch[count + index];
|
|
1030
|
+
const column = member % width;
|
|
1031
|
+
px = column + runNX * offset;
|
|
1032
|
+
py = (member - column) / width + runNY * offset;
|
|
1033
|
+
}
|
|
1034
|
+
const dist = dirX * (py - originY) - dirY * (px - originX);
|
|
1035
|
+
if (dist > tol || dist < -tol) {
|
|
1036
|
+
continue;
|
|
1037
|
+
}
|
|
792
1038
|
}
|
|
793
|
-
const dx =
|
|
794
|
-
const dy =
|
|
1039
|
+
const dx = px - originX;
|
|
1040
|
+
const dy = py - originY;
|
|
795
1041
|
kept += 1;
|
|
796
1042
|
sumX += dx;
|
|
797
1043
|
sumY += dy;
|
|
798
1044
|
sumXX += dx * dx;
|
|
799
1045
|
sumXY += dx * dy;
|
|
800
1046
|
sumYY += dy * dy;
|
|
801
|
-
normalX +=
|
|
802
|
-
normalY +=
|
|
803
|
-
const along =
|
|
804
|
-
|
|
805
|
-
|
|
1047
|
+
normalX += pnx;
|
|
1048
|
+
normalY += pny;
|
|
1049
|
+
const along = dirX * dx + dirY * dy;
|
|
1050
|
+
if (along < low) {
|
|
1051
|
+
low = along;
|
|
1052
|
+
}
|
|
1053
|
+
if (along > high) {
|
|
1054
|
+
high = along;
|
|
1055
|
+
}
|
|
806
1056
|
}
|
|
807
1057
|
if (kept === 0) {
|
|
808
1058
|
return false;
|
|
@@ -1314,21 +1564,45 @@ function placeTip_(convex) {
|
|
|
1314
1564
|
}
|
|
1315
1565
|
let low = -limit;
|
|
1316
1566
|
let high = limit;
|
|
1567
|
+
CORNER_AT_[0] = vertexX;
|
|
1568
|
+
CORNER_AT_[1] = vertexY;
|
|
1569
|
+
blockArea_(convex, left, top, right, bottom);
|
|
1570
|
+
const zeroExcess = RESULT_[0] - covered;
|
|
1571
|
+
if (Math.abs(zeroExcess) < 1e-6) {
|
|
1572
|
+
RESULT_[0] = 0;
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
let lowExcess;
|
|
1576
|
+
let highExcess;
|
|
1317
1577
|
CORNER_AT_[0] = vertexX + low * bisectorX;
|
|
1318
1578
|
CORNER_AT_[1] = vertexY + low * bisectorY;
|
|
1319
1579
|
blockArea_(convex, left, top, right, bottom);
|
|
1320
|
-
|
|
1321
|
-
CORNER_AT_[0] = vertexX + high * bisectorX;
|
|
1322
|
-
CORNER_AT_[1] = vertexY + high * bisectorY;
|
|
1323
|
-
blockArea_(convex, left, top, right, bottom);
|
|
1324
|
-
let highExcess = RESULT_[0] - covered;
|
|
1580
|
+
lowExcess = RESULT_[0] - covered;
|
|
1325
1581
|
if (lowExcess === 0) {
|
|
1326
1582
|
RESULT_[0] = low;
|
|
1327
1583
|
return;
|
|
1328
1584
|
}
|
|
1329
|
-
if (
|
|
1330
|
-
|
|
1331
|
-
|
|
1585
|
+
if (lowExcess * zeroExcess < 0) {
|
|
1586
|
+
high = 0;
|
|
1587
|
+
highExcess = zeroExcess;
|
|
1588
|
+
}
|
|
1589
|
+
else {
|
|
1590
|
+
CORNER_AT_[0] = vertexX + high * bisectorX;
|
|
1591
|
+
CORNER_AT_[1] = vertexY + high * bisectorY;
|
|
1592
|
+
blockArea_(convex, left, top, right, bottom);
|
|
1593
|
+
highExcess = RESULT_[0] - covered;
|
|
1594
|
+
if (highExcess === 0) {
|
|
1595
|
+
RESULT_[0] = high;
|
|
1596
|
+
return;
|
|
1597
|
+
}
|
|
1598
|
+
if (zeroExcess * highExcess < 0) {
|
|
1599
|
+
low = 0;
|
|
1600
|
+
lowExcess = zeroExcess;
|
|
1601
|
+
}
|
|
1602
|
+
else {
|
|
1603
|
+
RESULT_[0] = Math.abs(lowExcess) < Math.abs(highExcess) ? low : high;
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1332
1606
|
}
|
|
1333
1607
|
let side = 0;
|
|
1334
1608
|
let shift = 0;
|
|
@@ -1338,7 +1612,7 @@ function placeTip_(convex) {
|
|
|
1338
1612
|
CORNER_AT_[1] = vertexY + shift * bisectorY;
|
|
1339
1613
|
blockArea_(convex, left, top, right, bottom);
|
|
1340
1614
|
const excess = RESULT_[0] - covered;
|
|
1341
|
-
if (Math.abs(excess) < 1e-
|
|
1615
|
+
if (Math.abs(excess) < 1e-6 || high - low < 1e-5) {
|
|
1342
1616
|
break;
|
|
1343
1617
|
}
|
|
1344
1618
|
if (excess * highExcess > 0) {
|
|
@@ -1387,10 +1661,61 @@ function clippedBlock_(left, top, right, bottom, first, second) {
|
|
|
1387
1661
|
const vertexX = CORNER_AT_[0];
|
|
1388
1662
|
const vertexY = CORNER_AT_[1];
|
|
1389
1663
|
// the block's corners, relative to the vertex, where both lines pass
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1664
|
+
let lowX = left - 0.5 - vertexX;
|
|
1665
|
+
let highX = right + 0.5 - vertexX;
|
|
1666
|
+
let lowY = top - 0.5 - vertexY;
|
|
1667
|
+
let highY = bottom + 0.5 - vertexY;
|
|
1668
|
+
const n1x = CORNER_NORMALS_[first];
|
|
1669
|
+
const n1y = CORNER_NORMALS_[first + 1];
|
|
1670
|
+
const n2x = CORNER_NORMALS_[second];
|
|
1671
|
+
const n2y = CORNER_NORMALS_[second + 1];
|
|
1672
|
+
const a1H = Math.abs(n1x) < 1e-9;
|
|
1673
|
+
const a1V = !a1H && Math.abs(n1y) < 1e-9;
|
|
1674
|
+
const a2H = first === second ? a1H : Math.abs(n2x) < 1e-9;
|
|
1675
|
+
const a2V = first === second ? a1V : !a2H && Math.abs(n2y) < 1e-9;
|
|
1676
|
+
if ((a1H || a1V) && (a2H || a2V)) {
|
|
1677
|
+
if (a1H) {
|
|
1678
|
+
if (n1y > 0) {
|
|
1679
|
+
if (highY > 0) {
|
|
1680
|
+
highY = 0;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
else if (lowY < 0) {
|
|
1684
|
+
lowY = 0;
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
else if (n1x > 0) {
|
|
1688
|
+
if (highX > 0) {
|
|
1689
|
+
highX = 0;
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
else if (lowX < 0) {
|
|
1693
|
+
lowX = 0;
|
|
1694
|
+
}
|
|
1695
|
+
if (first !== second) {
|
|
1696
|
+
if (a2H) {
|
|
1697
|
+
if (n2y > 0) {
|
|
1698
|
+
if (highY > 0) {
|
|
1699
|
+
highY = 0;
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
else if (lowY < 0) {
|
|
1703
|
+
lowY = 0;
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
else if (n2x > 0) {
|
|
1707
|
+
if (highX > 0) {
|
|
1708
|
+
highX = 0;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
else if (lowX < 0) {
|
|
1712
|
+
lowX = 0;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
RESULT_[0] =
|
|
1716
|
+
highX > lowX && highY > lowY ? (highX - lowX) * (highY - lowY) : 0;
|
|
1717
|
+
return;
|
|
1718
|
+
}
|
|
1394
1719
|
BLOCK_[0] = lowX;
|
|
1395
1720
|
BLOCK_[1] = lowY;
|
|
1396
1721
|
BLOCK_[2] = highX;
|
|
@@ -1399,8 +1724,8 @@ function clippedBlock_(left, top, right, bottom, first, second) {
|
|
|
1399
1724
|
BLOCK_[5] = highY;
|
|
1400
1725
|
BLOCK_[6] = lowX;
|
|
1401
1726
|
BLOCK_[7] = highY;
|
|
1402
|
-
HALF_PLANES_[0] =
|
|
1403
|
-
HALF_PLANES_[1] =
|
|
1727
|
+
HALF_PLANES_[0] = n1x;
|
|
1728
|
+
HALF_PLANES_[1] = n1y;
|
|
1404
1729
|
HALF_PLANES_[2] = 0;
|
|
1405
1730
|
const vertices = polygon2ClipHalfPlane(CLIP_OUT_, BLOCK_, 4, HALF_PLANES_, 0);
|
|
1406
1731
|
// behind one line only: a second clip by it would change nothing
|
|
@@ -1408,8 +1733,8 @@ function clippedBlock_(left, top, right, bottom, first, second) {
|
|
|
1408
1733
|
RESULT_[0] = polygon2Area(CLIP_OUT_, vertices);
|
|
1409
1734
|
return;
|
|
1410
1735
|
}
|
|
1411
|
-
HALF_PLANES_[3] =
|
|
1412
|
-
HALF_PLANES_[4] =
|
|
1736
|
+
HALF_PLANES_[3] = n2x;
|
|
1737
|
+
HALF_PLANES_[4] = n2y;
|
|
1413
1738
|
HALF_PLANES_[5] = 0;
|
|
1414
1739
|
RESULT_[0] = polygon2Area(CLIP_IN_, polygon2ClipHalfPlane(CLIP_IN_, CLIP_OUT_, vertices, HALF_PLANES_, 3));
|
|
1415
1740
|
}
|
|
@@ -1426,12 +1751,61 @@ function predictCorner_(convex, column, row) {
|
|
|
1426
1751
|
const secondNormalY = CORNER_NORMALS_[3];
|
|
1427
1752
|
const firstLimit = firstNormalX * (vertexX - column) + firstNormalY * (vertexY - row);
|
|
1428
1753
|
const secondLimit = secondNormalX * (vertexX - column) + secondNormalY * (vertexY - row);
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1754
|
+
const half1 = ((firstNormalX < 0 ? -firstNormalX : firstNormalX) +
|
|
1755
|
+
(firstNormalY < 0 ? -firstNormalY : firstNormalY)) *
|
|
1756
|
+
0.5;
|
|
1757
|
+
const half2 = ((secondNormalX < 0 ? -secondNormalX : secondNormalX) +
|
|
1758
|
+
(secondNormalY < 0 ? -secondNormalY : secondNormalY)) *
|
|
1759
|
+
0.5;
|
|
1432
1760
|
if (convex) {
|
|
1761
|
+
if (firstLimit <= -half1 || secondLimit <= -half2) {
|
|
1762
|
+
RESULT_[0] = 0;
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
if (firstLimit >= half1) {
|
|
1766
|
+
EDGE_[0] = secondNormalX;
|
|
1767
|
+
EDGE_[1] = secondNormalY;
|
|
1768
|
+
EDGE_[2] = secondLimit;
|
|
1769
|
+
edgeCoverage_();
|
|
1770
|
+
RESULT_[0] = EDGE_[3];
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
if (secondLimit >= half2) {
|
|
1774
|
+
EDGE_[0] = firstNormalX;
|
|
1775
|
+
EDGE_[1] = firstNormalY;
|
|
1776
|
+
EDGE_[2] = firstLimit;
|
|
1777
|
+
edgeCoverage_();
|
|
1778
|
+
RESULT_[0] = EDGE_[3];
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
HALF_PLANES_[2] = firstLimit;
|
|
1782
|
+
HALF_PLANES_[5] = secondLimit;
|
|
1783
|
+
wedgeArea_();
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
if (firstLimit >= half1 || secondLimit >= half2) {
|
|
1787
|
+
RESULT_[0] = 1;
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1790
|
+
if (firstLimit <= -half1) {
|
|
1791
|
+
EDGE_[0] = secondNormalX;
|
|
1792
|
+
EDGE_[1] = secondNormalY;
|
|
1793
|
+
EDGE_[2] = secondLimit;
|
|
1794
|
+
edgeCoverage_();
|
|
1795
|
+
RESULT_[0] = EDGE_[3];
|
|
1796
|
+
return;
|
|
1797
|
+
}
|
|
1798
|
+
if (secondLimit <= -half2) {
|
|
1799
|
+
EDGE_[0] = firstNormalX;
|
|
1800
|
+
EDGE_[1] = firstNormalY;
|
|
1801
|
+
EDGE_[2] = firstLimit;
|
|
1802
|
+
edgeCoverage_();
|
|
1803
|
+
RESULT_[0] = EDGE_[3];
|
|
1433
1804
|
return;
|
|
1434
1805
|
}
|
|
1806
|
+
HALF_PLANES_[2] = firstLimit;
|
|
1807
|
+
HALF_PLANES_[5] = secondLimit;
|
|
1808
|
+
wedgeArea_();
|
|
1435
1809
|
const both = RESULT_[0];
|
|
1436
1810
|
EDGE_[0] = firstNormalX;
|
|
1437
1811
|
EDGE_[1] = firstNormalY;
|
|
@@ -1477,7 +1851,9 @@ function cornerMisfit_(convex, x, y, vertexColumn, vertexRow, radius, clear) {
|
|
|
1477
1851
|
const { width, height, coverage } = GRID_;
|
|
1478
1852
|
const limit = clear ? CLEAR_CORNER_RESIDUAL_ : CORNER_RESIDUAL_;
|
|
1479
1853
|
let misfit = 0;
|
|
1480
|
-
|
|
1854
|
+
const sameCentre = x === vertexColumn && y === vertexRow;
|
|
1855
|
+
const passes = sameCentre ? 1 : 2;
|
|
1856
|
+
for (let around = 0; around < passes; around += 1) {
|
|
1481
1857
|
const centreX = around === 0 ? x : vertexColumn;
|
|
1482
1858
|
const centreY = around === 0 ? y : vertexRow;
|
|
1483
1859
|
for (let row = Math.max(0, centreY - radius); row <= Math.min(height - 1, centreY + radius); row += 1) {
|
|
@@ -1530,55 +1906,77 @@ function wedgeArea_() {
|
|
|
1530
1906
|
* record the choice before any geometry moves, so the order of the corners does
|
|
1531
1907
|
* not matter. The `corners` holders are listed from `scratch[count]`.
|
|
1532
1908
|
*/
|
|
1909
|
+
let spreadCells_ = new Int32Array(0);
|
|
1533
1910
|
function spreadCorners_(scratch, corners) {
|
|
1534
1911
|
const { width, height, base, coverage } = GRID_;
|
|
1535
1912
|
const count = width * height;
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1913
|
+
let spreadCount = 0;
|
|
1914
|
+
for (let index = 0; index < corners; index += 1) {
|
|
1915
|
+
// `| 0`: integers the engine keeps as such, which a call never boxes
|
|
1916
|
+
const corner = scratch[count + index] | 0;
|
|
1917
|
+
const at = base + COVERAGE_STRIDE_ * corner;
|
|
1918
|
+
const cornerColumn = corner % width;
|
|
1919
|
+
const cornerRow = (corner - cornerColumn) / width;
|
|
1920
|
+
const vertexColumn = Math.round(cornerColumn + scratch[at]) | 0;
|
|
1921
|
+
const vertexRow = Math.round(cornerRow + scratch[at + 1]) | 0;
|
|
1922
|
+
const rowStart = Math.max(0, vertexRow - EDGE_SPREAD_REACH_);
|
|
1923
|
+
const rowEnd = Math.min(height - 1, vertexRow + EDGE_SPREAD_REACH_);
|
|
1924
|
+
const colStart = Math.max(0, vertexColumn - EDGE_SPREAD_REACH_);
|
|
1925
|
+
const colEnd = Math.min(width - 1, vertexColumn + EDGE_SPREAD_REACH_);
|
|
1926
|
+
for (let row = rowStart; row <= rowEnd; row += 1) {
|
|
1927
|
+
const dy = row >= vertexRow ? row - vertexRow : vertexRow - row;
|
|
1928
|
+
const rowBase = row * width;
|
|
1929
|
+
for (let column = colStart; column <= colEnd; column += 1) {
|
|
1930
|
+
const cell = rowBase + column;
|
|
1931
|
+
if (cell === corner) {
|
|
1932
|
+
continue;
|
|
1933
|
+
}
|
|
1934
|
+
const value = coverage[cell];
|
|
1935
|
+
if (value === 0 || value === 255) {
|
|
1936
|
+
const dx = column >= vertexColumn
|
|
1937
|
+
? column - vertexColumn
|
|
1938
|
+
: vertexColumn - column;
|
|
1939
|
+
if (dy > SPREAD_REACH_ || dx > SPREAD_REACH_) {
|
|
1554
1940
|
continue;
|
|
1555
1941
|
}
|
|
1556
|
-
const
|
|
1557
|
-
if (
|
|
1558
|
-
|
|
1559
|
-
copyCorner_(scratch, corner, cell);
|
|
1560
|
-
}
|
|
1942
|
+
const opposite = 255 - value;
|
|
1943
|
+
if (!(column + 1 < width && coverage[cell + 1] === opposite) &&
|
|
1944
|
+
!(cell + width < count && coverage[cell + width] === opposite)) {
|
|
1561
1945
|
continue;
|
|
1562
1946
|
}
|
|
1563
|
-
|
|
1564
|
-
|
|
1947
|
+
}
|
|
1948
|
+
const held = scratch[cell];
|
|
1949
|
+
if (held >= 0 && (held === cell || isRun_(scratch, base, held))) {
|
|
1950
|
+
continue;
|
|
1951
|
+
}
|
|
1952
|
+
spreadMisfit_(scratch, corner, cell);
|
|
1953
|
+
const misfit = RESULT_[0];
|
|
1954
|
+
if (misfit < Infinity) {
|
|
1955
|
+
let better = held < 0;
|
|
1956
|
+
if (!better) {
|
|
1957
|
+
spreadMisfit_(scratch, held, cell);
|
|
1958
|
+
better = misfit < RESULT_[0];
|
|
1565
1959
|
}
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
scratch[cell] = corner;
|
|
1960
|
+
if (better) {
|
|
1961
|
+
if (held < 0) {
|
|
1962
|
+
if (spreadCount === spreadCells_.length) {
|
|
1963
|
+
const grown = new Int32Array(Math.max(64, spreadCells_.length * 2));
|
|
1964
|
+
grown.set(spreadCells_);
|
|
1965
|
+
spreadCells_ = grown;
|
|
1966
|
+
}
|
|
1967
|
+
spreadCells_[spreadCount] = cell;
|
|
1968
|
+
spreadCount += 1;
|
|
1576
1969
|
}
|
|
1970
|
+
scratch[cell] = corner;
|
|
1577
1971
|
}
|
|
1578
1972
|
}
|
|
1579
1973
|
}
|
|
1580
1974
|
}
|
|
1581
1975
|
}
|
|
1976
|
+
for (let index = 0; index < spreadCount; index += 1) {
|
|
1977
|
+
const cell = spreadCells_[index];
|
|
1978
|
+
copyCorner_(scratch, scratch[cell] | 0, cell);
|
|
1979
|
+
}
|
|
1582
1980
|
}
|
|
1583
1981
|
/** Copies the corner site of cell `corner` into cell `cell`. */
|
|
1584
1982
|
function copyCorner_(scratch, corner, cell) {
|