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
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 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 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")