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