def auto_cleanup(sites, main_index): # test automatic cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include_near(site, sites[main_index]) VoronoiDiagram.show(d, title = "Automatic 'main site' cleanup mode")
def simple(sites, main_index): # simple test without cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include(site) if not d.fail_site is None: Preview.voronoi(d, title = "Fail") VoronoiDiagram.show(d, title = "Without cleanup")
def manual_cleanup(sites, main_index): # test manual cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include(site) d.remove_far_sites(sites[main_index]) VoronoiDiagram.show(d, title = "Manual 'main site' cleanup mode")
def __init__(self, sites, delay): with DiagramTest.lock: self.id = DiagramTest.num_threads DiagramTest.num_threads += 1 self.diagram = Delaunay(test = True) self.site_set = SiteSet(sites) self.delay = delay self.big_delay = DiagramTest._big_delay \ if delay < DiagramTest._big_delay \ else delay self.state = DiagramTest.STARTING threading.Thread.__init__(self) self.counter = 0
class DiagramTest(threading.Thread): ''' Generate a mono and mult thread test for Voronoi Diagram Previewer ''' STARTING = 1 RUNNING = 2 STOPPING = 3 num_threads = 0 lock = threading.Lock() _big_delay = 0.25 def __init__(self, sites, delay): with DiagramTest.lock: self.id = DiagramTest.num_threads DiagramTest.num_threads += 1 self.diagram = Delaunay(test = True) self.site_set = SiteSet(sites) self.delay = delay self.big_delay = DiagramTest._big_delay \ if delay < DiagramTest._big_delay \ else delay self.state = DiagramTest.STARTING threading.Thread.__init__(self) self.counter = 0 def start(self): threading.Thread.start(self) def stop(self): self.state = DiagramTest.STOPPING def must_exit(self): self.counter += 1 threading.Event().wait(self.delay) return self.state != DiagramTest.RUNNING or self.diagram.has_error() def must_exit_delay(self, delay): self.counter += 1 threading.Event().wait(delay) return self.state != DiagramTest.RUNNING or self.diagram.has_error() def run(self): self.state = DiagramTest.RUNNING while True: if self.must_exit(): break while self.site_set.can_pick(): self.diagram.include(self.site_set.pick()) if self.must_exit(): break while self.site_set.has_picked(): self.diagram.remove(self.site_set.unpick()) if self.must_exit(): break if self.must_exit(): break # keep running until receive a stop sign while self.state == DiagramTest.RUNNING: pass
#create connection map from Delaunay triangulation self.neighbors = [] for i in range(self.N): neighbors = set([]) for edge in delaunay_mesh.edgeList: if i in edge: neighbors.update(edge) neighbors.remove(i) neighbors = list(neighbors) neighbors.sort() self.neighbors.append(neighbors) #debugging code if __name__ == "__main__": np.random.seed(1234) n=100 x = np.random.random(n) y = np.random.random(n) fig = plt.figure() ax = fig.add_axes([0.1,0.1,0.8,0.8],aspect='equal') d = Delaunay(x,y) v = Voronoi(d) d.draw_delaunay(ax) fig.canvas.mpl_connect('pick_event',d.on_pick) plt.show()
from delaunay import Delaunay from cust_points import Point import matplotlib.pyplot as plt if __name__ == "__main__": liste = [] liste.append(Point(0, 0)) liste.append(Point(0, 1)) liste.append(Point(1, 1)) liste.append(Point(1, 2)) liste.append(Point(2, 2)) liste.append(Point(2, 0)) delaunay = Delaunay() delaunay.appliquer(liste) first = [] second = [] for p in liste: first.append(p.x) second.append(p.y) firstCenter = [] secondCenter = [] for t in delaunay.triangles: pt = t.centreCirconscrit() firstCenter.append(pt.x) secondCenter.append(pt.y) trix = [t.one.x, t.two.x, t.three.x, t.one.x] triy = [t.one.y, t.two.y, t.three.y, t.one.y] plt.plot(trix, triy, color='#004000') for (k, v) in delaunay.aretes.items(): for a in v: arx = [a.x.x, a.y.x]
numSeeds = 11 radius = 20 a, b = 5, 4 border = [[radius * 0, radius * 0], [radius * 0, radius * b], [radius * a, radius * 0], [radius * a, radius * b]] #seeds = (radius-1) * np.random.random((numSeeds, 2)) distribution = chaospy.J(chaospy.Uniform(0, a), chaospy.Uniform(0, b)) samples = distribution.sample(numSeeds, rule="hammersley") print(list(samples.T)) border.extend(list(radius * samples.T)) print(border) #seeds = [[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2]] seeds = border center = np.mean(seeds, axis=0) dt = Delaunay(center, (50 + a) * radius) for s in seeds: dt.add_point(s) fig, ax = plt.subplots() ax.margins(0.1) ax.set_aspect('equal') plt.axis([-1, radius * a + 1, -1, radius * b + 1]) # Грані cx, cy = zip(*seeds) dt_tris = dt.export_triangles() ax.triplot(matplotlib.tri.Triangulation(cx, cy, dt_tris), 'bo--') # Вершини
# обработка ввода точки в комплексную точку def get_point(): s = input() if not s: return return complex(*map(int, s.split())) print( "Введите граничный треугольник (внутри которого будет строиться триангуляция)." ) print("Введите координаты трёх точек через ';' в формате: a b; c d; e f") print("Например: 0 0; 1000 0; 500 866") start_points = get_triangle() delaunay = Delaunay(start_points) delaunay.plot() while True: print("\nВводите координаты новых точек через пробел в формате: a b") print( "Можно вводить только те точки, которые помещаются в начальный треугольник" ) print("Например: 10 10") print("Для выхода в любой момент нажмите Enter.") point = get_point() if not point: break delaunay.add_point(point) delaunay.plot()
Point2D(150, 125), Point2D(125, 150), Point2D(75, 150), Point2D(50, 125), Point2D(10, 100), Point2D(190, 100), Point2D(100, 10), Point2D(100, 190), Point2D(30, 30), Point2D(170, 170), Point2D(170, 30), Point2D(30, 170) ] # test manual cleanup: d = Delaunay() #d.add_as_main(Point2D(100, 100)) d.add(Point2D(100, 100)) for site in sites: d.add(site) #d.clear_far_sites() Preview.voronoi(d, title = "Manual 'main site' cleanup mode") # test automatic cleanup: d = Delaunay() d.add_as_main(Point2D(100, 100)) d.add(Point2D(100, 100)) for site in sites: d.add_near(site) Preview.voronoi(d, title = "Automatic 'main site' cleanup mode")