Blender V4.3
BPy_FrsNoise.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BPy_FrsNoise.h"
10#include "BPy_Convert.h"
11
12#include "../system/RandGen.h"
13
14#include "BLI_sys_types.h"
15
16#include <sstream>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22using namespace Freestyle;
23
25
26//-------------------MODULE INITIALIZATION--------------------------------
27int FrsNoise_Init(PyObject *module)
28{
29 if (module == nullptr) {
30 return -1;
31 }
32
33 if (PyType_Ready(&FrsNoise_Type) < 0) {
34 return -1;
35 }
36 PyModule_AddObjectRef(module, "Noise", (PyObject *)&FrsNoise_Type);
37
38 return 0;
39}
40
41//------------------------INSTANCE METHODS ----------------------------------
42
44 /* Wrap. */
45 FrsNoise_doc,
46 "Class to provide Perlin noise functionalities.\n"
47 "\n"
48 ".. method:: __init__(seed = -1)\n"
49 "\n"
50 " Builds a Noise object. Seed is an optional argument. The seed value is used\n"
51 " as a seed for random number generation if it is equal to or greater than zero;\n"
52 " otherwise, time is used as a seed.\n"
53 "\n"
54 " :arg seed: Seed for random number generation.\n"
55 " :type seed: int");
56
57static int FrsNoise_init(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
58{
59 static const char *kwlist[] = {"seed", nullptr};
60 long seed = -1;
61
62 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|l", (char **)kwlist, &seed)) {
63 return -1;
64 }
65 self->n = new Noise(seed);
66 self->pn = new PseudoNoise();
67 return 0;
68}
69
71{
72 delete self->n;
73 delete self->pn;
74 Py_TYPE(self)->tp_free((PyObject *)self);
75}
76
78{
79 return PyUnicode_FromFormat("Noise - address: %p", self->n);
80}
81
83 /* Wrap. */
84 FrsNoise_turbulence1_doc,
85 ".. method:: turbulence1(v, freq, amp, oct=4)\n"
86 "\n"
87 " Returns a noise value for a 1D element.\n"
88 "\n"
89 " :arg v: One-dimensional sample point.\n"
90 " :type v: float\n"
91 " :arg freq: Noise frequency.\n"
92 " :type freq: float\n"
93 " :arg amp: Amplitude.\n"
94 " :type amp: float\n"
95 " :arg oct: Number of octaves.\n"
96 " :type oct: int\n"
97 " :return: A noise value.\n"
98 " :rtype: float");
99
100static PyObject *FrsNoise_drand(BPy_FrsNoise * /*self*/, PyObject *args, PyObject *kwds)
101{
102 static const char *kwlist[] = {"seed", nullptr};
103 long seed = 0;
104 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|I", (char **)kwlist, &seed)) {
105 PyErr_SetString(PyExc_TypeError, "optional argument 1 must be of type int");
106 return nullptr;
107 }
108 if (seed) {
110 }
111 return PyFloat_FromDouble(RandGen::drand48());
112}
113
114static PyObject *FrsNoise_turbulence_smooth(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
115{
116 static const char *kwlist[] = {"v", "oct", nullptr};
117
118 double x; // NOTE: this has to be a double (not float)
119 uint nbOctaves = 8;
120
121 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|I", (char **)kwlist, &x, &nbOctaves)) {
122 return nullptr;
123 }
124 return PyFloat_FromDouble(self->pn->turbulenceSmooth(x, nbOctaves));
125}
126
127static PyObject *FrsNoise_turbulence1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
128{
129 static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
130 float f1, f2, f3;
131 uint i = 4;
132
133 if (!PyArg_ParseTupleAndKeywords(args, kwds, "fff|I", (char **)kwlist, &f1, &f2, &f3, &i)) {
134 return nullptr;
135 }
136 return PyFloat_FromDouble(self->n->turbulence1(f1, f2, f3, i));
137}
138
140 /* Wrap. */
141 FrsNoise_turbulence2_doc,
142 ".. method:: turbulence2(v, freq, amp, oct=4)\n"
143 "\n"
144 " Returns a noise value for a 2D element.\n"
145 "\n"
146 " :arg v: Two-dimensional sample point.\n"
147 " :type v: :class:`mathutils.Vector` | tuple[float, float] | list[float]\n"
148 " :arg freq: Noise frequency.\n"
149 " :type freq: float\n"
150 " :arg amp: Amplitude.\n"
151 " :type amp: float\n"
152 " :arg oct: Number of octaves.\n"
153 " :type oct: int\n"
154 " :return: A noise value.\n"
155 " :rtype: float");
156
157static PyObject *FrsNoise_turbulence2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
158{
159 static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
160 PyObject *obj1;
161 float f2, f3;
162 uint i = 4;
163 Vec2f vec;
164
165 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Off|I", (char **)kwlist, &obj1, &f2, &f3, &i)) {
166 return nullptr;
167 }
168 if (!Vec2f_ptr_from_PyObject(obj1, vec)) {
169 PyErr_SetString(PyExc_TypeError,
170 "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
171 return nullptr;
172 }
173 float t = self->n->turbulence2(vec, f2, f3, i);
174 return PyFloat_FromDouble(t);
175}
176
178 /* Wrap. */
179 FrsNoise_turbulence3_doc,
180 ".. method:: turbulence3(v, freq, amp, oct=4)\n"
181 "\n"
182 " Returns a noise value for a 3D element.\n"
183 "\n"
184 " :arg v: Three-dimensional sample point.\n"
185 " :type v: :class:`mathutils.Vector` | tuple[float, float, float] | list[float]\n"
186 " :arg freq: Noise frequency.\n"
187 " :type freq: float\n"
188 " :arg amp: Amplitude.\n"
189 " :type amp: float\n"
190 " :arg oct: Number of octaves.\n"
191 " :type oct: int\n"
192 " :return: A noise value.\n"
193 " :rtype: float");
194
195static PyObject *FrsNoise_turbulence3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
196{
197 static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
198 PyObject *obj1;
199 float f2, f3;
200 uint i = 4;
201 Vec3f vec;
202
203 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Off|I", (char **)kwlist, &obj1, &f2, &f3, &i)) {
204 return nullptr;
205 }
206 if (!Vec3f_ptr_from_PyObject(obj1, vec)) {
207 PyErr_SetString(PyExc_TypeError,
208 "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
209 return nullptr;
210 }
211 float t = self->n->turbulence3(vec, f2, f3, i);
212 return PyFloat_FromDouble(t);
213}
214
216 /* Wrap. */
217 FrsNoise_smoothNoise1_doc,
218 ".. method:: smoothNoise1(v)\n"
219 "\n"
220 " Returns a smooth noise value for a 1D element.\n"
221 "\n"
222 " :arg v: One-dimensional sample point.\n"
223 " :type v: float\n"
224 " :return: A smooth noise value.\n"
225 " :rtype: float");
226
227static PyObject *FrsNoise_smoothNoise1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
228{
229 static const char *kwlist[] = {"v", nullptr};
230 float f;
231
232 if (!PyArg_ParseTupleAndKeywords(args, kwds, "f", (char **)kwlist, &f)) {
233 return nullptr;
234 }
235 return PyFloat_FromDouble(self->n->smoothNoise1(f));
236}
237
239 /* Wrap. */
240 FrsNoise_smoothNoise2_doc,
241 ".. method:: smoothNoise2(v)\n"
242 "\n"
243 " Returns a smooth noise value for a 2D element.\n"
244 "\n"
245 " :arg v: Two-dimensional sample point.\n"
246 " :type v: :class:`mathutils.Vector` | tuple[float, float] | list[float]\n"
247 " :return: A smooth noise value.\n"
248 " :rtype: float");
249
250static PyObject *FrsNoise_smoothNoise2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
251{
252 static const char *kwlist[] = {"v", nullptr};
253 PyObject *obj;
254 Vec2f vec;
255
256 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &obj)) {
257 return nullptr;
258 }
259 if (!Vec2f_ptr_from_PyObject(obj, vec)) {
260 PyErr_SetString(PyExc_TypeError,
261 "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
262 return nullptr;
263 }
264 float t = self->n->smoothNoise2(vec);
265 return PyFloat_FromDouble(t);
266}
267
269 /* Wrap. */
270 FrsNoise_smoothNoise3_doc,
271 ".. method:: smoothNoise3(v)\n"
272 "\n"
273 " Returns a smooth noise value for a 3D element.\n"
274 "\n"
275 " :arg v: Three-dimensional sample point.\n"
276 " :type v: :class:`mathutils.Vector` | tuple[float, float, float] | list[float]\n"
277 " :return: A smooth noise value.\n"
278 " :rtype: float");
279
280static PyObject *FrsNoise_smoothNoise3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
281{
282 static const char *kwlist[] = {"v", nullptr};
283 PyObject *obj;
284 Vec3f vec;
285
286 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &obj)) {
287 return nullptr;
288 }
289 if (!Vec3f_ptr_from_PyObject(obj, vec)) {
290 PyErr_SetString(PyExc_TypeError,
291 "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
292 return nullptr;
293 }
294 float t = self->n->smoothNoise3(vec);
295 return PyFloat_FromDouble(t);
296}
297
298static PyMethodDef BPy_FrsNoise_methods[] = {
299 {"turbulence1",
300 (PyCFunction)FrsNoise_turbulence1,
301 METH_VARARGS | METH_KEYWORDS,
302 FrsNoise_turbulence1_doc},
303 {"turbulence2",
304 (PyCFunction)FrsNoise_turbulence2,
305 METH_VARARGS | METH_KEYWORDS,
306 FrsNoise_turbulence2_doc},
307 {"turbulence3",
308 (PyCFunction)FrsNoise_turbulence3,
309 METH_VARARGS | METH_KEYWORDS,
310 FrsNoise_turbulence3_doc},
311 {"smoothNoise1",
312 (PyCFunction)FrsNoise_smoothNoise1,
313 METH_VARARGS | METH_KEYWORDS,
314 FrsNoise_smoothNoise1_doc},
315 {"smoothNoise2",
316 (PyCFunction)FrsNoise_smoothNoise2,
317 METH_VARARGS | METH_KEYWORDS,
318 FrsNoise_smoothNoise2_doc},
319 {"smoothNoise3",
320 (PyCFunction)FrsNoise_smoothNoise3,
321 METH_VARARGS | METH_KEYWORDS,
322 FrsNoise_smoothNoise3_doc},
323 {"rand", (PyCFunction)FrsNoise_drand, METH_VARARGS | METH_KEYWORDS, nullptr},
324 {"turbulence_smooth",
325 (PyCFunction)FrsNoise_turbulence_smooth,
326 METH_VARARGS | METH_KEYWORDS,
327 nullptr},
328 {nullptr, nullptr, 0, nullptr},
329};
330
331/*-----------------------BPy_FrsNoise type definition ------------------------------*/
332
333PyTypeObject FrsNoise_Type = {
334 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
335 /*tp_name*/ "Noise",
336 /*tp_basicsize*/ sizeof(BPy_FrsNoise),
337 /*tp_itemsize*/ 0,
338 /*tp_dealloc*/ (destructor)FrsNoise_dealloc,
339 /*tp_vectorcall_offset*/ 0,
340 /*tp_getattr*/ nullptr,
341 /*tp_setattr*/ nullptr,
342 /*tp_as_async*/ nullptr,
343 /*tp_repr*/ (reprfunc)FrsNoise_repr,
344 /*tp_as_number*/ nullptr,
345 /*tp_as_sequence*/ nullptr,
346 /*tp_as_mapping*/ nullptr,
347 /*tp_hash*/ nullptr,
348 /*tp_call*/ nullptr,
349 /*tp_str*/ nullptr,
350 /*tp_getattro*/ nullptr,
351 /*tp_setattro*/ nullptr,
352 /*tp_as_buffer*/ nullptr,
353 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
354 /*tp_doc*/ FrsNoise_doc,
355 /*tp_traverse*/ nullptr,
356 /*tp_clear*/ nullptr,
357 /*tp_richcompare*/ nullptr,
358 /*tp_weaklistoffset*/ 0,
359 /*tp_iter*/ nullptr,
360 /*tp_iternext*/ nullptr,
361 /*tp_methods*/ BPy_FrsNoise_methods,
362 /*tp_members*/ nullptr,
363 /*tp_getset*/ nullptr,
364 /*tp_base*/ nullptr,
365 /*tp_dict*/ nullptr,
366 /*tp_descr_get*/ nullptr,
367 /*tp_descr_set*/ nullptr,
368 /*tp_dictoffset*/ 0,
369 /*tp_init*/ (initproc)FrsNoise_init,
370 /*tp_alloc*/ nullptr,
371 /*tp_new*/ PyType_GenericNew,
372};
373
375
376#ifdef __cplusplus
377}
378#endif
unsigned int uint
bool Vec3f_ptr_from_PyObject(PyObject *obj, Vec3f &vec)
bool Vec2f_ptr_from_PyObject(PyObject *obj, Vec2f &vec)
static PyObject * FrsNoise_repr(BPy_FrsNoise *self)
static PyObject * FrsNoise_turbulence2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static void FrsNoise_dealloc(BPy_FrsNoise *self)
int FrsNoise_Init(PyObject *module)
static PyObject * FrsNoise_turbulence_smooth(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_smoothNoise3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
PyDoc_STRVAR(FrsNoise_doc, "Class to provide Perlin noise functionalities.\n" "\n" ".. method:: __init__(seed = -1)\n" "\n" " Builds a Noise object. Seed is an optional argument. The seed value is used\n" " as a seed for random number generation if it is equal to or greater than zero;\n" " otherwise, time is used as a seed.\n" "\n" " :arg seed: Seed for random number generation.\n" " :type seed: int")
static PyObject * FrsNoise_turbulence3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
PyTypeObject FrsNoise_Type
static PyObject * FrsNoise_drand(BPy_FrsNoise *, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_smoothNoise1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_turbulence1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_smoothNoise2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyMethodDef BPy_FrsNoise_methods[]
static int FrsNoise_init(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
Pseudo-random number generator.
PyObject * self
static unsigned long seed
Definition btSoftBody.h:39
static void srand48(long seedval)
Definition RandGen.cpp:101
static real drand48()
Definition RandGen.cpp:94
inherits from class Rep
Definition AppCanvas.cpp:20
static uint x[3]
Definition RandGen.cpp:77
static struct PyModuleDef module
Definition python.cpp:991