Revision: 43790
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43790
Author:   campbellbarton
Date:     2012-01-31 05:02:24 +0000 (Tue, 31 Jan 2012)
Log Message:
-----------
apply back changes made since moving this file.

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils_noise.c

Modified: trunk/blender/source/blender/python/mathutils/mathutils_noise.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_noise.c     
2012-01-31 04:59:57 UTC (rev 43789)
+++ trunk/blender/source/blender/python/mathutils/mathutils_noise.c     
2012-01-31 05:02:24 UTC (rev 43790)
@@ -25,8 +25,8 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/python/generic/noise_py_api.c
- *  \ingroup pygen
+/** \file blender/python/mathutils/mathutils_noise.c
+ *  \ingroup mathutils
  *
  * This file defines the 'noise' module, a general purpose module to access
  * blenders noise functions.
@@ -42,12 +42,25 @@
 #include "structseq.h"
 
 #include "BLI_blenlib.h"
+#include "BLI_math.h"
 #include "BLI_utildefines.h"
 
+#include "MEM_guardedalloc.h"
+
 #include "DNA_texture_types.h"
 
-#include "noise_py_api.h"
+#include "mathutils.h"
+#include "mathutils_noise.h"
 
+/* 2.6 update
+ * Moved to submodule of mathutils.
+ * All vector functions now return mathutils.Vector
+ * Updated docs to be compatible with autodocs generation.
+ * Updated vector functions to use nD array functions.
+ * noise.vl_vector --> noise.variable_lacunarity
+ * noise.vector --> noise.noise_vector
+ */
+
 /*-----------------------------------------*/
 /* 'mersenne twister' random number generator */
 
@@ -96,25 +109,6 @@
    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
 */
 
-/* 2.5 update
- * Noise.setRandomSeed --> seed_set
- * Noise.randuvec --> random_unit_vector
- * Noise.vNoise --> noise_vector
- * Noise.vTurbulence --> turbulence_vector
- * Noise.multiFractal --> multi_fractal
- * Noise.cellNoise --> cell
- * Noise.cellNoiseV --> cell_vector
- * Noise.vlNoise --> vl_vector
- * Noise.heteroTerrain --> hetero_terrain
- * Noise.hybridMFractal --> hybrid_multi_fractal
- * Noise.fBm --> fractal
- * Noise.ridgedMFractal --> ridged_multi_fractal
- *
- * Const's *
- * Noise.NoiseTypes --> types
- * Noise.DistanceMetrics --> distance_metrics
- */
-
 /* Period parameters */
 #define N 624
 #define M 397
@@ -199,90 +193,27 @@
 }
 
 /*------------------------------------------------------------*/
+/* Utility Functions */
+/*------------------------------------------------------------*/
 
-/* returns random unit vector */
-static void randuvec(float v[3])
+/* Fills an array of length size with random numbers in the range (-1, 1)*/
+static void rand_vn(float *array_tar, const int size)
 {
-       float r;
-       v[2] = 2.f * frand() - 1.f;
-       if ((r = 1.f - v[2] * v[2]) > 0.f) {
-               float a = (float)(6.283185307f * frand());
-               r = (float)sqrt(r);
-               v[0] = (float)(r * cosf(a));
-               v[1] = (float)(r * sinf(a));
-       }
-       else {
-               v[2] = 1.f;
-       }
+       float *array_pt = array_tar + (size-1);
+       int i = size;
+       while (i--) { *(array_pt--) = 2.0f * frand() - 1.0f; }
 }
 
