Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model_layer.py
blob: f30020a5c97c9a1a3f55fda983fcef46aa09250c (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
# coding: UTF-8
# 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 random
import cairo
import model_random
import model_allele
import model_constraintpool
from gettext import gettext as _

## 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
#
#OPERATOR_CONSTRAINT = 'operatorconstraint'

class Layer(model_allele.Allele):
    """Layer
    """

    base_cdef = [{}]

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

#    def dump(self):
#        return (repr(type(self)), id(self),
#            self.random_seed,
#            self.OPERATOR_NAMES[self.operator],
#        )

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

    def randomize(self):
        """Randomize the layers components."""
#        cpool = model_constraintpool.ConstraintPool.get_pool()
        self.random_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 model_random.is_mutating():
#            cpool = model_constraintpool.ConstraintPool.get_pool()
#            operator_constraint = cpool.get(self, OPERATOR_CONSTRAINT)
#            self.operator = random.choice(operator_constraint)

    def swap_places(self):
        """Layer is an abstract class. Call swap_places() on sub classes only.
        """
        raise TypeError("Layer is an abstract class. " \
                        "Call swap_places() on sub classes only.")

    def crossingover(self, other):
        """Layer is an abstract class. Call crossingover() on sub classes only.
        """
        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 = model_random.crossing_sequence(cross_lenght + 2)
        new_one.random_seed = self.random_seed \
                         if cross_sequence[cross_lenght] else other.random_seed
#        new_one.operator = self.operator \
#                          if cross_sequence[cross_lenght+1] else other.operator
        return cross_sequence

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

    def begin_render(self, ctx, width, height):
        """
        pre: ctx is not None
        pre: width > 0
        pre: height > 0
        pre: width == height
        """
        ctx.set_operator(cairo.OPERATOR_SOURCE)

    def explain(self, formater):
        formater.text_item('Painters algorithm: ' + self.__class__.__name__)
        formater.text_item('Seed value for random generator: ' \
                           + str(self.random_seed))

    def copy(self):
        """Layer is an abstract class. "Call copy() on sub classes only."""
        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.random_seed = self.random_seed
#        new_one.operator = self.operator