perf: AncestorsAndSelf 后序传播(命中路径祖先逐层上传,消除 K×D 次枚举)

TraverseAndCollect 改为返回 bool(子树是否有盒内对象):
- 命中时只 visibleSet.Add(item) 自己
- 组节点 children 循环后:任一子命中 → 自己加入 visibleSet(post-order 上传)
- 共享祖先只被加入一次,AncestorsAndSelf 枚举完全消除

验证:
- 盒内对象/隐藏节点与优化前完全一致(228/4928)✓
- Architecture.nwd:~273ms → ~265ms(命中数小时收益小;大模型命中数大时更明显)
- 集成测试 25/25
This commit is contained in:
tian 2026-08-06 10:37:48 +08:00
parent 539e4a9f2c
commit 636defc959

View File

@ -305,12 +305,13 @@ namespace NavisworksTransport.Core
}
/// <summary>
/// 递归遍历收集盒内对象和可见节点
/// 递归遍历收集盒内对象和可见节点。
/// 返回:本子树是否存在盒内对象(后序传播用——命中路径的祖先由父级逐层加入 visibleSet
/// </summary>
private void TraverseAndCollect(ModelItem item, BoundingBox3D sectionBox,
private bool TraverseAndCollect(ModelItem item, BoundingBox3D sectionBox,
List<ModelItem> objectsInBox, HashSet<ModelItem> visibleSet, bool prune)
{
if (item == null || item.IsHidden) return;
if (item == null || item.IsHidden) return false;
bool hasGeometry = item.HasGeometry;
@ -323,7 +324,7 @@ namespace NavisworksTransport.Core
var groupBox = item.BoundingBox();
if (!BoundingBoxesIntersect(groupBox, sectionBox))
{
return;
return false;
}
}
@ -338,27 +339,34 @@ namespace NavisworksTransport.Core
double sizeZ = bbox.Max.Z - bbox.Min.Z;
bool hasVolume = sizeX > 0.0001 && sizeY > 0.0001 && sizeZ > 0.0001;
// 有体积且与剖面盒相交 → 是目标对象
// 有体积且与剖面盒相交 → 是目标对象(只加入自己,祖先由父级后序上传)
if (hasVolume && BoundingBoxesIntersect(bbox, sectionBox))
{
objectsInBox.Add(item);
// 将该对象及其所有祖先标记为可见
foreach (var ancestor in item.AncestorsAndSelf)
{
visibleSet.Add(ancestor);
}
visibleSet.Add(item);
return true;
}
// 找到几何体,停止向下遍历(原逻辑)
return;
return false;
}
// 没有几何体,继续遍历子节点
// 没有几何体,继续遍历子节点;后序传播:任一子命中 → 自己是祖先,加入 visibleSet
bool anyChildHit = false;
foreach (var child in item.Children)
{
TraverseAndCollect(child, sectionBox, objectsInBox, visibleSet, prune);
if (TraverseAndCollect(child, sectionBox, objectsInBox, visibleSet, prune))
{
anyChildHit = true;
}
}
if (anyChildHit)
{
visibleSet.Add(item);
}
return anyChildHit;
}
/// <summary>