/// <summary> /// Return the closest shape to "shape". /// </summary> /// <param name="shape">the shape.</param> /// <returns></returns> public KDShape GetClosestNeighbor(KDShape shape) { KDShape closeShape = null; float closeDistance = float.NaN; if (!IsLeaf()) { float currentPos = shape.GetPosition(dimensionalDepth); bool smaller = currentPos <= median; closeShape = (smaller ? smallerBranch : biggerBranch).GetClosestNeighbor(shape, ref closeDistance); if (closeShape != null) { if (shape.DistanceToWall(median, dimensionalDepth) >= closeDistance) { return(closeShape); } } KDShape otherClose = (smaller ? biggerBranch : smallerBranch).GetClosestNeighbor(shape, ref closeDistance); if (otherClose != null) { closeShape = otherClose; } } else { for (int i = 0; i < shapes.Count; i++) { if (shapes[i] != shape) { if (float.IsNaN(closeDistance)) { closeDistance = shape.GetDistance(shapes[i]); closeShape = shapes[i]; } else { float dis = shape.GetDistance(shapes[i]); if (dis < closeDistance) { closeDistance = dis; closeShape = shapes[i]; } } } } } return(closeShape); }
private void GetClosestNeighborsInRange(List <KDShape> closeShapes, KDShape shape, float sqrdistance) { if (!IsLeaf()) { float currentPos = shape.GetPosition(dimensionalDepth); bool smaller = currentPos <= median; (smaller ? smallerBranch : biggerBranch).GetClosestNeighborsInRange(closeShapes, shape, sqrdistance); if (shape.DistanceToWall(median, dimensionalDepth) >= sqrdistance) { return; } (smaller ? biggerBranch : smallerBranch).GetClosestNeighborsInRange(closeShapes, shape, sqrdistance); } else { for (int i = 0; i < shapes.Count; i++) { if (shapes[i] != shape) { float dis = shape.GetDistance(shapes[i]); if (dis < sqrdistance) { closeShapes.Add(shapes[i]); } } } } }