// 一次性探针(票 47):母版下半收尖的三次贝塞尔拟合。 // 假设:轮廓在帽圆某切点处离开圆,以一条三次贝塞尔到达尖端;对切点角度扫描取最优。 // 输出 bbox 坐标系(x-67, y-19)的 path 参数,直接嵌进 markerOverlay.js。 import { readFileSync } from 'node:fs' import { inflateSync } from 'node:zlib' function decodePng(path) { const buf = readFileSync(path) let pos = 8 const idat = [] let width = 0, height = 0, colorType = 0, trns = null while (pos < buf.length) { const len = buf.readUInt32BE(pos) const type = buf.toString('ascii', pos + 4, pos + 8) const data = buf.subarray(pos + 8, pos + 8 + len) if (type === 'IHDR') { width = data.readUInt32BE(0); height = data.readUInt32BE(4); colorType = data[9] } else if (type === 'tRNS') trns = Buffer.from(data) else if (type === 'IDAT') idat.push(data) else if (type === 'IEND') break pos += 12 + len } const channels = colorType === 6 ? 4 : colorType === 2 ? 3 : 1 const stride = width * channels const raw = inflateSync(Buffer.concat(idat)) const img = Buffer.alloc(height * stride) for (let y = 0; y < height; y++) { const filter = raw[y * (stride + 1)] const row = raw.subarray(y * (stride + 1) + 1, (y + 1) * stride) const prev = y > 0 ? img.subarray((y - 1) * stride, y * stride) : null const out = img.subarray(y * stride, (y + 1) * stride) for (let x = 0; x < stride; x++) { const a = x >= channels ? out[x - channels] : 0 const b = prev ? prev[x] : 0 const c = prev && x >= channels ? prev[x - channels] : 0 let v = row[x] if (filter === 1) v += a else if (filter === 2) v += b else if (filter === 3) v += (a + b) >> 1 else if (filter === 4) { const p = a + b - c const pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c) v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c } out[x] = v & 0xff } } const alphaAt = (x, y) => { if (colorType === 6) return img[y * stride + x * 4 + 3] const idx = img[y * stride + x] if (trns && idx < trns.length) return trns[idx] return 255 } return { width, height, alphaAt } } // bbox 坐标系(绝对像素减 (67,19)):帽圆 (188.5,189) r189;尖端 (188.5,474) const CAP = { cx: 188.5, cy: 189, r: 189 } const TIP = { x: 188.5, y: 474 } const src = decodePng(process.argv[2] || 'public/cockpit/assets/icons/T_MarkPoint_WhiteFull.png') // 左缘采样:每行第一个不透明点(bbox 坐标) function leftEdge(yAbs) { for (let x = 67; x <= 444; x++) if (src.alphaAt(x, yAbs) > 8) return x - 67 return null } const pts = [] for (let yAbs = 380; yAbs <= 492; yAbs += 2) { const x = leftEdge(yAbs) if (x != null) pts.push({ x, y: yAbs - 19 }) } console.log(`左缘采样 ${pts.length} 点(y ${pts[0].y}..${pts[pts.length - 1].y})`) console.log(pts.map((p) => `(${p.x.toFixed(1)},${p.y.toFixed(1)})`).join(' ')) // 三次贝塞尔最小二乘(P0/P3 固定,求 P1/P2;弦长参数化) function fitBezier(p0, p3, samples) { const cum = [0] for (let i = 1; i < samples.length; i++) { cum.push(cum[i - 1] + Math.hypot(samples[i].x - samples[i - 1].x, samples[i].y - samples[i - 1].y)) } const total = cum[cum.length - 1] || 1 const ts = samples.map((_, i) => 0.5 - 0.5 * Math.cos(Math.PI * (cum[i] / total))) // 余弦参数化,端点加密 const n = samples.length // 残差最小化:对每个样本 i,B(t_i) ≈ sample_i;B 对 P1,P2 线性 → 2×2 正规方程 let a11 = 0, a12 = 0, a22 = 0, b1x = 0, b1y = 0, b2x = 0, b2y = 0 const coef = (t) => { const mt = 1 - t return { p0: mt * mt * mt, p1: 3 * mt * mt * t, p2: 3 * mt * t * t, p3: t * t * t } } for (let i = 0; i < n; i++) { const c = coef(ts[i]) const r1 = c.p1, r2 = c.p2 a11 += r1 * r1; a12 += r1 * r2; a22 += r2 * r2 b1x += r1 * (samples[i].x - c.p0 * p0.x - c.p3 * p3.x) b1y += r1 * (samples[i].y - c.p0 * p0.y - c.p3 * p3.y) b2x += r2 * (samples[i].x - c.p0 * p0.x - c.p3 * p3.x) b2y += r2 * (samples[i].y - c.p0 * p0.y - c.p3 * p3.y) } const det = a11 * a22 - a12 * a12 const c1x = (b1x * a22 - b2x * a12) / det const c1y = (b1y * a22 - b2y * a12) / det const c2x = (b2x * a11 - b1x * a12) / det const c2y = (b2y * a11 - b1y * a12) / det const p1 = { x: c1x, y: c1y }, p2 = { x: c2x, y: c2y } let maxRes = 0 for (let i = 0; i < n; i++) { const c = coef(ts[i]) const bx = c.p0 * p0.x + c.p1 * p1.x + c.p2 * p2.x + c.p3 * TIP.x const by = c.p0 * p0.y + c.p1 * p1.y + c.p2 * p2.y + c.p3 * TIP.y maxRes = Math.max(maxRes, Math.hypot(bx - samples[i].x, by - samples[i].y)) } return { p1, p2, maxRes } } // 切点扫描:P0 在帽圆左半(θ 自 +x 轴、y 向下;左下段 θ∈95°..140°:x<188.5 且 y>189) let best = null for (let deg = 95; deg <= 140; deg += 1) { const rad = (deg * Math.PI) / 180 const p0 = { x: CAP.cx + CAP.r * Math.cos(rad), y: CAP.cy + CAP.r * Math.sin(rad) } if (p0.y < 300) continue // 路径方向 = 沿圆逆时针(屏幕系:顶→左→下),θ 递减方向的切向 = (sinθ, −cosθ) const tan = { x: Math.sin(rad), y: -Math.cos(rad) } // 样本 = 切点下方 3px 起的左缘点 const samples = pts.filter((p) => p.y >= p0.y + 3 && p.x <= CAP.cx) if (samples.length < 6) continue // P1 = p0 + k·tan(G1 方向固定,长度 k 粗扫) for (let k = 10; k <= 160; k += 5) { const p1 = { x: p0.x + tan.x * k, y: p0.y + tan.y * k } const r = fitBezierG1(p0, p1, samples) if (!best || r.maxRes < best.maxRes) best = { deg, k, p0, p1, p2: r.p2, maxRes: r.maxRes } } } function fitBezierG1(p0, p1, samples) { // 固定 P0,P1,P3,求 P2(线性) const cum = [0] for (let i = 1; i < samples.length; i++) cum.push(cum[i - 1] + Math.hypot(samples[i].x - samples[i - 1].x, samples[i].y - samples[i - 1].y)) const total = cum[cum.length - 1] || 1 const ts = samples.map((_, i) => cum[i] / total) let aa = 0, ab = 0, bb = 0 const sx = [], sy = [] for (let i = 0; i < samples.length; i++) { const t = ts[i], mt = 1 - t const c0 = mt * mt * mt, c1 = 3 * mt * mt * t, c2 = 3 * mt * t * t, c3 = t * t * t aa += c2 * c2; ab += c2 * c3; bb += c3 * c3 sx.push(samples[i].x - c0 * p0.x - c1 * p1.x) sy.push(samples[i].y - c0 * p0.y - c1 * p1.y) } // 最小化 Σ|c2·P2 + c3·TIP - s|²;未知 P2 二维,正规方程含 c3·TIP 交叉项 let a2t = 0, s2x = 0, s2y = 0, ttx = 0, tty = 0 for (let i = 0; i < samples.length; i++) { const t = ts[i], mt = 1 - t const c2 = 3 * mt * t * t, c3 = t * t * t a2t += c2 * c3; ttx += c3 * c3 * TIP.x; tty += c3 * c3 * TIP.y s2x += c2 * sx[i]; s2y += c2 * sy[i] } // aa·P2 + a2t·TIP = Sx(分量式)→ P2 = (Sx - a2t·TIP.x)/aa const p2 = { x: (s2x - a2t * TIP.x) / aa, y: (s2y - a2t * TIP.y) / aa } let maxRes = 0 for (let i = 0; i < samples.length; i++) { const t = ts[i], mt = 1 - t const c0 = mt * mt * mt, c1 = 3 * mt * mt * t, c2 = 3 * mt * t * t, c3 = t * t * t const bx = c0 * p0.x + c1 * p1.x + c2 * p2.x + c3 * TIP.x const by = c0 * p0.y + c1 * p1.y + c2 * p2.y + c3 * TIP.y maxRes = Math.max(maxRes, Math.hypot(bx - samples[i].x, by - samples[i].y)) } return { p2, maxRes } } if (!best) { console.log('无有效拟合'); process.exit(1) } console.log(`\n最优:切点角 ${best.deg}° → P0(${best.p0.x.toFixed(1)}, ${best.p0.y.toFixed(1)}),k=${best.k}`) console.log(`P1(${best.p1.x.toFixed(1)}, ${best.p1.y.toFixed(1)}) P2(${best.p2.x.toFixed(1)}, ${best.p2.y.toFixed(1)}) → TIP(188.5,474)`) console.log(`最大残差 ${best.maxRes.toFixed(2)}px`) // 完整闭合路径(bbox 空间,viewBox 外扩 40 同 billboard 裁剪窗): // 顶点起,左弧(视觉逆时针 = sweep 0)到切点 → 贝塞尔到尖端 → 镜像贝塞尔(反放:控制点 P2',P1')到右切点 → 右弧回顶。 console.log(`\npath: M188.5 0 A189 189 0 0 0 ${best.p0.x.toFixed(1)} ${best.p0.y.toFixed(1)} C${best.p1.x.toFixed(1)} ${best.p1.y.toFixed(1)} ${best.p2.x.toFixed(1)} ${best.p2.y.toFixed(1)} 188.5 474 C${(377 - best.p2.x).toFixed(1)} ${best.p2.y.toFixed(1)} ${(377 - best.p1.x).toFixed(1)} ${best.p1.y.toFixed(1)} ${(377 - best.p0.x).toFixed(1)} ${best.p0.y.toFixed(1)} A189 189 0 0 0 188.5 0 Z`)