import numpy as np import matplotlib.pyplot as plt from configobj import ConfigObj original = "exclusions_default.conf" def raw_to_xy(obj): raw = np.array(obj) x = np.array(raw[0]) y = np.array(raw[1]) return {"x": x, "y": y} def xy_to_raw(xy): raw = list() raw.append(list(xy["x"])) raw.append(list(xy["y"])) return raw def load_exclusions(path): raw = ConfigObj(infile=path, unrepr=True) excl = dict() excl["name"] = raw["NAME"] excl["theta"] = raw_to_xy(raw["KEEPOUT_THETA"]) excl["phi"] = raw_to_xy(raw["KEEPOUT_PHI"]) excl["gfa"] = raw_to_xy(raw["KEEPOUT_GFA"]) excl["petal"] = raw_to_xy(raw["KEEPOUT_PTL"]) return excl def dump_exclusions(excl, path): raw = ConfigObj(infile=None, unrepr=True) raw.filename = path raw.list_values = True raw["NAME"] = excl["name"] raw["KEEPOUT_THETA"] = xy_to_raw(excl["theta"]) raw["KEEPOUT_PHI"] = xy_to_raw(excl["phi"]) raw["KEEPOUT_GFA"] = xy_to_raw(excl["gfa"]) raw["KEEPOUT_PTL"] = xy_to_raw(excl["petal"]) raw.write() return def extendpoly(xdata, ydata, dist): xnew = list() ynew = list() ndat = len(xdata) # For each point, compute the direction orthogonal to the line # connecting the previous and next points. Then move the # current point along that direction. for i in range(ndat): prv = None nxt = None if i == 0: nxt = i + 1 prv = -1 elif i == ndat - 1: nxt = 0 prv = i - 1 else: nxt = i + 1 prv = i - 1 tang = np.arctan2(ydata[nxt] - ydata[prv], xdata[nxt] - xdata[prv]) # print("{}: prev {} ({}, {}) --> next {} ({}, {}) angle = {}".format( # i, prv, xdata[prv], ydata[prv], nxt, xdata[nxt], ydata[nxt], tang*180.0/np.pi) # ) tang -= 0.5 * np.pi xincr = dist * np.cos(tang) yincr = dist * np.sin(tang) # print(" radang = {}, xincr = {}, yincr = {}".format(tang, xincr, yincr)) xnew.append(xdata[i] + xincr) ynew.append(ydata[i] + yincr) return np.array(xnew), np.array(ynew) def plot_poly(ax, x, y, color="black"): fullx = np.empty(len(x) + 1) fullx[0:-1] = x fullx[-1] = x[0] fully = np.empty(len(y) + 1) fully[0:-1] = y fully[-1] = y[0] ax.plot(fullx, fully, color=color) return # Load original polygons orig = load_exclusions(original) # Add 100 microns to theta / phi exclusions p100 = dict() p100["name"] = "plus100" p100["gfa"] = orig["gfa"] p100["petal"] = orig["petal"] p100["theta"] = dict() p100["theta"]["x"], p100["theta"]["y"] = extendpoly( orig["theta"]["x"], orig["theta"]["y"], 0.1 ) p100["phi"] = dict() p100["phi"]["x"], p100["phi"]["y"] = extendpoly( orig["phi"]["x"], orig["phi"]["y"], 0.1 ) dump_exclusions(p100, "exclusions_plus100.conf") # Add 200 microns to theta / phi exclusions p200 = dict() p200["name"] = "plus200" p200["gfa"] = orig["gfa"] p200["petal"] = orig["petal"] p200["theta"] = dict() p200["theta"]["x"], p200["theta"]["y"] = extendpoly( orig["theta"]["x"], orig["theta"]["y"], 0.2 ) p200["phi"] = dict() p200["phi"]["x"], p200["phi"]["y"] = extendpoly( orig["phi"]["x"], orig["phi"]["y"], 0.2 ) dump_exclusions(p200, "exclusions_plus200.conf") # Plot the different cases fig = plt.figure(figsize=(8, 6), dpi=100) ax = fig.add_subplot(1, 1, 1) plot_poly(ax, orig["phi"]["x"], orig["phi"]["y"], color="black") plot_poly(ax, p100["phi"]["x"], p100["phi"]["y"], color="blue") plot_poly(ax, p200["phi"]["x"], p200["phi"]["y"], color="red") plt.savefig("exclusions_phi.png") #plt.show() plt.close() fig = plt.figure(figsize=(8, 6), dpi=100) ax = fig.add_subplot(1, 1, 1) plot_poly(ax, orig["theta"]["x"], orig["theta"]["y"], color="black") plot_poly(ax, p100["theta"]["x"], p100["theta"]["y"], color="blue") plot_poly(ax, p200["theta"]["x"], p200["theta"]["y"], color="red") plt.savefig("exclusions_theta.png") #plt.show() plt.close()