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
|
# 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 random
import cairo
import model_allele
import ka_random
class BorderMerger(model_allele.Allele):
"""BorderMerger
inv: 0.0 <= self.border_weight <= 0.5
inv: self.border_alfa == 0.0 or self.border_alfa == 1.0
"""
constraint_wieght = (0.0, 0.5)
def __init__(self, trunk):
"""Constructor for a simple merger."""
super(BorderMerger, self).__init__(trunk)
self.border_weight = 0.25
self.border_alfa = 1.0
self.constraint_weight = (0.0, 0.5)
def __eq__(self, other):
"""Equality based on same instance."""
equal = isinstance(other, BorderMerger) \
and self.border_weight == other.border_weight \
and self.border_alfa == other.border_alfa
return equal
def randomize(self):
"""No member variables. Nothing to do."""
self.border_weight = random.uniform(self.constraint_weight[0], self.constraint_weight[1])
self.border_alfa = 1.0 if random.randint(0, 1) > 0 else 0.0
def mutate(self):
"""No member variables. Nothing to do."""
if ka_random.is_mutating():
self.border_weight += ka_random.jitter_constrained(self.constraint_weight)
self.border_weight = ka_random.limitate_range(self.border_weight, self.constraint_weight[0], self.constraint_weight[1])
if ka_random.is_mutating():
self.border_alfa = 1.0 if random.randint(0, 1) > 0 else 0.0
def shuffle(self):
"""No member variables. Nothing to do."""
pass
def crossingover(self, other):
"""
pre: isinstance(other, BorderMerger)
pre: isinstance(self, BorderMerger)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
post: __return__ is not other
"""
new_one = BorderMerger(self.get_trunk())
cross_sequence = ka_random.crossing_sequence(2)
new_one.border_weight = self.border_weight if cross_sequence[0] else other.border_weight
new_one.border_alfa = self.border_alfa if cross_sequence[1] else other.border_alfa
return new_one
def draw_single_layer(self, single_layer, mask_layer, ctx, width, height):
"""
pre: single_layer is not None
pre: ctx is not None
pre: width > 0
pre: height > 0
pre: width == height
"""
# paint one layer
ctx.save()
msk_surface = ctx.get_target().create_similar(cairo.CONTENT_ALPHA,
width, height)
msk_ctx = cairo.Context(msk_surface)
msk_width = msk_surface.get_width()
msk_height = msk_surface.get_height()
# fill the whole background with an alpha value.
msk_ctx.set_operator(cairo.OPERATOR_SOURCE)
msk_ctx.set_source_rgba(1.0, 1.0, 1.0, self.border_alfa)
msk_ctx.paint()
# fill the interior with an alpha value.
msk_ctx.set_operator(cairo.OPERATOR_SOURCE)
msk_ctx.set_source_rgba(1.0, 1.0, 1.0, 1.0-self.border_alfa)
msk_ctx.rectangle(self.border_weight*msk_width,
self.border_weight*msk_height,
(1.0-2.0*self.border_weight)*msk_height,
(1.0-2.0*self.border_weight)*msk_width)
msk_ctx.fill()
ctx.save()
ctx.translate(-0.5, -0.5)
ctx.scale(1.0/msk_width, 1.0/msk_height)
ctx.mask_surface(msk_surface, 0.0, 0.0)
ctx.restore()
single_layer.draw(ctx, width, height)
ctx.restore()
def explain(self):
"""
post len(__return__) == 3
"""
return u'Border merger, border weight=' + unicode(self.border_weight) \
+ u' alfa=' + unicode(self.border_alfa), \
None, \
None
def copy(self):
"""A copy constructor.
post: isinstance(__return__, BorderMerger)
# check for distinct references, needs to copy content, not references
post: __return__ is not self
"""
new_one = BorderMerger(self.get_trunk())
new_one.border_weight = self.border_weight
new_one.border_alfa = self.border_alfa
return new_one
|