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