def plot_overlap(n): # Select an overlapping pixel and highlight it p1 = grid1[n] p2 = grid2[n] overlap = zip(*ext.poly_clip(p2.x, p2.y, p1.x.min(), p1.x.max(), p1.y.max(), p1.y.min())) ax = plt.subplot(121) ax.add_patch(patches.Polygon(zip(p1.x,p1.y), facecolor=[0.7, 0.7, 0.9])) ax.add_patch(patches.Polygon(zip(p2.x,p2.y), facecolor=[0.9, 0.8, 0.9])) ax.add_patch(patches.Polygon(overlap, facecolor=[0.3,0.9,0.3])) plotgrid(ax, grid1, 'r-') plotgrid(ax, grid2, 'b-') ax.axis('equal') ax.set_xticks([]) ax.set_yticks([]) ax.axis([-2,12,-2,12]) f = plt.subplot(122).get_frame() # print [f.get_x(),f.get_y(),f.get_width()/2,f.get_height()/2] # plt.axes() coords = poly_coords(Polygon([0,1,1,0],[0,0,1,1])) angles = np.linspace(-np.pi,np.pi,50) offsets = np.linspace(-1,1.5,50) weights = np.zeros((len(angles),len(offsets))) print "Calculating overlaps..." for i,theta in enumerate(angles): for j,offset in enumerate(offsets): x,y = np.dot(tf(theta,offset,offset), coords)[:2] x,y = ext.poly_clip(x,y,0,1,1,0) if len(x) >= 3: weights[i,j] = Polygon(x,y).area() plt.rcParams['figure.figsize'] = (6.67,3.335) plt.imshow(weights) plt.subplots_adjust(wspace=0.4) plt.xlabel("Offset") plt.ylabel("Angle") plt.xlim([0,50]) plt.ylim([0,50]) plt.xticks([0,25,50],('-1','0.25','1.5')) plt.yticks([0,25,50],('$\pi$','0','$-\pi$')) mkdir('output') plt.savefig('output/gridoverlap.eps')
def test_poly_clip(self): x = [0, 1, 2, 1] y = [0, -1, 0, 1] xc, yc = ext.poly_clip(x, y, 0, 1, 1, 0) assert_equal(Polygon(xc, yc).area(), 0.5) x = [-1, 1.5, 1.5, -1] y = [.5, 0.5, 1.5, 1.5] xc,yc = ext.poly_clip(x, y, 0, 1, 1, 0) assert_equal(Polygon(xc, yc).area(), 0.5) fail_poly_clip = raises(AssertionError)(ext.poly_clip) yield(fail_poly_clip, [1], [1, 2], 0, 0, 0, 0) yield(fail_poly_clip, [1, 2], [1, 2], 0, 10, -1, 10) yield(fail_poly_clip, [1, 2], [1, 2], 10, 0, 10, 0)
def test_poly_clip(self): x = [0, 1, 2, 1] y = [0, -1, 0, 1] xc, yc = ext.poly_clip(x, y, 0, 1, 1, 0) assert_equal(Polygon(xc, yc).area(), 0.5) x = [-1, 1.5, 1.5, -1] y = [.5, 0.5, 1.5, 1.5] xc, yc = ext.poly_clip(x, y, 0, 1, 1, 0) assert_equal(Polygon(xc, yc).area(), 0.5) fail_poly_clip = raises(AssertionError)(ext.poly_clip) yield (fail_poly_clip, [1], [1, 2], 0, 0, 0, 0) yield (fail_poly_clip, [1, 2], [1, 2], 0, 10, -1, 10) yield (fail_poly_clip, [1, 2], [1, 2], 10, 0, 10, 0)
def plot_overlap(n): # Select an overlapping pixel and highlight it p1 = grid1[n] p2 = grid2[n] overlap = zip(*ext.poly_clip(p2.x, p2.y, p1.x.min(), p1.x.max(), p1.y.max(), p1.y.min())) ax = plt.subplot(121) ax.add_patch(patches.Polygon(zip(p1.x, p1.y), facecolor=[0.7, 0.7, 0.9])) ax.add_patch(patches.Polygon(zip(p2.x, p2.y), facecolor=[0.9, 0.8, 0.9])) ax.add_patch(patches.Polygon(overlap, facecolor=[0.3, 0.9, 0.3])) plotgrid(ax, grid1, 'r-') plotgrid(ax, grid2, 'b-') ax.axis('equal') ax.set_xticks([]) ax.set_yticks([]) ax.axis([-2, 12, -2, 12]) f = plt.subplot(122).get_frame() # print [f.get_x(),f.get_y(),f.get_width()/2,f.get_height()/2] # plt.axes() coords = poly_coords(Polygon([0, 1, 1, 0], [0, 0, 1, 1])) angles = np.linspace(-np.pi, np.pi, 50) offsets = np.linspace(-1, 1.5, 50) weights = np.zeros((len(angles), len(offsets))) print "Calculating overlaps..." for i, theta in enumerate(angles): for j, offset in enumerate(offsets): x, y = np.dot(tf(theta, offset, offset), coords)[:2] x, y = ext.poly_clip(x, y, 0, 1, 1, 0) if len(x) >= 3: weights[i, j] = Polygon(x, y).area() plt.rcParams['figure.figsize'] = (6.67, 3.335) plt.imshow(weights) plt.subplots_adjust(wspace=0.4) plt.xlabel("Offset") plt.ylabel("Angle") plt.xlim([0, 50]) plt.ylim([0, 50]) plt.xticks([0, 25, 50], ('-1', '0.25', '1.5')) plt.yticks([0, 25, 50], ('$\pi$', '0', '$-\pi$')) mkdir('output') plt.savefig('output/gridoverlap.eps')
def plotclip(x,y,xleft,xright,ytop,ybottom): boxx = [xleft,xright,xright,xleft,xleft] boxy = [ytop,ytop,ybottom,ybottom,ytop] pcx, pcy = poly_clip(x, y, xleft, xright, ytop, ybottom) plotpoly(boxx, boxy, 'r-') plotpoly(x,y) plt.gca().add_patch(patches.Polygon(zip(x,y), alpha=0.4)) plt.gca().add_patch(patches.Polygon(zip(boxx,boxy), alpha=0.4, facecolor=[1,0,0])) plt.gca().add_patch(patches.Polygon(zip(pcx,pcy), linewidth=0, facecolor=[1,1,1]))
def plotclip(x, y, xleft, xright, ytop, ybottom): boxx = [xleft, xright, xright, xleft, xleft] boxy = [ytop, ytop, ybottom, ybottom, ytop] pcx, pcy = poly_clip(x, y, xleft, xright, ytop, ybottom) plotpoly(boxx, boxy, 'r-') plotpoly(x, y) plt.gca().add_patch(patches.Polygon(zip(x, y), alpha=0.4)) plt.gca().add_patch( patches.Polygon(zip(boxx, boxy), alpha=0.4, facecolor=[1, 0, 0])) plt.gca().add_patch( patches.Polygon(zip(pcx, pcy), linewidth=0, facecolor=[1, 1, 1]))
def fail_poly_clip(self, *args): ext.poly_clip(*args)