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