manual mode -- arbitrary order of corners
[imago.git] / manual.py
index c7f8e2b..55dfdd0 100644 (file)
--- a/manual.py
+++ b/manual.py
@@ -11,7 +11,7 @@ class Screen:
     def __init__(self, res):
         pygame.init()
         pygame.display.set_mode(res)
     def __init__(self, res):
         pygame.init()
         pygame.display.set_mode(res)
-        pygame.display.set_caption("Go image capture")
+        pygame.display.set_caption("Imago manual mode")
         self._screen = pygame.display.get_surface()
 
     def display_picture(self, im):
         self._screen = pygame.display.get_surface()
 
     def display_picture(self, im):
@@ -53,24 +53,12 @@ def find_lines(im_orig):
                     corners.append(pygame.mouse.get_pos())
                     draw.point(corners[:-1], fill=color)
                     if len(corners) == 4:
                     corners.append(pygame.mouse.get_pos())
                     draw.point(corners[:-1], fill=color)
                     if len(corners) == 4:
-                        draw.line((corners[0], corners[1]), fill=color,
-                                  width=line_width)
-                        draw.line((corners[1], corners[2]), fill=color,
-                                  width=line_width)
-                        draw.line((corners[2], corners[3]), fill=color,
-                                  width=line_width)
-                        draw.line((corners[3], corners[0]), fill=color,
-                                  width=line_width)
-                        l_vert = lines(corners, 0)
+                        l_vert, l_hor = lines(corners)
                         for l in l_vert:
                             draw.line(l, fill=color, width=line_width)
                         for l in l_vert:
                             draw.line(l, fill=color, width=line_width)
-                        l_hor = lines(corners[1:4] + [corners[0]], 0)
                         for l in l_hor:
                             draw.line(l, fill=color, width=line_width)
                         for l in l_hor:
                             draw.line(l, fill=color, width=line_width)
-                        l_vert += [(corners[0], corners[3]),
-                                   (corners[1], corners[2])]
-                        l_hor += [(corners[0], corners[1]),
-                                   (corners[2], corners[3])]
+                        #TODO sort by distance
                         l_vert.sort()
                         l_hor.sort()
                         for i in [3, 9, 15]:
                         l_vert.sort()
                         l_hor.sort()
                         for i in [3, 9, 15]:
@@ -83,11 +71,20 @@ def find_lines(im_orig):
         screen.display_picture(im)
         clock.tick(15)
 
         screen.display_picture(im)
         clock.tick(15)
 
-def lines(corners, n):
+def lines(corners):
+    cor_d = sorted([(corners[0][0] * c[1] - c[0] * corners[0][1], c) for c in
+                    corners[1:]])
+    corners = [corners[0]] + [c for _, c in cor_d]
+    return (_lines(corners, 0) + [(corners[0], corners[3]),
+                                  (corners[1], corners[2])],
+            _lines(corners[1:4] + [corners[0]], 0) + 
+            [(corners[0], corners[1]), (corners[2], corners[3])])
+
+def _lines(corners, n):
     if n == 0:
         x = half_line(corners)
     if n == 0:
         x = half_line(corners)
-        return (lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
-                lines([x[0], corners[1], corners[2], x[1]], n + 1))
+        return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
+                _lines([x[0], corners[1], corners[2], x[1]], n + 1))
     else:
         x = half_line(corners)
         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
     else:
         x = half_line(corners)
         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
@@ -96,9 +93,9 @@ def lines(corners, n):
              intersection(line(corners[2], corners[3]), line(c,d)))
         l2 = half_line([corners[0], l[0], l[1], corners[3]])
         if n == 1:
              intersection(line(corners[2], corners[3]), line(c,d)))
         l2 = half_line([corners[0], l[0], l[1], corners[3]])
         if n == 1:
-            return ([l, l2] + lines([l[0], l2[0], l2[1], l[1]], 2)
-                    + lines([corners[0], l2[0], l2[1], corners[3]], 2)
-                    + lines([l[0], corners[1], corners[2], l[1]], 2))
+            return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
+                    + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
+                    + _lines([l[0], corners[1], corners[2], l[1]], 2))
         if n == 2:
             return [l, l2]
 
         if n == 2:
             return [l, l2]