From 829ca86b6f7130decacfb87d8c23542f2703fc01 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Sat, 1 Apr 2017 13:59:36 -0700 Subject: [PATCH 1/2] Standardize on spaces for C files --- _noise.h | 10 +- _perlin.c | 431 +++++++++++++++++++++++++++-------------------------- _simplex.c | 357 ++++++++++++++++++++++---------------------- 3 files changed, 401 insertions(+), 397 deletions(-) diff --git a/_noise.h b/_noise.h index 95ee6c9..355d754 100644 --- a/_noise.h +++ b/_noise.h @@ -1,3 +1,5 @@ +// -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- +// // Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com) // see LICENSE.txt for details @@ -9,10 +11,10 @@ #endif const float GRAD3[][3] = { - {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, - {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, - {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}, - {1,0,-1},{-1,0,-1},{0,-1,1},{0,1,1}}; + {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, + {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, + {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}, + {1,0,-1},{-1,0,-1},{0,-1,1},{0,1,1}}; const float GRAD4[][4] = { {0,1,1,1}, {0,1,1,-1}, {0,1,-1,1}, {0,1,-1,-1}, diff --git a/_perlin.c b/_perlin.c index a535d71..57884d0 100644 --- a/_perlin.c +++ b/_perlin.c @@ -1,6 +1,7 @@ +// -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- +// // Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com) // see LICENSE.txt for details -// $Id$ #include "Python.h" #include @@ -16,260 +17,260 @@ float inline grad1(const int hash, const float x) { - float g = (hash & 7) + 1.0f; - if (hash & 8) - g = -1; - return (g * x); + float g = (hash & 7) + 1.0f; + if (hash & 8) + g = -1; + return (g * x); } float noise1(float x, const int repeat, const int base) { - float fx; - int i = (int)floorf(x) % repeat; - int ii = (i + 1) % repeat; - i = (i & 255) + base; - ii = (ii & 255) + base; + float fx; + int i = (int)floorf(x) % repeat; + int ii = (i + 1) % repeat; + i = (i & 255) + base; + ii = (ii & 255) + base; - x -= floorf(x); - fx = x*x*x * (x * (x * 6 - 15) + 10); + x -= floorf(x); + fx = x*x*x * (x * (x * 6 - 15) + 10); - return lerp(fx, grad1(PERM[i], x), grad1(PERM[ii], x - 1)) * 0.4f; + return lerp(fx, grad1(PERM[i], x), grad1(PERM[ii], x - 1)) * 0.4f; } static PyObject * py_noise1(PyObject *self, PyObject *args, PyObject *kwargs) { - float x; - int octaves = 1; - float persistence = 0.5f; + float x; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; - int repeat = 1024; // arbitrary - int base = 0; - - static char *kwlist[] = {"x", "octaves", "persistence", "lacunarity", "repeat", "base", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "f|iffii:noise1", kwlist, - &x, &octaves, &persistence, &lacunarity, &repeat, &base)) - return NULL; - - if (octaves == 1) { - // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise1(x, repeat, base)); - } else if (octaves > 1) { - int i; - float freq = 1.0f; - float amp = 1.0f; - float max = 0.0f; - float total = 0.0f; - - for (i = 0; i < octaves; i++) { - total += noise1(x * freq, (const int)(repeat * freq), base) * amp; - max += amp; - freq *= lacunarity; - amp *= persistence; - } - return (PyObject *) PyFloat_FromDouble((double) (total / max)); - } else { - PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); - return NULL; - } + int repeat = 1024; // arbitrary + int base = 0; + + static char *kwlist[] = {"x", "octaves", "persistence", "lacunarity", "repeat", "base", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "f|iffii:noise1", kwlist, + &x, &octaves, &persistence, &lacunarity, &repeat, &base)) + return NULL; + + if (octaves == 1) { + // Single octave, return simple noise + return (PyObject *) PyFloat_FromDouble((double) noise1(x, repeat, base)); + } else if (octaves > 1) { + int i; + float freq = 1.0f; + float amp = 1.0f; + float max = 0.0f; + float total = 0.0f; + + for (i = 0; i < octaves; i++) { + total += noise1(x * freq, (const int)(repeat * freq), base) * amp; + max += amp; + freq *= lacunarity; + amp *= persistence; + } + return (PyObject *) PyFloat_FromDouble((double) (total / max)); + } else { + PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); + return NULL; + } } float inline grad2(const int hash, const float x, const float y) { - const int h = hash & 15; - return x * GRAD3[h][0] + y * GRAD3[h][1]; + const int h = hash & 15; + return x * GRAD3[h][0] + y * GRAD3[h][1]; } float noise2(float x, float y, const float repeatx, const float repeaty, const int base) { - float fx, fy; - int A, AA, AB, B, BA, BB; - int i = (int)floorf(fmodf(x, repeatx)); - int j = (int)floorf(fmodf(y, repeaty)); - int ii = (int)fmodf(i + 1, repeatx); - int jj = (int)fmodf(j + 1, repeaty); - i = (i & 255) + base; - j = (j & 255) + base; - ii = (ii & 255) + base; - jj = (jj & 255) + base; - - x -= floorf(x); y -= floorf(y); - fx = x*x*x * (x * (x * 6 - 15) + 10); - fy = y*y*y * (y * (y * 6 - 15) + 10); - - A = PERM[i]; - AA = PERM[A + j]; - AB = PERM[A + jj]; - B = PERM[ii]; - BA = PERM[B + j]; - BB = PERM[B + jj]; - - return lerp(fy, lerp(fx, grad2(PERM[AA], x, y), - grad2(PERM[BA], x - 1, y)), - lerp(fx, grad2(PERM[AB], x, y - 1), - grad2(PERM[BB], x - 1, y - 1))); + float fx, fy; + int A, AA, AB, B, BA, BB; + int i = (int)floorf(fmodf(x, repeatx)); + int j = (int)floorf(fmodf(y, repeaty)); + int ii = (int)fmodf(i + 1, repeatx); + int jj = (int)fmodf(j + 1, repeaty); + i = (i & 255) + base; + j = (j & 255) + base; + ii = (ii & 255) + base; + jj = (jj & 255) + base; + + x -= floorf(x); y -= floorf(y); + fx = x*x*x * (x * (x * 6 - 15) + 10); + fy = y*y*y * (y * (y * 6 - 15) + 10); + + A = PERM[i]; + AA = PERM[A + j]; + AB = PERM[A + jj]; + B = PERM[ii]; + BA = PERM[B + j]; + BB = PERM[B + jj]; + + return lerp(fy, lerp(fx, grad2(PERM[AA], x, y), + grad2(PERM[BA], x - 1, y)), + lerp(fx, grad2(PERM[AB], x, y - 1), + grad2(PERM[BB], x - 1, y - 1))); } static PyObject * py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) { - float x, y; - int octaves = 1; - float persistence = 0.5f; + float x, y; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; - float repeatx = 1024; // arbitrary - float repeaty = 1024; // arbitrary - int base = 0; - - static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|iffffi:noise2", kwlist, - &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &base)) - return NULL; - - if (octaves == 1) { - // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise2(x, y, repeatx, repeaty, base)); - } else if (octaves > 1) { - int i; - float freq = 1.0f; - float amp = 1.0f; - float max = 0.0f; - float total = 0.0f; - - for (i = 0; i < octaves; i++) { - total += noise2(x * freq, y * freq, repeatx * freq, repeaty * freq, base) * amp; - max += amp; - freq *= lacunarity; - amp *= persistence; - } - return (PyObject *) PyFloat_FromDouble((double) (total / max)); - } else { - PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); - return NULL; - } + float repeatx = 1024; // arbitrary + float repeaty = 1024; // arbitrary + int base = 0; + + static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|iffffi:noise2", kwlist, + &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &base)) + return NULL; + + if (octaves == 1) { + // Single octave, return simple noise + return (PyObject *) PyFloat_FromDouble((double) noise2(x, y, repeatx, repeaty, base)); + } else if (octaves > 1) { + int i; + float freq = 1.0f; + float amp = 1.0f; + float max = 0.0f; + float total = 0.0f; + + for (i = 0; i < octaves; i++) { + total += noise2(x * freq, y * freq, repeatx * freq, repeaty * freq, base) * amp; + max += amp; + freq *= lacunarity; + amp *= persistence; + } + return (PyObject *) PyFloat_FromDouble((double) (total / max)); + } else { + PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); + return NULL; + } } float inline grad3(const int hash, const float x, const float y, const float z) { - const int h = hash & 15; - return x * GRAD3[h][0] + y * GRAD3[h][1] + z * GRAD3[h][2]; + const int h = hash & 15; + return x * GRAD3[h][0] + y * GRAD3[h][1] + z * GRAD3[h][2]; } float noise3(float x, float y, float z, const int repeatx, const int repeaty, const int repeatz, - const int base) + const int base) { - float fx, fy, fz; - int A, AA, AB, B, BA, BB; - int i = (int)floorf(fmodf(x, repeatx)); - int j = (int)floorf(fmodf(y, repeaty)); - int k = (int)floorf(fmodf(z, repeatz)); - int ii = (int)fmodf(i + 1, repeatx); - int jj = (int)fmodf(j + 1, repeaty); - int kk = (int)fmodf(k + 1, repeatz); - i = (i & 255) + base; - j = (j & 255) + base; - k = (k & 255) + base; - ii = (ii & 255) + base; - jj = (jj & 255) + base; - kk = (kk & 255) + base; - - x -= floorf(x); y -= floorf(y); z -= floorf(z); - fx = x*x*x * (x * (x * 6 - 15) + 10); - fy = y*y*y * (y * (y * 6 - 15) + 10); - fz = z*z*z * (z * (z * 6 - 15) + 10); - - A = PERM[i]; - AA = PERM[A + j]; - AB = PERM[A + jj]; - B = PERM[ii]; - BA = PERM[B + j]; - BB = PERM[B + jj]; - - return lerp(fz, lerp(fy, lerp(fx, grad3(PERM[AA + k], x, y, z), - grad3(PERM[BA + k], x - 1, y, z)), - lerp(fx, grad3(PERM[AB + k], x, y - 1, z), - grad3(PERM[BB + k], x - 1, y - 1, z))), - lerp(fy, lerp(fx, grad3(PERM[AA + kk], x, y, z - 1), - grad3(PERM[BA + kk], x - 1, y, z - 1)), - lerp(fx, grad3(PERM[AB + kk], x, y - 1, z - 1), - grad3(PERM[BB + kk], x - 1, y - 1, z - 1)))); + float fx, fy, fz; + int A, AA, AB, B, BA, BB; + int i = (int)floorf(fmodf(x, repeatx)); + int j = (int)floorf(fmodf(y, repeaty)); + int k = (int)floorf(fmodf(z, repeatz)); + int ii = (int)fmodf(i + 1, repeatx); + int jj = (int)fmodf(j + 1, repeaty); + int kk = (int)fmodf(k + 1, repeatz); + i = (i & 255) + base; + j = (j & 255) + base; + k = (k & 255) + base; + ii = (ii & 255) + base; + jj = (jj & 255) + base; + kk = (kk & 255) + base; + + x -= floorf(x); y -= floorf(y); z -= floorf(z); + fx = x*x*x * (x * (x * 6 - 15) + 10); + fy = y*y*y * (y * (y * 6 - 15) + 10); + fz = z*z*z * (z * (z * 6 - 15) + 10); + + A = PERM[i]; + AA = PERM[A + j]; + AB = PERM[A + jj]; + B = PERM[ii]; + BA = PERM[B + j]; + BB = PERM[B + jj]; + + return lerp(fz, lerp(fy, lerp(fx, grad3(PERM[AA + k], x, y, z), + grad3(PERM[BA + k], x - 1, y, z)), + lerp(fx, grad3(PERM[AB + k], x, y - 1, z), + grad3(PERM[BB + k], x - 1, y - 1, z))), + lerp(fy, lerp(fx, grad3(PERM[AA + kk], x, y, z - 1), + grad3(PERM[BA + kk], x - 1, y, z - 1)), + lerp(fx, grad3(PERM[AB + kk], x, y - 1, z - 1), + grad3(PERM[BB + kk], x - 1, y - 1, z - 1)))); } static PyObject * py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) { - float x, y, z; - int octaves = 1; - float persistence = 0.5f; + float x, y, z; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; - int repeatx = 1024; // arbitrary - int repeaty = 1024; // arbitrary - int repeatz = 1024; // arbitrary - int base = 0; - - static char *kwlist[] = {"x", "y", "z", "octaves", "persistence", "lacunarity", - "repeatx", "repeaty", "repeatz", "base", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iffiiii:noise3", kwlist, - &x, &y, &z, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &repeatz, &base)) - return NULL; - - if (octaves == 1) { - // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z, - repeatx, repeaty, repeatz, base)); - } else if (octaves > 1) { - int i; - float freq = 1.0f; - float amp = 1.0f; - float max = 0.0f; - float total = 0.0f; - - for (i = 0; i < octaves; i++) { - total += noise3(x * freq, y * freq, z * freq, - (const int)(repeatx*freq), (const int)(repeaty*freq), (const int)(repeatz*freq), base) * amp; - max += amp; - freq *= lacunarity; - amp *= persistence; - } - return (PyObject *) PyFloat_FromDouble((double) (total / max)); - } else { - PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); - return NULL; - } + int repeatx = 1024; // arbitrary + int repeaty = 1024; // arbitrary + int repeatz = 1024; // arbitrary + int base = 0; + + static char *kwlist[] = {"x", "y", "z", "octaves", "persistence", "lacunarity", + "repeatx", "repeaty", "repeatz", "base", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iffiiii:noise3", kwlist, + &x, &y, &z, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &repeatz, &base)) + return NULL; + + if (octaves == 1) { + // Single octave, return simple noise + return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z, + repeatx, repeaty, repeatz, base)); + } else if (octaves > 1) { + int i; + float freq = 1.0f; + float amp = 1.0f; + float max = 0.0f; + float total = 0.0f; + + for (i = 0; i < octaves; i++) { + total += noise3(x * freq, y * freq, z * freq, + (const int)(repeatx*freq), (const int)(repeaty*freq), (const int)(repeatz*freq), base) * amp; + max += amp; + freq *= lacunarity; + amp *= persistence; + } + return (PyObject *) PyFloat_FromDouble((double) (total / max)); + } else { + PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); + return NULL; + } } static PyMethodDef perlin_functions[] = { - {"noise1", (PyCFunction) py_noise1, METH_VARARGS | METH_KEYWORDS, - "noise1(x, octaves=1, persistence=0.5, lacunarity=2.0, repeat=1024, base=0.0)\n\n" - "1 dimensional perlin improved noise function (see noise3 for more info)"}, - {"noise2", (PyCFunction) py_noise2, METH_VARARGS | METH_KEYWORDS, - "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=0.0)\n\n" - "2 dimensional perlin improved noise function (see noise3 for more info)"}, - {"noise3", (PyCFunction) py_noise3, METH_VARARGS | METH_KEYWORDS, - "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0, " - "repeatx=1024, repeaty=1024, repeatz=1024, base=0.0)\n\n" - "return perlin \"improved\" noise value for specified coordinate\n\n" - "octaves -- specifies the number of passes for generating fBm noise,\n" - "defaults to 1 (simple noise).\n\n" - "persistence -- specifies the amplitude of each successive octave relative\n" - "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" - "is halved). Note the amplitude of the first pass is always 1.0.\n\n" + {"noise1", (PyCFunction) py_noise1, METH_VARARGS | METH_KEYWORDS, + "noise1(x, octaves=1, persistence=0.5, lacunarity=2.0, repeat=1024, base=0.0)\n\n" + "1 dimensional perlin improved noise function (see noise3 for more info)"}, + {"noise2", (PyCFunction) py_noise2, METH_VARARGS | METH_KEYWORDS, + "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=0.0)\n\n" + "2 dimensional perlin improved noise function (see noise3 for more info)"}, + {"noise3", (PyCFunction) py_noise3, METH_VARARGS | METH_KEYWORDS, + "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0, " + "repeatx=1024, repeaty=1024, repeatz=1024, base=0.0)\n\n" + "return perlin \"improved\" noise value for specified coordinate\n\n" + "octaves -- specifies the number of passes for generating fBm noise,\n" + "defaults to 1 (simple noise).\n\n" + "persistence -- specifies the amplitude of each successive octave relative\n" + "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" + "is halved). Note the amplitude of the first pass is always 1.0.\n\n" "lacunarity -- specifies the frequency of each successive octave relative\n" "to the one below it, similar to persistence. Defaults to 2.0.\n\n" - "repeatx, repeaty, repeatz -- specifies the interval along each axis when \n" - "the noise values repeat. This can be used as the tile size for creating \n" - "tileable textures\n\n" - "base -- specifies a fixed offset for the input coordinates. Useful for\n" - "generating different noise textures with the same repeat interval"}, - {NULL} + "repeatx, repeaty, repeatz -- specifies the interval along each axis when \n" + "the noise values repeat. This can be used as the tile size for creating \n" + "tileable textures\n\n" + "base -- specifies a fixed offset for the input coordinates. Useful for\n" + "generating different noise textures with the same repeat interval"}, + {NULL} }; PyDoc_STRVAR(module_doc, "Native-code tileable Perlin \"improved\" noise functions"); @@ -277,15 +278,15 @@ PyDoc_STRVAR(module_doc, "Native-code tileable Perlin \"improved\" noise functio #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_perlin", - module_doc, - -1, /* m_size */ - perlin_functions, /* m_methods */ - NULL, /* m_reload (unused) */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL /* m_free */ + PyModuleDef_HEAD_INIT, + "_perlin", + module_doc, + -1, /* m_size */ + perlin_functions, /* m_methods */ + NULL, /* m_reload (unused) */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL /* m_free */ }; PyObject * @@ -299,7 +300,7 @@ PyInit__perlin(void) void init_perlin(void) { - Py_InitModule3("_perlin", perlin_functions, module_doc); + Py_InitModule3("_perlin", perlin_functions, module_doc); } #endif diff --git a/_simplex.c b/_simplex.c index 0ec88d5..11d6350 100644 --- a/_simplex.c +++ b/_simplex.c @@ -1,6 +1,7 @@ +// -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- +// // Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com) // see LICENSE.txt for details -// $Id$ #include "Python.h" #include @@ -14,41 +15,41 @@ float noise2(float x, float y) { - int i1, j1, I, J, c; - float s = (x + y) * F2; - float i = floorf(x + s); - float j = floorf(y + s); - float t = (i + j) * G2; - - float xx[3], yy[3], f[3]; - float noise[3] = {0.0f, 0.0f, 0.0f}; - int g[3]; - - xx[0] = x - (i - t); - yy[0] = y - (j - t); - - i1 = xx[0] > yy[0]; - j1 = xx[0] <= yy[0]; - - xx[2] = xx[0] + G2 * 2.0f - 1.0f; - yy[2] = yy[0] + G2 * 2.0f - 1.0f; - xx[1] = xx[0] - i1 + G2; - yy[1] = yy[0] - j1 + G2; - - I = (int) i & 255; - J = (int) j & 255; - g[0] = PERM[I + PERM[J]] % 12; - g[1] = PERM[I + i1 + PERM[J + j1]] % 12; - g[2] = PERM[I + 1 + PERM[J + 1]] % 12; - - for (c = 0; c <= 2; c++) - f[c] = 0.5f - xx[c]*xx[c] - yy[c]*yy[c]; - - for (c = 0; c <= 2; c++) - if (f[c] > 0) - noise[c] = f[c]*f[c]*f[c]*f[c] * (GRAD3[g[c]][0]*xx[c] + GRAD3[g[c]][1]*yy[c]); - - return (noise[0] + noise[1] + noise[2]) * 70.0f; + int i1, j1, I, J, c; + float s = (x + y) * F2; + float i = floorf(x + s); + float j = floorf(y + s); + float t = (i + j) * G2; + + float xx[3], yy[3], f[3]; + float noise[3] = {0.0f, 0.0f, 0.0f}; + int g[3]; + + xx[0] = x - (i - t); + yy[0] = y - (j - t); + + i1 = xx[0] > yy[0]; + j1 = xx[0] <= yy[0]; + + xx[2] = xx[0] + G2 * 2.0f - 1.0f; + yy[2] = yy[0] + G2 * 2.0f - 1.0f; + xx[1] = xx[0] - i1 + G2; + yy[1] = yy[0] - j1 + G2; + + I = (int) i & 255; + J = (int) j & 255; + g[0] = PERM[I + PERM[J]] % 12; + g[1] = PERM[I + i1 + PERM[J + j1]] % 12; + g[2] = PERM[I + 1 + PERM[J + 1]] % 12; + + for (c = 0; c <= 2; c++) + f[c] = 0.5f - xx[c]*xx[c] - yy[c]*yy[c]; + + for (c = 0; c <= 2; c++) + if (f[c] > 0) + noise[c] = f[c]*f[c]*f[c]*f[c] * (GRAD3[g[c]][0]*xx[c] + GRAD3[g[c]][1]*yy[c]); + + return (noise[0] + noise[1] + noise[2]) * 70.0f; } #define dot3(v1, v2) ((v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2]) @@ -61,69 +62,69 @@ noise2(float x, float y) float noise3(float x, float y, float z) { - int c, o1[3], o2[3], g[4], I, J, K; - float f[4], noise[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - float s = (x + y + z) * F3; - float i = floorf(x + s); - float j = floorf(y + s); - float k = floorf(z + s); - float t = (i + j + k) * G3; - - float pos[4][3]; - - pos[0][0] = x - (i - t); - pos[0][1] = y - (j - t); - pos[0][2] = z - (k - t); - - if (pos[0][0] >= pos[0][1]) { - if (pos[0][1] >= pos[0][2]) { - ASSIGN(o1, 1, 0, 0); - ASSIGN(o2, 1, 1, 0); - } else if (pos[0][0] >= pos[0][2]) { - ASSIGN(o1, 1, 0, 0); - ASSIGN(o2, 1, 0, 1); - } else { - ASSIGN(o1, 0, 0, 1); - ASSIGN(o2, 1, 0, 1); - } - } else { - if (pos[0][1] < pos[0][2]) { - ASSIGN(o1, 0, 0, 1); - ASSIGN(o2, 0, 1, 1); - } else if (pos[0][0] < pos[0][2]) { - ASSIGN(o1, 0, 1, 0); - ASSIGN(o2, 0, 1, 1); - } else { - ASSIGN(o1, 0, 1, 0); - ASSIGN(o2, 1, 1, 0); - } - } - - for (c = 0; c <= 2; c++) { - pos[3][c] = pos[0][c] - 1.0f + 3.0f * G3; - pos[2][c] = pos[0][c] - o2[c] + 2.0f * G3; - pos[1][c] = pos[0][c] - o1[c] + G3; - } - - I = (int) i & 255; - J = (int) j & 255; - K = (int) k & 255; - g[0] = PERM[I + PERM[J + PERM[K]]] % 12; - g[1] = PERM[I + o1[0] + PERM[J + o1[1] + PERM[o1[2] + K]]] % 12; - g[2] = PERM[I + o2[0] + PERM[J + o2[1] + PERM[o2[2] + K]]] % 12; - g[3] = PERM[I + 1 + PERM[J + 1 + PERM[K + 1]]] % 12; - - for (c = 0; c <= 3; c++) { - f[c] = 0.6f - pos[c][0]*pos[c][0] - pos[c][1]*pos[c][1] - pos[c][2]*pos[c][2]; - } - - for (c = 0; c <= 3; c++) { - if (f[c] > 0) { - noise[c] = f[c]*f[c]*f[c]*f[c] * dot3(pos[c], GRAD3[g[c]]); - } - } - - return (noise[0] + noise[1] + noise[2] + noise[3]) * 32.0f; + int c, o1[3], o2[3], g[4], I, J, K; + float f[4], noise[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + float s = (x + y + z) * F3; + float i = floorf(x + s); + float j = floorf(y + s); + float k = floorf(z + s); + float t = (i + j + k) * G3; + + float pos[4][3]; + + pos[0][0] = x - (i - t); + pos[0][1] = y - (j - t); + pos[0][2] = z - (k - t); + + if (pos[0][0] >= pos[0][1]) { + if (pos[0][1] >= pos[0][2]) { + ASSIGN(o1, 1, 0, 0); + ASSIGN(o2, 1, 1, 0); + } else if (pos[0][0] >= pos[0][2]) { + ASSIGN(o1, 1, 0, 0); + ASSIGN(o2, 1, 0, 1); + } else { + ASSIGN(o1, 0, 0, 1); + ASSIGN(o2, 1, 0, 1); + } + } else { + if (pos[0][1] < pos[0][2]) { + ASSIGN(o1, 0, 0, 1); + ASSIGN(o2, 0, 1, 1); + } else if (pos[0][0] < pos[0][2]) { + ASSIGN(o1, 0, 1, 0); + ASSIGN(o2, 0, 1, 1); + } else { + ASSIGN(o1, 0, 1, 0); + ASSIGN(o2, 1, 1, 0); + } + } + + for (c = 0; c <= 2; c++) { + pos[3][c] = pos[0][c] - 1.0f + 3.0f * G3; + pos[2][c] = pos[0][c] - o2[c] + 2.0f * G3; + pos[1][c] = pos[0][c] - o1[c] + G3; + } + + I = (int) i & 255; + J = (int) j & 255; + K = (int) k & 255; + g[0] = PERM[I + PERM[J + PERM[K]]] % 12; + g[1] = PERM[I + o1[0] + PERM[J + o1[1] + PERM[o1[2] + K]]] % 12; + g[2] = PERM[I + o2[0] + PERM[J + o2[1] + PERM[o2[2] + K]]] % 12; + g[3] = PERM[I + 1 + PERM[J + 1 + PERM[K + 1]]] % 12; + + for (c = 0; c <= 3; c++) { + f[c] = 0.6f - pos[c][0]*pos[c][0] - pos[c][1]*pos[c][1] - pos[c][2]*pos[c][2]; + } + + for (c = 0; c <= 3; c++) { + if (f[c] > 0) { + noise[c] = f[c]*f[c]*f[c]*f[c] * dot3(pos[c], GRAD3[g[c]]); + } + } + + return (noise[0] + noise[1] + noise[2] + noise[3]) * 32.0f; } inline float @@ -256,25 +257,25 @@ fbm_noise4(float x, float y, float z, float w, int octaves, float persistence, f static PyObject * py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) { - float x, y; - int octaves = 1; - float persistence = 0.5f; + float x, y; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; float repeatx = FLT_MAX; float repeaty = FLT_MAX; float z = 0.0f; - static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", + static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|ifffff:snoise2", kwlist, - &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &z)) { - return NULL; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|ifffff:snoise2", kwlist, + &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &z)) { + return NULL; } if (octaves <= 0) { PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); return NULL; } - + if (repeatx == FLT_MAX && repeaty == FLT_MAX) { // Flat noise, no tiling float freq = 1.0f; @@ -324,89 +325,89 @@ py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) static PyObject * py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) { - float x, y, z; - int octaves = 1; - float persistence = 0.5f; + float x, y, z; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; - static char *kwlist[] = {"x", "y", "z", "octaves", "persistence", "lacunarity", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iff:snoise3", kwlist, - &x, &y, &z, &octaves, &persistence, &lacunarity)) - return NULL; - - if (octaves == 1) { - // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z)); - } else if (octaves > 1) { - return (PyObject *) PyFloat_FromDouble( + static char *kwlist[] = {"x", "y", "z", "octaves", "persistence", "lacunarity", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iff:snoise3", kwlist, + &x, &y, &z, &octaves, &persistence, &lacunarity)) + return NULL; + + if (octaves == 1) { + // Single octave, return simple noise + return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z)); + } else if (octaves > 1) { + return (PyObject *) PyFloat_FromDouble( (double) fbm_noise3(x, y, z, octaves, persistence, lacunarity)); - } else { - PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); - return NULL; - } + } else { + PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); + return NULL; + } } static PyObject * py_noise4(PyObject *self, PyObject *args, PyObject *kwargs) { - float x, y, z, w; - int octaves = 1; - float persistence = 0.5f; + float x, y, z, w; + int octaves = 1; + float persistence = 0.5f; float lacunarity = 2.0f; - static char *kwlist[] = {"x", "y", "z", "w", "octaves", "persistence", "lacunarity", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff|iff:snoise4", kwlist, - &x, &y, &z, &w, &octaves, &persistence)) - return NULL; - - if (octaves == 1) { - // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise4(x, y, z, w)); - } else if (octaves > 1) { - return (PyObject *) PyFloat_FromDouble( + static char *kwlist[] = {"x", "y", "z", "w", "octaves", "persistence", "lacunarity", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff|iff:snoise4", kwlist, + &x, &y, &z, &w, &octaves, &persistence)) + return NULL; + + if (octaves == 1) { + // Single octave, return simple noise + return (PyObject *) PyFloat_FromDouble((double) noise4(x, y, z, w)); + } else if (octaves > 1) { + return (PyObject *) PyFloat_FromDouble( (double) fbm_noise4(x, y, z, w, octaves, persistence, lacunarity)); - } else { - PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); - return NULL; - } + } else { + PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); + return NULL; + } } static PyMethodDef simplex_functions[] = { - {"noise2", (PyCFunction)py_noise2, METH_VARARGS | METH_KEYWORDS, - "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=None, repeaty=None, base=0.0) " + {"noise2", (PyCFunction)py_noise2, METH_VARARGS | METH_KEYWORDS, + "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=None, repeaty=None, base=0.0) " "return simplex noise value for specified 2D coordinate.\n\n" - "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" - "persistence -- specifies the amplitude of each successive octave relative\n" - "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" - "is halved). Note the amplitude of the first pass is always 1.0.\n\n" + "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" + "persistence -- specifies the amplitude of each successive octave relative\n" + "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" + "is halved). Note the amplitude of the first pass is always 1.0.\n\n" "lacunarity -- specifies the frequency of each successive octave relative\n" "to the one below it, similar to persistence. Defaults to 2.0.\n\n" "repeatx, repeaty -- specifies the interval along each axis when \n" - "the noise values repeat. This can be used as the tile size for creating \n" - "tileable textures\n\n" - "base -- specifies a fixed offset for the noise coordinates. Useful for\n" - "generating different noise textures with the same repeat interval"}, - {"noise3", (PyCFunction)py_noise3, METH_VARARGS | METH_KEYWORDS, - "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " - "specified 3D coordinate\n\n" - "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" - "persistence -- specifies the amplitude of each successive octave relative\n" - "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" - "is halved). Note the amplitude of the first pass is always 1.0.\n\n" + "the noise values repeat. This can be used as the tile size for creating \n" + "tileable textures\n\n" + "base -- specifies a fixed offset for the noise coordinates. Useful for\n" + "generating different noise textures with the same repeat interval"}, + {"noise3", (PyCFunction)py_noise3, METH_VARARGS | METH_KEYWORDS, + "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " + "specified 3D coordinate\n\n" + "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" + "persistence -- specifies the amplitude of each successive octave relative\n" + "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" + "is halved). Note the amplitude of the first pass is always 1.0.\n\n" "lacunarity -- specifies the frequency of each successive octave relative\n" "to the one below it, similar to persistence. Defaults to 2.0."}, - {"noise4", (PyCFunction)py_noise4, METH_VARARGS | METH_KEYWORDS, - "noise4(x, y, z, w, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " - "specified 4D coordinate\n\n" - "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" - "persistence -- specifies the amplitude of each successive octave relative\n" - "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" - "is halved). Note the amplitude of the first pass is always 1.0.\n\n" + {"noise4", (PyCFunction)py_noise4, METH_VARARGS | METH_KEYWORDS, + "noise4(x, y, z, w, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " + "specified 4D coordinate\n\n" + "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" + "persistence -- specifies the amplitude of each successive octave relative\n" + "to the one below it. Defaults to 0.5 (each higher octave's amplitude\n" + "is halved). Note the amplitude of the first pass is always 1.0.\n\n" "lacunarity -- specifies the frequency of each successive octave relative\n" "to the one below it, similar to persistence. Defaults to 2.0."}, - {NULL} + {NULL} }; PyDoc_STRVAR(module_doc, "Native-code simplex noise functions"); @@ -414,15 +415,15 @@ PyDoc_STRVAR(module_doc, "Native-code simplex noise functions"); #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "_simplex", - module_doc, - -1, /* m_size */ - simplex_functions, /* m_methods */ - NULL, /* m_reload (unused) */ - NULL, /* m_traverse */ - NULL, /* m_clear */ - NULL /* m_free */ + PyModuleDef_HEAD_INIT, + "_simplex", + module_doc, + -1, /* m_size */ + simplex_functions, /* m_methods */ + NULL, /* m_reload (unused) */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL /* m_free */ }; PyObject * @@ -436,7 +437,7 @@ PyInit__simplex(void) void init_simplex(void) { - Py_InitModule3("_simplex", simplex_functions, module_doc); + Py_InitModule3("_simplex", simplex_functions, module_doc); } #endif From 174dcb93a0736d50ea96eafe5c2d34c53cc3710c Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Sat, 1 Apr 2017 14:01:58 -0700 Subject: [PATCH 2/2] Remove trailing whitespace --- _noise.h | 7 +++---- _perlin.c | 22 +++++++++++----------- _simplex.c | 46 +++++++++++++++++++++++----------------------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/_noise.h b/_noise.h index 355d754..0f38323 100644 --- a/_noise.h +++ b/_noise.h @@ -11,8 +11,8 @@ #endif const float GRAD3[][3] = { - {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, - {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, + {1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0}, + {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1}, {0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}, {1,0,-1},{-1,0,-1},{0,-1,1},{0,1,1}}; @@ -70,7 +70,7 @@ const unsigned char SIMPLEX[][4] = { {0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},{1,0,2,3},{1,0,3,2},{0,0,0,0}, {0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},{0,0,0,0},{0,0,0,0}, {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,1,3}, - {0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0}, + {0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0}, {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1}, {3,2,1,0}}; @@ -106,4 +106,3 @@ inline float fast_cos(float x) { return fast_sin(x + 0.5f); } - diff --git a/_perlin.c b/_perlin.c index 57884d0..1343023 100644 --- a/_perlin.c +++ b/_perlin.c @@ -53,7 +53,7 @@ py_noise1(PyObject *self, PyObject *args, PyObject *kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "f|iffii:noise1", kwlist, &x, &octaves, &persistence, &lacunarity, &repeat, &base)) return NULL; - + if (octaves == 1) { // Single octave, return simple noise return (PyObject *) PyFloat_FromDouble((double) noise1(x, repeat, base)); @@ -108,7 +108,7 @@ noise2(float x, float y, const float repeatx, const float repeaty, const int bas B = PERM[ii]; BA = PERM[B + j]; BB = PERM[B + jj]; - + return lerp(fy, lerp(fx, grad2(PERM[AA], x, y), grad2(PERM[BA], x - 1, y)), lerp(fx, grad2(PERM[AB], x, y - 1), @@ -131,7 +131,7 @@ py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|iffffi:noise2", kwlist, &x, &y, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &base)) return NULL; - + if (octaves == 1) { // Single octave, return simple noise return (PyObject *) PyFloat_FromDouble((double) noise2(x, y, repeatx, repeaty, base)); @@ -163,7 +163,7 @@ grad3(const int hash, const float x, const float y, const float z) } float -noise3(float x, float y, float z, const int repeatx, const int repeaty, const int repeatz, +noise3(float x, float y, float z, const int repeatx, const int repeaty, const int repeatz, const int base) { float fx, fy, fz; @@ -192,7 +192,7 @@ noise3(float x, float y, float z, const int repeatx, const int repeaty, const in B = PERM[ii]; BA = PERM[B + j]; BB = PERM[B + jj]; - + return lerp(fz, lerp(fy, lerp(fx, grad3(PERM[AA + k], x, y, z), grad3(PERM[BA + k], x - 1, y, z)), lerp(fx, grad3(PERM[AB + k], x, y - 1, z), @@ -221,10 +221,10 @@ py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iffiiii:noise3", kwlist, &x, &y, &z, &octaves, &persistence, &lacunarity, &repeatx, &repeaty, &repeatz, &base)) return NULL; - + if (octaves == 1) { // Single octave, return simple noise - return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z, + return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z, repeatx, repeaty, repeatz, base)); } else if (octaves > 1) { int i; @@ -234,7 +234,7 @@ py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) float total = 0.0f; for (i = 0; i < octaves; i++) { - total += noise3(x * freq, y * freq, z * freq, + total += noise3(x * freq, y * freq, z * freq, (const int)(repeatx*freq), (const int)(repeaty*freq), (const int)(repeatz*freq), base) * amp; max += amp; freq *= lacunarity; @@ -248,13 +248,13 @@ py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) } static PyMethodDef perlin_functions[] = { - {"noise1", (PyCFunction) py_noise1, METH_VARARGS | METH_KEYWORDS, + {"noise1", (PyCFunction) py_noise1, METH_VARARGS | METH_KEYWORDS, "noise1(x, octaves=1, persistence=0.5, lacunarity=2.0, repeat=1024, base=0.0)\n\n" "1 dimensional perlin improved noise function (see noise3 for more info)"}, - {"noise2", (PyCFunction) py_noise2, METH_VARARGS | METH_KEYWORDS, + {"noise2", (PyCFunction) py_noise2, METH_VARARGS | METH_KEYWORDS, "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=0.0)\n\n" "2 dimensional perlin improved noise function (see noise3 for more info)"}, - {"noise3", (PyCFunction) py_noise3, METH_VARARGS | METH_KEYWORDS, + {"noise3", (PyCFunction) py_noise3, METH_VARARGS | METH_KEYWORDS, "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0, " "repeatx=1024, repeaty=1024, repeatz=1024, base=0.0)\n\n" "return perlin \"improved\" noise value for specified coordinate\n\n" diff --git a/_simplex.c b/_simplex.c index 11d6350..7cb0c37 100644 --- a/_simplex.c +++ b/_simplex.c @@ -12,8 +12,8 @@ #define F2 0.3660254037844386f // 0.5 * (sqrt(3.0) - 1.0) #define G2 0.21132486540518713f // (3.0 - sqrt(3.0)) / 6.0 -float -noise2(float x, float y) +float +noise2(float x, float y) { int i1, j1, I, J, c; float s = (x + y) * F2; @@ -44,11 +44,11 @@ noise2(float x, float y) for (c = 0; c <= 2; c++) f[c] = 0.5f - xx[c]*xx[c] - yy[c]*yy[c]; - + for (c = 0; c <= 2; c++) if (f[c] > 0) noise[c] = f[c]*f[c]*f[c]*f[c] * (GRAD3[g[c]][0]*xx[c] + GRAD3[g[c]][1]*yy[c]); - + return (noise[0] + noise[1] + noise[2]) * 70.0f; } @@ -59,8 +59,8 @@ noise2(float x, float y) #define F3 (1.0f / 3.0f) #define G3 (1.0f / 6.0f) -float -noise3(float x, float y, float z) +float +noise3(float x, float y, float z) { int c, o1[3], o2[3], g[4], I, J, K; float f[4], noise[4] = {0.0f, 0.0f, 0.0f, 0.0f}; @@ -99,31 +99,31 @@ noise3(float x, float y, float z) ASSIGN(o2, 1, 1, 0); } } - + for (c = 0; c <= 2; c++) { pos[3][c] = pos[0][c] - 1.0f + 3.0f * G3; pos[2][c] = pos[0][c] - o2[c] + 2.0f * G3; pos[1][c] = pos[0][c] - o1[c] + G3; } - I = (int) i & 255; - J = (int) j & 255; + I = (int) i & 255; + J = (int) j & 255; K = (int) k & 255; g[0] = PERM[I + PERM[J + PERM[K]]] % 12; g[1] = PERM[I + o1[0] + PERM[J + o1[1] + PERM[o1[2] + K]]] % 12; g[2] = PERM[I + o2[0] + PERM[J + o2[1] + PERM[o2[2] + K]]] % 12; - g[3] = PERM[I + 1 + PERM[J + 1 + PERM[K + 1]]] % 12; + g[3] = PERM[I + 1 + PERM[J + 1 + PERM[K + 1]]] % 12; for (c = 0; c <= 3; c++) { f[c] = 0.6f - pos[c][0]*pos[c][0] - pos[c][1]*pos[c][1] - pos[c][2]*pos[c][2]; } - + for (c = 0; c <= 3; c++) { if (f[c] > 0) { noise[c] = f[c]*f[c]*f[c]*f[c] * dot3(pos[c], GRAD3[g[c]]); } } - + return (noise[0] + noise[1] + noise[2] + noise[3]) * 32.0f; } @@ -149,7 +149,7 @@ fbm_noise3(float x, float y, float z, int octaves, float persistence, float lacu #define F4 0.30901699437494745f /* (sqrt(5.0) - 1.0) / 4.0 */ #define G4 0.1381966011250105f /* (5.0 - sqrt(5.0)) / 20.0 */ -float +float noise4(float x, float y, float z, float w) { float noise[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; @@ -201,9 +201,9 @@ noise4(float x, float y, float z, float w) { int K = (int)k & 255; int L = (int)l & 255; int gi0 = PERM[I + PERM[J + PERM[K + PERM[L]]]] & 0x1f; - int gi1 = PERM[I + i1 + PERM[J + j1 + PERM[K + k1 + PERM[L + l1]]]] & 0x1f; - int gi2 = PERM[I + i2 + PERM[J + j2 + PERM[K + k2 + PERM[L + l2]]]] & 0x1f; - int gi3 = PERM[I + i3 + PERM[J + j3 + PERM[K + k3 + PERM[L + l3]]]] & 0x1f; + int gi1 = PERM[I + i1 + PERM[J + j1 + PERM[K + k1 + PERM[L + l1]]]] & 0x1f; + int gi2 = PERM[I + i2 + PERM[J + j2 + PERM[K + k2 + PERM[L + l2]]]] & 0x1f; + int gi3 = PERM[I + i3 + PERM[J + j3 + PERM[K + k3 + PERM[L + l3]]]] & 0x1f; int gi4 = PERM[I + 1 + PERM[J + 1 + PERM[K + 1 + PERM[L + 1]]]] & 0x1f; float t0, t1, t2, t3, t4; @@ -264,7 +264,7 @@ py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) float repeatx = FLT_MAX; float repeaty = FLT_MAX; float z = 0.0f; - static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", + static char *kwlist[] = {"x", "y", "octaves", "persistence", "lacunarity", "repeatx", "repeaty", "base", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ff|ifffff:snoise2", kwlist, @@ -275,7 +275,7 @@ py_noise2(PyObject *self, PyObject *args, PyObject *kwargs) PyErr_SetString(PyExc_ValueError, "Expected octaves value > 0"); return NULL; } - + if (repeatx == FLT_MAX && repeaty == FLT_MAX) { // Flat noise, no tiling float freq = 1.0f; @@ -335,7 +335,7 @@ py_noise3(PyObject *self, PyObject *args, PyObject *kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "fff|iff:snoise3", kwlist, &x, &y, &z, &octaves, &persistence, &lacunarity)) return NULL; - + if (octaves == 1) { // Single octave, return simple noise return (PyObject *) PyFloat_FromDouble((double) noise3(x, y, z)); @@ -361,7 +361,7 @@ py_noise4(PyObject *self, PyObject *args, PyObject *kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff|iff:snoise4", kwlist, &x, &y, &z, &w, &octaves, &persistence)) return NULL; - + if (octaves == 1) { // Single octave, return simple noise return (PyObject *) PyFloat_FromDouble((double) noise4(x, y, z, w)); @@ -375,7 +375,7 @@ py_noise4(PyObject *self, PyObject *args, PyObject *kwargs) } static PyMethodDef simplex_functions[] = { - {"noise2", (PyCFunction)py_noise2, METH_VARARGS | METH_KEYWORDS, + {"noise2", (PyCFunction)py_noise2, METH_VARARGS | METH_KEYWORDS, "noise2(x, y, octaves=1, persistence=0.5, lacunarity=2.0, repeatx=None, repeaty=None, base=0.0) " "return simplex noise value for specified 2D coordinate.\n\n" "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" @@ -389,7 +389,7 @@ static PyMethodDef simplex_functions[] = { "tileable textures\n\n" "base -- specifies a fixed offset for the noise coordinates. Useful for\n" "generating different noise textures with the same repeat interval"}, - {"noise3", (PyCFunction)py_noise3, METH_VARARGS | METH_KEYWORDS, + {"noise3", (PyCFunction)py_noise3, METH_VARARGS | METH_KEYWORDS, "noise3(x, y, z, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " "specified 3D coordinate\n\n" "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n" @@ -398,7 +398,7 @@ static PyMethodDef simplex_functions[] = { "is halved). Note the amplitude of the first pass is always 1.0.\n\n" "lacunarity -- specifies the frequency of each successive octave relative\n" "to the one below it, similar to persistence. Defaults to 2.0."}, - {"noise4", (PyCFunction)py_noise4, METH_VARARGS | METH_KEYWORDS, + {"noise4", (PyCFunction)py_noise4, METH_VARARGS | METH_KEYWORDS, "noise4(x, y, z, w, octaves=1, persistence=0.5, lacunarity=2.0) return simplex noise value for " "specified 4D coordinate\n\n" "octaves -- specifies the number of passes, defaults to 1 (simple noise).\n\n"