Revision: 30521 http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=30521 Author: campbellbarton Date: 2010-07-20 05:14:21 +0200 (Tue, 20 Jul 2010)
Log Message: ----------- noise python module back from 2.4x, personal request from ant author Jimmy Haze Changes: /* 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 */ Modified Paths: -------------- trunk/blender/source/blender/python/generic/mathutils.h trunk/blender/source/blender/python/intern/bpy.c Added Paths: ----------- trunk/blender/source/blender/python/generic/noise.c Modified: trunk/blender/source/blender/python/generic/mathutils.h =================================================================== --- trunk/blender/source/blender/python/generic/mathutils.h 2010-07-20 02:55:08 UTC (rev 30520) +++ trunk/blender/source/blender/python/generic/mathutils.h 2010-07-20 03:14:21 UTC (rev 30521) @@ -61,6 +61,7 @@ void BaseMathObject_dealloc(BaseMathObject * self); PyObject *Mathutils_Init(void); +PyObject *Noise_Init(void); /* lazy, saves having own header */ PyObject *quat_rotation(PyObject *arg1, PyObject *arg2); Added: trunk/blender/source/blender/python/generic/noise.c =================================================================== --- trunk/blender/source/blender/python/generic/noise.c (rev 0) +++ trunk/blender/source/blender/python/generic/noise.c 2010-07-20 03:14:21 UTC (rev 30521) @@ -0,0 +1,760 @@ +/** + * $Id$ + * + * Blender.Noise BPython module implementation. + * This submodule has functions to generate noise of various types. + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * This is a new part of Blender. + * + * Contributor(s): eeshlo + * + * ***** END GPL LICENSE BLOCK ***** +*/ + +/************************/ +/* Blender Noise Module */ +/************************/ + +#include <Python.h> +#include "structseq.h" + +#include "BLI_blenlib.h" +#include "DNA_texture_types.h" +/*-----------------------------------------*/ +/* 'mersenne twister' random number generator */ + +/* + A C-program for MT19937, with initialization improved 2002/2/10. + Coded by Takuji Nishimura and Makoto Matsumoto. + This is a faster version by taking Shawn Cokus's optimization, + Matthe Bellew's simplification, Isaku Wada's real version. + + Before using, initialize the state by using init_genrand(seed) + or init_by_array(init_key, key_length). + + Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + Any feedback is very welcome. + http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html + 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 +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ +#define UMASK 0x80000000UL /* most significant w-r bits */ +#define LMASK 0x7fffffffUL /* least significant r bits */ +#define MIXBITS(u,v) (((u) & UMASK) | ((v) & LMASK)) +#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL)) + +static unsigned long state[N]; /* the array for the state vector */ +static int left = 1; +static int initf = 0; +static unsigned long *next; + +PyObject *Noise_Init(void); + +/* initializes state[N] with a seed */ +static void init_genrand(unsigned long s) +{ + int j; + state[0] = s & 0xffffffffUL; + for(j = 1; j < N; j++) { + state[j] = + (1812433253UL * + (state[j - 1] ^ (state[j - 1] >> 30)) + j); + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ + /* In the previous versions, MSBs of the seed affect */ + /* only MSBs of the array state[]. */ + /* 2002/01/09 modified by Makoto Matsumoto */ + state[j] &= 0xffffffffUL; /* for >32 bit machines */ + } + left = 1; + initf = 1; +} + +static void next_state(void) +{ + unsigned long *p = state; + int j; + + /* if init_genrand() has not been called, */ + /* a default initial seed is used */ + if(initf == 0) + init_genrand(5489UL); + + left = N; + next = state; + + for(j = N - M + 1; --j; p++) + *p = p[M] ^ TWIST(p[0], p[1]); + + for(j = M; --j; p++) + *p = p[M - N] ^ TWIST(p[0], p[1]); + + *p = p[M - N] ^ TWIST(p[0], state[0]); +} + +/*------------------------------------------------------------*/ + +static void setRndSeed(int seed) +{ + if(seed == 0) + init_genrand(time(NULL)); + else + init_genrand(seed); +} + +/* float number in range [0, 1) using the mersenne twister rng */ +static float frand() +{ + unsigned long y; + + if(--left == 0) + next_state(); + y = *next++; + + /* Tempering */ + y ^= (y >> 11); + y ^= (y << 7) & 0x9d2c5680UL; + y ^= (y << 15) & 0xefc60000UL; + y ^= (y >> 18); + + return (float) y / 4294967296.f; +} + +/*------------------------------------------------------------*/ + +/* returns random unit vector */ +static void randuvec(float v[3]) +{ + 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 * cos(a)); + v[1] = (float)(r * sin(a)); + } else + v[2] = 1.f; +} + +static PyObject *Noise_random(PyObject * self) +{ + return PyFloat_FromDouble(frand()); +} + +static PyObject *Noise_random_unit_vector(PyObject * 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 * 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 * 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.0 * BLI_gNoise(1.0, x, y, z, 0, nb) - 1.0)); +} + +/*-------------------------------------------------------------------------*/ + +/* General Vector noise */ + +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.0 * BLI_gNoise(1.f, x + 9.321f, y - 1.531f, z - 7.951f, 0, + nb) - 1.0); + v[1] = (float)(2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0); + v[2] = (float)(2.0 * BLI_gNoise(1.f, x + 6.327f, y + 0.1671f, z - 2.672f, 0, + nb) - 1.0); +} + +static PyObject *Noise_vector(PyObject * 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 */ + +static float turb(float x, float y, float z, int oct, int hard, int nb, + float ampscale, float freqscale) +{ + float amp, out, t; + int i; + amp = 1.f; + out = (float)(2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0); + if(hard) + out = (float)fabs(out); + for(i = 1; i < oct; i++) { + amp *= ampscale; + x *= freqscale; + y *= freqscale; + z *= freqscale; + t = (float)(amp * (2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0)); + if(hard) + t = (float)fabs(t); + out += t; + } + return out; +} + +static PyObject *Noise_turbulence(PyObject * 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 */ + +static void vTurb(float x, float y, float z, int oct, int hard, int nb, + float ampscale, float freqscale, float v[3]) +{ + float amp, t[3]; + int i; + amp = 1.f; + noise_vector(x, y, z, nb, v); + if(hard) { + v[0] = (float)fabs(v[0]); + v[1] = (float)fabs(v[1]); + v[2] = (float)fabs(v[2]); + } + for(i = 1; i < oct; i++) { @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org http://lists.blender.org/mailman/listinfo/bf-blender-cvs