better peak extraction
[imago.git] / manual.py
index 83e024e..467882e 100644 (file)
--- a/manual.py
+++ b/manual.py
@@ -1,8 +1,9 @@
 """Manual grid selection module"""
 
 import pygame
-import Image, ImageDraw
-from math import atan, sin, cos, pi
+import ImageDraw
+from math import sqrt, acos, copysign
+from geometry import l2ad, line, intersection
 
 class UserQuitError(Exception):
     pass
@@ -14,8 +15,8 @@ class Screen:
         pygame.display.set_caption("Imago manual mode")
         self._screen = pygame.display.get_surface()
 
-    def display_picture(self, im):
-        pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
+    def display_picture(self, img):
+        pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
         self._screen.blit(pg_img, (0,0))
         pygame.display.flip()
 
@@ -31,7 +32,7 @@ def find_lines(im_orig):
     hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
                  fill=(255, 64, 64))
     corners = []
-    color=(64, 64, 255)
+    color = (64, 64, 255)
     line_width = 1
     lines_r = []
 
@@ -53,38 +54,36 @@ def find_lines(im_orig):
                     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)
+                        l_vert, l_hor = lines(corners)
                         for l in l_vert:
                             draw.line(l, fill=color, width=line_width)
-                        l_hor = lines(corners[1:4] + [corners[0]])
                         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]:
                             for j in [3, 9, 15]:
                                 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
                                                    line(l_hor[j][0], l_hor[j][1])))
-                        lines_r = [[l2ad(l[0], l[1], im.size) for l in l_vert], 
-                                   [l2ad(l[0], l[1], im.size) for l in l_hor]]
+                        lines_r = [[l2ad(l, im.size) for l in l_vert], 
+                                   [l2ad(l, im.size) for l in l_hor]]
 
         screen.display_picture(im)
         clock.tick(15)
 
 def lines(corners):
-    return _lines(corners, 0)
+    #TODO Error on triangle 
+    cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
+             corners[1:]]
+    cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
+              sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
+    cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
+    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:
@@ -95,8 +94,14 @@ def _lines(corners, n):
         x = half_line(corners)
         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
         d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
-        l = (intersection(line(corners[0], corners[1]), line(c,d)),
-             intersection(line(corners[2], corners[3]), line(c,d)))
+        if d:
+            l = (intersection(line(corners[0], corners[1]), line(c, d)),
+                 intersection(line(corners[2], corners[3]), line(c, d)))
+        else:
+            lx = line(c, (c[0] + corners[0][0] - corners[3][0], 
+                      c[1] + corners[0][1] - corners[3][1]))
+            l = (intersection(line(corners[0], corners[1]), lx),
+                 intersection(line(corners[2], corners[3]), lx))
         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)
@@ -122,31 +127,3 @@ def half_line(corners):
 def center(corners):
     return intersection(line(corners[0], corners[2]), 
                         line(corners[1], corners[3]))
-def line(x, y):
-    a = x[1] - y[1]
-    b = y[0] - x[0]
-    c = a * y[0] + b * y[1]
-    return (a, b, c)
-
-def intersection(p, q):
-    det = p[0] * q[1] - p[1] * q[0]
-    if det == 0:
-        return None
-    return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)), 
-            int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))
-
-def l2ad(a, b, size):
-    if (a[0] - b[0]) == 0:
-        angle = pi / 2
-    else:
-        q = float(a[1] - b[1]) / (a[0] - b[0])
-        angle = atan(q)
-
-    if angle < 0:
-        angle += pi
-    if angle > pi:
-        angle -= pi
-
-    distance = (((a[0] - (size[0] / 2)) * sin(angle)) + 
-                ((a[1] - (size[1] / 2)) * - cos(angle)))
-    return (angle, distance)