|
Blender
V2.59
|
00001 /* 00002 * $Id: noise_py_api.c 36871 2011-05-24 16:05:51Z campbellbarton $ 00003 * 00004 * Blender.Noise BPython module implementation. 00005 * This submodule has functions to generate noise of various types. 00006 * 00007 * ***** BEGIN GPL LICENSE BLOCK ***** 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00024 * All rights reserved. 00025 * 00026 * This is a new part of Blender. 00027 * 00028 * Contributor(s): eeshlo 00029 * 00030 * ***** END GPL LICENSE BLOCK ***** 00031 */ 00032 00038 /************************/ 00039 /* Blender Noise Module */ 00040 /************************/ 00041 00042 #include <Python.h> 00043 00044 #include "structseq.h" 00045 00046 #include "BLI_blenlib.h" 00047 #include "BLI_utildefines.h" 00048 00049 #include "DNA_texture_types.h" 00050 00051 #include "noise_py_api.h" 00052 00053 /*-----------------------------------------*/ 00054 /* 'mersenne twister' random number generator */ 00055 00056 /* 00057 A C-program for MT19937, with initialization improved 2002/2/10. 00058 Coded by Takuji Nishimura and Makoto Matsumoto. 00059 This is a faster version by taking Shawn Cokus's optimization, 00060 Matthe Bellew's simplification, Isaku Wada's real version. 00061 00062 Before using, initialize the state by using init_genrand(seed) 00063 or init_by_array(init_key, key_length). 00064 00065 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 00066 All rights reserved. 00067 00068 Redistribution and use in source and binary forms, with or without 00069 modification, are permitted provided that the following conditions 00070 are met: 00071 00072 1. Redistributions of source code must retain the above copyright 00073 notice, this list of conditions and the following disclaimer. 00074 00075 2. Redistributions in binary form must reproduce the above copyright 00076 notice, this list of conditions and the following disclaimer in the 00077 documentation and/or other materials provided with the distribution. 00078 00079 3. The names of its contributors may not be used to endorse or promote 00080 products derived from this software without specific prior written 00081 permission. 00082 00083 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00084 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00085 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00086 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00087 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00088 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00089 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00090 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00091 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00092 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00093 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00094 00095 00096 Any feedback is very welcome. 00097 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 00098 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 00099 */ 00100 00101 /* 2.5 update 00102 * Noise.setRandomSeed --> seed_set 00103 * Noise.randuvec --> random_unit_vector 00104 * Noise.vNoise --> noise_vector 00105 * Noise.vTurbulence --> turbulence_vector 00106 * Noise.multiFractal --> multi_fractal 00107 * Noise.cellNoise --> cell 00108 * Noise.cellNoiseV --> cell_vector 00109 * Noise.vlNoise --> vl_vector 00110 * Noise.heteroTerrain --> hetero_terrain 00111 * Noise.hybridMFractal --> hybrid_multi_fractal 00112 * Noise.fBm --> fractal 00113 * Noise.ridgedMFractal --> ridged_multi_fractal 00114 * 00115 * Const's * 00116 * Noise.NoiseTypes --> types 00117 * Noise.DistanceMetrics --> distance_metrics 00118 */ 00119 00120 /* Period parameters */ 00121 #define N 624 00122 #define M 397 00123 #define MATRIX_A 0x9908b0dfUL /* constant vector a */ 00124 #define UMASK 0x80000000UL /* most significant w-r bits */ 00125 #define LMASK 0x7fffffffUL /* least significant r bits */ 00126 #define MIXBITS(u,v) (((u) & UMASK) | ((v) & LMASK)) 00127 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL)) 00128 00129 static unsigned long state[N]; /* the array for the state vector */ 00130 static int left = 1; 00131 static int initf = 0; 00132 static unsigned long *next; 00133 00134 /* initializes state[N] with a seed */ 00135 static void init_genrand(unsigned long s) 00136 { 00137 int j; 00138 state[0] = s & 0xffffffffUL; 00139 for(j = 1; j < N; j++) { 00140 state[j] = 00141 (1812433253UL * 00142 (state[j - 1] ^ (state[j - 1] >> 30)) + j); 00143 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 00144 /* In the previous versions, MSBs of the seed affect */ 00145 /* only MSBs of the array state[]. */ 00146 /* 2002/01/09 modified by Makoto Matsumoto */ 00147 state[j] &= 0xffffffffUL; /* for >32 bit machines */ 00148 } 00149 left = 1; 00150 initf = 1; 00151 } 00152 00153 static void next_state(void) 00154 { 00155 unsigned long *p = state; 00156 int j; 00157 00158 /* if init_genrand() has not been called, */ 00159 /* a default initial seed is used */ 00160 if(initf == 0) 00161 init_genrand(5489UL); 00162 00163 left = N; 00164 next = state; 00165 00166 for(j = N - M + 1; --j; p++) 00167 *p = p[M] ^ TWIST(p[0], p[1]); 00168 00169 for(j = M; --j; p++) 00170 *p = p[M - N] ^ TWIST(p[0], p[1]); 00171 00172 *p = p[M - N] ^ TWIST(p[0], state[0]); 00173 } 00174 00175 /*------------------------------------------------------------*/ 00176 00177 static void setRndSeed(int seed) 00178 { 00179 if(seed == 0) 00180 init_genrand(time(NULL)); 00181 else 00182 init_genrand(seed); 00183 } 00184 00185 /* float number in range [0, 1) using the mersenne twister rng */ 00186 static float frand(void) 00187 { 00188 unsigned long y; 00189 00190 if(--left == 0) 00191 next_state(); 00192 y = *next++; 00193 00194 /* Tempering */ 00195 y ^= (y >> 11); 00196 y ^= (y << 7) & 0x9d2c5680UL; 00197 y ^= (y << 15) & 0xefc60000UL; 00198 y ^= (y >> 18); 00199 00200 return (float) y / 4294967296.f; 00201 } 00202 00203 /*------------------------------------------------------------*/ 00204 00205 /* returns random unit vector */ 00206 static void randuvec(float v[3]) 00207 { 00208 float r; 00209 v[2] = 2.f * frand() - 1.f; 00210 if((r = 1.f - v[2] * v[2]) > 0.f) { 00211 float a = (float)(6.283185307f * frand()); 00212 r = (float)sqrt(r); 00213 v[0] = (float)(r * cos(a)); 00214 v[1] = (float)(r * sin(a)); 00215 } 00216 else { 00217 v[2] = 1.f; 00218 } 00219 } 00220 00221 static PyObject *Noise_random(PyObject *UNUSED(self)) 00222 { 00223 return PyFloat_FromDouble(frand()); 00224 } 00225 00226 static PyObject *Noise_random_unit_vector(PyObject *UNUSED(self)) 00227 { 00228 float v[3] = {0.0f, 0.0f, 0.0f}; 00229 randuvec(v); 00230 return Py_BuildValue("[fff]", v[0], v[1], v[2]); 00231 } 00232 00233 /*---------------------------------------------------------------------*/ 00234 00235 /* Random seed init. Only used for MT random() & randuvec() */ 00236 00237 static PyObject *Noise_seed_set(PyObject *UNUSED(self), PyObject *args) 00238 { 00239 int s; 00240 if(!PyArg_ParseTuple(args, "i:seed_set", &s)) 00241 return NULL; 00242 setRndSeed(s); 00243 Py_RETURN_NONE; 00244 } 00245 00246 /*-------------------------------------------------------------------------*/ 00247 00248 /* General noise */ 00249 00250 static PyObject *Noise_noise(PyObject *UNUSED(self), PyObject *args) 00251 { 00252 float x, y, z; 00253 int nb = 1; 00254 if(!PyArg_ParseTuple(args, "(fff)|i:noise", &x, &y, &z, &nb)) 00255 return NULL; 00256 00257 return PyFloat_FromDouble((2.0 * BLI_gNoise(1.0, x, y, z, 0, nb) - 1.0)); 00258 } 00259 00260 /*-------------------------------------------------------------------------*/ 00261 00262 /* General Vector noise */ 00263 00264 static void noise_vector(float x, float y, float z, int nb, float v[3]) 00265 { 00266 /* Simply evaluate noise at 3 different positions */ 00267 v[0] = (float)(2.0 * BLI_gNoise(1.f, x + 9.321f, y - 1.531f, z - 7.951f, 0, 00268 nb) - 1.0); 00269 v[1] = (float)(2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0); 00270 v[2] = (float)(2.0 * BLI_gNoise(1.f, x + 6.327f, y + 0.1671f, z - 2.672f, 0, 00271 nb) - 1.0); 00272 } 00273 00274 static PyObject *Noise_vector(PyObject *UNUSED(self), PyObject *args) 00275 { 00276 float x, y, z, v[3]; 00277 int nb = 1; 00278 if(!PyArg_ParseTuple(args, "(fff)|i:vector", &x, &y, &z, &nb)) 00279 return NULL; 00280 noise_vector(x, y, z, nb, v); 00281 return Py_BuildValue("[fff]", v[0], v[1], v[2]); 00282 } 00283 00284 /*---------------------------------------------------------------------------*/ 00285 00286 /* General turbulence */ 00287 00288 static float turb(float x, float y, float z, int oct, int hard, int nb, 00289 float ampscale, float freqscale) 00290 { 00291 float amp, out, t; 00292 int i; 00293 amp = 1.f; 00294 out = (float)(2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0); 00295 if(hard) 00296 out = (float)fabs(out); 00297 for(i = 1; i < oct; i++) { 00298 amp *= ampscale; 00299 x *= freqscale; 00300 y *= freqscale; 00301 z *= freqscale; 00302 t = (float)(amp * (2.0 * BLI_gNoise(1.f, x, y, z, 0, nb) - 1.0)); 00303 if(hard) 00304 t = (float)fabs(t); 00305 out += t; 00306 } 00307 return out; 00308 } 00309 00310 static PyObject *Noise_turbulence(PyObject *UNUSED(self), PyObject *args) 00311 { 00312 float x, y, z; 00313 int oct, hd, nb = 1; 00314 float as = 0.5, fs = 2.0; 00315 if(!PyArg_ParseTuple(args, "(fff)ii|iff:turbulence", &x, &y, &z, &oct, &hd, &nb, &as, &fs)) 00316 return NULL; 00317 00318 return PyFloat_FromDouble(turb(x, y, z, oct, hd, nb, as, fs)); 00319 } 00320 00321 /*--------------------------------------------------------------------------*/ 00322 00323 /* Turbulence Vector */ 00324 00325 static void vTurb(float x, float y, float z, int oct, int hard, int nb, 00326 float ampscale, float freqscale, float v[3]) 00327 { 00328 float amp, t[3]; 00329 int i; 00330 amp = 1.f; 00331 noise_vector(x, y, z, nb, v); 00332 if(hard) { 00333 v[0] = (float)fabs(v[0]); 00334 v[1] = (float)fabs(v[1]); 00335 v[2] = (float)fabs(v[2]); 00336 } 00337 for(i = 1; i < oct; i++) { 00338 amp *= ampscale; 00339 x *= freqscale; 00340 y *= freqscale; 00341 z *= freqscale; 00342 noise_vector(x, y, z, nb, t); 00343 if(hard) { 00344 t[0] = (float)fabs(t[0]); 00345 t[1] = (float)fabs(t[1]); 00346 t[2] = (float)fabs(t[2]); 00347 } 00348 v[0] += amp * t[0]; 00349 v[1] += amp * t[1]; 00350 v[2] += amp * t[2]; 00351 } 00352 } 00353 00354 static PyObject *Noise_turbulence_vector(PyObject *UNUSED(self), PyObject *args) 00355 { 00356 float x, y, z, v[3]; 00357 int oct, hd, nb = 1; 00358 float as = 0.5, fs = 2.0; 00359 if(!PyArg_ParseTuple(args, "(fff)ii|iff:turbulence_vector", &x, &y, &z, &oct, &hd, &nb, &as, &fs)) 00360 return NULL; 00361 vTurb(x, y, z, oct, hd, nb, as, fs, v); 00362 return Py_BuildValue("[fff]", v[0], v[1], v[2]); 00363 } 00364 00365 /*---------------------------------------------------------------------*/ 00366 00367 /* F. Kenton Musgrave's fractal functions */ 00368 00369 static PyObject *Noise_fractal(PyObject *UNUSED(self), PyObject *args) 00370 { 00371 float x, y, z, H, lac, oct; 00372 int nb = 1; 00373 if(!PyArg_ParseTuple(args, "(fff)fff|i:fractal", &x, &y, &z, &H, &lac, &oct, &nb)) 00374 return NULL; 00375 return PyFloat_FromDouble(mg_fBm(x, y, z, H, lac, oct, nb)); 00376 } 00377 00378 /*------------------------------------------------------------------------*/ 00379 00380 static PyObject *Noise_multi_fractal(PyObject *UNUSED(self), PyObject *args) 00381 { 00382 float x, y, z, H, lac, oct; 00383 int nb = 1; 00384 if(!PyArg_ParseTuple(args, "(fff)fff|i:multi_fractal", &x, &y, &z, &H, &lac, &oct, &nb)) 00385 return NULL; 00386 00387 return PyFloat_FromDouble(mg_MultiFractal(x, y, z, H, lac, oct, nb)); 00388 } 00389 00390 /*------------------------------------------------------------------------*/ 00391 00392 static PyObject *Noise_vl_vector(PyObject *UNUSED(self), PyObject *args) 00393 { 00394 float x, y, z, d; 00395 int nt1 = 1, nt2 = 1; 00396 if(!PyArg_ParseTuple(args, "(fff)f|ii:vl_vector", &x, &y, &z, &d, &nt1, &nt2)) 00397 return NULL; 00398 return PyFloat_FromDouble(mg_VLNoise(x, y, z, d, nt1, nt2)); 00399 } 00400 00401 /*-------------------------------------------------------------------------*/ 00402 00403 static PyObject *Noise_hetero_terrain(PyObject *UNUSED(self), PyObject *args) 00404 { 00405 float x, y, z, H, lac, oct, ofs; 00406 int nb = 1; 00407 if(!PyArg_ParseTuple(args, "(fff)ffff|i:hetero_terrain", &x, &y, &z, &H, &lac, &oct, &ofs, &nb)) 00408 return NULL; 00409 00410 return PyFloat_FromDouble(mg_HeteroTerrain(x, y, z, H, lac, oct, ofs, nb)); 00411 } 00412 00413 /*-------------------------------------------------------------------------*/ 00414 00415 static PyObject *Noise_hybrid_multi_fractal(PyObject *UNUSED(self), PyObject *args) 00416 { 00417 float x, y, z, H, lac, oct, ofs, gn; 00418 int nb = 1; 00419 if(!PyArg_ParseTuple(args, "(fff)fffff|i:hybrid_multi_fractal", &x, &y, &z, &H, &lac, &oct, &ofs, &gn, &nb)) 00420 return NULL; 00421 00422 return PyFloat_FromDouble(mg_HybridMultiFractal(x, y, z, H, lac, oct, ofs, gn, nb)); 00423 } 00424 00425 /*------------------------------------------------------------------------*/ 00426 00427 static PyObject *Noise_ridged_multi_fractal(PyObject *UNUSED(self), PyObject *args) 00428 { 00429 float x, y, z, H, lac, oct, ofs, gn; 00430 int nb = 1; 00431 if(!PyArg_ParseTuple(args, "(fff)fffff|i:ridged_multi_fractal", &x, &y, &z, &H, &lac, &oct, &ofs, &gn, &nb)) 00432 return NULL; 00433 return PyFloat_FromDouble(mg_RidgedMultiFractal(x, y, z, H, lac, oct, ofs, gn, nb)); 00434 } 00435 00436 /*-------------------------------------------------------------------------*/ 00437 00438 static PyObject *Noise_voronoi(PyObject *UNUSED(self), PyObject *args) 00439 { 00440 float x, y, z, da[4], pa[12]; 00441 int dtype = 0; 00442 float me = 2.5; /* default minkovsky exponent */ 00443 if(!PyArg_ParseTuple(args, "(fff)|if:voronoi", &x, &y, &z, &dtype, &me)) 00444 return NULL; 00445 voronoi(x, y, z, da, pa, me, dtype); 00446 return Py_BuildValue("[[ffff][[fff][fff][fff][fff]]]", 00447 da[0], da[1], da[2], da[3], 00448 pa[0], pa[1], pa[2], 00449 pa[3], pa[4], pa[5], 00450 pa[6], pa[7], pa[8], pa[9], pa[10], pa[11]); 00451 } 00452 00453 /*-------------------------------------------------------------------------*/ 00454 00455 static PyObject *Noise_cell(PyObject *UNUSED(self), PyObject *args) 00456 { 00457 float x, y, z; 00458 if(!PyArg_ParseTuple(args, "(fff):cell", &x, &y, &z)) 00459 return NULL; 00460 00461 return PyFloat_FromDouble(cellNoise(x, y, z)); 00462 } 00463 00464 /*--------------------------------------------------------------------------*/ 00465 00466 static PyObject *Noise_cell_vector(PyObject *UNUSED(self), PyObject *args) 00467 { 00468 float x, y, z, ca[3]; 00469 if(!PyArg_ParseTuple(args, "(fff):cell_vector", &x, &y, &z)) 00470 return NULL; 00471 cellNoiseV(x, y, z, ca); 00472 return Py_BuildValue("[fff]", ca[0], ca[1], ca[2]); 00473 } 00474 00475 /*--------------------------------------------------------------------------*/ 00476 /* For all other Blender modules, this stuff seems to be put in a header file. 00477 This doesn't seem really appropriate to me, so I just put it here, feel free to change it. 00478 In the original module I actually kept the docs stings with the functions themselves, 00479 but I grouped them here so that it can easily be moved to a header if anyone thinks that is necessary. */ 00480 00481 PyDoc_STRVAR(random__doc__, 00482 "() No arguments.\n\n\ 00483 Returns a random floating point number in the range [0, 1)" 00484 ); 00485 00486 PyDoc_STRVAR(random_unit_vector__doc__, 00487 "() No arguments.\n\nReturns a random unit vector (3-float list)." 00488 ); 00489 00490 PyDoc_STRVAR(seed_set__doc__, 00491 "(seed value)\n\n\ 00492 Initializes random number generator.\n\ 00493 if seed is zero, the current time will be used instead." 00494 ); 00495 00496 PyDoc_STRVAR(noise__doc__, 00497 "((x,y,z) tuple, [noisetype])\n\n\ 00498 Returns general noise of the optional specified type.\n\ 00499 Optional argument noisetype determines the type of noise, STDPERLIN by default, see NoiseTypes." 00500 ); 00501 00502 PyDoc_STRVAR(noise_vector__doc__, 00503 "((x,y,z) tuple, [noisetype])\n\n\ 00504 Returns noise vector (3-float list) of the optional specified type.\ 00505 Optional argument noisetype determines the type of noise, STDPERLIN by default, see NoiseTypes." 00506 ); 00507 00508 PyDoc_STRVAR(turbulence__doc__, 00509 "((x,y,z) tuple, octaves, hard, [noisebasis], [ampscale], [freqscale])\n\n\ 00510 Returns general turbulence value using the optional specified noisebasis function.\n\ 00511 octaves (integer) is the number of noise values added.\n\ 00512 hard (bool), when false (0) returns 'soft' noise, when true (1) returns 'hard' noise (returned value always positive).\n\ 00513 Optional arguments:\n\ 00514 noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes.\n\ 00515 ampscale sets the amplitude scale value of the noise frequencies added, 0.5 by default.\n\ 00516 freqscale sets the frequency scale factor, 2.0 by default." 00517 ); 00518 00519 PyDoc_STRVAR(turbulence_vector__doc__, 00520 "((x,y,z) tuple, octaves, hard, [noisebasis], [ampscale], [freqscale])\n\n\ 00521 Returns general turbulence vector (3-float list) using the optional specified noisebasis function.\n\ 00522 octaves (integer) is the number of noise values added.\n\ 00523 hard (bool), when false (0) returns 'soft' noise, when true (1) returns 'hard' noise (returned vector always positive).\n\ 00524 Optional arguments:\n\ 00525 noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes.\n\ 00526 ampscale sets the amplitude scale value of the noise frequencies added, 0.5 by default.\n\ 00527 freqscale sets the frequency scale factor, 2.0 by default." 00528 ); 00529 00530 PyDoc_STRVAR(fractal__doc__, 00531 "((x,y,z) tuple, H, lacunarity, octaves, [noisebasis])\n\n\ 00532 Returns Fractal Brownian Motion noise value(fBm).\n\ 00533 H is the fractal increment parameter.\n\ 00534 lacunarity is the gap between successive frequencies.\n\ 00535 octaves is the number of frequencies in the fBm.\n\ 00536 Optional argument noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes." 00537 ); 00538 00539 PyDoc_STRVAR(multi_fractal__doc__, 00540 "((x,y,z) tuple, H, lacunarity, octaves, [noisebasis])\n\n\ 00541 Returns Multifractal noise value.\n\ 00542 H determines the highest fractal dimension.\n\ 00543 lacunarity is gap between successive frequencies.\n\ 00544 octaves is the number of frequencies in the fBm.\n\ 00545 Optional argument noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes." 00546 ); 00547 00548 PyDoc_STRVAR(vl_vector__doc__, 00549 "((x,y,z) tuple, distortion, [noisetype1], [noisetype2])\n\n\ 00550 Returns Variable Lacunarity Noise value, a distorted variety of noise.\n\ 00551 distortion sets the amount of distortion.\n\ 00552 Optional arguments noisetype1 and noisetype2 set the noisetype to distort and the noisetype used for the distortion respectively.\n\ 00553 See NoiseTypes, both are STDPERLIN by default." 00554 ); 00555 00556 PyDoc_STRVAR(hetero_terrain__doc__, 00557 "((x,y,z) tuple, H, lacunarity, octaves, offset, [noisebasis])\n\n\ 00558 returns Heterogeneous Terrain value\n\ 00559 H determines the fractal dimension of the roughest areas.\n\ 00560 lacunarity is the gap between successive frequencies.\n\ 00561 octaves is the number of frequencies in the fBm.\n\ 00562 offset raises the terrain from 'sea level'.\n\ 00563 Optional argument noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes." 00564 ); 00565 00566 PyDoc_STRVAR(hybrid_multi_fractal__doc__, 00567 "((x,y,z) tuple, H, lacunarity, octaves, offset, gain, [noisebasis])\n\n\ 00568 returns Hybrid Multifractal value.\n\ 00569 H determines the fractal dimension of the roughest areas.\n\ 00570 lacunarity is the gap between successive frequencies.\n\ 00571 octaves is the number of frequencies in the fBm.\n\ 00572 offset raises the terrain from 'sea level'.\n\ 00573 gain scales the values.\n\ 00574 Optional argument noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes." 00575 ); 00576 00577 PyDoc_STRVAR(ridged_multi_fractal__doc__, 00578 "((x,y,z) tuple, H, lacunarity, octaves, offset, gain [noisebasis])\n\n\ 00579 returns Ridged Multifractal value.\n\ 00580 H determines the fractal dimension of the roughest areas.\n\ 00581 lacunarity is the gap between successive frequencies.\n\ 00582 octaves is the number of frequencies in the fBm.\n\ 00583 offset raises the terrain from 'sea level'.\n\ 00584 gain scales the values.\n\ 00585 Optional argument noisebasis determines the type of noise used for the turbulence, STDPERLIN by default, see NoiseTypes." 00586 ); 00587 00588 PyDoc_STRVAR(voronoi__doc__, 00589 "((x,y,z) tuple, distance_metric, [exponent])\n\n\ 00590 returns a list, containing a list of distances in order of closest feature,\n\ 00591 and a list containing the positions of the four closest features\n\ 00592 Optional arguments:\n\ 00593 distance_metric: see DistanceMetrics, default is DISTANCE\n\ 00594 exponent is only used with MINKOVSKY, default is 2.5." 00595 ); 00596 00597 PyDoc_STRVAR(cell__doc__, 00598 "((x,y,z) tuple)\n\n\ 00599 returns cellnoise float value." 00600 ); 00601 00602 PyDoc_STRVAR(cell_vector__doc__, 00603 "((x,y,z) tuple)\n\n\ 00604 returns cellnoise vector/point/color (3-float list)." 00605 ); 00606 00607 PyDoc_STRVAR(Noise__doc__, 00608 "Blender Noise and Turbulence Module\n\n\ 00609 This module can be used to generate noise of various types.\n\ 00610 This can be used for terrain generation, to create textures,\n\ 00611 make animations more 'animated', object deformation, etc.\n\ 00612 As an example, this code segment when scriptlinked to a framechanged event,\n\ 00613 will make the camera sway randomly about, by changing parameters this can\n\ 00614 look like anything from an earthquake to a very nervous or maybe even drunk cameraman...\n\ 00615 (the camera needs an ipo with at least one Loc & Rot key for this to work!):\n\ 00616 \n\ 00617 \tfrom Blender import Get, Scene, Noise\n\ 00618 \n\ 00619 \t####################################################\n\ 00620 \t# This controls jitter speed\n\ 00621 \tsl = 0.025\n\ 00622 \t# This controls the amount of position jitter\n\ 00623 \tsp = 0.1\n\ 00624 \t# This controls the amount of rotation jitter\n\ 00625 \tsr = 0.25\n\ 00626 \t####################################################\n\ 00627 \n\ 00628 \ttime = Get('curtime')\n\ 00629 \tob = Scene.GetCurrent().getCurrentCamera()\n\ 00630 \tps = (sl*time, sl*time, sl*time)\n\ 00631 \t# To add jitter only when the camera moves, use this next line instead\n\ 00632 \t#ps = (sl*ob.LocX, sl*ob.LocY, sl*ob.LocZ)\n\ 00633 \trv = Noise.turbulence_vector(ps, 3, 0, Noise.NoiseTypes.NEWPERLIN)\n\ 00634 \tob.dloc = (sp*rv[0], sp*rv[1], sp*rv[2])\n\ 00635 \tob.drot = (sr*rv[0], sr*rv[1], sr*rv[2])\n\ 00636 \n" 00637 ); 00638 00639 /* Just in case, declarations for a header file */ 00640 /* 00641 static PyObject *Noise_random(PyObject *UNUSED(self)); 00642 static PyObject *Noise_random_unit_vector(PyObject *UNUSED(self)); 00643 static PyObject *Noise_seed_set(PyObject *UNUSED(self), PyObject *args); 00644 static PyObject *Noise_noise(PyObject *UNUSED(self), PyObject *args); 00645 static PyObject *Noise_vector(PyObject *UNUSED(self), PyObject *args); 00646 static PyObject *Noise_turbulence(PyObject *UNUSED(self), PyObject *args); 00647 static PyObject *Noise_turbulence_vector(PyObject *UNUSED(self), PyObject *args); 00648 static PyObject *Noise_fractal(PyObject *UNUSED(self), PyObject *args); 00649 static PyObject *Noise_multi_fractal(PyObject *UNUSED(self), PyObject *args); 00650 static PyObject *Noise_vl_vector(PyObject *UNUSED(self), PyObject *args); 00651 static PyObject *Noise_hetero_terrain(PyObject *UNUSED(self), PyObject *args); 00652 static PyObject *Noise_hybrid_multi_fractal(PyObject *UNUSED(self), PyObject *args); 00653 static PyObject *Noise_ridged_multi_fractal(PyObject *UNUSED(self), PyObject *args); 00654 static PyObject *Noise_voronoi(PyObject *UNUSED(self), PyObject *args); 00655 static PyObject *Noise_cell(PyObject *UNUSED(self), PyObject *args); 00656 static PyObject *Noise_cell_vector(PyObject *UNUSED(self), PyObject *args); 00657 */ 00658 00659 static PyMethodDef NoiseMethods[] = { 00660 {"seed_set", (PyCFunction) Noise_seed_set, METH_VARARGS, seed_set__doc__}, 00661 {"random", (PyCFunction) Noise_random, METH_NOARGS, random__doc__}, 00662 {"random_unit_vector", (PyCFunction) Noise_random_unit_vector, METH_NOARGS, random_unit_vector__doc__}, 00663 {"noise", (PyCFunction) Noise_noise, METH_VARARGS, noise__doc__}, 00664 {"vector", (PyCFunction) Noise_vector, METH_VARARGS, noise_vector__doc__}, 00665 {"turbulence", (PyCFunction) Noise_turbulence, METH_VARARGS, turbulence__doc__}, 00666 {"turbulence_vector", (PyCFunction) Noise_turbulence_vector, METH_VARARGS, turbulence_vector__doc__}, 00667 {"fractal", (PyCFunction) Noise_fractal, METH_VARARGS, fractal__doc__}, 00668 {"multi_fractal", (PyCFunction) Noise_multi_fractal, METH_VARARGS, multi_fractal__doc__}, 00669 {"vl_vector", (PyCFunction) Noise_vl_vector, METH_VARARGS, vl_vector__doc__}, 00670 {"hetero_terrain", (PyCFunction) Noise_hetero_terrain, METH_VARARGS, hetero_terrain__doc__}, 00671 {"hybrid_multi_fractal", (PyCFunction) Noise_hybrid_multi_fractal, METH_VARARGS, hybrid_multi_fractal__doc__}, 00672 {"ridged_multi_fractal", (PyCFunction) Noise_ridged_multi_fractal, METH_VARARGS, ridged_multi_fractal__doc__}, 00673 {"voronoi", (PyCFunction) Noise_voronoi, METH_VARARGS, voronoi__doc__}, 00674 {"cell", (PyCFunction) Noise_cell, METH_VARARGS, cell__doc__}, 00675 {"cell_vector", (PyCFunction) Noise_cell_vector, METH_VARARGS, cell_vector__doc__}, 00676 {NULL, NULL, 0, NULL} 00677 }; 00678 00679 /*----------------------------------------------------------------------*/ 00680 00681 static struct PyModuleDef noise_module_def = { 00682 PyModuleDef_HEAD_INIT, 00683 "noise", /* m_name */ 00684 Noise__doc__, /* m_doc */ 00685 0, /* m_size */ 00686 NoiseMethods, /* m_methods */ 00687 NULL, /* m_reload */ 00688 NULL, /* m_traverse */ 00689 NULL, /* m_clear */ 00690 NULL, /* m_free */ 00691 }; 00692 00693 PyObject *BPyInit_noise(void) 00694 { 00695 PyObject *submodule = PyModule_Create(&noise_module_def); 00696 00697 /* use current time as seed for random number generator by default */ 00698 setRndSeed(0); 00699 00700 /* Constant noisetype dictionary */ 00701 if(submodule) { 00702 static PyStructSequence_Field noise_types_fields[] = { 00703 {(char *)"BLENDER", NULL}, 00704 {(char *)"STDPERLIN", NULL}, 00705 {(char *)"NEWPERLIN", NULL}, 00706 {(char *)"VORONOI_F1", NULL}, 00707 {(char *)"VORONOI_F2", NULL}, 00708 {(char *)"VORONOI_F3", NULL}, 00709 {(char *)"VORONOI_F4", NULL}, 00710 {(char *)"VORONOI_F2F1", NULL}, 00711 {(char *)"VORONOI_CRACKLE", NULL}, 00712 {(char *)"CELLNOISE", NULL}, 00713 {NULL} 00714 }; 00715 00716 static PyStructSequence_Desc noise_types_info_desc = { 00717 (char *)"noise.types", /* name */ 00718 (char *)"Noise type", /* doc */ 00719 noise_types_fields, /* fields */ 00720 (sizeof(noise_types_fields)/sizeof(PyStructSequence_Field)) - 1 00721 }; 00722 00723 static PyTypeObject NoiseType; 00724 00725 PyObject *noise_types; 00726 00727 int pos = 0; 00728 00729 PyStructSequence_InitType(&NoiseType, &noise_types_info_desc); 00730 00731 noise_types = PyStructSequence_New(&NoiseType); 00732 if (noise_types == NULL) { 00733 return NULL; 00734 } 00735 00736 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_BLENDER)); 00737 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_STDPERLIN)); 00738 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_NEWPERLIN)); 00739 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_F1)); 00740 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_F2)); 00741 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_F3)); 00742 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_F4)); 00743 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_F2F1)); 00744 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_VORONOI_CRACKLE)); 00745 PyStructSequence_SET_ITEM(noise_types, pos++, PyLong_FromLong(TEX_CELLNOISE)); 00746 00747 PyModule_AddObject(submodule, "types", noise_types); 00748 } 00749 00750 if(submodule) { 00751 static PyStructSequence_Field distance_metrics_fields[] = { 00752 {(char *)"DISTANCE", NULL}, 00753 {(char *)"DISTANCE_SQUARED", NULL}, 00754 {(char *)"MANHATTAN", NULL}, 00755 {(char *)"CHEBYCHEV", NULL}, 00756 {(char *)"MINKOVSKY_HALF", NULL}, 00757 {(char *)"MINKOVSKY_FOUR", NULL}, 00758 {(char *)"MINKOVSKY", NULL}, 00759 {NULL} 00760 }; 00761 00762 static PyStructSequence_Desc noise_types_info_desc = { 00763 (char *)"noise.distance_metrics", /* name */ 00764 (char *)"Distance Metrics for noise module.", /* doc */ 00765 distance_metrics_fields, /* fields */ 00766 (sizeof(distance_metrics_fields)/sizeof(PyStructSequence_Field)) - 1 00767 }; 00768 00769 static PyTypeObject DistanceMetrics; 00770 00771 PyObject *distance_metrics; 00772 00773 int pos = 0; 00774 00775 PyStructSequence_InitType(&DistanceMetrics, &noise_types_info_desc); 00776 00777 distance_metrics = PyStructSequence_New(&DistanceMetrics); 00778 if (distance_metrics == NULL) { 00779 return NULL; 00780 } 00781 00782 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_DISTANCE)); 00783 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_DISTANCE_SQUARED)); 00784 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_MANHATTAN)); 00785 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_CHEBYCHEV)); 00786 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_MINKOVSKY_HALF)); 00787 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_MINKOVSKY_FOUR)); 00788 PyStructSequence_SET_ITEM(distance_metrics, pos++, PyLong_FromLong(TEX_MINKOVSKY)); 00789 00790 PyModule_AddObject(submodule, "distance_metrics", distance_metrics); 00791 } 00792 00793 return submodule; 00794 }