glassPlate.test.ts
14.2 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* 数字的透明玻璃底盘(第四十七轮):浓度定在哪,由两条判据的交点算出来,不由眼睛调。
*
* 现场口径与主测试同源(同一片深圳合成案量、同一套分阶与淡底抬升),量的是一件具体的事:
* 每个数字底下那一圈 72px 格子的计数被画成盘之后,热力场的层次掉了多少、而盘自己看得见吗。
*
* 定案 α=0.26:可行区算出来只有 0.26~0.30 这一档,取它的下沿(肉眼阈值沿用第四十三轮那条 12)。
* 两段底色按色带位置切:暖底 = stop≥0.35(热核与橙红那一片),淡底 = stop<0.35(青蓝与本底)。
* 下沿「盘要在暖底上看得见」由 z13/z14 卡住 —— α0.22 时只有 10.9 / 11.7,
* 等于白加一层没有存在感的水渍;α0.26 才 12.9 / 13.8(z15 14.8)。
* 上沿「淡底那一段不许铺墙」由 z14/z15 卡住 —— α0.30 时淡底上已 12.4 / 13.3,
* α0.4 是 14.0 / 15.0,CSS 面板那档 0.62 是 25.6 / 27.4;那都是几百个亮块连成的白盘地毯,
* 正是第三十八轮被否掉的形状。取 0.26 而不是 0.30:淡底那一侧多留 0.5~1.5 的余量。
* 代价照记:恒定 α 对底下颜色不敏感,压在最浅那一段上等于抹平局部,盘内 0.15→0.3 掉到 9.5;
* 整屏口径同一条仍 12.7 ≥12(盘的面积只占屏 4.2%)。换字重与描边都买不回盘内这一段。
*/
import { describe, expect, it } from 'vitest';
import { Frame, mercY } from '@hm/map-overlay';
import { BAIDU_TILE_SIZE } from './BaiduBasemap';
import {
RAW_RADIUS_PX,
blurRadiusFor,
boxBlur2d,
buildToneTable,
coldLift,
fieldAlpha,
radiusZoomFactor,
toneLookup,
} from './RawCanvasHeatLayer';
import { buildLut } from './CanvasHeatLayer';
import { CLUSTER_LABEL, GLASS_PLATE } from '../../palette';
const W = 1280;
const H = 800;
const GS = 0.5;
const gw = Math.round(W * GS);
const gh = Math.round(H * GS);
const total = gw * gh;
/** 数字格子的屏幕边长:与现场同一档,一格一个数 */
const GRID_PX = 72;
/** 第四十三轮定的肉眼阈值:两色的亮度差与色度差,取大的那个 */
const MIN_VISIBLE = 12;
const N = 42800;
let seed = 20261126;
const rnd = () => {
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
return seed / 0x7fffffff;
};
const gauss = () => {
const u = Math.max(1e-9, rnd());
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rnd());
};
const cores: [number, number, number, number][] = [
[114.11, 22.55, 0.02, 0.26],
[114.06, 22.62, 0.028, 0.22],
[114.19, 22.65, 0.022, 0.18],
[114.32, 22.72, 0.02, 0.1],
[113.95, 22.71, 0.024, 0.1],
[114.25, 22.54, 0.02, 0.08],
];
const pLng = new Float64Array(N);
const pMerc = new Float64Array(N);
for (let i = 0; i < N; i += 1) {
const r = rnd();
let acc = 0;
let chosen = -1;
for (let c = 0; c < cores.length; c += 1) {
acc += cores[c][3];
if (r <= acc) {
chosen = c;
break;
}
}
if (chosen < 0) {
pLng[i] = 113.75 + rnd() * 0.85;
pMerc[i] = mercY(22.4 + rnd() * 0.42);
} else {
const [cl, ca, sl, sa] = cores[chosen];
pLng[i] = cl + gauss() * sl;
pMerc[i] = mercY(ca + gauss() * sa);
}
}
const LUT = buildLut();
const WHITE = [255, 255, 255];
function linear(v: number): number {
const s = v / 255;
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
}
function lum(c: number[]): number {
return 0.2126 * linear(c[0]) + 0.7152 * linear(c[1]) + 0.0722 * linear(c[2]);
}
function dist(p: number[], q: number[]): number {
const dl = Math.abs(lum(p) - lum(q)) * 100;
const dc = Math.max(Math.abs(p[0] - q[0]), Math.abs(p[1] - q[1]), Math.abs(p[2] - q[2])) / 2.55;
return Math.max(dl, dc);
}
/** 白 α 叠在任意底色上 —— 盘这一笔在画布上就是这个合成(画布拿不到底图,按白底图计) */
function overWhite(rgb: number[], alpha: number): number[] {
return WHITE.map((v, i) => rgb[i] * (1 - alpha) + v * alpha);
}
function avg(list: number[][]): number[] {
const out = [0, 0, 0];
for (const c of list) for (let i = 0; i < 3; i += 1) out[i] += c[i] / list.length;
return out;
}
interface Screen {
/** 每个灰度像素的色带位置(0 = 不落地)与归一化浓度 */
stop: Float32Array;
t: Float32Array;
/** 叠到底图之后的颜色 */
heatOver: (number[] | null)[];
/** 数字(一格一个):屏幕位置、盘半径、本格案量 */
discs: { x: number; y: number; r: number; count: number }[];
/** 像素 → 压在它上面的数字;-1 表示这一格没有被盘盖住 */
discOf: Int32Array;
/** 盘面积占屏比例 */
plateArea: number;
}
function build(zoom: number): Screen {
const frame = new Frame();
const center = zoom >= 14 ? cores[0] : ([114.06, 22.62] as number[]);
frame.set(
{ lngLatToContainer: () => ({ x: W / 2, y: H / 2 }) } as never,
zoom,
center[0],
center[1],
{ tileSize: BAIDU_TILE_SIZE, referenceLat: 22.5, latSlope: 0.99424 },
);
const counts = new Int32Array(total);
const cellCount = new Map<number, number>();
const cellSumX = new Map<number, number>();
const cellSumY = new Map<number, number>();
for (let i = 0; i < N; i += 1) {
const cx = frame.x(pLng[i]);
const cy = frame.y(pMerc[i]);
if (cx < 0 || cx >= W || cy < 0 || cy >= H) continue;
counts[((cy * GS) | 0) * gw + ((cx * GS) | 0)] += 1;
const key = (Math.floor(cx / GRID_PX) << 16) | Math.floor(cy / GRID_PX);
cellCount.set(key, (cellCount.get(key) ?? 0) + 1);
cellSumX.set(key, (cellSumX.get(key) ?? 0) + cx);
cellSumY.set(key, (cellSumY.get(key) ?? 0) + cy);
}
const brush = RAW_RADIUS_PX * radiusZoomFactor(zoom);
const r = blurRadiusFor(Math.max(2, Math.round(brush * GS)));
const tmp = new Float32Array(total);
const a = new Float32Array(total);
const b = new Float32Array(total);
const prefix = new Float64Array(Math.max(gw, gh) + 1);
boxBlur2d(counts, a, tmp, gw, gh, r, prefix);
boxBlur2d(a, b, tmp, gw, gh, r, prefix);
boxBlur2d(b, a, tmp, gw, gh, r, prefix);
let peak = 0;
for (let p = 0; p < total; p += 1) if (a[p] > peak) peak = a[p];
const tone = buildToneTable(zoom);
const stop = new Float32Array(total);
const t = new Float32Array(total);
const heatOver: (number[] | null)[] = new Array(total).fill(null);
for (let p = 0; p < total; p += 1) {
const tv = peak > 0 ? a[p] / peak : 0;
t[p] = tv;
const s = coldLift(toneLookup(tone, tv));
stop[p] = s;
if (s <= 0) continue;
const k = Math.min(255, Math.round(s * 255)) * 3;
const alpha = fieldAlpha(tv);
heatOver[p] = [
LUT[k] * alpha + 255 * (1 - alpha),
LUT[k + 1] * alpha + 255 * (1 - alpha),
LUT[k + 2] * alpha + 255 * (1 - alpha),
];
}
// 盘半径只由文字宽度决定(现场同一式:max(LABEL_MIN_R=8, 半宽 + padX=3),位数越多越宽)
const discs: Screen['discs'] = [];
for (const [key, count] of cellCount) {
const x = (cellSumX.get(key) ?? 0) / count;
const y = (cellSumY.get(key) ?? 0) / count;
if (x < GRID_PX || x > W - GRID_PX || y < GRID_PX || y > H - GRID_PX) continue;
const digits = String(count).length;
const half = (digits * (digits > 3 ? 5.6 : 6.2)) / 2;
discs.push({ x, y, r: Math.max(8, half + 3), count });
}
const discOf = new Int32Array(total).fill(-1);
discs.forEach((d, di) => {
const cx = d.x * GS;
const cy = d.y * GS;
const rr = d.r * GS;
for (let y = Math.max(0, Math.floor(cy - rr)); y <= Math.min(gh - 1, Math.ceil(cy + rr)); y += 1) {
for (let x = Math.max(0, Math.floor(cx - rr)); x <= Math.min(gw - 1, Math.ceil(cx + rr)); x += 1) {
if ((x - cx) ** 2 + (y - cy) ** 2 <= rr * rr) discOf[y * gw + x] = di;
}
}
});
let area = 0;
for (let p = 0; p < total; p += 1) if (discOf[p] >= 0) area += 1;
return { stop, t, heatOver, discs, discOf, plateArea: area / total };
}
const CACHE = new Map<number, Screen>();
function screenAt(zoom: number): Screen {
let s = CACHE.get(zoom);
if (!s) {
s = build(zoom);
CACHE.set(zoom, s);
}
return s;
}
/**
* 盘的可见度:被数字盖住的像素,「加盘之后」与「原色」的平均色距。
* 分两段按色带位置切:onWarm = ≥0.35(橙红往上,热核与暖区都算),onPale = <0.35(淡底与本底)。
* 这条分界与第四十六轮那套「淡底/暖底」的口径同源,别改成按 0.5 切 —— 那样数字会整体抬 2~3,
* 文档里的 12.9/8.0 那一组数就对不上现场了。
*/
function plateVisibility(s: Screen, alpha: number): { onWarm: number; onPale: number } {
const warm: number[] = [];
const pale: number[] = [];
for (let p = 0; p < total; p += 1) {
if (s.discOf[p] < 0) continue;
const heat = s.heatOver[p];
if (!heat) continue;
const d = dist(heat, overWhite(heat, alpha));
if (s.stop[p] >= 0.35) warm.push(d);
else pale.push(d);
}
const m = (arr: number[]) => (arr.length ? arr.reduce((x, y) => x + y, 0) / arr.length : 0);
return { onWarm: m(warm), onPale: m(pale) };
}
/**
* 局部对比:t≈lo 与 t≈hi 两堆像素各自「加盘之后」的颜色取平均,再算色距。
* onlyPlate = 只算被数字盘盖住的那一小块(盘内),否则整屏(盘外不动,只算盘内那一小圈的抬亮)。
*/
function localDelta(s: Screen, lo: number, hi: number, alpha: number, onlyPlate: boolean): number {
const A: number[][] = [];
const B: number[][] = [];
for (let p = 0; p < total; p += 1) {
if (s.stop[p] <= 0) continue;
if (onlyPlate && s.discOf[p] < 0) continue;
const heat = s.heatOver[p]!;
const a = onlyPlate || s.discOf[p] >= 0 ? alpha : 0;
const c = overWhite(heat, a);
if (Math.abs(s.t[p] - lo) <= 0.012) A.push(c);
else if (Math.abs(s.t[p] - hi) <= 0.012) B.push(c);
}
if (A.length < 16 || B.length < 16) return -1;
return dist(avg(A), avg(B));
}
describe('数字玻璃盘:浓度卡在「盘看得见」与「淡底不铺墙」的交点(第四十七轮)', () => {
const ALPHA = GLASS_PLATE.alpha;
it('可行区只有 0.26~0.30 这一档:下沿由暖底可见度定(z13/z14),上沿由近景淡底定(z14/z15)', () => {
expect(ALPHA).toBeCloseTo(0.26, 9);
// 下沿:α0.22 那一档,z13 的暖底上只有 10.9、z14 只有 11.7 —— 加了等于没加
expect(plateVisibility(screenAt(13), 0.22).onWarm).toBeLessThan(MIN_VISIBLE);
expect(plateVisibility(screenAt(14), 0.22).onWarm).toBeLessThan(MIN_VISIBLE);
// 上沿:α0.30 那一档,z14 的淡底已经 12.4、z15 到 13.3 —— 淡底上也亮出一块,开始铺墙
expect(plateVisibility(screenAt(14), 0.3).onPale).toBeGreaterThanOrEqual(MIN_VISIBLE);
expect(plateVisibility(screenAt(15), 0.3).onPale).toBeGreaterThanOrEqual(MIN_VISIBLE);
// 两头之间只剩 0.26:暖底上三档都看得见,淡底上三档都还看不见
for (const zoom of [13, 14, 15]) {
const v = plateVisibility(screenAt(zoom), ALPHA);
expect(v.onWarm).toBeGreaterThanOrEqual(MIN_VISIBLE);
expect(v.onPale).toBeLessThan(MIN_VISIBLE);
}
});
it('盘在暖底(热核那一片)上看得见:三档缩放的叠色差都 ≥12(12.9 / 13.8 / 14.8)', () => {
const by: number[] = [];
for (const zoom of [13, 14, 15]) by.push(plateVisibility(screenAt(zoom), ALPHA).onWarm);
for (const v of by) expect(v).toBeGreaterThanOrEqual(MIN_VISIBLE);
expect(by[0]).toBeCloseTo(12.9, 0);
});
it('盘在淡底那一段近乎不存在:<12,所以默认视图不会变成满屏白盘', () => {
for (const zoom of [13, 14, 15])
expect(plateVisibility(screenAt(zoom), ALPHA).onPale).toBeLessThan(MIN_VISIBLE);
// CSS 面板那一档(0.62)搬来做盘就是墙:同一个判据直接把它否掉
expect(plateVisibility(screenAt(13), 0.62).onPale).toBeGreaterThanOrEqual(MIN_VISIBLE);
});
it('整屏那条 2× 判据不因盘破;代价在盘内那一小圈,照数记下', () => {
const s = screenAt(13);
expect(localDelta(s, 0.15, 0.3, ALPHA, false)).toBeGreaterThanOrEqual(MIN_VISIBLE);
// 盘内 12.9→9.5:恒定 α 不认底下是什么颜色,压在最浅的带上就把局部抹平了一点。
// 这不是「可以接受」,是已知的代价:买回来只能让盘的浓度跟着底色走,
// 那会让「盘的存在感」变成第三种主次口径,读的人得和字色对账。
const inside = localDelta(s, 0.15, 0.3, ALPHA, true);
expect(inside).toBeGreaterThan(6);
expect(inside).toBeLessThan(MIN_VISIBLE);
expect(inside).toBeLessThan(localDelta(s, 0.15, 0.3, 0, false));
});
it('盘的面积是个位数百分比:「热力是主角」这句话由面积钉住', () => {
const at13 = screenAt(13).plateArea;
expect(at13).toBeGreaterThan(0);
expect(at13).toBeLessThan(0.06);
// 近景格子摊得更开、数字更少,盘的占比只会更小
expect(screenAt(14).plateArea).toBeLessThan(at13);
expect(screenAt(15).plateArea).toBeLessThan(screenAt(14).plateArea);
});
it('一个常数就是一屏都同一个数:盘把每个底色朝白推同一个**比例**,不携带案量信息', () => {
expect(GLASS_PLATE.fill).toBe(`rgba(255,255,255,${ALPHA})`);
const s = screenAt(13);
// 恒定 α 的定义式:(加盘后的色 − 原色) ÷ (白 − 原色) 对任何底色都等于同一个 α。
// 所以盘既不分档、也不按案量浓淡 —— 谁多谁少仍只由字色那一支说话;
// 副作用是在最亮的红核上「抬的绝对量」最小(R 通道本来就接近 255),热核上洗得最轻。
let checked = 0;
for (let p = 0; p < total && checked < 400; p += 1) {
const heat = s.heatOver[p];
if (!heat || s.discOf[p] < 0) continue;
checked += 1;
const plated = overWhite(heat, ALPHA);
for (let i = 0; i < 3; i += 1) {
const gap = 255 - heat[i];
if (gap < 1) continue;
expect((plated[i] - heat[i]) / gap).toBeCloseTo(ALPHA, 6);
}
}
expect(checked).toBeGreaterThan(100);
});
it('玻璃比 CSS 那套面板淡得多,也与黑点盘心的白托不是同一支', () => {
expect(ALPHA).toBeLessThan(0.4);
expect(ALPHA).toBeLessThan(0.62);
expect(CLUSTER_LABEL.plate).toBe('rgba(255,255,255,.4)');
expect(GLASS_PLATE.rim).toBe('rgba(255,255,255,0.6)');
expect(GLASS_PLATE.rimWidth).toBe(1);
});
});