[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43789] trunk/blender/source/blender/ python/mathutils/mathutils_noise.c: svn cp \

Campbell Barton ideasman42 at gmail.com
Tue Jan 31 05:59:58 CET 2012


Revision: 43789
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43789
Author:   campbellbarton
Date:     2012-01-31 04:59:57 +0000 (Tue, 31 Jan 2012)
Log Message:
-----------
svn cp \
 https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/python/generic/noise_py_api.c@r42248 \
 https://svn.blender.org/svnroot/bf-blender/trunk/blender/source/blender/python/mathutils/mathutils_noise.c

Revision Links:
--------------
    http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42248

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

Copied: trunk/blender/source/blender/python/mathutils/mathutils_noise.c (from rev 42248, trunk/blender/source/blender/python/generic/noise_py_api.c)
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_noise.c	                        (rev 0)
+++ trunk/blender/source/blender/python/mathutils/mathutils_noise.c	2012-01-31 04:59:57 UTC (rev 43789)
@@ -0,0 +1,792 @@
+/*
+ * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/generic/noise_py_api.c
+ *  \ingroup pygen
+ *
+ * This file defines the 'noise' module, a general purpose module to access
+ * blenders noise functions.
+ */
+
+
+/************************/
+/* Blender Noise Module */
+/************************/
+
+#include <Python.h>
+
+#include "structseq.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_texture_types.h"
+
+#include "noise_py_api.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;
+
+/* 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(void)
+{
+	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 * cosf(a));
+		v[1] = (float)(r * sinf(a));
+	}
+	else {
+		v[2] = 1.f;
+	}
+}
+
+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 */
+
+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);
+}
+
+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 */
+
+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.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f);
+	if (hard)
+		out = fabsf(out);
+	for (i = 1; i < oct; i++) {
+		amp *= ampscale;
+		x *= freqscale;
+		y *= freqscale;
+		z *= freqscale;
+		t = (float)(amp * (2.0f * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0f));
+		if (hard)
+			t = fabsf(t);
+		out += t;
+	}
+	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 */
+
+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] = fabsf(v[0]);
+		v[1] = fabsf(v[1]);
+		v[2] = fabsf(v[2]);
+	}
+	for (i = 1; i < oct; i++) {
+		amp *= ampscale;
+		x *= freqscale;
+		y *= freqscale;
+		z *= freqscale;
+		noise_vector(x, y, z, nb, t);
+		if (hard) {
+			t[0] = fabsf(t[0]);
+			t[1] = fabsf(t[1]);
+			t[2] = fabsf(t[2]);
+		}
+		v[0] += amp * t[0];

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list