示例#1
0
    def testStatic(self):
        board = [
            [1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        ]

        gol = GoL(initBoard=board)
        gol.step(100)
        nextBoard = gol.board

        self.assertTrue(boardIO.checkEquals(nextBoard, board))
示例#2
0
 def __init__(self):
     super().__init__(500, 500)
     self.res = 10
     self.gol = GoL(self.get_size()[0], self.get_size()[1], self.res)
     pyglet.clock.schedule_interval(self.update, 1.0 / 60.0)
     self.label = pyglet.text.Label(font_name='Lucida Sans',
                                    font_size=15,
                                    bold=True,
                                    x = 5, y = 30,
                                    width = self.width / 2, multiline=True,)
示例#3
0
    def test2Oszillator2(self):
        board = [
            [1, 1, 0, 0, 0],
            [1, 0, 0, 0, 0],
            [0, 0, 0, 1, 0],
            [0, 0, 1, 1, 0],
            [0, 0, 0, 0, 0],
        ]

        gol = GoL(initBoard=board)
        gol.step(2)
        nextBoard = gol.board

        self.assertTrue(boardIO.checkEquals(nextBoard, board))
示例#4
0
    def testSmallBoard(self):
        board = [
            [1, 1, 0, 1, 0],
            [1, 0, 1, 0, 1],
            [0, 0, 1, 0, 1],
            [1, 1, 0, 1, 0],
            [1, 0, 0, 0, 0],
        ]

        nextBoardValidated = [[1, 1, 1, 1, 0], [1, 0, 1, 0,
                                                1], [1, 0, 1, 0, 1],
                              [1, 1, 1, 1, 0], [1, 1, 0, 0, 0]]
        gol = GoL(initBoard=board)
        gol.step()
        nextBoard = gol.board
        self.assertTrue(boardIO.checkEquals(nextBoard, nextBoardValidated))
示例#5
0
    def appendGoL(self, gol: GoL, maxGenerations=100,
                  tl=(0, 0), br=(-1, -1), preview=False, abortCondition=None, onColorChange=0, offColorChange=0,
                  **kwargs):

        minTL, maxTL = tl
        minBR, maxBR = br
        try:
            _, _ = minTL
        except TypeError:
            minTL = maxTL = tl

        try:
            _, _ = minBR
        except TypeError:
            minBR = maxBR = br

        maxGenerations += 1
        progressRange = tqdm(range(maxGenerations))
        for i in progressRange:
            for frameNo in range(self.fpg):
                curTL = [min_tl + (max_tl - min_tl) / (maxGenerations * self.fpg) * ((i - 1) * self.fpg + frameNo) for
                         min_tl, max_tl in zip(minTL, maxTL)]
                curBR = [min_br + (max_br - min_br) / (maxGenerations * self.fpg) * ((i - 1) * self.fpg + frameNo) for
                         min_br, max_br in zip(minBR, maxBR)]
                self.renderSettings.topLeft = curTL
                self.renderSettings.bottomRight = curBR

                img = self.renderer(gol, self.renderSettings)
                if preview:
                    cv2.imshow(self.folder, img)
                    cv2.setWindowTitle(self.folder, f"{self.folder} - {i} ({frameNo}/{self.fpg})")
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
                cv2.imwrite(str(Path(self.folder)) + f"/{gol.name}_{gol.generation}_{frameNo:02d}.jpg", img)
            gol.step()

            if abortCondition is not None and abortCondition(gol):
                progressRange.close()
                return

            changeOnColor = (0.5 - random.random()) * 2 * onColorChange
            changeOffColor = (0.5 - random.random()) * 2 * offColorChange
            self.renderSettings.onColorIndex = min(max(self.renderSettings.onColorIndex + changeOnColor, 128), 255)
            self.renderSettings.offColorIndex = min(max(self.renderSettings.offColorIndex + changeOffColor, 0), 128)
示例#6
0
class Window(pyglet.window.Window):

    def __init__(self):
        super().__init__(500, 500)
        self.res = 10
        self.gol = GoL(self.get_size()[0], self.get_size()[1], self.res)
        pyglet.clock.schedule_interval(self.update, 1.0 / 60.0)
        self.label = pyglet.text.Label(font_name='Lucida Sans',
                                       font_size=15,
                                       bold=True,
                                       x = 5, y = 30,
                                       width = self.width / 2, multiline=True,)

    def on_draw(self):
        self.clear()
        self.gol.draw()
        self.label.draw()

    def update(self, t):
        self.gol.next_gen()
        self.label.text = 'Cells alive: ' + str(self.gol.total) \
                          + "\nAverage age: " + '{0:.2f}'.format(self.gol.av_age)

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        self.gol.populate(math.floor(x / self.res), math.floor(y / self.res))
示例#7
0
    def testMediumBoard(self):
        board = boardIO.loadCompressedBoard(TEST_DIR + "mediumRulesTest")
        validBoard1 = boardIO.loadCompressedBoard(TEST_DIR +
                                                  "mediumRulesTestResult1")
        validBoard21 = boardIO.loadCompressedBoard(TEST_DIR +
                                                   "mediumRulesTestResult21")

        gol = GoL(initBoard=board)
        gol.step()
        nextBoard = gol.board
        self.assertTrue(boardIO.checkEquals(nextBoard, validBoard1))
        gol.step(20)
        nextBoard = gol.board
        self.assertTrue(boardIO.checkEquals(nextBoard, validBoard21))
from utils import boardIO

if __name__ == '__main__':
    colormap = cm.COLORMAP_WHITE_GREEN
    golVideo = GoLImageRenderer("../data/images/tasks2",
                                540,
                                540,
                                colormap=colormap)

    # Block
    board = boardIO.emptyBoard(3, 3)
    board[0][1] = 1
    board[0][2] = 1
    board[1][1] = 1
    board[1][2] = 1
    gol = GoL(board)
    gol.name = "block"
    golVideo.appendGoL(gol, maxGenerations=0)

    # o
    board = boardIO.emptyBoard(3, 3)
    board[1][0] = 1  # Top
    board[2][1] = 1  # Right
    board[1][2] = 1  # Bottom
    board[0][1] = 1  # Left
    gol = GoL(board)
    gol.name = "o"
    golVideo.appendGoL(gol, maxGenerations=0)

    # Blinker
    board = boardIO.emptyBoard(3, 3)
def renderImage(gol: GoL, settings: RenderSettings):
    h = settings.height
    w = settings.width
    img = np.zeros((h, w), dtype="B")

    xMin, yMin = settings.topLeft
    xMax, yMax = settings.bottomRight
    if xMax < 0:
        xMax += gol.width
    if yMax < 0:
        yMax += gol.height

    scaling = min(w / (xMax - xMin + 1), h / (yMax - yMin + 1))

    xOffset = int((xMin - int(xMin) - 1) * scaling)
    yOffset = int((yMin - int(yMin) - 1) * scaling)

    font = cv2.FONT_HERSHEY_PLAIN
    textScaling = 0.05
    if settings.showNeighbours:
        while max(cv2.getTextSize("0", font, textScaling, 1)[0]) < scaling * 0.9:
            textScaling += 0.05

    lastX = xOffset
    for x in range(int(xMin), int(xMax + 1) + 1):
        nextX = int((x + 1 - xMin) * scaling)
        lastY = yOffset
        for y in range(int(yMin), int(yMax + 1) + 1):
            nextY = int((y + 1 - yMin) * scaling)

            try:
                grayValue = settings.onColorIndex if gol.getXY(x, y) else settings.offColorIndex
            except:
                grayValue = settings.offColorIndex
            if settings.showGridlines:
                cv2.rectangle(img, (lastX, lastY), (nextX, nextY), 255, 2)

            cv2.rectangle(img, (lastX, lastY), (nextX, nextY), grayValue, -1)
            lastY = nextY

            if settings.showNeighbours:
                nb = gol.countNeighbours(x, y)
                if nb == 0:
                    continue
                textColor = [255 - grayValue for _ in range(3)]
                actualNeighbours = nb - gol.getXY(x, y)
                cv2.putText(img, str(actualNeighbours), (int(lastX + 0.05 * scaling), int(lastY - 0.05 * scaling)),
                            font, textScaling, textColor, 1,
                            cv2.LINE_AA)
        lastX = nextX

    coloredImg = cm.colorize(img, settings.colormap)
    for position, text, color in settings.texts:
        tmp1, tmp2 = position
        x = int((tmp1 - xMin + 0.05) * scaling)
        y = int((tmp2 - yMin + 0.95) * scaling)
        cv2.putText(coloredImg, text, (x, y),
                    font, textScaling, color, 1,
                    cv2.LINE_AA)

    for position, color, size in settings.highlights:
        tmp1, tmp2 = position
        try:
            x1, y1 = tmp1
            x2, y2 = tmp2
            x1 = int((x1 - xMin) * scaling)
            y1 = int((y1 - yMin) * scaling)
            x2 = int((x2 - xMin) * scaling)
            y2 = int((y2 - yMin) * scaling)
        except:
            x1 = int((tmp1 - xMin) * scaling)
            y1 = int((tmp2 - yMin) * scaling)
            x2 = int((tmp1 + 1 - xMin) * scaling)
            y2 = int((tmp2 + 1 - yMin) * scaling)
        cv2.rectangle(coloredImg, (x1, y1), (x2, y2), color, size)
    return coloredImg
示例#10
0
import utils.colormaps as cm
from VideoRenderer import GoLVideoRenderer
from gol import GoL
from utils import boardIO

if __name__ == '__main__':
    board = boardIO.fromImageToSpecificSize(
        "../data/imgs/qr-github.comdwoiwodeGame-Of-Life-Media-Renderer.png",
        size=(33, 33))
    border = 500
    board = boardIO.addBorder(board, border)
    golVideo = GoLVideoRenderer("../data/videos/qr_github2.avi",
                                1080,
                                1080,
                                fps=60,
                                fpg=2,
                                colormap=cm.COLORMAP_WHITE_GREEN)
    tl = (border, border)
    br = (-border - 1, -border - 1)
    gol = GoL(board)
    golVideo.addHighlight((tl, (border + 33, border + 33)), "ff0000")
    golVideo.appendGoL(gol, 120, tl=tl, br=br, preview=True)

    tl = (tl, (450, 450))
    br = (br, (-451, -451))
    golVideo.appendGoL(gol, 80, tl=tl, br=br, preview=True)
    golVideo.appendGoL(gol, 500, tl=tl[1], br=br[1], preview=True)
    # gol = GoLPygame(initBoard=board, colormap=cm.COLORMAP_WHITE_BLACK)
    # gol.start()
from imageRenderer import renderImage
from utils import boardIO

if __name__ == '__main__':
    board = boardIO.loadBoard("../data/boards/r-pentomino")
    board = boardIO.addBorder(board, 400)
    colormap = cm.COLORMAP_WHITE_GREEN

    tl = (350, 350)
    br = (-351, -351)
    # === Static Images ===
    golImages = GoLImageRenderer("../data/images/r-pentomino",
                                 1080,
                                 1080,
                                 colormap=colormap)
    gol = GoL(board)
    gol.name = "r-pentomino"
    golImages.appendGoL(gol, maxGenerations=1199, tl=tl, br=br)

    # === Zoom out Video ===
    gol = GoL(board)
    golVideo = GoLVideoRenderer("../data/videos/r-pentomino-zoom.avi",
                                1080,
                                1080,
                                fps=24,
                                fpg=4,
                                colormap=colormap,
                                renderer=renderImage)
    tl = ((398, 398), tl)
    br = ((-399, -399), br)
    # golVideo.appendGoL(gol, 200, tl, br,preview=False)
示例#12
0
from golImage import GoLImageRenderer
from utils import boardIO

if __name__ == '__main__':
    import utils.colormaps as cm

    golTraining = GoLImageRenderer("../data/images/training",
                                   512,
                                   512,
                                   colormap=cm.COLORMAP_WHITE_GREEN,
                                   showGridlines=True)
    for i in range(20):
        # Base settings
        rndThresh = random.random() * 0.4 + 0.2  # range 0.2 - 0.6
        board = boardIO.createRandomBoard(10, 10, rndThresh)
        gol = GoL(board)

        # Marked tiles
        colors = cm.COLORS_MARK3
        n = len(colors)

        allTiles = itertools.product(range(1, gol.width - 1),
                                     range(1, gol.height - 1))
        nTiles = random.sample(list(allTiles), n)
        for color, tile in zip(colors, nTiles):
            golTraining.addHighlight(tile, color, 5)

        # Record images
        golTraining.renderSettings.showNeighbours = False
        gol.name = f"Training{i:02d}_"
        golTraining.appendGoL(gol)
 def renderImage(self, gol: GoL):
     if not isinstance(gol, GoL):
         gol = GoL(gol)
     return self.renderer(gol, self.renderSettings)
    gliderBoard[6][9] = True
    gliderBoard[7][7] = True
    gliderBoard[7][8] = True

    golVid = GoLVideoRenderer("../data/videos/movingGlider2.avi", 1080 / 2, 1080 / 2, fps=24, showGridlines=True,
                              colormap=cm.COLORMAP_WHITE_GREEN, fpg=12)

    tl = [
        (2, 3),
        (3, 2),
    ]
    br = [
        (-4, -3),
        (-3, -4),
    ]
    golVid.appendGoL(GoL(gliderBoard), 4, preview=True, tl=tl, br=br)
    tl = [
        (2, 3),
        (3, 2),
    ]
    br = [
        (-4, -3),
        (-3, -4),
    ]
    golVid.appendGoL(GoL(gliderBoard), 4, preview=True, tl=tl, br=br)
    tl = [
        (2, 3),
        (3, 2),
    ]
    br = [
        (-4, -3),