Skip to content
Snippets Groups Projects

Implement smarter keypoint insertion (closes #5)

Merged André Anjos requested to merge issue-5 into master
2 files
+ 43
21
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -225,7 +225,7 @@ class Annotation(object):
def _highlight_widget(self, k):
"""Highlights a given widget on the screen"""
if not self.active: return
if (not self.active) or (not self.point) or (k is None): return
self._deemphasize_widgets()
self.canvas.itemconfig(self.widget[k], fill=COLOR_ACTIVE)
@@ -238,7 +238,7 @@ class Annotation(object):
annotated point.
"""
if self.active: return #ignore
if self.active or (not self.point): return #ignore
self.active = True
@@ -289,8 +289,19 @@ class Annotation(object):
``(y,x)`` format
"""
return numpy.argmin(scipy.spatial.distance.cdist(self.point,
[unzoom_point(self.zoom, p)]))
if not self.point: return None
return self._k_closest_annotations(p, 1)[0]
def _k_closest_annotations(self, p, k):
"""Returns the ``k`` closest elements to the location provided (in
``(y,x)`` format)
"""
distances = scipy.spatial.distance.cdist(self.point,
[unzoom_point(self.zoom, p)])[:,0]
if len(distances) == 1: return [0]
return numpy.argsort(distances)[:k]
def insert_point(self, p):
@@ -302,7 +313,23 @@ class Annotation(object):
# can't insert if no points are there...
if not self.point: return
closest_point = self._closest_annotation(p)
# finds first and second closest points
closest_points = self._k_closest_annotations(p, 2)
closest_point = None
second_closest = None
if len(closest_points) >= 1:
closest_point = closest_points[0]
if len(closest_points) >= 2:
second_closest = closest_points[1]
if closest_point is None: # no points yet, just append
return self.append_point(p)
if second_closest is not None: # there are two points, choose
if closest_point < second_closest:
# the pointer is in highlighting the annotation before
closest_point = second_closest
self.point.insert(closest_point, p)
self.widget.insert(closest_point, self._make_cross(p))
@@ -417,7 +444,7 @@ class Annotation(object):
def _remove_point(self, k):
"""Removes the active annotation"""
if not self.point:
if (not self.point) or (k is None):
return
self.point.pop(k)
Loading