Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_layer_filledspline.py
diff options
context:
space:
mode:
authorThomas Jourdan <b.vehikel@googlemail.com>2009-09-04 16:01:44 (GMT)
committer Thomas Jourdan <b.vehikel@googlemail.com>2009-09-04 16:01:44 (GMT)
commit7ee9a0b36814d90954929a4fead2bf9ff4314c19 (patch)
tree02da3bc99dfef061be32ba0f0f96bf1ac2794726 /ep_layer_filledspline.py
parent6d7854472b3674eb30a1845690f1104b685f7980 (diff)
Additional samplers added. Using set_sensitive to disable GUI elements.
Diffstat (limited to 'ep_layer_filledspline.py')
-rw-r--r--ep_layer_filledspline.py228
1 files changed, 228 insertions, 0 deletions
diff --git a/ep_layer_filledspline.py b/ep_layer_filledspline.py
new file mode 100644
index 0000000..1a7a6ae
--- /dev/null
+++ b/ep_layer_filledspline.py
@@ -0,0 +1,228 @@
+# 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 math
+#import random
+import model_layer
+import ka_random
+import ka_factory
+import model_constraintpool
+import exon_color
+import exon_position
+#import exon_direction
+
+SAMPLERTYPE_CONSTRAINT = 'samplertype_constraint'
+
+class FilledSpline(model_layer.Layer):
+ """FilledSpline
+ """
+
+ cdef = [{'bind' : 'length',
+ 'name' : 'Number of interpolation points',
+ 'domain': model_constraintpool.INT_RANGE,
+ 'min' : 4, 'max': 32},
+ {'bind' : 'line_width',
+ 'name' : 'Line width',
+ 'domain': model_constraintpool.FLOAT_RANGE,
+ 'min' : 0.001, 'max': 0.25},
+ {'bind' : 'roundness',
+ 'name' : 'Roundness of spline',
+ 'domain': model_constraintpool.FLOAT_RANGE,
+ 'min' : 0.1, 'max': 20.0},
+ {'bind' : SAMPLERTYPE_CONSTRAINT,
+ 'name' : 'Permitted sampler types',
+ 'domain': model_constraintpool.STRING_1_OF_N,
+ 'enum' : ka_factory.get_factory('sampler').keys()},
+ ]
+
+ def __init__(self, trunk):
+ """FilledSpline diagram layer constructor"""
+ super(FilledSpline, self).__init__(trunk)
+ cpool = model_constraintpool.ConstraintPool.get_pool()
+ self.linecolor = exon_color.Color(self.path, 0, 0, 0, 1)
+ self.fillcolor = exon_color.Color(self.path, 0, 0, 0, 1)
+ self.line_width = cpool.get(self, 'line_width')[0]
+ self.roundness = cpool.get(self, 'roundness')[0]
+ self.center = exon_position.Position(self.path, 0.0, 0.0)
+# self.direction_steps = [exon_direction.Direction(self.path, 0.0, 0.0)]
+ sampler_factory = ka_factory.get_factory('sampler')
+ sampler_key = sampler_factory.keys()[0]
+ self.sampler = sampler_factory.create(sampler_key, self.path)
+
+ def __eq__(self, other):
+ """Equality based on the objects components."""
+ equal = isinstance(other, FilledSpline) \
+ and super(FilledSpline, self).__eq__(other) \
+ and self.linecolor == other.linecolor \
+ and self.fillcolor == other.fillcolor \
+ and self.line_width == other.line_width \
+ and self.roundness == other.roundness \
+ and self.center == other.center \
+ and self.sampler == other.sampler
+# and len(self.direction_steps) == len(other.direction_steps)
+# if equal:
+# for index, direction in enumerate(self.direction_steps):
+# equal = equal and direction == other.direction_steps[index]
+ return equal
+
+ def randomize(self):
+ """Randomize the layers components."""
+ super(FilledSpline, self).randomize()
+ cpool = model_constraintpool.ConstraintPool.get_pool()
+ length_constraint = cpool.get(self, 'length')
+ line_width_constraint = cpool.get(self, 'line_width')
+ roundness_constraint = cpool.get(self, 'roundness')
+ self.linecolor.randomize()
+ self.fillcolor.randomize()
+ self.line_width = ka_random.uniform_constrained(line_width_constraint)
+ self.roundness = ka_random.uniform_constrained(roundness_constraint)
+ self.center.randomize()
+# self.direction_steps = []
+# length = random.randint(length_constraint[0], length_constraint[1])
+# for i in range(length):
+# direction = exon_direction.Direction(self.path, 0.0, 0.0)
+# direction.randomize()
+# self.direction_steps.append(direction)
+ sampler_factory = ka_factory.get_factory('sampler')
+ samplertype_constraint = cpool.get(self, SAMPLERTYPE_CONSTRAINT)
+ self.sampler = sampler_factory.create_random(samplertype_constraint,
+ self.path)
+ self.sampler.randomize()
+
+ def mutate(self):
+ """Make small random changes to the layers components."""
+ super(FilledSpline, self).mutate()
+ cpool = model_constraintpool.ConstraintPool.get_pool()
+ length_constraint = cpool.get(self, 'length')
+ line_width_constraint = cpool.get(self, 'line_width')
+ roundness_constraint = cpool.get(self, 'roundness')
+ self.linecolor.mutate()
+ self.fillcolor.mutate()
+# ka_random.mutate_list(self.direction_steps,
+# length_constraint,
+# exon_direction.Direction(self.path, 0.0, 0.0))
+ if ka_random.is_mutating():
+ self.line_width += ka_random.jitter_constrained(line_width_constraint)
+ if ka_random.is_mutating():
+ self.roundness += ka_random.jitter_constrained(roundness_constraint)
+ self.center.mutate()
+ self.sampler.mutate()
+# for direction in self.direction_steps:
+# direction.mutate()
+
+ def shuffle(self):
+ """Shuffle similar components."""
+ super(FilledSpline, self).shuffle()
+ if ka_random.is_shuffling():
+ self.sampler.shuffle()
+# self.direction_steps.shuffle()
+
+ def crossingover(self, other):
+ """
+ pre: isinstance(other, FilledSpline)
+ pre: isinstance(self, FilledSpline)
+ # check for distinct references, needs to copy content, not references
+ post: __return__ is not self
+ post: __return__ is not other
+ """
+ new_one = FilledSpline(self.get_trunk())
+ crossing = self.crossingover_base(new_one, other, 3)
+ new_one.linecolor = self.linecolor.crossingover(other.linecolor)
+ new_one.fillcolor = self.fillcolor.crossingover(other.fillcolor)
+ new_one.line_width = self.line_width if crossing[0] else other.line_width
+ new_one.roundness = self.roundness if crossing[1] else other.roundness
+ new_one.center = self.center if crossing[2] else other.center
+# new_one.direction_steps = ka_random.crossingover(self.direction_steps, \
+# other.direction_steps)
+ new_one.sampler = self.sampler if crossing[1] else other.sampler
+ return new_one
+
+ def render(self, ctx, width, height):
+ """
+ pre: ctx is not None
+ pre: width > 0
+ pre: height > 0
+ pre: width == height
+ """
+ self.begin_render(ctx, width, height)
+
+ px, py = self.center.x_pos, self.center.y_pos
+ ctx.move_to(px, py)
+ points = self.sampler.get_sample_points()
+# position_list = []
+# for step in self.direction_steps:
+# px += step.offset * math.cos(step.radian)
+# py += step.offset * math.sin(step.radian)
+# position_list.append((px, py, step.radian))
+ for index, step_0 in enumerate(points):
+ if index < 2:
+# ctx.line_to(step_0[0], step_0[1])
+ ctx.move_to(px, py)
+ elif index == len(points)-1:
+# ctx.line_to(step_0[0], step_0[1])
+ ctx.move_to(px, py)
+ else:
+ step_p1 = points[index+1]
+ step_m1 = points[index-1]
+ step_m2 = points[index-2]
+ sp_delta_x = self.roundness * (step_m1[0] - step_m2[0])
+ sp_delta_y = self.roundness * (step_m1[1] - step_m2[1])
+ ap_delta_x = step_0[0] - step_m1[0]
+ ap_delta_y = step_0[1] - step_m1[1]
+ np_delta_x = self.roundness * (step_p1[0] - step_0[0])
+ np_delta_y = self.roundness * (step_p1[1] - step_0[1])
+ ctx.rel_curve_to (ap_delta_x-np_delta_x,
+ ap_delta_y-np_delta_y, # end control point
+ sp_delta_x, sp_delta_y, # start control point
+ ap_delta_x, ap_delta_y) # end point
+ rgba = self.fillcolor.rgba
+ ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3])
+ ctx.fill_preserve()
+ ctx.set_line_width(self.line_width)
+ rgba = self.linecolor.rgba
+ ctx.set_source_rgba(rgba[0], rgba[1], rgba[2], rgba[3])
+ ctx.stroke()
+
+ def explain(self, formater):
+# super(FilledSpline, self).explain(formater)
+ formater.color_item(self.linecolor, 'line color:')
+ formater.text_item('line width: ' + str(self.line_width))
+ formater.text_item('roundness: ' + str(self.roundness))
+ formater.color_item(self.fillcolor, 'fill color:')
+ formater.position_item(self.center, 'center:')
+ text, surface, descr = self.sampler.explain()
+ if surface is not None:
+ formater.surface_item(surface, 'sampling points:' + text, descr)
+ else:
+ formater.text_item(text)
+# formater.direction_array(self.direction_steps, 'direction:')
+
+ def copy(self):
+ """The FilledSpline diagram layers copy constructor.
+ # check for distinct references, needs to copy content, not references
+ post: __return__ is not self
+ """
+ new_one = FilledSpline(self.get_trunk())
+ self.copy_base(new_one)
+ new_one.linecolor = self.linecolor.copy()
+ new_one.fillcolor = self.fillcolor.copy()
+ new_one.line_width = self.line_width
+ new_one.roundness = self.roundness
+ new_one.center = self.center.copy()
+# new_one.direction_steps = ka_random.copy_list(self.direction_steps)
+ new_one.sampler = self.sampler.copy()
+ return new_one