/** * ============================================================ * vision/geometry.ts —— 坐标系(纯函数) * ------------------------------------------------------------ * 整个 computer-use 只有**一个**对外坐标约定: * * 0–1000 相对值。左上 (0,0),右下 (1000,1000)。 * * 照抄 MiniMax M3 在 OSWorld computer-use 评测里的设置。它一次性绕开了 * 三类真实事故: * - macOS Retina:物理像素是逻辑点的 2 倍(scaleFactor=2),截图坐标 * 直接当鼠标坐标用会偏一半 * - Windows 显示缩放:进程若没声明 per-monitor DPI aware,125%/150% 下 * 系统会悄悄给缩放过的坐标,落点系统性偏移(第三方普遍踩这个坑) * - 多显示器 / 换分辨率:apps/*.md 里写的位置描述若绑死像素就全废 * * **物理换算只发生在本文件里。** 其它模块一律只见相对坐标。 * ============================================================ */ import type { Rect } from './raster.js'; /** 相对坐标空间的边长。取 1000 而非 100 是为了保留亚百分点精度(小控件需要)。 */ export const REL_SPAN = 1000; export interface Point { x: number; y: number; } /** 逻辑点空间里的一块屏幕。 */ export interface Screen { x: number; y: number; width: number; height: number; } export interface Size { width: number; height: number; } /** * scaleFactor = 物理像素 / 逻辑点。 * **必须实测算出,不能假设 1 或 2** —— 外接显示器、缩放设置、Sidecar * 都会给出非整数或非 2 的值。 */ export function scaleFactor(physical: Size, logical: Size): number { if (!logical.width) return 1; return physical.width / logical.width; } /** 相对点 → 逻辑点(鼠标事件用的坐标系)。越界钳住而非报错:模型偶尔给 1000 或轻微越界。 */ export function relToLogical(rel: Point, screen: Screen): Point { return { x: screen.x + clamp((rel.x / REL_SPAN) * screen.width, 0, screen.width - 1), y: screen.y + clamp((rel.y / REL_SPAN) * screen.height, 0, screen.height - 1), }; } /** 逻辑点 → 相对点(把 OS 报来的 caret/窗口位置翻译给模型看)。 */ export function logicalToRel(point: Point, screen: Screen): Point { if (!screen.width || !screen.height) return { x: 0, y: 0 }; return { x: Math.round(((point.x - screen.x) / screen.width) * REL_SPAN), y: Math.round(((point.y - screen.y) / screen.height) * REL_SPAN), }; } /** 相对矩形 → 某张图上的像素矩形(图可以是任意缩放尺寸,故传 imageSize 而非 screen)。 */ export function relRectToPixels(rect: Rect, imageSize: Size): Rect { const x = (rect.x / REL_SPAN) * imageSize.width; const y = (rect.y / REL_SPAN) * imageSize.height; return { x: Math.round(x), y: Math.round(y), width: Math.max(1, Math.round((rect.width / REL_SPAN) * imageSize.width)), height: Math.max(1, Math.round((rect.height / REL_SPAN) * imageSize.height)), }; } /** 像素矩形 → 相对矩形。 */ export function pixelRectToRel(rect: Rect, imageSize: Size): Rect { if (!imageSize.width || !imageSize.height) return { x: 0, y: 0, width: 0, height: 0 }; return { x: Math.round((rect.x / imageSize.width) * REL_SPAN), y: Math.round((rect.y / imageSize.height) * REL_SPAN), width: Math.round((rect.width / imageSize.width) * REL_SPAN), height: Math.round((rect.height / imageSize.height) * REL_SPAN), }; } /** * 向外扩边。变化区检出的包围盒往往刚好卡住控件边缘,裁出来会切掉半个按钮或 * 一圈阴影 —— 按比例外扩一圈(默认 20%)再裁,模型才看得到上下文。 */ export function padRect(rect: Rect, ratio = 0.2, bounds: Size | null = null): Rect { const dx = rect.width * ratio; const dy = rect.height * ratio; const padded = { x: rect.x - dx, y: rect.y - dy, width: rect.width + dx * 2, height: rect.height + dy * 2, }; return bounds ? clampRectTo(padded, bounds) : padded; } /** 钳进边界(bounds 用 {width, height},原点视为 0,0)。 */ export function clampRectTo(rect: Rect, bounds: Size): Rect { const x = clamp(rect.x, 0, bounds.width - 1); const y = clamp(rect.y, 0, bounds.height - 1); return { x, y, width: Math.max(1, Math.min(bounds.width - x, Math.round(rect.width))), height: Math.max(1, Math.min(bounds.height - y, Math.round(rect.height))), }; } /** 两个矩形的最小外接矩形。 */ export function unionRect(a: Rect | null, b: Rect | null): Rect | null { if (!a) return b; if (!b) return a; const x = Math.min(a.x, b.x); const y = Math.min(a.y, b.y); return { x, y, width: Math.max(a.x + a.width, b.x + b.width) - x, height: Math.max(a.y + a.height, b.y + b.height) - y, }; } /** 是否相交(用于判断「模型要点的地方」是否落在变化区里 —— epoch 陈旧性检查的核心)。 */ export function rectsIntersect(a: Rect, b: Rect): boolean { return ( a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height ); } /** 点是否在矩形内。 */ export function pointInRect(point: Point, rect: Rect): boolean { return ( point.x >= rect.x && point.x < rect.x + rect.width && point.y >= rect.y && point.y < rect.y + rect.height ); } export function clamp(value: number, min: number, max: number): number { return Math.max(min, Math.min(max, Math.round(value))); } /** 人话描述一个相对矩形,供文本通道使用(比塞一张图便宜 20 倍)。 */ export function describeRelRect(rect: Rect): string { return `(${rect.x},${rect.y})-(${rect.x + rect.width},${rect.y + rect.height})`; }