private QuadGroupNode <T> InitTree(int row, int col, T[] array) { if (row <= 0 || col <= 0 || array == null || array.Length <= 0) { return(null); } int rCnt = Mathf.CeilToInt(((float)row) / ((float)_cPer)); int cCnt = Mathf.CeilToInt(((float)col) / ((float)_cPer)); Func <int, T> GetItem = (int idx) => { if (idx < 0 || idx >= array.Length) { return(null); } return(array[idx]); }; List <QuadGroupNode <T> > nodeList = new List <QuadGroupNode <T> >(); for (int r = 0; r < rCnt; ++r) { for (int c = 0; c < cCnt; ++c) { /* * 3 4 * 1 2 */ int rr = r * _cPer; int cc = c * _cPer; int idx1 = (rr) * col + cc; int idx2 = -1; bool isVaildCC = cc + 1 < col; if (isVaildCC) { idx2 = (rr) * col + cc + 1; } int idx3 = -1; bool isVaildrr = rr + 1 < row; if (isVaildrr) { idx3 = (rr + 1) * col + cc; } int idx4 = -1; if (isVaildCC && isVaildrr) { idx4 = (rr + 1) * col + cc + 1; } QuadGroupNode <T> root = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; QuadGroupNode <T> n1 = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; QuadGroupNode <T> n2 = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; QuadGroupNode <T> n3 = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; QuadGroupNode <T> n4 = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; T item1 = GetItem(idx1); T item2 = GetItem(idx2); T item3 = GetItem(idx3); T item4 = GetItem(idx4); n1.Init(item1); n2.Init(item2); n3.Init(item3); n4.Init(item4); QuadGroupNodeArray <T> nodeArray = AbstractPool <QuadGroupNodeArray <T> > .GetNode() as QuadGroupNodeArray <T>; nodeArray[0] = n1; nodeArray[1] = n2; nodeArray[2] = n3; nodeArray[3] = n4; root.Init(nodeArray); nodeList.Add(root); } } while (true) { InitTree(ref rCnt, ref cCnt, ref nodeList); if (nodeList.Count <= 1) { break; } } if (nodeList.Count <= 0) { return(null); } return(nodeList[0]); }
private void InitTree(ref int row, ref int col, ref List <QuadGroupNode <T> > parentNodeList) { if (parentNodeList == null || parentNodeList.Count <= 0) { return; } int rCnt = Mathf.CeilToInt(((float)row) / ((float)_cPer)); int cCnt = Mathf.CeilToInt(((float)col) / ((float)_cPer)); var pNodeList = parentNodeList; Func <int, QuadGroupNode <T> > GetItem = (int idx) => { if (idx < 0 || idx >= pNodeList.Count) { return(null); } return(pNodeList[idx]); }; List <QuadGroupNode <T> > nodeList = new List <QuadGroupNode <T> >(); for (int r = 0; r < rCnt; ++r) { for (int c = 0; c < cCnt; ++c) { /* * 3 4 * 1 2 */ int rr = r * _cPer; int cc = c * _cPer; int idx1 = (rr) * col + cc; int idx2 = -1; bool isVaildCC = cc + 1 < col; if (isVaildCC) { idx2 = (rr) * col + cc + 1; } int idx3 = -1; bool isVaildrr = rr + 1 < row; if (isVaildrr) { idx3 = (rr + 1) * col + cc; } int idx4 = -1; if (isVaildCC && isVaildrr) { idx4 = (rr + 1) * col + cc + 1; } QuadGroupNode <T> root = AbstractPool <QuadGroupNode <T> > .GetNode() as QuadGroupNode <T>; var n1 = GetItem(idx1); var n2 = GetItem(idx2); var n3 = GetItem(idx3); var n4 = GetItem(idx4); QuadGroupNodeArray <T> nodeArray = AbstractPool <QuadGroupNodeArray <T> > .GetNode() as QuadGroupNodeArray <T>; nodeArray[0] = n1; nodeArray[1] = n2; nodeArray[2] = n3; nodeArray[3] = n4; root.Init(nodeArray); nodeList.Add(root); } } row = rCnt; col = cCnt; parentNodeList = nodeList; }