def mask_roi(rows, cols, bounds): """Return a mask for operating only on pixels inside the given bounds. Parameters ---------- rows, cols : int Shape of the target image. bounds : (M, 3) array of (x, y, 1) coordinates Boundary coordinates. """ # Sort corners clockwise. This can be done with a single # swap operation, but that's a bit more work. mask = (bounds < 1e-14) bounds[mask] -= 0.5 bounds[~mask] += 0.5 centroid = bounds.mean(axis=0) diff = bounds - centroid angle = np.arctan2(diff[:, 1], diff[:, 0]) bounds = bounds[np.argsort(angle)] bounds = np.vstack((bounds, bounds[0])) p = Polygon(bounds[:, 0], bounds[:, 1]) g = Grid(rows, cols) return p.inside(g['cols'].flat, g['rows'].flat).reshape(rows, cols)
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')
Calculate and display the overlap of a single block. """ import numpy as np import matplotlib.pyplot as plt from matplotlib import patches from supreme.geometry import Polygon from supreme import ext from supreme.config import mkdir grid1 = [] for col in range(10): for row in range(10): grid1.append( Polygon([row, row + 1, row + 1, row, row], [col, col, col + 1, col + 1, col])) theta = 3 / 180.0 * np.pi t_x = 0.04 t_y = 0.04 def tf(angle, offset_x, offset_y): return np.array([[np.cos(angle), -np.sin(angle), offset_x], [np.sin(angle), np.cos(angle), offset_y], [0, 0, 1]], float) def poly_coords(p): coords = np.empty((3, len(p.x))) coords[0] = p.x
import numpy as np import matplotlib.pyplot as plt import sys import supreme from supreme.geometry import Polygon grid_y, grid_x = np.mgrid[0:1:0.1, 0:1:0.1].reshape(2, -1) # simple area test xp = [0.15, 0.85, 0.85, 0.15] yp = [0.15, 0.15, 0.85, 0.85] pa = Polygon(xp, yp) print pa print "Area expected: %f, area found: %f" % ((0.85 - 0.15)**2, pa.area()) print "Centroid: ", pa.centroid() print # concave enclosure test-case for inside. xp = [0.15, 0.25, 0.45, 0.45, 0.25, 0.25, 0.65, 0.65, 0.85, 0.85, 0.15] yp = [0.15, 0.15, 0.15, 0.25, 0.25, 0.55, 0.55, 0.15, 0.15, 0.85, 0.85] pb = Polygon(xp, yp) xc, yc = pb.centroid() print pb print "Area: ", pb.area() print "Centroid: ", xc, yc print