+import random
+from math import sqrt
+
+from intrsc import intersections_from_angl_dist
+import linef as linef
+import ransac as ransac
+import manual as manual
+from geometry import l2ad
+
+# TODO comments, refactoring, move methods to appropriate modules
+
+def plot_line(line, c):
+ points = linef.line_from_angl_dist(line, (520, 390))
+ pyplot.plot(*zip(*points), color=c)
+
+def plot_line_g((a, b, c), max_x):
+ find_y = lambda x: - (c + a * x) / b
+ pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
+
+class Diagonal_model:
+ def __init__(self, data):
+ self.data = [p for p in sum(data, []) if p]
+ self.lines = data
+ self.gen = self.initial_g()
+
+ def initial_g(self):
+ l1, l2 = random.sample(self.lines, 2)
+ for i in xrange(len(l1)):
+ for j in xrange(len(l2)):
+ if i == j:
+ continue
+ if l1[i] and l2[j]:
+ yield (l1[i], l2[j])
+
+ def initial(self):
+ try:
+ return self.gen.next()
+ except StopIteration:
+ self.gen = self.initial_g()
+ return self.gen.next()
+
+ def get(self, sample):
+ if len(sample) == 2:
+ return ransac.points_to_line(*sample)
+ else:
+ return ransac.least_squares(sample)
+
+def intersection((a1, b1, c1), (a2, b2, c2)):
+ delim = float(a1 * b2 - b1 * a2)
+ x = (b1 * c2 - c1 * b2) / delim
+ y = (c1 * a2 - a1 * c2) / delim
+ return x, y
+
+class Point:
+ def __init__(self, (x, y)):
+ self.x = x
+ self.y = y
+
+ def __getitem__(self, key):
+ if key == 0:
+ return self.x
+ elif key == 1:
+ return self.y
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+ def __len__(self):
+ return 2
+
+class Line:
+ def __init__(self, (a, b, c)):
+ self.a, self.b, self.c = (a, b, c)
+ self.points = []
+
+ @classmethod
+ def from_ad(cls, (a, d), size):
+ p = linef.line_from_angl_dist((a, d), size)
+ return cls(ransac.points_to_line(*p))
+
+ def __iter__(self):
+ yield self.a
+ yield self.b
+ yield self.c
+
+ def __len__(self):
+ return 3
+
+ def __getitem__(self, key):
+ if key == 0:
+ return self.a
+ elif key == 1:
+ return self.b
+ elif key == 2:
+ return self.c
+
+def gen_corners(d1, d2):
+ for c1 in d1.points:
+ if c1 in d2.points:
+ continue
+ pass
+ c2 = [p for p in d2.points if p in c1.l1.points][0]
+ c3 = [p for p in d1.points if p in c2.l2.points][0]
+ c4 = [p for p in d2.points if p in c3.l1.points][0]
+ yield [c1, c2, c3, c4]
+
+def dst(p, l):
+ (x, y), (a, b, c) = p, ransac.points_to_line(*l)
+ return abs(a * x + b * y + c) / sqrt(a*a+b*b)
+
+def score(lines, points):
+ score = 0
+ for p in points:
+ s = min(map(lambda l: dst(p, l), lines))
+ s = min(s, 2)
+ score += s
+ print score
+ return score
+
+
+def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
+ logger("finding the grid")
+ new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
+ new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
+ for l1 in new_lines1:
+ for l2 in new_lines2:
+ p = Point(intersection(l1, l2))
+ p.l1 = l1
+ p.l2 = l2
+ l1.points.append(p)
+ l2.points.append(p)
+
+ points = [l.points for l in new_lines1]
+
+ line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
+ points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
+ line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
+ center = intersection(line1, line2)
+ data = sum(points, [])
+ diag1 = Line(line1)
+ diag1.points = ransac.filter_near(data, diag1, 2)
+ diag2 = Line(line2)
+ diag2.points = ransac.filter_near(data, diag2, 2)
+
+ grids = map(manual.lines, list(gen_corners(diag1, diag2)))
+
+ sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
+
+ grid_lines = [[l2ad(l, size) for l in grid[0]],
+ [l2ad(l, size) for l in grid[1]]]
+
+ return grid, grid_lines
+
+def test():
+ import pickle
+ import matplotlib.pyplot as pyplot
+
+ lines = pickle.load(open('lines.pickle'))
+
+ size = (520, 390)
+ new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
+ new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
+ for l1 in new_lines1:
+ for l2 in new_lines2:
+ p = Point(intersection(l1, l2))
+ p.l1 = l1
+ p.l2 = l2
+ l1.points.append(p)
+ l2.points.append(p)
+
+ points = [l.points for l in new_lines1]
+
+ line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
+ points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
+ line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
+ center = intersection(line1, line2)
+ data = sum(points, [])
+ diag1 = Line(line1)
+ diag1.points = ransac.filter_near(data, diag1, 2)
+ diag2 = Line(line2)
+ diag2.points = ransac.filter_near(data, diag2, 2)
+
+ plot_line_g(diag1, 520)
+ plot_line_g(diag2, 520)
+ pyplot.scatter(*zip(*sum(points, [])))
+ pyplot.scatter([center[0]], [center[1]], color='r')
+ pyplot.xlim(0, 520)
+ pyplot.ylim(0, 390)
+ pyplot.show()
+
+ grids = map(manual.lines, list(gen_corners(diag1, diag2)))
+ plot_grid = lambda g: map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(g, []))
+ map(plot_grid, grids)
+ pyplot.show()
+
+ sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
+
+ map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(grid, []))
+ pyplot.scatter(*zip(*sum(points, [])))
+ pyplot.xlim(0, 520)
+ pyplot.ylim(0, 390)
+ pyplot.show()