-
Notifications
You must be signed in to change notification settings - Fork 70
/
_perlin.c
305 lines (265 loc) · 8.63 KB
/
_perlin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2008, Casey Duncan (casey dot duncan at gmail dot com)
// see LICENSE.txt for details
// $Id$
#include "Python.h"
#include <math.h>
#include <stdio.h>
#include "_noise.h"
#ifdef _MSC_VER
#define inline __inline
#endif
#define lerp(t, a, b) ((a) + (t) * ((b) - (a)))
static inline float
grad1(const int hash, const float 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;
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;
}
static PyObject *
py_noise1(PyObject *self, PyObject *args, PyObject *kwargs)
{
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;
}
}
static inline float
grad2(const int hash, const float x, const float y)
{
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)));
}
static PyObject *
py_noise2(PyObject *self, PyObject *args, PyObject *kwargs)
{
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;
}
}
static inline float
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];
}
float
noise3(float x, float y, float z, const int repeatx, const int repeaty, const int repeatz,
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))));
}
static PyObject *
py_noise3(PyObject *self, PyObject *args, PyObject *kwargs)
{
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;
}
}
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"
"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}
};
PyDoc_STRVAR(module_doc, "Native-code tileable Perlin \"improved\" noise functions");
#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 */
};
PyObject *
PyInit__perlin(void)
{
return PyModule_Create(&moduledef);
}
#else
void
init_perlin(void)
{
Py_InitModule3("_perlin", perlin_functions, module_doc);
}
#endif