Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model_layer.py
blob: 5d2eb25514eb26ee0be116ecb7ab400a7cc13bb8 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import time
import random
import cairo
import ka_random
import model_allele
import model_constraintpool

# Cairo's compositing operators 
#    OPERATOR_ADD = 12
#    OPERATOR_ATOP = 5
#    OPERATOR_CLEAR = 0
#    OPERATOR_DEST = 6
#    OPERATOR_DEST_ATOP = 10
#    OPERATOR_DEST_IN = 8
#    OPERATOR_DEST_OUT = 9
#    OPERATOR_DEST_OVER = 7
#    OPERATOR_IN = 3
#    OPERATOR_OUT = 4
#    OPERATOR_OVER = 2
#    OPERATOR_SATURATE = 13
#    OPERATOR_SOURCE = 1
#    OPERATOR_XOR = 11
#OPERATORS = [operator for operator in range(cairo.OPERATOR_SATURATE+1)]
#

OPERATOR_CONSTRAINT = 'operatorconstraint'

class Layer(model_allele.Allele):
    """Layer
    inv: cairo.OPERATOR_CLEAR <= self.operator <= cairo.OPERATOR_SATURATE
    """

    base_cdef = [{'bind'  : OPERATOR_CONSTRAINT,
                  'name'  : 'Permitted operators for combining two layers',
                  'domain': model_constraintpool.INT_M_OF_N,
                  'enum'  : [('add',  cairo.OPERATOR_ADD),
                            ('atop',  cairo.OPERATOR_ATOP),
#                            ('clear',  cairo.OPERATOR_CLEAR),
#                            ('destination',  cairo.OPERATOR_DEST),
                            ('destination atop',  cairo.OPERATOR_DEST_ATOP),
                            ('destination in',  cairo.OPERATOR_DEST_IN),
                            ('destination out',  cairo.OPERATOR_DEST_OUT),
#                            ('destination over',  cairo.OPERATOR_DEST_OVER),
#                            ('in',  cairo.OPERATOR_IN),
                            ('out',  cairo.OPERATOR_OUT),
                            ('over',  cairo.OPERATOR_OVER),
#                            ('saturate',  cairo.OPERATOR_SATURATE),
                            ('source',  cairo.OPERATOR_SOURCE),
                            ('xor',  cairo.OPERATOR_XOR),],},
                ]
    OPERATORS = [o[1] for o in base_cdef[0]['enum']]
#    OPERATOR_NAMES = [o[0] for o in base_cdef[0]['enum']]
    OPERATOR_NAMES = {
        cairo.OPERATOR_ADD:       'ADD',
        cairo.OPERATOR_ATOP:      'ATOP',
        cairo.OPERATOR_CLEAR:     'CLEAR',
        cairo.OPERATOR_DEST:      'DEST',
        cairo.OPERATOR_DEST_ATOP: 'DEST_ATOP',
        cairo.OPERATOR_DEST_IN:   'DEST_IN',
        cairo.OPERATOR_DEST_OUT:  'DEST_OUT',
        cairo.OPERATOR_DEST_OVER: 'DEST_OVER',
        cairo.OPERATOR_IN:        'IN',
        cairo.OPERATOR_OUT:       'OUT',
        cairo.OPERATOR_OVER:      'OVER',
        cairo.OPERATOR_SATURATE:  'SATURATE',
        cairo.OPERATOR_SOURCE:    'SOURCE',
        cairo.OPERATOR_XOR:       'XOR',
    }

    def __init__(self, trunk):
        """Layer constructor"""
        super(Layer, self).__init__(trunk)
        self.seed = 1512
        self.operator = cairo.OPERATOR_OVER

    def __eq__(self, other):
        """Equality based on the cells color components."""
        equal = isinstance(other, Layer) \
                and self.seed == other.seed \
                and self.operator == other.operator
        return equal

    def randomize(self):
        """Randomize the layers components."""
        cpool = model_constraintpool.ConstraintPool.get_pool()
        self.seed = random.randint(1, 65535)
        operator_constraint = cpool.get(self, OPERATOR_CONSTRAINT)
        self.operator = random.choice(operator_constraint)

    def mutate(self):
        """Make small random changes to the layers components."""
        if ka_random.is_mutating():
            cpool = model_constraintpool.ConstraintPool.get_pool()
            operator_constraint = cpool.get(self, OPERATOR_CONSTRAINT)
            self.operator = random.choice(operator_constraint)

    def shuffle(self):
        """Shuffle similar componets.
        For example exchange foreground and background color."""

    def crossingover(self, other):
        """
        """
        raise TypeError("Layer is an abstract class. Call crossingover() on sub classes only.")

    def crossingover_base(self, new_one, other, cross_lenght):
        """
        pre: isinstance(new_one, Layer)
        pre: isinstance(other, Layer)
        pre: cross_lenght >= 0
        """
        cross_sequence = ka_random.crossing_sequence(cross_lenght + 2)
        if cross_sequence[cross_lenght]:
            new_one.seed = self.seed
        else:
            new_one.seed = other.seed
        if cross_sequence[cross_lenght+1]:
            new_one.operator = self.operator
        else:
            new_one.operator = other.operator
        return cross_sequence

    def draw(self, ctx, width, height):
        raise TypeError("Layer is an abstract class. Call draw() on sub classes only.")

    def begin_draw(self, ctx, width, height):
        """
        pre: ctx is not None
        pre: width > 0
        pre: height > 0
        pre: width == height
        """
        time.sleep(0.001) # Python can not set thread priorities.
                          # Workaround: regularly released the global inter-
                          # preter lock GIL to give the GUI Thread a chance.
        ctx.set_operator(self.operator)

    def explain(self, formater):
        formater.text_item('Compositing drawing operator: ' \
                           + Layer.OPERATOR_NAMES[self.operator])

    def copy(self):
        raise TypeError("Layer is an abstract class. Call copy() on sub classes only.")
    
    def copy_base(self, new_one):
        """The layers copy constructor
        pre: isinstance(new_one, Layer)
        """
        new_one.seed = self.seed
        new_one.operator = self.operator