KdTreeNode getNearestNeighbour(Vector3 find, KdTreeNode searchNode, KdTreeNode best) { if (searchNode == null) return best; if (best == null) best = searchNode; bool isNearLeft = isLeftOfAxis(searchNode.axis, searchNode.value.position, find); var nearChild = isNearLeft ? searchNode.left : searchNode.right; var farChild = isNearLeft ? searchNode.right : searchNode.left; //consider currenet node if (distance(find, searchNode.value) < distance(find, best.value)) best = searchNode; //search the near branch best = getNearestNeighbour(find, nearChild, best); //search away branch maybe if(distance_axis(find,searchNode)<distance(find,best.value)) best = getNearestNeighbour(find, farChild, best); return best; }
KdTreeNode buildTree(List<MapPoint> points, int depth) { if (points.Count > 0) { int axis = depth % 3; List<MapPoint> leftList = new List<MapPoint>(); List<MapPoint> rightList = new List<MapPoint>(); KdTreeNode node = new KdTreeNode(); MapPoint selected = points[0]; for (int i = 1; i < points.Count; i++) { if (isLeftOfAxis(axis, selected.position, points[i].position)) leftList.Add(points[i]); else rightList.Add(points[i]); } node.value = selected; node.left = buildTree(leftList, depth + 1); node.right = buildTree(rightList, depth + 1); node.axis = axis; return node; } else return null; }
KdTreeNode buildTree(List <MapPoint> points, int depth) { if (points.Count > 0) { int axis = depth % 3; List <MapPoint> leftList = new List <MapPoint>(); List <MapPoint> rightList = new List <MapPoint>(); KdTreeNode node = new KdTreeNode(); MapPoint selected = points[0]; for (int i = 1; i < points.Count; i++) { if (isLeftOfAxis(axis, selected.position, points[i].position)) { leftList.Add(points[i]); } else { rightList.Add(points[i]); } } node.value = selected; node.left = buildTree(leftList, depth + 1); node.right = buildTree(rightList, depth + 1); node.axis = axis; return(node); } else { return(null); } }
KdTreeNode getNearestNeighbour(Vector3 find, KdTreeNode searchNode, KdTreeNode best) { if (searchNode == null) { return(best); } if (best == null) { best = searchNode; } bool isNearLeft = isLeftOfAxis(searchNode.axis, searchNode.value.position, find); var nearChild = isNearLeft ? searchNode.left : searchNode.right; var farChild = isNearLeft ? searchNode.right : searchNode.left; //consider currenet node if (distance(find, searchNode.value) < distance(find, best.value)) { best = searchNode; } //search the near branch best = getNearestNeighbour(find, nearChild, best); //search away branch maybe if (distance_axis(find, searchNode) < distance(find, best.value)) { best = getNearestNeighbour(find, farChild, best); } return(best); }
float distance_axis(Vector3 queryPoint, KdTreeNode axisSpliter) { switch (axisSpliter.axis) { case 0: return Math.Abs(axisSpliter.value.position.X - queryPoint.X); //break; case 1: return Math.Abs(axisSpliter.value.position.Y - queryPoint.Y); //break; case 2: return Math.Abs(axisSpliter.value.position.Z - queryPoint.Z); default: throw new Exception(); } }
float distance_axis(Vector3 queryPoint, KdTreeNode axisSpliter) { switch (axisSpliter.axis) { case 0: return(Math.Abs(axisSpliter.value.position.X - queryPoint.X)); //break; case 1: return(Math.Abs(axisSpliter.value.position.Y - queryPoint.Y)); //break; case 2: return(Math.Abs(axisSpliter.value.position.Z - queryPoint.Z)); default: throw new Exception(); } }
public KdTree3(List<MapPoint> input) { Random r = new Random(242); root = buildTree(input.OrderBy((m) => r.Next()).ToList(), 0);//build tree after randomizing input order }
public KdTree3(List <MapPoint> input) { Random r = new Random(242); root = buildTree(input.OrderBy((m) => r.Next()).ToList(), 0);//build tree after randomizing input order }