Blender  V3.3
BPy_FrsNoise.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BPy_FrsNoise.h"
8 #include "BPy_Convert.h"
9 
10 #include "../system/RandGen.h"
11 
12 #include <sstream>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 using namespace Freestyle;
19 
21 
22 //-------------------MODULE INITIALIZATION--------------------------------
23 int 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  Py_INCREF(&FrsNoise_Type);
33  PyModule_AddObject(module, "Noise", (PyObject *)&FrsNoise_Type);
34 
35  return 0;
36 }
37 
38 //------------------------INSTANCE METHODS ----------------------------------
39 
40 PyDoc_STRVAR(FrsNoise_doc,
41  "Class to provide Perlin noise functionalities.\n"
42  "\n"
43  ".. method:: __init__(seed = -1)\n"
44  "\n"
45  " Builds a Noise object. Seed is an optional argument. The seed value is used\n"
46  " as a seed for random number generation if it is equal to or greater than zero;\n"
47  " otherwise, time is used as a seed.\n"
48  "\n"
49  " :arg seed: Seed for random number generation.\n"
50  " :type seed: int");
51 
52 static 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 
65 static void FrsNoise_dealloc(BPy_FrsNoise *self)
66 {
67  delete self->n;
68  delete self->pn;
69  Py_TYPE(self)->tp_free((PyObject *)self);
70 }
71 
72 static PyObject *FrsNoise_repr(BPy_FrsNoise *self)
73 {
74  return PyUnicode_FromFormat("Noise - address: %p", self->n);
75 }
76 
77 PyDoc_STRVAR(FrsNoise_turbulence1_doc,
78  ".. method:: turbulence1(v, freq, amp, oct=4)\n"
79  "\n"
80  " Returns a noise value for a 1D element.\n"
81  "\n"
82  " :arg v: One-dimensional sample point.\n"
83  " :type v: float\n"
84  " :arg freq: Noise frequency.\n"
85  " :type freq: float\n"
86  " :arg amp: Amplitude.\n"
87  " :type amp: float\n"
88  " :arg oct: Number of octaves.\n"
89  " :type oct: int\n"
90  " :return: A noise value.\n"
91  " :rtype: float");
92 
93 static PyObject *FrsNoise_drand(BPy_FrsNoise * /*self*/, PyObject *args, PyObject *kwds)
94 {
95  static const char *kwlist[] = {"seed", nullptr};
96  long seed = 0;
97  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|I", (char **)kwlist, &seed)) {
98  PyErr_SetString(PyExc_TypeError, "optional argument 1 must be of type int");
99  return nullptr;
100  }
101  if (seed) {
103  }
104  return PyFloat_FromDouble(RandGen::drand48());
105 }
106 
107 static PyObject *FrsNoise_turbulence_smooth(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
108 {
109  static const char *kwlist[] = {"v", "oct", nullptr};
110 
111  double x; // NOTE: this has to be a double (not float)
112  unsigned nbOctaves = 8;
113 
114  if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|I", (char **)kwlist, &x, &nbOctaves)) {
115  return nullptr;
116  }
117  return PyFloat_FromDouble(self->pn->turbulenceSmooth(x, nbOctaves));
118 }
119 
120 static PyObject *FrsNoise_turbulence1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
121 {
122  static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
123  float f1, f2, f3;
124  unsigned int i = 4;
125 
126  if (!PyArg_ParseTupleAndKeywords(args, kwds, "fff|I", (char **)kwlist, &f1, &f2, &f3, &i)) {
127  return nullptr;
128  }
129  return PyFloat_FromDouble(self->n->turbulence1(f1, f2, f3, i));
130 }
131 
132 PyDoc_STRVAR(FrsNoise_turbulence2_doc,
133  ".. method:: turbulence2(v, freq, amp, oct=4)\n"
134  "\n"
135  " Returns a noise value for a 2D element.\n"
136  "\n"
137  " :arg v: Two-dimensional sample point.\n"
138  " :type v: :class:`mathutils.Vector`, list or tuple of 2 real numbers\n"
139  " :arg freq: Noise frequency.\n"
140  " :type freq: float\n"
141  " :arg amp: Amplitude.\n"
142  " :type amp: float\n"
143  " :arg oct: Number of octaves.\n"
144  " :type oct: int\n"
145  " :return: A noise value.\n"
146  " :rtype: float");
147 
148 static PyObject *FrsNoise_turbulence2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
149 {
150  static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
151  PyObject *obj1;
152  float f2, f3;
153  unsigned int i = 4;
154  Vec2f vec;
155 
156  if (!PyArg_ParseTupleAndKeywords(args, kwds, "Off|I", (char **)kwlist, &obj1, &f2, &f3, &i)) {
157  return nullptr;
158  }
159  if (!Vec2f_ptr_from_PyObject(obj1, vec)) {
160  PyErr_SetString(PyExc_TypeError,
161  "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
162  return nullptr;
163  }
164  float t = self->n->turbulence2(vec, f2, f3, i);
165  return PyFloat_FromDouble(t);
166 }
167 
168 PyDoc_STRVAR(FrsNoise_turbulence3_doc,
169  ".. method:: turbulence3(v, freq, amp, oct=4)\n"
170  "\n"
171  " Returns a noise value for a 3D element.\n"
172  "\n"
173  " :arg v: Three-dimensional sample point.\n"
174  " :type v: :class:`mathutils.Vector`, list or tuple of 3 real numbers\n"
175  " :arg freq: Noise frequency.\n"
176  " :type freq: float\n"
177  " :arg amp: Amplitude.\n"
178  " :type amp: float\n"
179  " :arg oct: Number of octaves.\n"
180  " :type oct: int\n"
181  " :return: A noise value.\n"
182  " :rtype: float");
183 
184 static PyObject *FrsNoise_turbulence3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
185 {
186  static const char *kwlist[] = {"v", "freq", "amp", "oct", nullptr};
187  PyObject *obj1;
188  float f2, f3;
189  unsigned int i = 4;
190  Vec3f vec;
191 
192  if (!PyArg_ParseTupleAndKeywords(args, kwds, "Off|I", (char **)kwlist, &obj1, &f2, &f3, &i)) {
193  return nullptr;
194  }
195  if (!Vec3f_ptr_from_PyObject(obj1, vec)) {
196  PyErr_SetString(PyExc_TypeError,
197  "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
198  return nullptr;
199  }
200  float t = self->n->turbulence3(vec, f2, f3, i);
201  return PyFloat_FromDouble(t);
202 }
203 
204 PyDoc_STRVAR(FrsNoise_smoothNoise1_doc,
205  ".. method:: smoothNoise1(v)\n"
206  "\n"
207  " Returns a smooth noise value for a 1D element.\n"
208  "\n"
209  " :arg v: One-dimensional sample point.\n"
210  " :type v: float\n"
211  " :return: A smooth noise value.\n"
212  " :rtype: float");
213 
214 static PyObject *FrsNoise_smoothNoise1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
215 {
216  static const char *kwlist[] = {"v", nullptr};
217  float f;
218 
219  if (!PyArg_ParseTupleAndKeywords(args, kwds, "f", (char **)kwlist, &f)) {
220  return nullptr;
221  }
222  return PyFloat_FromDouble(self->n->smoothNoise1(f));
223 }
224 
225 PyDoc_STRVAR(FrsNoise_smoothNoise2_doc,
226  ".. method:: smoothNoise2(v)\n"
227  "\n"
228  " Returns a smooth noise value for a 2D element.\n"
229  "\n"
230  " :arg v: Two-dimensional sample point.\n"
231  " :type v: :class:`mathutils.Vector`, list or tuple of 2 real numbers\n"
232  " :return: A smooth noise value.\n"
233  " :rtype: float");
234 
235 static PyObject *FrsNoise_smoothNoise2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
236 {
237  static const char *kwlist[] = {"v", nullptr};
238  PyObject *obj;
239  Vec2f vec;
240 
241  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &obj)) {
242  return nullptr;
243  }
244  if (!Vec2f_ptr_from_PyObject(obj, vec)) {
245  PyErr_SetString(PyExc_TypeError,
246  "argument 1 must be a 2D vector (either a list of 2 elements or Vector)");
247  return nullptr;
248  }
249  float t = self->n->smoothNoise2(vec);
250  return PyFloat_FromDouble(t);
251 }
252 
253 PyDoc_STRVAR(FrsNoise_smoothNoise3_doc,
254  ".. method:: smoothNoise3(v)\n"
255  "\n"
256  " Returns a smooth noise value for a 3D element.\n"
257  "\n"
258  " :arg v: Three-dimensional sample point.\n"
259  " :type v: :class:`mathutils.Vector`, list or tuple of 3 real numbers\n"
260  " :return: A smooth noise value.\n"
261  " :rtype: float");
262 
263 static PyObject *FrsNoise_smoothNoise3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
264 {
265  static const char *kwlist[] = {"v", nullptr};
266  PyObject *obj;
267  Vec3f vec;
268 
269  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &obj)) {
270  return nullptr;
271  }
272  if (!Vec3f_ptr_from_PyObject(obj, vec)) {
273  PyErr_SetString(PyExc_TypeError,
274  "argument 1 must be a 3D vector (either a list of 3 elements or Vector)");
275  return nullptr;
276  }
277  float t = self->n->smoothNoise3(vec);
278  return PyFloat_FromDouble(t);
279 }
280 
281 static PyMethodDef BPy_FrsNoise_methods[] = {
282  {"turbulence1",
283  (PyCFunction)FrsNoise_turbulence1,
284  METH_VARARGS | METH_KEYWORDS,
285  FrsNoise_turbulence1_doc},
286  {"turbulence2",
287  (PyCFunction)FrsNoise_turbulence2,
288  METH_VARARGS | METH_KEYWORDS,
289  FrsNoise_turbulence2_doc},
290  {"turbulence3",
291  (PyCFunction)FrsNoise_turbulence3,
292  METH_VARARGS | METH_KEYWORDS,
293  FrsNoise_turbulence3_doc},
294  {"smoothNoise1",
295  (PyCFunction)FrsNoise_smoothNoise1,
296  METH_VARARGS | METH_KEYWORDS,
297  FrsNoise_smoothNoise1_doc},
298  {"smoothNoise2",
299  (PyCFunction)FrsNoise_smoothNoise2,
300  METH_VARARGS | METH_KEYWORDS,
301  FrsNoise_smoothNoise2_doc},
302  {"smoothNoise3",
303  (PyCFunction)FrsNoise_smoothNoise3,
304  METH_VARARGS | METH_KEYWORDS,
305  FrsNoise_smoothNoise3_doc},
306  {"rand", (PyCFunction)FrsNoise_drand, METH_VARARGS | METH_KEYWORDS, nullptr},
307  {"turbulence_smooth",
308  (PyCFunction)FrsNoise_turbulence_smooth,
309  METH_VARARGS | METH_KEYWORDS,
310  nullptr},
311  {nullptr, nullptr, 0, nullptr},
312 };
313 
314 /*-----------------------BPy_FrsNoise type definition ------------------------------*/
315 
316 PyTypeObject FrsNoise_Type = {
317  PyVarObject_HEAD_INIT(nullptr, 0) "Noise", /* tp_name */
318  sizeof(BPy_FrsNoise), /* tp_basicsize */
319  0, /* tp_itemsize */
320  (destructor)FrsNoise_dealloc, /* tp_dealloc */
321  0, /* tp_vectorcall_offset */
322  nullptr, /* tp_getattr */
323  nullptr, /* tp_setattr */
324  nullptr, /* tp_reserved */
325  (reprfunc)FrsNoise_repr, /* tp_repr */
326  nullptr, /* tp_as_number */
327  nullptr, /* tp_as_sequence */
328  nullptr, /* tp_as_mapping */
329  nullptr, /* tp_hash */
330  nullptr, /* tp_call */
331  nullptr, /* tp_str */
332  nullptr, /* tp_getattro */
333  nullptr, /* tp_setattro */
334  nullptr, /* tp_as_buffer */
335  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
336  FrsNoise_doc, /* tp_doc */
337  nullptr, /* tp_traverse */
338  nullptr, /* tp_clear */
339  nullptr, /* tp_richcompare */
340  0, /* tp_weaklistoffset */
341  nullptr, /* tp_iter */
342  nullptr, /* tp_iternext */
343  BPy_FrsNoise_methods, /* tp_methods */
344  nullptr, /* tp_members */
345  nullptr, /* tp_getset */
346  nullptr, /* tp_base */
347  nullptr, /* tp_dict */
348  nullptr, /* tp_descr_get */
349  nullptr, /* tp_descr_set */
350  0, /* tp_dictoffset */
351  (initproc)FrsNoise_init, /* tp_init */
352  nullptr, /* tp_alloc */
353  PyType_GenericNew, /* tp_new */
354 };
355 
357 
358 #ifdef __cplusplus
359 }
360 #endif
bool Vec3f_ptr_from_PyObject(PyObject *obj, Vec3f &vec)
bool Vec2f_ptr_from_PyObject(PyObject *obj, Vec2f &vec)
static PyObject * FrsNoise_smoothNoise1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_smoothNoise2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static void FrsNoise_dealloc(BPy_FrsNoise *self)
static PyObject * FrsNoise_turbulence2(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
int FrsNoise_Init(PyObject *module)
static PyObject * FrsNoise_turbulence1(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_repr(BPy_FrsNoise *self)
static PyObject * FrsNoise_drand(BPy_FrsNoise *, 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_turbulence_smooth(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
PyTypeObject FrsNoise_Type
static PyObject * FrsNoise_turbulence3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyObject * FrsNoise_smoothNoise3(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
static PyMethodDef BPy_FrsNoise_methods[]
static int FrsNoise_init(BPy_FrsNoise *self, PyObject *args, PyObject *kwds)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color or the default fallback if none is specified Separate Split a vector into its and Z components Generates normals with round corners and may slow down renders Vector Displace the surface along an arbitrary direction White Noise
PyObject * self
Definition: bpy_driver.c:165
static unsigned long seed
Definition: btSoftBody.h:39
static void srand48(long seedval)
Definition: RandGen.cpp:97
static real drand48()
Definition: RandGen.cpp:90
inherits from class Rep
Definition: AppCanvas.cpp:18
static unsigned x[3]
Definition: RandGen.cpp:73
static struct PyModuleDef module
Definition: python.cpp:972