1 """Manual grid selection module"""
5 from math import sqrt, acos, copysign
7 from geometry import l2ad, line, intersection
9 class UserQuitError(Exception):
13 # TODO isn't this a duplicate of something?
14 def __init__(self, res):
16 pygame.display.set_mode(res)
17 pygame.display.set_caption("Imago manual mode")
18 self._screen = pygame.display.get_surface()
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))
25 def find_lines(im_orig):
26 # TODO rename, refactor, comment
30 screen = Screen(im.size)
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),
42 for event in pygame.event.get():
43 if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
49 if event.type == pygame.MOUSEBUTTONDOWN:
53 draw = ImageDraw.Draw(im)
56 corners.append(pygame.mouse.get_pos())
57 draw.point(corners[:-1], fill=color)
59 l_vert, l_hor = lines(corners)
61 draw.line(l, fill=color, width=line_width)
63 draw.line(l, fill=color, width=line_width)
64 # TODO sort by distance
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]]
74 screen.display_picture(im)
78 # TODO Error on triangle
79 cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
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])])
90 def _lines(corners, n):
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))
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]))
101 l = (intersection(line(corners[0], corners[1]), line(c, d)),
102 intersection(line(corners[2], corners[3]), line(c, d)))
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]])
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))
117 def half_line(corners):
120 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
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]))
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]))