1 """Manual grid selection module"""
5 from math import sqrt, acos, copysign
6 from geometry import l2ad, line, intersection
8 class UserQuitError(Exception):
12 def __init__(self, res):
14 pygame.display.set_mode(res)
15 pygame.display.set_caption("Imago manual mode")
16 self._screen = pygame.display.get_surface()
18 def display_picture(self, img):
19 pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
20 self._screen.blit(pg_img, (0,0))
23 def find_lines(im_orig):
27 screen = Screen(im.size)
30 clock = pygame.time.Clock()
31 draw = ImageDraw.Draw(im)
32 hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
40 for event in pygame.event.get():
41 if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
47 if event.type == pygame.MOUSEBUTTONDOWN:
51 draw = ImageDraw.Draw(im)
54 corners.append(pygame.mouse.get_pos())
55 draw.point(corners[:-1], fill=color)
57 l_vert, l_hor = lines(corners)
59 draw.line(l, fill=color, width=line_width)
61 draw.line(l, fill=color, width=line_width)
62 #TODO sort by distance
67 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
68 line(l_hor[j][0], l_hor[j][1])))
69 lines_r = [[l2ad(l, im.size) for l in l_vert],
70 [l2ad(l, im.size) for l in l_hor]]
72 screen.display_picture(im)
76 #TODO Error on triangle
77 cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
79 cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
80 sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
81 cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
82 corners = [corners[0]] + [c for _, c in cor_d]
83 return (_lines(corners, 0) + [(corners[0], corners[3]),
84 (corners[1], corners[2])],
85 _lines(corners[1:4] + [corners[0]], 0) +
86 [(corners[0], corners[1]), (corners[2], corners[3])])
88 def _lines(corners, n):
90 x = half_line(corners)
91 return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
92 _lines([x[0], corners[1], corners[2], x[1]], n + 1))
94 x = half_line(corners)
95 c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
96 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
98 l = (intersection(line(corners[0], corners[1]), line(c, d)),
99 intersection(line(corners[2], corners[3]), line(c, d)))
101 lx = line(c, (c[0] + corners[0][0] - corners[3][0],
102 c[1] + corners[0][1] - corners[3][1]))
103 l = (intersection(line(corners[0], corners[1]), lx),
104 intersection(line(corners[2], corners[3]), lx))
105 l2 = half_line([corners[0], l[0], l[1], corners[3]])
107 return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
108 + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
109 + _lines([l[0], corners[1], corners[2], l[1]], 2))
114 def half_line(corners):
116 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
120 l = line(c, (c[0] + corners[0][0] - corners[3][0],
121 c[1] + corners[0][1] - corners[3][1]))
122 p1 = intersection(l, line(corners[0], corners[1]))
123 p2 = intersection(l, line(corners[2], corners[3]))
128 return intersection(line(corners[0], corners[2]),
129 line(corners[1], corners[3]))