From eae3539ee280a417c57fab6236fc6125cf70b066 Mon Sep 17 00:00:00 2001 From: Ilya Portnov Date: Fri, 9 Jan 2015 20:03:54 +0500 Subject: [PATCH 1/2] Implement Fibonacci sequence node. refs #517. --- menu.py | 1 + nodes/__init__.py | 1 + nodes/number/fibonacci.py | 100 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 nodes/number/fibonacci.py diff --git a/menu.py b/menu.py index 35c5b401f..5796346cf 100644 --- a/menu.py +++ b/menu.py @@ -225,6 +225,7 @@ def make_node_cats(): ["SvFormulaShapeNode", "Formula shape", "IPO"], ["SvScriptNodeMK2", "Script 2"], ['SvNGonNode', 'NGon', 'RNDCURVE'], + ["SvGenFibonacci", "Fibonacci sequence"], ] node_cats["Alpha Nodes"] = [ diff --git a/nodes/__init__.py b/nodes/__init__.py index a6412e1bf..c0b0c1057 100644 --- a/nodes/__init__.py +++ b/nodes/__init__.py @@ -148,6 +148,7 @@ nodes_dict = { 'range_map', 'range_float', 'range_int', + 'fibonacci', 'logic' ], diff --git a/nodes/number/fibonacci.py b/nodes/number/fibonacci.py new file mode 100644 index 000000000..72e1ab7bd --- /dev/null +++ b/nodes/number/fibonacci.py @@ -0,0 +1,100 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# 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 2 +# 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +import bpy +from bpy.props import IntProperty, FloatProperty + +from sverchok.node_tree import SverchCustomTreeNode +from sverchok.data_structure import updateNode, match_long_repeat + +def fibonacci(x1, x2, count, maxValue): + result = [x1,x2] + for i in range(count-2): + r = x1 + x2 + result.append(r) + x1 = x2 + x2 = r + + if maxValue: + actualMax = max(map(abs, result)) + if actualMax == 0.0: + return result + result = [x*maxValue/actualMax for x in result] + + return result + + +class SvGenFibonacci(bpy.types.Node, SverchCustomTreeNode): + ''' Generator range list of floats''' + bl_idname = 'SvGenFibonacci' + bl_label = 'Fibonacci series' + bl_icon = 'OUTLINER_OB_EMPTY' + + x1_ = FloatProperty( + name='x1', description='First sequence value', + default=1.0, + options={'ANIMATABLE'}, update=updateNode) + + x2_ = FloatProperty( + name='x2', description='Second sequence value', + default=1.0, + options={'ANIMATABLE'}, update=updateNode) + + count_ = IntProperty( + name='count', description='Number of items to generate', + default=10, + options={'ANIMATABLE'}, min=3, update=updateNode) + + maxValue_ = FloatProperty( + name='max', description='Maximum (absolute) value', + default=0.0, min=0.0, + options={'ANIMATABLE'}, update=updateNode) + + def sv_init(self, context): + self.inputs.new('StringsSocket', "X1").prop_name = 'x1_' + self.inputs.new('StringsSocket', "X2").prop_name = 'x2_' + self.inputs.new('StringsSocket', "Count").prop_name = 'count_' + self.inputs.new('StringsSocket', "Maximum").prop_name = 'maxValue_' + + self.outputs.new('StringsSocket', "Sequence") + + def process(self): + # inputs + x1 = self.inputs['X1'].sv_get()[0] + x2 = self.inputs['X2'].sv_get()[0] + count = self.inputs['Count'].sv_get()[0] + m = self.inputs['Maximum'].sv_get(default=None) + if m is None: + maxValue = None + else: + maxValue = m[0] + + if not self.outputs['Sequence'].is_linked: + return + + parameters = match_long_repeat([x1,x2, count, maxValue]) + result = [list(fibonacci(*args)) for args in zip(*parameters)] + self.outputs['Sequence'].sv_set(result) + + +def register(): + bpy.utils.register_class(SvGenFibonacci) + + +def unregister(): + bpy.utils.unregister_class(SvGenFibonacci) -- GitLab From a2387cbe632c4f1c6787ae2ecb10f0cc48150618 Mon Sep 17 00:00:00 2001 From: Ilya Portnov Date: Sat, 10 Jan 2015 18:15:43 +0500 Subject: [PATCH 2/2] Documentation on Fibonacci node. Update fibonacci.rst --- docs/nodes/beta/beta.rst | 1 + docs/nodes/beta/fibonacci.rst | 57 +++++++++++++++++++++++++++++++++++ menu.py | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 docs/nodes/beta/fibonacci.rst diff --git a/docs/nodes/beta/beta.rst b/docs/nodes/beta/beta.rst index cc71b8e64..806ce36e9 100644 --- a/docs/nodes/beta/beta.rst +++ b/docs/nodes/beta/beta.rst @@ -6,5 +6,6 @@ Beta :maxdepth: 2 image_decompose + fibonacci ngon diff --git a/docs/nodes/beta/fibonacci.rst b/docs/nodes/beta/fibonacci.rst new file mode 100644 index 000000000..2398d6e21 --- /dev/null +++ b/docs/nodes/beta/fibonacci.rst @@ -0,0 +1,57 @@ +Fibonacci Sequence +================== + +*destination after Beta: Number* + +Functionality +------------- + +This node produces specified number of items from Fibonacci sequence:: + + 1, 1, 2, 3, 5, 8, 13, 21 ... + +Each next item is sum of two previous. + +This node allows you to specify first two items for your sequence. Note that these numbers can be even negative. + +Sequence can be re-scaled so that maximum of absolute values of produced items will be equal to specified value. + +Inputs & Parameters +------------------- + +All parameters can be given by the node or an external input. +This node has the following parameters: + ++----------------+---------------+-------------+----------------------------------------------------+ +| Parameter | Type | Default | Description | ++================+===============+=============+====================================================+ +| **X1** | Float | 1.0 | First item of sequence. | ++----------------+---------------+-------------+----------------------------------------------------+ +| **X2** | Float | 1.0 | Second item of sequence. | ++----------------+---------------+-------------+----------------------------------------------------+ +| **Count** | Int | 10 | Number of items to produce. Minimal value is 3. | ++----------------+---------------+-------------+----------------------------------------------------+ +| **Max** | Float | 0.0 | If non-zero, then all output sequence will be | +| | | | re-scaled so that maximum of absolute values will | +| | | | be equal to number specified. | ++----------------+---------------+-------------+----------------------------------------------------+ + +Outputs +------- + +This node has one output: **Sequence**. + +Inputs and outputs are vectorized, so if series of values is passed to one of +inputs, then this node will produce several sequences. + +Example of usage +---------------- + +Given simplest nodes setup: + +.. image:: https://cloud.githubusercontent.com/assets/284644/5691665/22a8bc0e-98f5-11e4-9ecc-924addf22178.png + +you will have something like: + +.. image:: https://cloud.githubusercontent.com/assets/284644/5691664/227c9d0e-98f5-11e4-87c9-fb6f552e1b89.png + diff --git a/menu.py b/menu.py index 5796346cf..3a4c29bdf 100644 --- a/menu.py +++ b/menu.py @@ -224,8 +224,8 @@ def make_node_cats(): ["SvWafelNode", "Wafel"], ["SvFormulaShapeNode", "Formula shape", "IPO"], ["SvScriptNodeMK2", "Script 2"], - ['SvNGonNode', 'NGon', 'RNDCURVE'], ["SvGenFibonacci", "Fibonacci sequence"], + ['SvNGonNode', 'NGon', 'RNDCURVE'], ] node_cats["Alpha Nodes"] = [ -- GitLab