Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
random.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Inkscape::LivePathEffectParameters
4 *
5 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
6 *
7 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
8 */
9
10#include "random.h"
11
12#include <glibmm/i18n.h>
13#include <glibmm/ustring.h>
14
15#include "live_effects/effect.h"
16#include "svg/stringstream.h"
17#include "svg/svg.h"
18#include "ui/icon-names.h"
19
20#define noLPERANDOMPARAM_DEBUG
21
22/* RNG stolen from /display/nr-filter-turbulence.cpp */
23static constexpr int RAND_m = 2147483647; // 2**31 - 1
24static constexpr int RAND_a = 16807; // 7**5; primitive root of m
25static constexpr int RAND_q = 127773; // m / a
26static constexpr int RAND_r = 2836; // m % a
27static constexpr int BSize = 0x100;
28
30
31RandomParam::RandomParam(const Glib::ustring& label, const Glib::ustring& tip,
32 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
33 Effect* effect, gdouble default_value, long default_seed, bool randomsign)
34 : Parameter(label, tip, key, wr, effect)
35{
36 defvalue = default_value;
40 integer = false;
41
42 defseed = default_seed;
45 _randomsign = randomsign;
46}
47
48bool
49RandomParam::param_readSVGValue(const gchar * strvalue)
50{
51 double newval, newstartseed;
52 gchar** stringarray = g_strsplit (strvalue, ";", 2);
53 unsigned int success = sp_svg_number_read_d(stringarray[0], &newval);
54 if (success == 1) {
55 success += sp_svg_number_read_d(stringarray[1], &newstartseed);
56 if (success == 2) {
57 param_set_value(newval, static_cast<long>(newstartseed));
58 } else {
59 param_set_value(newval, defseed);
60 }
61 g_strfreev(stringarray);
62 return true;
63 }
64 g_strfreev(stringarray);
65 return false;
66}
67
68Glib::ustring
70{
72 os << value << ';' << startseed;
73 return os.str();
74}
75
76Glib::ustring
78{
80 os << defvalue << ';' << defseed;
81 return os.str();
82}
83
84void
89
90void
91RandomParam::param_update_default(gdouble default_value){
92 defvalue = default_value;
93}
94
95void
96RandomParam::param_update_default(const gchar * default_value){
97 double newval;
98 unsigned int success = sp_svg_number_read_d(default_value, &newval);
99 if (success == 1) {
100 param_update_default(newval);
101 }
102}
103
104void
105RandomParam::param_set_value(gdouble val, long newseed)
106{
107 value = val;
108 if (integer)
109 value = round(value);
110 if (value > max)
111 value = max;
112 if (value < min)
113 value = min;
114
115 startseed = setup_seed(newseed);
116 // we reach maximum value so randomize over to fix duple in next cycle
117 Glib::ustring version = param_effect->lpeversion.param_getSVGValue();
118 if (startseed == RAND_m - 1 && ((
120 effectType() != ROUGHEN) ||
121 version >= "1.2"))
122 {
124 }
125 seed = startseed;
126}
127
128void
129RandomParam::param_set_range(gdouble min, gdouble max)
130{
131 // if you look at client code, you'll see that many effects
132 // has a tendency to set an upper range of Geom::infinity().
133 // Once again, in gtk2, this is not a problem. But in gtk3,
134 // widgets get allocated the amount of size they ask for,
135 // leading to excessively long widgets.
137 this->min = min;
138 } else {
139 this->min = -SCALARPARAM_G_MAXDOUBLE;
140 }
142 this->max = max;
143 } else {
144 this->max = SCALARPARAM_G_MAXDOUBLE;
145 }
146}
147
148void
150{
151 integer = yes;
152}
153
154void
159
160Gtk::Widget *
162{
163 auto const regrandom = Gtk::make_managed<UI::Widget::RegisteredRandom>( param_label,
165 param_key,
166 *param_wr,
169 regrandom->setValue(value, startseed);
170 if (integer) {
171 regrandom->setDigits(0);
172 regrandom->setIncrements(1, 10);
173 }
174 regrandom->setRange(min, max);
175 regrandom->setProgrammatically = false;
176 regrandom->signal_value_changed().connect(sigc::mem_fun(*this, &RandomParam::on_value_changed));
177 regrandom->set_undo_parameters(_("Change random parameter"), INKSCAPE_ICON("dialog-path-effects"));
178 return regrandom;
179}
180
185
186RandomParam::operator gdouble()
187{
188 if (_randomsign) {
189 return (rand() * value) - (rand() * value);
190 } else {
191 return rand() * value;
192 }
193}
194
195long
197{
198 if (lSeed <= 0) lSeed = -(lSeed % (RAND_m - 1)) + 1;
199 if (lSeed > RAND_m - 1) lSeed = RAND_m - 1;
200 return lSeed;
201}
202
203// generates random number between 0 and 1
204gdouble
206{
207 long result;
208 result = RAND_a * (seed % RAND_q) - RAND_r * (seed / RAND_q);
209 if (result <= 0) result += RAND_m;
210 seed = result;
211
212 gdouble dresult = (gdouble)(result % BSize) / BSize;
213 return dresult;
214}
215
216} // namespace Inkscape::LivePathEffect
217
218/*
219 Local Variables:
220 mode:c++
221 c-file-style:"stroustrup"
222 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223 indent-tabs-mode:nil
224 fill-column:99
225 End:
226*/
227// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
Inkscape::XML::Node * getRepr()
Definition effect.cpp:1928
Glib::ustring param_getSVGValue() const override
Definition hidden.cpp:53
Inkscape::UI::Widget::Registry * param_wr
Definition parameter.h:99
RandomParam(const Glib::ustring &label, const Glib::ustring &tip, const Glib::ustring &key, Inkscape::UI::Widget::Registry *wr, Effect *effect, gdouble default_value=1.0, long default_seed=0, bool randomsign=false)
Definition random.cpp:31
void param_set_value(gdouble val, long newseed)
Definition random.cpp:105
Gtk::Widget * param_newWidget() override
Definition random.cpp:161
void param_make_integer(bool yes=true)
Definition random.cpp:149
bool param_readSVGValue(const gchar *strvalue) override
Definition random.cpp:49
Glib::ustring param_getDefaultSVGValue() const override
Definition random.cpp:77
void param_set_range(gdouble min, gdouble max)
Definition random.cpp:129
Glib::ustring param_getSVGValue() const override
Definition random.cpp:69
void param_update_default(gdouble default_value)
Definition random.cpp:91
std::string str() const
Css & result
Macro for icon names used in Inkscape.
constexpr double SCALARPARAM_G_MAXDOUBLE
Definition parameter.h:27
static constexpr int RAND_a
Definition random.cpp:24
static constexpr int RAND_r
Definition random.cpp:26
static constexpr int RAND_m
Definition random.cpp:23
static constexpr int BSize
Definition random.cpp:27
static constexpr int RAND_q
Definition random.cpp:25
Glib::ustring label
Live Path Effects code.
static cairo_user_data_key_t key
unsigned int sp_svg_number_read_d(gchar const *str, double *val)
TODO: insert short description here.