significance.test.ts 7.22 KB
import { describe, expect, it } from 'vitest';
import {
  computeKeepMask,
  countBackgroundBuckets,
  isSignificantAt,
  KEEP_MARGIN_M,
  pointTouchesSignificant,
  type SignificanceMask,
} from './significance';

/**
 * 显著性判定的单测。
 *
 * 这一层是「降噪后重新渲染」在地图侧的唯一判据:掩码之外的桶既淡出数字、也不进热力。
 * 判定余量(见 significance.ts 的 KEEP_MARGIN_M)是**固定米制、与缩放无关**的 ——
 * 这正是修掉「角落孤点光晕随缩放闪进闪出」的关键:旧实现用屏幕聚合桶的半边长当余量,
 * 那个边长随缩放剧烈变化,低缩放把远处的孤点也圈进显著、高缩放又摘掉。
 */

const ORIGIN = { originLng: 113.75, originLat: 22.45 };
/** 100m 见方的分析格子(约 0.001° 经度 × 0.0009° 纬度,这里取等边方便算) */
const D = 0.001;

/** 只点亮 (cx,cy) 这一格的掩码 */
function singleCellMask(cx: number, cy: number): SignificanceMask {
  const w = 100;
  const h = 100;
  const mask = new Uint8Array(w * h);
  mask[cy * w + cx] = 1;
  return { mask, w, h, dLng: D, dLat: D, ...ORIGIN };
}

/** 点亮 [cx0,cx1]×[cy0,cy1] 这块矩形区域(模拟一个黑点密集区) */
function blockMask(cx0: number, cx1: number, cy0: number, cy1: number): SignificanceMask {
  const w = 100;
  const h = 100;
  const mask = new Uint8Array(w * h);
  for (let cy = cy0; cy <= cy1; cy += 1) {
    for (let cx = cx0; cx <= cx1; cx += 1) mask[cy * w + cx] = 1;
  }
  return { mask, w, h, dLng: D, dLat: D, ...ORIGIN };
}

/** 第 50 行格子的纬度中心 */
const LAT_50 = ORIGIN.originLat + 50.5 * D;

describe('pointTouchesSignificant · 固定米制余量', () => {
  it('没有掩码时一切照旧(不过滤)', () => {
    expect(pointTouchesSignificant(null, 113.9, 22.6)).toBe(true);
    expect(isSignificantAt(null, 0, 0)).toBe(true);
  });

  it('中心落在显著格内 → 保留;远离显著区 → 剔除', () => {
    const s = singleCellMask(50, 50);
    expect(isSignificantAt(s, 113.8005, LAT_50)).toBe(true);
    expect(pointTouchesSignificant(s, 113.8005, LAT_50)).toBe(true);
    expect(pointTouchesSignificant(s, 114.1, 22.7)).toBe(false);
  });

  it('中心偏出显著区、但仍在固定余量内 → 仍然保留(黑点在地图上不能整块不亮)', () => {
    const s = singleCellMask(50, 50);
    // 中心在 cx=49.9(掩码 0),距显著格(cx=50)中心约 60m,落在 400m 余量内
    const center = 113.7999;
    expect(isSignificantAt(s, center, LAT_50)).toBe(false);
    expect(pointTouchesSignificant(s, center, LAT_50)).toBe(true);
  });

  it('整点落在栅格之外 → 剔除,且不越界取值', () => {
    const s = singleCellMask(50, 50);
    expect(pointTouchesSignificant(s, 114.9, 23.9)).toBe(false);
    expect(isSignificantAt(s, -200, -200)).toBe(false);
  });

  it('孤点光晕修复:0.8km 外的孤点在 z12 与 z16 下 keep 同为 0(旧实现会 1→0 闪动)', () => {
    const s = blockMask(40, 60, 40, 60);
    const blockWestEdge = ORIGIN.originLng + 40 * D;
    const isolatedLng = blockWestEdge - 0.0078; // 距显著块约 800m,远超固定余量
    const lng = Float64Array.from([isolatedLng]);
    const lat = Float64Array.from([LAT_50]);
    // 新实现:固定 KEEP_MARGIN_M 余量,与缩放无关 → 两种缩放下都是 0
    expect(computeKeepMask(s, lng, lat)[0]).toBe(0);
    expect(pointTouchesSignificant(s, isolatedLng, LAT_50)).toBe(false);
    // 旧实现的等价推算(仅说明,不进断言):
    //   z12:屏幕桶半边长 ≈ 2km,窗口盖到显著块 → keep=1(光晕出现)
    //   z16:屏幕桶半边长 ≈ 100m,窗口够不到显著块 → keep=0(光晕消失)
    // 这正是「低缩放有光圈、放大后光圈消失」的来源。
  });

  it('余量边界是 KEEP_MARGIN_M:块缘外 ~399m 仍保留、~401m 才剔除', () => {
    const s = blockMask(40, 60, 40, 60);
    const blockWestEdge = ORIGIN.originLng + 40 * D;
    // 约 399m ≈ 0.00388°、约 401m ≈ 0.00390°(本纬度 1°≈102.9km)
    expect(pointTouchesSignificant(s, blockWestEdge - 0.00388, LAT_50)).toBe(true);
    expect(pointTouchesSignificant(s, blockWestEdge - 0.0039, LAT_50)).toBe(false);
    expect(KEEP_MARGIN_M).toBe(400);
  });
});

