class ArtGallery(object): """ This class resolves The art gallery problem. It is a visibility problem in computational geometry. Guarding an art gallery with the minimum number of guards who together can observe the whole gallery """ def __init__(self, point): self._point = Point(point.id, point.x, point.y) self._points = [point] self._polygon = [(point.x, point.y)] self._triangulation = Triangulation(self._points) self._color = Coloring(self._points, self._triangulation) self.lock = threading.RLock() self.version = 0 @staticmethod def load(file_name): points = [] with open(file_name, "r") as hfile: hfile.readline() i = 0 for line in hfile: x, y = line.split() point = Point(i, Decimal(x), Decimal(y)) i += 1 points.append(point) return points def _update(self): self._points.sort() self._polygon = [] for p in self._points: self._polygon.append((p.x, p.y)) def get_polygon(self): with self.lock: return self._polygon def get_process_point(self): with self.lock: return self._point def get_points(self): with self.lock: return self._points def get_diagonals(self): with self.lock: return self._triangulation.get_diagonals() def get_min_color(self): with self.lock: return self._color.get_min_color() def is_guard(self, point): with self.lock: return point.color == self._color.get_min_color() def include(self, point): """ Add a new point to the art gallery """ with self.lock: self._points.append(Point(point.id, point.x, point.y)) self._update() triangulated = self._triangulation.process() if (triangulated): self._color.process() self.version += 1
i1 += 1 return (area / 2) # Testing the class if __name__ == "__main__": from art_gallery import ArtGallery points = ArtGallery.load("inputs/test_0.poly") points.sort() triangulation = Triangulation(points) triangulation.process() from coloring import Coloring color = Coloring(points, triangulation) color.process() i = 0 for p in triangulation._polygon: i += 1 print "Point %d => %s" % (i, p) i = 0 for t in triangulation.get_triangles(): i += 1 print "Triangle %d => (%s,%s)[%s] (%s,%s)[%s] (%s,%s)[%s]" \ % (i, t.u.x, t.u.y, t.u.color, t.v.x, t.v.y, t.v.color, t.w.x, t.w.y, t.w.color) if points.count(t.v) != 1: print "Where is v " + t.v if points.count(t.u) != 1: print "Where is u " + t.u