docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
@@ -9,12 +9,15 @@ use std::collections::HashMap;
|
||||
#[derive(Debug, Default)]
|
||||
/// 有限屏幕格子和后备集合。候选筛选允许保守多取,但不能因索引预算漏掉有效命中。
|
||||
pub struct Index {
|
||||
// 建立索引时的 Mount 总数,用来发现明显过期的索引;数量相同不代表几何未改变。
|
||||
count: usize,
|
||||
// 格子坐标映射到原 mounts 切片下标,保留原始绘制顺序信息,不持有托管对象。
|
||||
cells: HashMap<(i32, i32), Vec<usize>>,
|
||||
// 不能安全装入网格的候选,每次查询都参与;由后续精确 contains 排除误命中。
|
||||
fallback: Vec<usize>,
|
||||
}
|
||||
impl Index {
|
||||
/// 根据当前输入构造本模块的派生结果,具体的验证与资源边界见函数内步骤。
|
||||
/// 从帧几何构建 128 单位屏幕网格;大对象、投影面板和超预算项进入保守后备集合。
|
||||
pub fn build(mounts: &[MountGeometry]) -> Self {
|
||||
let mut result = Self {
|
||||
count: mounts.len(),
|
||||
@@ -22,6 +25,7 @@ impl Index {
|
||||
};
|
||||
let mut entries = 0;
|
||||
for (index, mount) in mounts.iter().enumerate() {
|
||||
// 只有可交互项、场景视口或滚动容器需要参与输入目标宽阶段,其余外观节点跳过。
|
||||
if !mount.interactive
|
||||
&& !mount.canvas_viewport
|
||||
&& !mount.scene3d_viewport
|
||||
@@ -29,16 +33,19 @@ impl Index {
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// 3D 面板使用射线投影,不能把局部矩形当成屏幕 AABB 安全近似,统一交给精确阶段。
|
||||
if mount.panel.is_some() {
|
||||
result.fallback.push(index);
|
||||
continue;
|
||||
}
|
||||
// 普通二维节点取两对角点并应用视图变换,再排序上下界,兼容坐标方向变化。
|
||||
let a = Point::new(mount.x, mount.y);
|
||||
let b = Point::new(mount.x + mount.width, mount.y + mount.height);
|
||||
let a = mount.view.map_or(a, |view| view.point(a));
|
||||
let b = mount.view.map_or(b, |view| view.point(b));
|
||||
let (mut x0, mut y0, mut x1, mut y1) =
|
||||
(a.x.min(b.x), a.y.min(b.y), a.x.max(b.x), a.y.max(b.y));
|
||||
// 先与有效裁剪求交,缩小入网范围;完全不可见的矩形不需要成为候选。
|
||||
if let Some([x, y, w, h]) = mount.clip {
|
||||
x0 = x0.max(x);
|
||||
y0 = y0.max(y);
|
||||
@@ -48,6 +55,7 @@ impl Index {
|
||||
if x1 < x0 || y1 < y0 {
|
||||
continue;
|
||||
}
|
||||
// 极端或非有限坐标不做网格展开,转后备集合;不能因转换溢出而漏掉待精确检查的目标。
|
||||
if ![x0, y0, x1, y1]
|
||||
.iter()
|
||||
.all(|value| value.is_finite() && value.abs() < 1e12)
|
||||
@@ -56,8 +64,10 @@ impl Index {
|
||||
continue;
|
||||
}
|
||||
let (left, top, right, bottom) = (cell(x0), cell(y0), cell(x1), cell(y1));
|
||||
// 用 i64 计算格子跨度和饱和乘积,避免极大矩形在预算检查前发生算术溢出。
|
||||
let cells = (i64::from(right) - i64::from(left) + 1)
|
||||
.saturating_mul(i64::from(bottom) - i64::from(top) + 1);
|
||||
// 单项最多展开 256 格,全索引最多 65536 项格子引用;超预算只降级,不丢弃目标。
|
||||
if cells > 256 || entries + cells > 65_536 {
|
||||
result.fallback.push(index);
|
||||
continue;
|
||||
@@ -73,6 +83,7 @@ impl Index {
|
||||
}
|
||||
/// 返回当前网格格子与后备集合中的候选,去重并恢复逆绘制顺序。
|
||||
pub fn candidates(&self, point: Point, count: usize) -> Vec<usize> {
|
||||
// 索引总数与当前帧不一致时,下标集合不再可靠;直接返回整帧逆序候选。
|
||||
if self.count != count {
|
||||
return (0..count).rev().collect();
|
||||
}
|
||||
@@ -80,6 +91,7 @@ impl Index {
|
||||
if let Some(items) = self.cells.get(&(cell(point.x), cell(point.y))) {
|
||||
output.extend(items);
|
||||
}
|
||||
// 按下标降序还原从前景到背景的绘制顺序;去重确保同一出现不会重复执行 hitTest。
|
||||
output.sort_unstable_by(|a, b| b.cmp(a));
|
||||
output.dedup();
|
||||
output
|
||||
@@ -87,6 +99,7 @@ impl Index {
|
||||
}
|
||||
/// 把屏幕坐标映射到有界整数网格坐标,处理负方向与极端数值。
|
||||
fn cell(value: f32) -> i32 {
|
||||
// 每格 128 个屏幕单位;floor 让 -1 属于负格而不是零格,再限制到整数表示范围。
|
||||
(value / 128.)
|
||||
.floor()
|
||||
.clamp(i32::MIN as f32, i32::MAX as f32) as i32
|
||||
@@ -123,6 +136,7 @@ mod tests {
|
||||
}
|
||||
let index = Index::build(&mounts);
|
||||
assert_eq!(index.candidates(Point::new(5., 5.), mounts.len()), vec![0]);
|
||||
// 把最前景节点扩大到覆盖海量格子,验证其进入后备集合后仍排在普通候选之前且可精确命中。
|
||||
mounts[199].x = -1_000_000.;
|
||||
mounts[199].width = 2_000_000.;
|
||||
let index = Index::build(&mounts);
|
||||
|
||||
Reference in New Issue
Block a user