/**
 * 「背景淡化 N 处」这一个数。
 *
 * 它以前不是这样算的:界面拿「点位数 − 热力点位」减出来。那两个数之间还隔着
 * 绘制端的一步补偿(右缘抽屉压住的那半边不画热力,见 MapStage 的 excludeUnderDrawer),
 * 于是被抽屉挡住的方格也被念成「被降噪判为背景」。数目会凑巧差几个,
 * 更糟的是同一屏上「点位数」「热力点位」「背景淡化」三个数减不拢,读起来就像数错了。
 * 现在它只数降噪那张表,与画不画、被谁挡住无关。
 */
describe('countBackgroundBuckets', () => {
  it('没开降噪时没有「被淡化」的方格可言', () => {
    expect(countBackgroundBuckets(null)).toBe(0);
  });

  it('只数降噪表里判为背景的那几格', () => {
    expect(countBackgroundBuckets(new Uint8Array([1, 1, 1]))).toBe(0);
    expect(countBackgroundBuckets(new Uint8Array([0, 0, 0]))).toBe(3);
    expect(countBackgroundBuckets(new Uint8Array([1, 0, 1, 0, 0]))).toBe(3);
  });

  it('背景 + 保留 = 这一批方格总数:工具条三个数里的两个必须能加回点位数', () => {
    const table = new Uint8Array(500);
    for (let i = 0; i < table.length; i += 1) table[i] = i % 5 === 0 ? 0 : 1;
    const background = countBackgroundBuckets(table);
    let drawn = 0;
    for (let i = 0; i < table.length; i += 1) if (table[i] === 1) drawn += 1;
    expect(background).toBe(100);
    expect(background + drawn).toBe(table.length);
  });
});

/**
 * 格子归属的取整方向。
 *
 * `| 0` 是向零截断:`(-0.4) | 0 === -0`,而 `-0 < 0` 为 false —— 框外西侧半个格的点
 * 会被算成 0 号格,继承它的显著性;同样距离在东/北侧(`cx === w`)却被判框外。
 * 结果是降噪留白在西/南比东/北宽一格,同一段距离两种结论。
 */
describe('isSignificantAt · 框外不能借 0 号格', () => {
  const eps = 0.4 * D;

  it('西侧 / 南侧框外半个格 → 不显著', () => {
    const s = singleCellMask(0, 0);
    const insideX = ORIGIN.originLng + 0.5 * D;
    const insideY = ORIGIN.originLat + 0.5 * D;
    expect(isSignificantAt(s, insideX, insideY)).toBe(true);
    expect(isSignificantAt(s, ORIGIN.originLng - eps, insideY)).toBe(false);
    expect(isSignificantAt(s, insideX, ORIGIN.originLat - eps)).toBe(false);
  });

  it('东侧 / 北侧框外半个格 → 不显著(与西/南对称)', () => {
    const s = singleCellMask(99, 99);
    const insideX = ORIGIN.originLng + 99.5 * D;
    const insideY = ORIGIN.originLat + 99.5 * D;
    expect(isSignificantAt(s, insideX, insideY)).toBe(true);
    expect(isSignificantAt(s, ORIGIN.originLng + 100 * D + eps, insideY)).toBe(false);
    expect(isSignificantAt(s, insideX, ORIGIN.originLat + 100 * D + eps)).toBe(false);
  });

  it('坐标是 NaN 时判定为不显著,不落到 0 号格', () => {
    const s = singleCellMask(0, 0);
    expect(isSignificantAt(s, Number.NaN, ORIGIN.originLat + 0.5 * D)).toBe(false);
  });
});