def intersect_polygons(polygon1, polygon2): """ Intersect two polygons. Parameters ---------- polygon1 : list of arrays Vertices of the first polygon in counterclockwise order. polygon1 : list of arrays Vertices of the second polygon in counterclockwise order. Returns ------- intersection : list of arrays Vertices of the intersection in counterclockwise order. """ from pyclipper import Pyclipper, PT_CLIP, PT_SUBJECT, CT_INTERSECTION from pyclipper import scale_to_clipper, scale_from_clipper # could be accelerated by removing the scale_to/from_clipper() subj, clip = (polygon1,), polygon2 pc = Pyclipper() pc.AddPath(scale_to_clipper(clip), PT_CLIP) pc.AddPaths(scale_to_clipper(subj), PT_SUBJECT) solution = pc.Execute(CT_INTERSECTION) if not solution: return [] return scale_from_clipper(solution)[0]
def find_common_coverage(locuses): clipper = Pyclipper() current_locus = locuses[0] for locus in locuses[1:]: clipper.AddPaths(scale_to_clipper(current_locus), PT_SUBJECT) clipper.AddPaths(scale_to_clipper(locus), PT_CLIP) current_locus = scale_from_clipper(clipper.Execute(CT_INTERSECTION)) clipper.Clear() l_x, l_y = split_locus_lists([current_locus]) return l_x, l_y
f.close() f = open("plot.dat", "w") for i, loop in enumerate(sol): f.write("") for p in loop: f.write("%f, %f\n"%(p[0]/1000.0, p[1]/1000.0) ) f.write("\n\n") p = loop[0] f.write("%f, %f\n"%(p[0]/1000.0, p[1]/1000.0) ) f.write("\n\n") f.close() ''' #offsetting clip = Pyclipper() shapes = [] shape = [] for i in range(3): p = [500+int(1000*random.random()), 500+int(1000*random.random())] shape.append(p) shapes.append(shape) for i in range(3): p = [500+int(1000*random.random()), 500+int(1000*random.random())] shape.append(p) shapes.append(shape) shapes = simplify_polygons(shapes) sol = offset(shapes, delta=100, jointype=1)
def make_figures(scan): ub_matrix = ft.UBMatrix(scan['latparam'], scan['hkl1'], scan['hkl2'], scan['plot_x'], scan['plot_y']) A3_starts, A3_ends, A4_starts, A4_ends = (scan['A3_starts'], scan['A3_ends'], scan['A4_starts'], scan['A4_ends']) NPs = [int(x) for x in scan['NPs']] kis = [ft.e_to_k(e) for e in scan['eis']] kfs = [ft.e_to_k(e) for e in ft.EF_LIST] hm = scan['hm'] hm_hkl = scan['hm_hkl'] hm_ssr = scan['hm_ssr'] unique_kis = sorted(list(set(kis))) indexes_of_ki = [[ind for ind in range(len(kis)) if kis[ind] == ki] for ki in unique_kis] locus_palette = ['#FFCE98', '#F6FF8D', '#94FFD5', '#909CFF', '#FF8AD8'] locuses_dict = {} scatters_dict = {} colors_dict = {} senses_dict = {} for nth_ki, ki in enumerate(unique_kis): clippers = [Pyclipper() for _ in range(ft.EF_CHANNELS)] scatter_arrays = [[] for _ in range(ft.EF_CHANNELS)] color_arrays = [[] for _ in range(ft.EF_CHANNELS)] for scan_no in indexes_of_ki[nth_ki]: angles = (A3_starts[scan_no], A3_ends[scan_no], A4_starts[scan_no], A4_ends[scan_no], ub_matrix) locuses = [ ft.calculate_locus(ki, kf, *angles, no_points=NPs[scan_no]) for kf in kfs ] scatter_coords = [ ft.scatter_coords(ki, kf, *angles, no_points=NPs[scan_no]) for kf in kfs ] scatter_colors = [ ft.scatter_color(ki, kf, *angles, name=hm, ssr=hm_ssr, north=hm_hkl, no_points=NPs[scan_no]) for kf in kfs ] if A4_starts[scan_no] > 0: senses_dict[ki] = 1 else: senses_dict[ki] = -1 for i in range(ft.EF_CHANNELS): clippers[i].AddPath(scale_to_clipper(locuses[i]), PT_SUBJECT) scatter_arrays[i] += scatter_coords[i] color_arrays[i] += scatter_colors[i] locuses_ki = [ scale_from_clipper(clippers[i].Execute(CT_UNION, PFT_NONZERO)) for i in range(ft.EF_CHANNELS) ] locuses_ki_x, locuses_ki_y = split_locus_lists(locuses_ki) scatters_ki_x, scatters_ki_y = split_scatter_lists(scatter_arrays) common_locus_x, common_locus_y = find_common_coverage(locuses_ki) locuses_dict[ki] = [ locuses_ki_x, locuses_ki_y, common_locus_x, common_locus_y ] scatters_dict[ki] = [scatters_ki_x, scatters_ki_y, color_arrays] p_col = [] plots = [] x_axis = np.array(scan['plot_x']) y_axis = np.array(scan['plot_y']) for ki in unique_kis: TOOLS = "pan,wheel_zoom,reset,save" main_plot = figure(plot_width=700, plot_height=600, title='Ei = %s meV' % fmt2(ft.k_to_e(ki)), tools=TOOLS) main_plot.xaxis.axis_label = 'x * %s' % bracketed_vector(x_axis) main_plot.yaxis.axis_label = 'y * %s' % bracketed_vector(y_axis) ticker = SingleIntervalTicker(interval=0.5, num_minor_ticks=1) main_plot.axis.ticker = ticker main_plot.grid.ticker = ticker locus = locuses_dict[ki] efs_str = [fmt1(ft.k_to_e(ki) - ft.k_to_e(kf)) for kf in kfs] sources = [] source_handle = ColumnDataSource(dict(x=[], y=[], colors=[])) scatter_off = ColumnDataSource(dict(x=[], y=[], colors=[])) for i in reversed(range(ft.EF_CHANNELS)): color = locus_palette[i] x_list = locus[0][i] y_list = locus[1][i] main_plot.patches(x_list, y_list, alpha=0.35, fill_color=color, muted_fill_color='black', muted_fill_alpha=0.01, muted_line_alpha=0.1, line_width=1, legend='dE=' + efs_str[i]) set_aspect(main_plot, x_list[0], y_list[0], aspect=ub_matrix.figure_aspect) for i in range(ft.EF_CHANNELS): sources.append( ColumnDataSource( dict(x=scatters_dict[ki][0][i], y=scatters_dict[ki][1][i], colors=scatters_dict[ki][2][i]))) main_plot.circle(x='x', y='y', size=4.5, fill_alpha=1, visible=True, fill_color='colors', line_alpha=0.2, source=source_handle) main_plot.patches(locus[2][0], locus[3][0], fill_alpha=0.0, line_width=1.2, legend='Common', line_color='red', muted_line_alpha=0.0, muted_fill_alpha=0.0) glyph_dots = plot_lattice_points(main_plot, x_axis, y_axis) main_plot.legend.click_policy = 'mute' plot_brillouin_zones(main_plot, x_axis, y_axis) cs = sources callback = CustomJS(args=dict(s0=cs[0], s1=cs[1], s2=cs[2], s3=cs[3], s4=cs[4], s5=scatter_off, source=source_handle), code=""" var f = cb_obj.active; switch (f) { case 0: source.data = s0.data; break; case 1: source.data = s1.data; break; case 2: source.data = s2.data; break; case 3: source.data = s3.data; break; case 4: source.data = s4.data; break; case 5: source.data = s5.data; break; } source.change.emit(); """) en_buttons = RadioButtonGroup( labels=['2.5', '3.0', '3.5', '4.0', '4.5', 'Off'], active=5, callback=callback) en_button_caption = Div() en_button_caption.text = """<span style="font-weight: bold;">Active channel:</span>""" hover = HoverTool(renderers=[glyph_dots], tooltips=[('Q', '@coord')]) main_plot.add_tools(hover) message_div = Div(width=600, height=200) if hm != 'no': plot_radar = draw_radar(main_plot, message_div, en_buttons, hm, ki, scan['hkl1'], hm_hkl, hm_ssr, ub_matrix, senses_dict[ki]) plot_radar.axis.visible = False ctrl_col = column( [en_button_caption, en_buttons, plot_radar, message_div]) else: plot_radar = draw_radar(main_plot, message_div, en_buttons, hm, ki, scan['hkl1'], scan['hkl1'], 0, ub_matrix, senses_dict[ki]) plot_radar.axis.visible = False ctrl_col = column( [en_button_caption, en_buttons, plot_radar, message_div]) # ctrl_col = column([en_button_caption, en_buttons, message_div]) plots.append(main_plot) p_col.append([main_plot, ctrl_col]) for each in plots: each.x_range = plots[0].x_range each.y_range = plots[0].y_range grid = layout(p_col) script, div = components(grid) return script, div