def art_gallery_problem(interface, points=None, show_decomposition=True): if points is None: points = ioclass.read_from_file(filename) poly = Polygon() poly.set_points(points) size = len(points) list_points = [] for p in points: list_points.append([p.x, p.y]) seidel_triangalator = seidel.Triangulator(list_points) triangles1 = seidel_triangalator.triangles() # Now for the GUI. Both the polygon and its triangulation have been scaled, # as specified above. Now we need to draw them on a Tkinter Canvas. # Setup and init a canvas: if show_decomposition: interface.draw_triangles(triangles1) # The last step is to output the triangulation of the original, non-scaled # polygon to the console: # ioclass.print_triangles_to_console(triangles1) art_gallery_coloring = Coloring() art_gallery_coloring.set_triangulation(points, triangles1) points_str, res = art_gallery_coloring.colorize() list_res = [] for p in points_str: p = p.name list_res.append([points[int(p)].x, points[int(p)].y]) interface.draw_result(list_res) interface.set_result(res)
def seidel_segment_art_gallery_problem(interface, points=None, show_decomposition=True): if points is None: points = ioclass.read_from_file(filename) poly = Polygon() poly.set_points(points) size = len(points) list_points = [] for p in points: list_points.append([p.x, p.y]) #test = [[0, 0], [1, 0], [1, 1], [0, 1]] seidel_triangalator = seidel.Triangulator(list_points) triangles1 = seidel_triangalator.triangles() if show_decomposition: interface.draw_triangles(triangles1) triangles_per_point = dict() segment_tree = SegmentTree(0, size - 1) for triangle in triangles1: p1, p2, p3 = triangle[0], triangle[1], triangle[2] if p1 not in triangles_per_point.keys(): triangles_per_point[p1] = list() if p2 not in triangles_per_point.keys(): triangles_per_point[p2] = list() if p3 not in triangles_per_point.keys(): triangles_per_point[p3] = list() triangles_per_point[p1].append([p1, p2, p3]) triangles_per_point[p2].append([p1, p2, p3]) triangles_per_point[p3].append([p1, p2, p3]) for point in triangles_per_point: segment_tree.add(int(point), int(point), len(triangles_per_point[point])) current_max = size start = 0 end = size - 1 current_max = segment_tree.query_max(start, end) current_max_index = segment_tree.query_max_index(start, end) res = [] while current_max > 0: res.append(points[current_max_index]) for triangle in triangles_per_point[current_max_index]: p1, p2, p3 = triangle[0], triangle[1], triangle[2] p1 = int(p1) p2 = int(p2) p3 = int(p3) segment_tree.add(p1, p1, -1) segment_tree.add(p2, p2, -1) segment_tree.add(p3, p3, -1) current_max = segment_tree.query_max(start, end) current_max_index = segment_tree.query_max_index(start, end) interface.draw_result_points(res) interface.set_result(len(res)) #test = [[0, 0], [1, 0], [1, 1], [0, 1]] #seidel_1 = seidel.Triangulator(test) #triangles = seidel_1.triangles()
dude = [[174.50415,494.59368],[215.21844,478.87939],[207.36129,458.87939],[203.07558,441.02225],[203.07558,418.1651], [210.93272,394.59367],[224.50415,373.1651],[241.64701,358.1651],[257.36129,354.59367],[275.93272,356.73653], [293.07558,359.59367],[309.50415,377.45082],[322.36129,398.1651],[331.64701,421.73653],[335.21844,437.45082], [356.64701,428.52225],[356.1113,428.34367],[356.1113,428.34367],[368.78987,419.59368],[349.50415,384.59367], [323.78987,359.59367],[290.93272,343.87939],[267.36129,341.02225],[264.50415,331.02225],[264.50415,321.02225], [268.78987,306.02225],[285.93272,286.02225],[295.21844,270.30796],[303.78987,254.59367],[306.64701,213.87939], [320,202.36218],[265,202.36218],[286.64701,217.45082],[293.78987,241.02225],[285,257.36218],[270.93272,271.73653], [254.50415,266.02225],[250.93272,248.1651],[256.64701,233.1651],[256.64701,221.02225],[245.93272,215.30796], [238.78987,216.73653],[233.78987,232.45082],[232.36129,249.59367],[243.07558,257.09367],[242.89701,270.30796], [235.93272,279.95082],[222.36129,293.1651],[205.21844,300.6651],[185,297.36218],[170,242.36218],[175,327.36218], [185,322.36218],[195,317.36218],[230.75415,301.02225],[235.39701,312.45082],[240.57558,323.52225], [243.61129,330.48653],[245.21844,335.12939],[245.03987,344.4151],[229.86129,349.4151],[209.14701,362.09367], [192.89701,377.80796],[177.18272,402.27225],[172.36129,413.87939],[169.14701,430.48653],[168.61129,458.52225], [168.61129,492.80796]] seidel = seidel.Triangulator(dude) triangles = seidel.triangles() trapezoids = seidel.trapezoids #trapezoids = seidel.trapezoidal_map. plt.figure() for t in trapezoids: #t = self.trapezoids[0] #verts = self.trapezoids[t].vertices() verts = t.vertices() plt.gca().add_patch(PolygonPatch(Polygon(verts))) plt.gca().autoscale(tight=False) # plt.show() # for vert in verts:
def triangulate_seidel(points): # based on seidels port of poly2tri,see: https://code.google.com/p/poly2tri/source/browse/python/seidel.py?repo=archive&r=5ad6efedc1c120ea194bbce2a0d4ed849e6e6903 # however, something wrong, triangles not correct import seidel points = [point[:2] for point in points] return seidel.Triangulator(points).triangles()
(360.89481504000003, 680.89354191999996), (360.84740125000002, 685.50766749999991), (360.79221175999999, 690.33982888000003), (360.73024022999999, 695.29852593999999), (360.66248032000004, 700.29225856000005), (360.58992569000003, 705.22952662000012), (360.51357000000002, 710.01882999999998), (360.04131999999998, 738.41168000000005), (310.51454999999999, 738.41168000000005), (260.98779999999999, 738.41168000000005), (260.98779999999999, 748.41168000000005), (260.98779999999999, 758.41168000000005), (256.47133000000002, 758.41168000000005), (251.95484999999999, 758.41168000000005)] seid = seidel.Triangulator(test) triangles = seid.triangles() trapezoids = seid.trapezoids #trapezoids = seidel.trapezoidal_map. trapezoid_verts_list = [ (), ] plt.figure() for t in trapezoids: verts = t.vertices() plt.gca().add_patch(PolygonPatch(Polygon(verts))) plt.gca().autoscale(tight=False)