1 """Manual grid selection module"""
5 from math import sqrt, acos, copysign
7 from geometry import l2ad, line, intersection
9 class UserQuitError(Exception):
13 def __init__(self, res):
15 pygame.display.set_mode(res)
16 pygame.display.set_caption("Imago manual mode")
17 self._screen = pygame.display.get_surface()
19 def display_picture(self, img):
20 pg_img = pygame.image.frombuffer(img.tostring(), img.size, img.mode)
21 self._screen.blit(pg_img, (0, 0))
24 def find_lines(im_orig):
28 screen = Screen(im.size)
31 clock = pygame.time.Clock()
32 draw = ImageDraw.Draw(im)
33 hoshi = lambda c: draw.ellipse((c[0] - 1, c[1] - 1, c[0] + 1, c[1] + 1),
41 for event in pygame.event.get():
42 if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
48 if event.type == pygame.MOUSEBUTTONDOWN:
52 draw = ImageDraw.Draw(im)
55 corners.append(pygame.mouse.get_pos())
56 draw.point(corners[:-1], fill=color)
58 l_vert, l_hor = lines(corners)
60 draw.line(l, fill=color, width=line_width)
62 draw.line(l, fill=color, width=line_width)
63 #TODO sort by distance
68 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
69 line(l_hor[j][0], l_hor[j][1])))
70 lines_r = [[l2ad(l, im.size) for l in l_vert],
71 [l2ad(l, im.size) for l in l_hor]]
73 screen.display_picture(im)
77 #TODO Error on triangle
78 cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
80 cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
81 sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
82 cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
83 corners = [corners[0]] + [c for _, c in cor_d]
84 return (_lines(corners, 0) + [(corners[0], corners[3]),
85 (corners[1], corners[2])],
86 _lines(corners[1:4] + [corners[0]], 0) +
87 [(corners[0], corners[1]), (corners[2], corners[3])])
89 def _lines(corners, n):
91 x = half_line(corners)
92 return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] +
93 _lines([x[0], corners[1], corners[2], x[1]], n + 1))
95 x = half_line(corners)
96 c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
97 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
99 l = (intersection(line(corners[0], corners[1]), line(c, d)),
100 intersection(line(corners[2], corners[3]), line(c, d)))
102 lx = line(c, (c[0] + corners[0][0] - corners[3][0],
103 c[1] + corners[0][1] - corners[3][1]))
104 l = (intersection(line(corners[0], corners[1]), lx),
105 intersection(line(corners[2], corners[3]), lx))
106 l2 = half_line([corners[0], l[0], l[1], corners[3]])
108 return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
109 + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
110 + _lines([l[0], corners[1], corners[2], l[1]], 2))
115 def half_line(corners):
117 d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
121 l = line(c, (c[0] + corners[0][0] - corners[3][0],
122 c[1] + corners[0][1] - corners[3][1]))
123 p1 = intersection(l, line(corners[0], corners[1]))
124 p2 = intersection(l, line(corners[2], corners[3]))
129 return intersection(line(corners[0], corners[2]),
130 line(corners[1], corners[3]))