diff options
Diffstat (limited to 'ep_merger_border.py')
-rw-r--r-- | ep_merger_border.py | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/ep_merger_border.py b/ep_merger_border.py new file mode 100644 index 0000000..548cb31 --- /dev/null +++ b/ep_merger_border.py @@ -0,0 +1,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 |