summaryrefslogtreecommitdiffstats
path: root/share/extensions/jitternodes.py
blob: 7fcc69d0d683d9293c186586991d3dcc6c4d92ec (plain)
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
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2012 Juan Pablo Carbajal ajuanpi-dev@gmail.com
# Copyright (C) 2005 Aaron Spike, aaron@ekips.org
#
# 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

import math
import random
import inkex


class JitterNodes(inkex.EffectExtension):
    """Jiggle nodes around"""

    def add_arguments(self, pars):
        pars.add_argument("--tab")
        pars.add_argument("--radiusx", type=float, default=10.0, help="Randum radius X")
        pars.add_argument("--radiusy", type=float, default=10.0, help="Randum radius Y")
        pars.add_argument(
            "--ctrl", type=inkex.Boolean, default=False, help="Randomize ctrl points"
        )
        pars.add_argument(
            "--end", type=inkex.Boolean, default=True, help="Randomize nodes"
        )
        pars.add_argument(
            "--dist",
            type=self.arg_method("dist"),
            default=self.dist_uniform,
            help="Distribution of displacement",
        )

    def effect(self):
        for node in self.svg.selection.filter(inkex.PathElement):
            path = node.path.to_superpath()
            for subpath in path:
                closed = subpath[0] == subpath[-1]
                for index, csp in enumerate(subpath):
                    if closed and index == len(subpath) - 1:
                        subpath[index] = subpath[0]
                        break
                    if self.options.end:
                        delta = self.randomize([0, 0])
                        csp[0][0] += delta[0]
                        csp[0][1] += delta[1]
                        csp[1][0] += delta[0]
                        csp[1][1] += delta[1]
                        csp[2][0] += delta[0]
                        csp[2][1] += delta[1]
                    if self.options.ctrl:
                        csp[0] = self.randomize(csp[0])
                        csp[2] = self.randomize(csp[2])
            node.path = path

    def randomize(self, pos):
        """Randomise the given position [x, y] as set in the options"""
        delta = self.options.dist(self.options.radiusx, self.options.radiusy)
        return [pos[0] + delta[0], pos[1] + delta[1]]

    @staticmethod
    def dist_gaussian(x, y):
        """Gaussian distribution"""
        return random.gauss(0.0, x), random.gauss(0.0, y)

    @staticmethod
    def dist_pareto(x, y):
        """Pareto distribution"""
        # sign is used to fake a double sided pareto distribution.
        # for parameter value between 1 and 2 the distribution has infinite variance
        # I truncate the distribution to a high value and then normalize it.
        # The idea is to get spiky distributions, any distribution with long-tails is
        # good (ideal would be Levy distribution).
        sign = random.uniform(-1.0, 1.0)
        return x * math.copysign(
            min(random.paretovariate(1.0), 20.0) / 20.0, sign
        ), y * math.copysign(min(random.paretovariate(1.0), 20.0) / 20.0, sign)

    @staticmethod
    def dist_lognorm(x, y):
        """Log Norm distribution"""
        sign = random.uniform(-1.0, 1.0)
        return x * math.copysign(
            random.lognormvariate(0.0, 1.0) / 3.5, sign
        ), y * math.copysign(random.lognormvariate(0.0, 1.0) / 3.5, sign)

    @staticmethod
    def dist_uniform(x, y):
        """Uniform distribution"""
        return random.uniform(-x, x), random.uniform(-y, y)


if __name__ == "__main__":
    JitterNodes().run()