ソースを参照

fix nearest vertex selection

The original method just selected the first encountered vertex in some
epsilon-neighborhood of the mouse cursor. Now the closest one is
picked. This is done in O(number_of_vertices) and could be someday
improved by more efficient nearest neighbor using k-d tree.
serycjon 7 年 前
コミット
b6e0df882c
1 ファイル変更7 行追加3 行削除
  1. 7 3
      labelme/shape.py

+ 7 - 3
labelme/shape.py

@@ -147,10 +147,14 @@ class Shape(object):
             assert False, "unsupported vertex shape"
 
     def nearestVertex(self, point, epsilon):
+        min_distance = float('inf')
+        min_i = None
         for i, p in enumerate(self.points):
-            if distance(p - point) <= epsilon:
-                return i
-        return None
+            dist = distance(p - point)
+            if dist <= epsilon and dist < min_distance:
+                min_distance = dist
+                min_i = i
+        return min_i
 
     def containsPoint(self, point):
         return self.makePath().contains(point)