-static PyObject *Noise_random(PyObject *UNUSED(self))
-{
-       return PyFloat_FromDouble(frand());
-}
-
-static PyObject *Noise_random_unit_vector(PyObject *UNUSED(self))
-{
-       float v[3] = {0.0f, 0.0f, 0.0f};
-       randuvec(v);
-       return Py_BuildValue("[fff]", v[0], v[1], v[2]);
-}
-
-/*---------------------------------------------------------------------*/
-
-/* Random seed init. Only used for MT random() & randuvec() */
-
-static PyObject *Noise_seed_set(PyObject *UNUSED(self), PyObject *args)
-{
-       int s;
-       if (!PyArg_ParseTuple(args, "i:seed_set", &s))
-               return NULL;
-       setRndSeed(s);
-       Py_RETURN_NONE;
-}
-
-/*-------------------------------------------------------------------------*/
-
-/* General noise */
-
-static PyObject *Noise_noise(PyObject *UNUSED(self), PyObject *args)
-{
-       float x, y, z;
-       int nb = 1;
-       if (!PyArg_ParseTuple(args, "(fff)|i:noise", &x, &y, &z, &nb))
-               return NULL;
-
-       return PyFloat_FromDouble((2.0f * BLI_gNoise(1.0f, x, y, z, 0, nb) - 
1.0f));
-}
-
-/*-------------------------------------------------------------------------*/
-
-/* General Vector noise */
-
+/* Fills an array of length 3 with noise values */
 static void noise_vector(float x, float y, float z, int nb, float v[3])
 {
        /* Simply evaluate noise at 3 different positions */
-       v[0]= (float)(2.0f * BLI_gNoise(1.f, x + 9.321f, y - 1.531f, z - 
7.951f, 0,
-                                       nb) - 1.0f);
-       v[1]= (float)(2.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f);
-       v[2]= (float)(2.0f * BLI_gNoise(1.f, x + 6.327f, y + 0.1671f, z - 
2.672f, 0,
-                                       nb) - 1.0f);
+       v[0] = (float)(2.0f * BLI_gNoise(1.f, x + 9.321f, y - 1.531f, z - 
7.951f, 0, nb) - 1.0f);
+       v[1] = (float)(2.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f);
+       v[2] = (float)(2.0f * BLI_gNoise(1.f, x + 6.327f, y + 0.1671f, z - 
2.672f, 0, nb) - 1.0f);
 }
 
-static PyObject *Noise_vector(PyObject *UNUSED(self), PyObject *args)
-{
-       float x, y, z, v[3];
-       int nb = 1;
-       if (!PyArg_ParseTuple(args, "(fff)|i:vector", &x, &y, &z, &nb))
-               return NULL;
-       noise_vector(x, y, z, nb, v);
-       return Py_BuildValue("[fff]", v[0], v[1], v[2]);
-}
-
-/*---------------------------------------------------------------------------*/
-
-/* General turbulence */
-
+/* Returns a turbulence value for a given position (x, y, z) */
 static float turb(float x, float y, float z, int oct, int hard, int nb,
                   float ampscale, float freqscale)
 {
@@ -305,21 +236,8 @@
        return out;
 }
 
-static PyObject *Noise_turbulence(PyObject *UNUSED(self), PyObject *args)
-{
-       float x, y, z;
-       int oct, hd, nb = 1;
-       float as = 0.5, fs = 2.0;
-       if (!PyArg_ParseTuple(args, "(fff)ii|iff:turbulence", &x, &y, &z, &oct, 
&hd, &nb, &as, &fs))
-               return NULL;
-
-       return PyFloat_FromDouble(turb(x, y, z, oct, hd, nb, as, fs));
-}
-
-/*--------------------------------------------------------------------------*/
-
-/* Turbulence Vector */
-
+/* Fills an array of length 3 with the turbulence vector for a given
+position (x, y, z) */
 static void vTurb(float x, float y, float z, int oct, int hard, int nb,
                   float ampscale, float freqscale, float v[3])
 {
@@ -349,444 +267,645 @@
        }
 }
 
-static PyObject *Noise_turbulence_vector(PyObject *UNUSED(self), PyObject 
*args)
+/*-------------------------DOC STRINGS ---------------------------*/
+PyDoc_STRVAR(M_Noise_doc,
+"The Blender noise module"
+);
+
+/*------------------------------------------------------------*/
+/* Python Functions */
+/*------------------------------------------------------------*/
+
+PyDoc_STRVAR(M_Noise_random_doc,
+".. function:: random()\n"
+"\n"
+"   Returns a random number in the range [0, 1].\n"
+"\n"
+"   :return: The random number.\n"
+"   :rtype: float\n"
+);
+static PyObject *M_Noise_random(PyObject *UNUSED(self))
 {
-       float x, y, z, v[3];
-       int oct, hd, nb = 1;
-       float as = 0.5, fs = 2.0;
-       if (!PyArg_ParseTuple(args, "(fff)ii|iff:turbulence_vector", &x, &y, 
&z, &oct, &hd, &nb, &as, &fs))
+       return PyFloat_FromDouble(frand());
+}
+
+PyDoc_STRVAR(M_Noise_random_unit_vector_doc,
+".. function:: random_unit_vector(size=3)\n"
+"\n"
+"   Returns a unit vector with random entries.\n"
+"\n"
+"   :arg size: The size of the vector to be produced.\n"
+"   :type size: Int\n"
+"   :return: The random unit vector.\n"
+"   :rtype: :class:`mathutils.Vector`\n"
+);
+static PyObject *M_Noise_random_unit_vector(PyObject *UNUSED(self), PyObject 
*args)
+{
+       float vec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+       float norm = 2.0f;
+       int size = 3;
+
+       if (!PyArg_ParseTuple(args, "|i:random_vector", &size))
                return NULL;
-       vTurb(x, y, z, oct, hd, nb, as, fs, v);
-       return Py_BuildValue("[fff]", v[0], v[1], v[2]);
+
+       if (size > 4 || size < 2) {
+               PyErr_SetString(PyExc_ValueError, "Vector(): invalid size");
+               return NULL;
+       }
+
+       while (norm == 0.0f || norm >= 1.0f) {
+               rand_vn(vec, size);
+               norm = normalize_vn(vec, size);
+       }
+
+       return Vector_CreatePyObject(vec, size, Py_NEW, NULL);
 }
