summaryrefslogtreecommitdiff
path: root/qml/python/transformations.py
blob: 40aafc0b4356753a3da8b961e037e64484b99446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random
random.seed()

class Translation(object):
    """ A translation transformation.
    """

    def __init__(self, board):
        """ Create a new translation from the board.
        """
        self.board = board

    def is_valid(self):
        """ Apply if a translation if the goban is not maximized.
        """
        return self.board.min_x != 0 or self.board.min_y != 0

    def apply_points(self, coord, name = None):
        """ Move the points to the lower position.
        """
        x, y = coord
        return (x - self.board.min_x, y - self.board.min_y)

    def get_new_size(self):
        """ The goban size does not change.
        """
        return 0, 0, self.board.max_x - self.board.min_x, self.board.max_y - self.board.min_y

    def get_new_side(self):
        """ There is no changes on the sides.
        """
        return self.board.side

class Rotation(object):
    """ A rotation transformation.
    """

    def __init__(self, board):
        """ Create a new roation from the board.
        """
        self.board = board

    def is_valid(self):
        """ Apply the rotation in following cases :
        * if the board is widther than heigther
        * randomly is width == heigth
        * never if the board is heigther than widther
        """

        width, heigth = self.board.get_size()
        should = heigth - width
        if should == 0:
            return random.randint(0, 1) == 1
        return should < 0

    def apply_points(self, coord, name = None):
        """ Apply the transformation on a point.
        """
        x, y = coord
        return (self.board.max_y - y, x)

    def get_new_size(self):
        """ Apply the transformation on the goban size.
        """
        max_x = self.board.max_y - self.board.min_y
        return 0, self.board.min_x, max_x, self.board.max_x

    def get_new_side(self):
        """ Apply the transformations on the sides.
        """
        return {
            "BOTTOM":   self.board.side["RIGHT"],
            "RIGHT":  self.board.side["TOP"],
            "LEFT": self.board.side["BOTTOM"],
            "TOP":self.board.side["LEFT"]
        }

class Symmetry(object):
    """ A translation transformation.
    """

    def __init__(self, board):
        self.board = board

        self.x_flip = random.randint(0, 1) == 1
        self.y_flip = random.randint(0, 1) == 1

    def is_valid(self):
        """ The transformation is valid if one flip is required in one
        direction.  """
        return self.x_flip or self.y_flip

    def apply_points(self, coord, name = None):
        """ Flip in both directions.
        """
        x, y = coord
        if self.x_flip:
            new_x = self.board.max_x - x + self.board.min_x
        else:
            new_x = x

        if self.y_flip:
            new_y = self.board.max_y - y + self.board.min_y
        else:
            new_y = y

        return (new_x, new_y)

    def get_new_size(self):
        """ The size is not changed.
        """
        return self.board.min_x, self.board.min_y, self.board.max_x, self.board.max_y

    def get_new_side(self):
        """ There is no changes on the sides.
        """
        return {
            "TOP":   self.board.side["BOTTOM" if self.y_flip else "TOP"],
            "LEFT":  self.board.side["RIGHT" if self.x_flip else "LEFT"],
            "RIGHT": self.board.side["LEFT" if self.x_flip else "RIGHT"],
            "BOTTOM":self.board.side["TOP" if self.y_flip else "BOTTOM"]
        }

class ToIndex(object):
    """" Transform each point position in point index.
    """

    def __init__(self, board):
        self.board = board

    def is_valid(self):
        """ This transformation is always valid.
        """
        return True;

    def apply_points(self, coord, name = None):
        """
        """
        x_size = min(19, self.board.max_x - self.board.min_x + 1)
        x, y = coord
        return (x - self.board.min_x) + (y - self.board.min_y) * x_size

    def get_new_size(self):
        """ The size is not changed.
        """
        return self.board.min_x, self.board.min_y, self.board.max_x, self.board.max_y

    def get_new_side(self):
        """ There is no changes on the sides.
        """
        return self.board.side