diff options
author | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-12-06 12:37:03 (GMT) |
---|---|---|
committer | Thomas Jourdan <b.vehikel@googlemail.com> | 2009-12-06 12:37:03 (GMT) |
commit | bcde11455168a07de8a3b17f2a4d77ce8931e75d (patch) | |
tree | a5ba40242065b6c68940311f0d5335ab4656baa0 /ep_modifier_flip.py | |
parent | 572b826a4446ce3d44a57f4d29a9edeca086df42 (diff) |
Layers are now arranged as a tree data structure.
Diffstat (limited to 'ep_modifier_flip.py')
-rw-r--r-- | ep_modifier_flip.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/ep_modifier_flip.py b/ep_modifier_flip.py new file mode 100644 index 0000000..cbd9f9d --- /dev/null +++ b/ep_modifier_flip.py @@ -0,0 +1,111 @@ +# 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 model_random +import model_locus +import model_allele + +class FlipModifier(model_allele.Allele): + """FlipModifier: Reverse the layer horizontally or vertically. + inv: self.xFlip == -1.0 or self.xFlip == 1.0 + inv: self.yFlip == -1.0 or self.yFlip == 1.0 + """ + + def __init__(self, trunk): + """Constructor for a flip modifier.""" + super(FlipModifier, self).__init__(trunk) + self.xFlip = 1.0 + self.yFlip = 1.0 + + def __eq__(self, other): + """Equality based on fliping horizontally or vertically.""" + equal = isinstance(other, FlipModifier) \ + and self.xFlip == other.xFlip \ + and self.yFlip == other.yFlip + return equal + + def randomize(self): + """Randomize the layers components.""" + self.xFlip = 1.0 if random.randint(0, 1) > 0 else -1.0 + self.yFlip = 1.0 if random.randint(0, 1) > 0 else -1.0 + + def mutate(self): + """Make small random changes to the layers components.""" + if model_random.is_mutating(): + self.xFlip = 1.0 if random.randint(0, 1) > 0 else -1.0 + if model_random.is_mutating(): + self.yFlip = 1.0 if random.randint(0, 1) > 0 else -1.0 + + def swap_places(self): + """Exchange x and y flip.""" + self.xFlip, self.yFlip = model_random.swap_parameters(self.xFlip, + self.yFlip) + def crossingover(self, other): + """ + pre: isinstance(other, FlipModifier) + pre: isinstance(self, FlipModifier) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + post: __return__ is not other + post: model_locus.unique_check(__return__, self, other) == '' + """ + new_one = FlipModifier(self.get_trunk()) + cross_sequence = model_random.crossing_sequence(2) + new_one.xFlip = self.xFlip if cross_sequence[0] else other.xFlip + new_one.yFlip = self.yFlip if cross_sequence[1] else other.yFlip + return new_one + + def render_single_layer(self, single_layer, single_treenode, 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.scale(self.xFlip, self.yFlip) + ctx.save() + single_layer.render(ctx, width, height) + single_treenode.render(ctx, width, height) + ctx.restore() + + def explain(self): + """ + post: len(__return__) == 3 + """ + if self.xFlip and self.yFlip: + text = u'Flip modifier: flip horizontally and vertically.' + elif self.xFlip: + text = u'Flip modifier: flip horizontally.' + elif self.yFlip: + text = u'Flip modifier: flip vertically.' + else: + text = u'Flip modifier: did not flip.' + return text, None, None + + def copy(self): + """A copy constructor. + post: isinstance(__return__, FlipModifier) + # check for distinct references, needs to copy content, not references + post: __return__ is not self + """ + new_one = FlipModifier(self.get_trunk()) + new_one.xFlip = self.xFlip + new_one.yFlip = self.yFlip + return new_one |