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