reorganise folders
[imago.git] / src / manual.py
1 """Manual grid selection module"""
2
3 import pygame
4 import ImageDraw
5 from math import sqrt, acos, copysign
6
7 from geometry import l2ad, line, intersection
8
9 class UserQuitError(Exception):
10     pass
11
12 class Screen:
13     # TODO isn't this a duplicate of something?
14     def __init__(self, res):
15         pygame.init()
16         pygame.display.set_mode(res)
17         pygame.display.set_caption("Imago manual mode")
18         self._screen = pygame.display.get_surface()
19
20     def display_picture(self, img):
21         pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
22         self._screen.blit(pg_img, (0, 0))
23         pygame.display.flip()
24
25 def find_lines(im_orig):
26     # TODO rename, refactor, comment
27
28     im = im_orig.copy()
29
30     screen = Screen(im.size)
31
32     clock = pygame.time.Clock()
33     draw = ImageDraw.Draw(im)
34     hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
35                  fill=(255, 64, 64))
36     corners = []
37     color = (64, 64, 255)
38     line_width = 1
39     lines_r = []
40
41     while True:
42         for event in pygame.event.get():
43             if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
44                 pygame.quit()
45                 if len(corners) == 4:
46                     return lines_r
47                 else:
48                     raise UserQuitError 
49             if event.type == pygame.MOUSEBUTTONDOWN:
50                 if len(corners) >= 4: 
51                     corners = []
52                     im = im_orig.copy()
53                     draw = ImageDraw.Draw(im)
54
55                 if len(corners) < 4:
56                     corners.append(pygame.mouse.get_pos())
57                     draw.point(corners[:-1], fill=color)
58                     if len(corners) == 4:
59                         l_vert, l_hor = lines(corners)
60                         for l in l_vert:
61                             draw.line(l, fill=color, width=line_width)
62                         for l in l_hor:
63                             draw.line(l, fill=color, width=line_width)
64                         # TODO sort by distance
65                         l_vert.sort()
66                         l_hor.sort()
67                         for i in [3, 9, 15]:
68                             for j in [3, 9, 15]:
69                                 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
70                                                    line(l_hor[j][0], l_hor[j][1])))
71                         lines_r = [[l2ad(l, im.size) for l in l_vert], 
72                                    [l2ad(l, im.size) for l in l_hor]]
73
74         screen.display_picture(im)
75         clock.tick(15)
76
77 def lines(corners):
78     # TODO Error on triangle 
79     cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
80              corners[1:]]
81     cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
82               sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
83     cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
84     corners = [corners[0]] + [c for _, c in cor_d]
85     return (_lines(corners, 0) + [(corners[0], corners[3]),
86                                   (corners[1], corners[2])],
87             _lines(corners[1:4] + [corners[0]], 0) + 
88             [(corners[0], corners[1]), (corners[2], corners[3])])
89
90 def _lines(corners, n):
91     # TODO what is this?
92     if n == 0:
93         x = half_line(corners)
94         return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
95                 _lines([x[0], corners[1], corners[2], x[1]], n + 1))
96     else:
97         x = half_line(corners)
98         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
99         d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
100         if d:
101             l = (intersection(line(corners[0], corners[1]), line(c, d)),
102                  intersection(line(corners[2], corners[3]), line(c, d)))
103         else:
104             lx = line(c, (c[0] + corners[0][0] - corners[3][0], 
105                       c[1] + corners[0][1] - corners[3][1]))
106             l = (intersection(line(corners[0], corners[1]), lx),
107                  intersection(line(corners[2], corners[3]), lx))
108         l2 = half_line([corners[0], l[0], l[1], corners[3]])
109         if n == 1:
110             return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
111                     + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
112                     + _lines([l[0], corners[1], corners[2], l[1]], 2))
113         if n == 2:
114             return [l, l2]
115
116
117 def half_line(corners):
118     # TODO what is this?
119     c = center(corners)
120     d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
121     if d:
122         l = line(c, d)
123     else:
124         l = line(c, (c[0] + corners[0][0] - corners[3][0], 
125                      c[1] + corners[0][1] - corners[3][1]))
126     p1 = intersection(l, line(corners[0], corners[1]))
127     p2 = intersection(l, line(corners[2], corners[3]))
128     return (p1, p2)
129
130 def center(corners):
131     """Given a list of four corner points, return the center of the square."""
132     return intersection(line(corners[0], corners[2]), 
133                         line(corners[1], corners[3]))