Index: trunk/psLib/src/types/psTree.c
===================================================================
--- trunk/psLib/src/types/psTree.c	(revision 18333)
+++ trunk/psLib/src/types/psTree.c	(revision 18334)
@@ -461,7 +461,10 @@
 }
 
-
-// Given an arbitrary point, return the index of the nearest neighbour
-long psTreeNearest(const psTree *tree, const psVector *coords)
+// Return the index of the nearest neighbour to given coordinates, within some radius
+// This is the engine for psTreeNearest() and psTreeNearestWithin()
+static inline long treeNearestWithin(const psTree *tree, // Tree
+                                     const psVector *coords, // Coordinates of interest
+                                     double bestDistance // Distance (radius-squared) to best point
+    );
 {
 #if 1 // Might be in a tight loop
@@ -475,5 +478,4 @@
     psTreeNode *leaf = psTreeLeaf(tree, coords); // Leaf containing the point of interest
     long bestIndex = -1;                // Index of best point
-    double bestDistance = INFINITY;     // Distance to best point
     treeLeafSearchNearest(&bestIndex, &bestDistance, tree, leaf, coords);
 
@@ -515,4 +517,17 @@
 }
 
+// Given an arbitrary point, return the index of the nearest neighbour
+long psTreeNearest(const psTree *tree, const psVector *coords)
+{
+    return treeNearestWithin(tree, coords, INFINITY);
+}
+
+
+long psTreeNearestWithin(const psTree *tree, const psVector *coords, double radius)
+{
+    return treeNearestWithin(tree, coords, PS_SQR(radius));
+}
+
+
 // Search a leaf node for points within radius squared
 static inline long treeLeafSearchWithin(double radius2, // Radius squared to search
Index: trunk/psLib/src/types/psTree.h
===================================================================
--- trunk/psLib/src/types/psTree.h	(revision 18333)
+++ trunk/psLib/src/types/psTree.h	(revision 18334)
@@ -89,4 +89,10 @@
                    );
 
+/// Return the index of the nearest neighbour to given coordinates, but within some radius
+long psTreeNearestWithin(const psTree *tree,  ///< Tree
+                         const psVector *coords ///< Coordinates of interest
+                         double radius  ///< Radius of interest
+                         );
+
 /// Return the number of points within some radius of given coordinates
 long psTreeWithin(const psTree *tree,   ///< Tree
