public void Draw(TreeSettings aSettings, int aDepth, Vector3 aPt) { int axis = aSettings.GetAxis(aDepth); if (axis == 0) { Gizmos.color = Color.red; Gizmos.DrawLine(point.point + Vector3.left, point.point + Vector3.right); } else if (axis == 1) { Gizmos.color = Color.green; Gizmos.DrawLine(point.point + Vector3.up, point.point + Vector3.down); } else if (axis == 2) { Gizmos.color = Color.blue; Gizmos.DrawLine(point.point + Vector3.forward, point.point + Vector3.back); } if (left != null) { left.Draw(aSettings, aDepth + 1, point.point); Gizmos.color = point.data; Gizmos.DrawLine(point.point, left.point.point); } if (right != null) { right.Draw(aSettings, aDepth + 1, point.point); Gizmos.color = point.data; Gizmos.DrawLine(point.point, right.point.point); } }
public TreeNode(TreeSettings aSettings, List <TreePoint> aPoints, int aDepth) { int axis = aSettings.GetAxis(aDepth); if (axis == 0) { aPoints.Sort(sortX); } else if (axis == 1) { aPoints.Sort(sortY); } else if (axis == 2) { aPoints.Sort(sortZ); } int median = aPoints.Count / 2; point = aPoints[median]; List <TreePoint> leftList = aPoints.GetRange(0, median); List <TreePoint> rightList = aPoints.GetRange(median + 1, aPoints.Count - (median + 1)); if (leftList.Count > 0) { left = new TreeNode(aSettings, leftList, aDepth + 1); } if (rightList.Count > 0) { right = new TreeNode(aSettings, rightList, aDepth + 1); } }
public void GetNearest(TreeSettings aSettings, int aDepth, Vector3 aPt, ref TreePoint aClosest, ref float aClosestDist) { if (IsLeaf) { float dist = (point.point - aPt).sqrMagnitude; if (aClosest == null || dist < aClosestDist) { aClosest = point; aClosestDist = dist; } return; } int axis = aSettings.GetAxis(aDepth); bool goLeft = false; if (axis == 0) { goLeft = aPt.x <= point.point.x ? true : false; } else if (axis == 1) { goLeft = aPt.y <= point.point.y ? true : false; } else if (axis == 2) { goLeft = aPt.z <= point.point.z ? true : false; } TreeNode first = goLeft ? left : right; TreeNode other = goLeft ? right: left; if (first == null) { first = other; other = null; } first.GetNearest(aSettings, aDepth + 1, aPt, ref aClosest, ref aClosestDist); float thisDist = (point.point - aPt).sqrMagnitude; if (thisDist < aClosestDist) { aClosest = point; aClosestDist = thisDist; } if (other != null) { float axisDist = aSettings.AxisDist(axis, point.point, aPt); if (axisDist * axisDist <= aClosestDist) { other.GetNearest(aSettings, aDepth + 1, aPt, ref aClosest, ref aClosestDist); } } }