/// <summary> /// 递归构建Agent 树 /// </summary> /// <param name="begin">agent 开始索引,包含begin</param> /// <param name="end">agent 结束索引,不包含end</param> /// <param name="node"></param> public void BuildTreeRecursive() { if (this.parent == null) { return; } if (this.listIndex == null || this.listIndex.Count < 2) { return; } int total = CalcTotalCount(); if (this.parent.CheckNeedCull(total) == false) { return; } // 进行分割。 CullUnitHelp.Clear(); foreach (int index in this.listIndex) { TreeData data = this.parent.GetData(index); CullUnitHelp.Process(index, data, this.isXCulling); } // 获取分割轴 bool ret = CullUnitHelp.GetBestCulling(ref CullingValue); if (ret == true) { List <int> listLeft = null; List <int> listRight = null; CullProcess(ref listLeft, ref listRight); if (listLeft != null && listRight != null) { if (this.isXCulling == true) { this.left = new KdTreeNode(this.parent, this.min, new Double2(this.CullingValue, this.max.y), !this.isXCulling, listLeft); this.right = new KdTreeNode(this.parent, new Double2(this.CullingValue, this.min.y), this.max, !this.isXCulling, listRight); } else { this.left = new KdTreeNode(this.parent, this.min, new Double2(this.max.x, this.CullingValue), !this.isXCulling, listLeft); this.right = new KdTreeNode(this.parent, new Double2(this.min.x, this.CullingValue), this.max, !this.isXCulling, listRight); } this.left.BuildTreeRecursive(); this.right.BuildTreeRecursive(); } } }
/// <summary> /// 构建KD树 /// </summary> public void BuildTree(Double2 min, Double2 max, List <TreeData> listData) { m_ListData = listData; if (m_ListData == null || m_ListData.Count == 0) { return; } Double2 diff = max - min; bool isXCulling = diff.x >= diff.y; List <int> listIndex = new List <int>(); for (int i = 0; i < m_ListData.Count; i++) { listIndex.Add(i); } m_Tree = new KdTreeNode(this, min, max, isXCulling, listIndex); m_Tree.BuildTreeRecursive(); }