class TwoPhaseModel: def __init__(self, MI, NI, granulometry, matrixLabel): self.MI = MI self.NI = NI self.granulometry = granulometry self.matrixLabel = matrixLabel xv = np.linspace(0, self.MI - 1, self.MI, endpoint=True).astype(np.int32) yv = np.linspace(0, self.NI - 1, self.NI, endpoint=True).astype(np.int32) X, Y = np.int32(np.meshgrid(xv, yv)) self.coords = {(x, y) for x, y in zip(X.ravel(), Y.ravel())} depth = 4 self.qtree = Quadtree(int(depth), Rect(0, 0, int(self.MI), int(self.NI))) (self.XX, self.YY) = np.meshgrid(range(0, self.NI), range(0, self.MI)) def compute(self): Objs = [] Image = np.ones((self.MI, self.NI), np.int32) * self.matrixLabel Image = Image.astype(np.int32) start = time.time() for ix, value in enumerate( zip(self.granulometry.a_reversed, self.granulometry.c_reversed)): print(ix, value[0], value[1]) area_set = 0 while area_set < value[1]: cy, cx = self.coords.pop() #if Image[cy,cx] == self.matrixLabel: if True: b = self.granulometry.getB(value[0]) theta = random.uniform(0, np.pi) c = Ellipse(cy, cx, int(value[0]), int(b), theta) objs = self.qtree.query(c) if len(objs) == 0: self.qtree.insert(c) Objs.append(c) area_set += c.area() #ellipseMatrix(c.y(), c.x(), c.a(), c.b(), c.theta(), Image, int(self.granulometry.Label), self.XX, self.YY) #ellipseDiscard(c.y(), c.x(), c.a(), c.b(), c.theta(), self.XX, self.YY, self.coords, Image, self.granulometry.Label) else: self.coords.add((cy, cx)) print(time.time() - start) return Objs, int(self.granulometry.Label), self.XX, self.YY, Image
def main(): pygame.init() screen = pygame.display.set_mode((width, height)) map = pygame.Surface((map_width, map_height)) pygame.display.set_caption("quadtree") font = pygame.font.SysFont('arial', 20) clock = pygame.time.Clock() particles = Group([ Particle( random.gauss(map_width / 2, 150) % map_width, random.gauss(map_height / 2, 150) % map_height, 4, map) for _ in range(1000) ]) qtree = Quadtree(map.get_rect(), 4, map) for p in particles: qtree.insert(p) while True: clock.tick(60) fps_text = font.render(f'fps:{int(clock.get_fps())}', True, THECOLORS['white']) fps_rect = fps_text.get_rect() for event in pygame.event.get(): if event.type == pygame.QUIT: exit() qtree = Quadtree(map.get_rect(), 4, map) for p in particles: qtree.insert(p) particles.update() for p in particles: p.highlight = False query_rect = Rect(p.rect.x, p.rect.y, p.radius * 2, p.radius * 2) if len(qtree.query(query_rect)) != 1: p.highlight = True map.fill(THECOLORS['black']) particles.draw(map) pygame.transform.scale(map, (width, height), screen) screen.blit(fps_text, fps_rect) pygame.display.update()
def update_boids(): tree = Quadtree(b[0], 0, 0, WIDTH, HEIGHT) for i in range(1, len(b)): tree.insert(b[i]) for boid in b: accel = [0, 0] possible_close_boids = tree.findInCircle(boid.pos[0], boid.pos[1], NEIGHBORHOOD_THRESHOLD) if len(possible_close_boids) > 1: accel = boid.compute_acceleration(possible_close_boids, SEPARATION_THRESHOLD, C, A, S) if Quadtree.pointInCircle(tree, boid.pos[0], boid.pos[1], mouse[0], mouse[1], NEIGHBORHOOD_THRESHOLD): accel = list(map(sum, zip(accel, boid.avoid(mouse)))) boid.update(WIDTH, HEIGHT, accel)
from quadtree import Quadtree import numpy as np import time np.random.seed(5) p = Quadtree(0, 0, 100, 100, 5) N = 5000000 data = 100 * np.random.random(size=(N, 2)) data = data.astype(np.float32) start = time.time() p.insert(data) print(time.time() - start) start = time.time() r = .1 d = p.select_from(data, r) print(time.time() - start) print(d[0]) print()
from quadtree import Point, Rectangle, Quadtree DPI = 72 np.random.seed(60) width, height = 600, 400 N = 500 coords = np.random.randn(N, 2) * height / 3 + (width / 2, height / 2) points = [Point(*coord) for coord in coords] print(len(points)) center = Point(width / 2, height / 2) domain = Rectangle(center, height, width) qtree = Quadtree(domain, 3) for point in points: qtree.insert(point) print('Number of points in the domain =', len(qtree)) # it might not be adding all the points for some reason fig = plt.figure(figsize=(700 / DPI, 500 / DPI), dpi=DPI) ax = plt.subplot() ax.set_xlim(0, width) ax.set_ylim(0, height) qtree.draw(ax) ax.scatter([p.x for p in points], [p.y for p in points], s=4) ax.set_xticks([]) ax.set_yticks([]) domain.draw(ax, color='r', linewidth=5)