+/* This is dumb, most people will want a unit vector anyway, since this 
doesn't have uniform distribution over a sphere*/
+/*
+PyDoc_STRVAR(M_Noise_random_vector_doc,
+".. function:: random_vector(size=3)\n"
+"\n"
+"   Returns a vector with random entries in the range [0, 1).\n"
+"\n"
+"   :arg size: The size of the vector to be produced.\n"
+"   :type size: Int\n"
+"   :return: The random vector.\n"
+"   :rtype: :class:`mathutils.Vector`\n"
+);
+static PyObject *M_Noise_random_vector(PyObject *UNUSED(self), PyObject *args)
+{
+       float vec[4]= {0.0f, 0.0f, 0.0f, 0.0f};
+       int size= 3;
 
-/*---------------------------------------------------------------------*/
+       if (!PyArg_ParseTuple(args, "|i:random_vector", &size))
+               return NULL;
 
-/* F. Kenton Musgrave's fractal functions */
+       if (size > 4 || size < 2) {
+               PyErr_SetString(PyExc_ValueError, "Vector(): invalid size");
+               return NULL;
+       }
 
-static PyObject *Noise_fractal(PyObject *UNUSED(self), PyObject *args)
+       rand_vn(vec, size);
+
+       return Vector_CreatePyObject(vec, size, Py_NEW, NULL);
+}
+*/
+
+PyDoc_STRVAR(M_Noise_seed_set_doc,
+".. function:: seed_set(seed)\n"
+"\n"
+"   Sets the random seed used for random_unit_vector, random_vector and 
random.\n"
+"\n"
+"   :arg seed: Seed used for the random generator.\n"
+"   :type seed: Int\n"
+);
+static PyObject *M_Noise_seed_set(PyObject *UNUSED(self), PyObject *args)
 {
-       float x, y, z, H, lac, oct;
+       int s;
+       if (!PyArg_ParseTuple(args, "i:seed_set", &s))
+               return NULL;
+       setRndSeed(s);
+       Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(M_Noise_noise_doc,
+".. function:: noise(position, noise_basis=noise.types.STDPERLIN)\n"
+"\n"
+"   Returns noise value from the noise basis at the position specified.\n"
+"\n"
+"   :arg position: The position to evaluate the selected noise function at.\n"
+"   :type position: :class:`mathutils.Vector`\n"
+"   :arg noise_basis: The type of noise to be evaluated.\n"
+"   :type noise_basis: Value in noise.types or int\n"
+"   :return: The noise value.\n"
+"   :rtype: float\n"
+);
+static PyObject *M_Noise_noise(PyObject *UNUSED(self), PyObject *args)
+{
+       PyObject *value;
+       float vec[3];
        int nb = 1;
-       if (!PyArg_ParseTuple(args, "(fff)fff|i:fractal", &x, &y, &z, &H, &lac, 
&oct, &nb))
+       if (!PyArg_ParseTuple(args, "O|i:noise", &value, &nb))
                return NULL;
-       return PyFloat_FromDouble(mg_fBm(x, y, z, H, lac, oct, nb));
+
+       if (mathutils_array_parse(vec, 3, 3, value, "noise: invalid 'position' 
arg") == -1)
+               return NULL;
+
+       return PyFloat_FromDouble((2.0f * BLI_gNoise(1.0f, vec[0], vec[1], 
vec[2], 0, nb) - 1.0f));
 }
 
-/*------------------------------------------------------------------------*/
-
-static PyObject *Noise_multi_fractal(PyObject *UNUSED(self), PyObject *args)
+PyDoc_STRVAR(M_Noise_noise_vector_doc,

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to