-
Notifications
You must be signed in to change notification settings - Fork 48
/
noisefield.h
398 lines (317 loc) · 13.4 KB
/
noisefield.h
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/******************************************************************************
*
* MantaFlow fluid solver framework
* Copyright 2011 Tobias Pfaff, Nils Thuerey
*
* This program is free software, distributed under the terms of the
* Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Wavelet noise field
*
******************************************************************************/
#ifndef _NOISEFIELD_H_
#define _NOISEFIELD_H_
#include "vectorbase.h"
#include "manta.h"
#include "grid.h"
#include <atomic>
namespace Manta {
#define NOISE_TILE_SIZE 128
// wrapper for a parametrized field of wavelet noise
PYTHON(name=NoiseField)
class WaveletNoiseField : public PbClass {
public:
PYTHON() WaveletNoiseField( FluidSolver* parent, int fixedSeed=-1 , int loadFromFile=false );
~WaveletNoiseField() {
if(mNoiseTile && !mNoiseReferenceCount) { delete mNoiseTile; mNoiseTile=NULL; }
};
//! evaluate noise
inline Real evaluate(Vec3 pos, int tile=0) const;
//! evaluate noise as a vector
inline Vec3 evaluateVec(Vec3 pos, int tile=0) const;
//! evaluate curl noise
inline Vec3 evaluateCurl(Vec3 pos) const;
//! direct data access
Real* data() { return mNoiseTile; }
//! compute wavelet decomposition of an input grid (stores residual coefficients)
static void computeCoefficients(Grid<Real>& input, Grid<Real>& tempIn1, Grid<Real>& tempIn2);
// helper
std::string toString();
// texcoord position and scale
PYTHON(name=posOffset) Vec3 mPosOffset;
PYTHON(name=posScale) Vec3 mPosScale;
// value offset & scale
PYTHON(name=valOffset) Real mValOffset;
PYTHON(name=valScale) Real mValScale;
// clamp? (default 0-1)
PYTHON(name=clamp) bool mClamp;
PYTHON(name=clampNeg) Real mClampNeg;
PYTHON(name=clampPos) Real mClampPos;
// animated over time
PYTHON(name=timeAnim) Real mTimeAnim;
protected:
// noise evaluation functions
static inline Real WNoiseDx (const Vec3& p, Real *data);
static inline Vec3 WNoiseVec(const Vec3& p, Real *data);
static inline Real WNoise (const Vec3& p, Real *data);
// helpers for tile generation , for periodic 128 grids only
static void downsample(Real *from, Real *to, int n, int stride);
static void upsample (Real *from, Real *to, int n, int stride);
// for grids with arbitrary sizes, and neumann boundary conditions
static void downsampleNeumann(const Real *from, Real *to, int n, int stride);
static void upsampleNeumann (const Real *from, Real *to, int n, int stride);
static inline int modSlow(int x, int n) { int m = x % n; return (m<0) ? m+n : m; }
// warning - noiseTileSize has to be 128^3!
#define modFast128(x) ((x) & 127)
inline Real getTime() const { return mParent->getTime() * mParent->getDx() * mTimeAnim; }
// pre-compute tile data for wavelet noise
void generateTile( int loadFromFile );
// animation over time
// grid size normalization (inverse size)
Real mGsInvX, mGsInvY, mGsInvZ;
// random offset into tile to simulate different random seeds
Vec3 mSeedOffset;
static Real* mNoiseTile;
// global random seed storage
static int randomSeed;
// global reference count for noise tile
static std::atomic<int> mNoiseReferenceCount;
};
// **************************************************************************
// Implementation
#define ADD_WEIGHTED(x,y,z)\
weight = 1.0f;\
xC = modFast128(midX + (x));\
weight *= w[0][(x) + 1];\
yC = modFast128(midY + (y));\
weight *= w[1][(y) + 1];\
zC = modFast128(midZ + (z));\
weight *= w[2][(z) + 1];\
result += weight * data[(zC * NOISE_TILE_SIZE + yC) * NOISE_TILE_SIZE + xC];
//////////////////////////////////////////////////////////////////////////////////////////
// derivatives of 3D noise - unrolled for performance
//////////////////////////////////////////////////////////////////////////////////////////
inline Real WaveletNoiseField::WNoiseDx(const Vec3& p, Real *data) {
Real w[3][3], t, result = 0;
// Evaluate quadratic B-spline basis functions
int midX = (int)ceil(p[0] - 0.5f);
t = midX - (p[0] - 0.5f);
w[0][0] = -t;
w[0][2] = (1.f - t);
w[0][1] = 2.0f * t - 1.0f;
int midY = (int)ceil(p[1] - 0.5f);
t = midY - (p[1] - 0.5f);
w[1][0] = t * t * 0.5f;
w[1][2] = (1.f - t) * (1.f - t) *0.5f;
w[1][1] = 1.f - w[1][0] - w[1][2];
int midZ = (int)ceil(p[2] - 0.5f);
t = midZ - (p[2] - 0.5f);
w[2][0] = t * t * 0.5f;
w[2][2] = (1.f - t) * (1.f - t) *0.5f;
w[2][1] = 1.f - w[2][0] - w[2][2];
// Evaluate noise by weighting noise coefficients by basis function values
int xC, yC, zC;
Real weight = 1;
ADD_WEIGHTED(-1,-1, -1); ADD_WEIGHTED( 0,-1, -1); ADD_WEIGHTED( 1,-1, -1);
ADD_WEIGHTED(-1, 0, -1); ADD_WEIGHTED( 0, 0, -1); ADD_WEIGHTED( 1, 0, -1);
ADD_WEIGHTED(-1, 1, -1); ADD_WEIGHTED( 0, 1, -1); ADD_WEIGHTED( 1, 1, -1);
ADD_WEIGHTED(-1,-1, 0); ADD_WEIGHTED( 0,-1, 0); ADD_WEIGHTED( 1,-1, 0);
ADD_WEIGHTED(-1, 0, 0); ADD_WEIGHTED( 0, 0, 0); ADD_WEIGHTED( 1, 0, 0);
ADD_WEIGHTED(-1, 1, 0); ADD_WEIGHTED( 0, 1, 0); ADD_WEIGHTED( 1, 1, 0);
ADD_WEIGHTED(-1,-1, 1); ADD_WEIGHTED( 0,-1, 1); ADD_WEIGHTED( 1,-1, 1);
ADD_WEIGHTED(-1, 0, 1); ADD_WEIGHTED( 0, 0, 1); ADD_WEIGHTED( 1, 0, 1);
ADD_WEIGHTED(-1, 1, 1); ADD_WEIGHTED( 0, 1, 1); ADD_WEIGHTED( 1, 1, 1);
return result;
}
inline Real WaveletNoiseField::WNoise(const Vec3& p, Real *data) {
Real w[3][3], t, result = 0;
// Evaluate quadratic B-spline basis functions
int midX = (int)ceilf(p[0] - 0.5f);
t = midX - (p[0] - 0.5f);
w[0][0] = t * t * 0.5f;
w[0][2] = (1.f - t) * (1.f - t) *0.5f;
w[0][1] = 1.f - w[0][0] - w[0][2];
int midY = (int)ceilf(p[1] - 0.5f);
t = midY - (p[1] - 0.5f);
w[1][0] = t * t * 0.5f;
w[1][2] = (1.f - t) * (1.f - t) *0.5f;
w[1][1] = 1.f - w[1][0] - w[1][2];
int midZ = (int)ceilf(p[2] - 0.5f);
t = midZ - (p[2] - 0.5f);
w[2][0] = t * t * 0.5f;
w[2][2] = (1.f - t) * (1.f - t) *0.5f;
w[2][1] = 1.f - w[2][0] - w[2][2];
// Evaluate noise by weighting noise coefficients by basis function values
int xC, yC, zC;
Real weight = 1;
ADD_WEIGHTED(-1,-1, -1); ADD_WEIGHTED( 0,-1, -1); ADD_WEIGHTED( 1,-1, -1);
ADD_WEIGHTED(-1, 0, -1); ADD_WEIGHTED( 0, 0, -1); ADD_WEIGHTED( 1, 0, -1);
ADD_WEIGHTED(-1, 1, -1); ADD_WEIGHTED( 0, 1, -1); ADD_WEIGHTED( 1, 1, -1);
ADD_WEIGHTED(-1,-1, 0); ADD_WEIGHTED( 0,-1, 0); ADD_WEIGHTED( 1,-1, 0);
ADD_WEIGHTED(-1, 0, 0); ADD_WEIGHTED( 0, 0, 0); ADD_WEIGHTED( 1, 0, 0);
ADD_WEIGHTED(-1, 1, 0); ADD_WEIGHTED( 0, 1, 0); ADD_WEIGHTED( 1, 1, 0);
ADD_WEIGHTED(-1,-1, 1); ADD_WEIGHTED( 0,-1, 1); ADD_WEIGHTED( 1,-1, 1);
ADD_WEIGHTED(-1, 0, 1); ADD_WEIGHTED( 0, 0, 1); ADD_WEIGHTED( 1, 0, 1);
ADD_WEIGHTED(-1, 1, 1); ADD_WEIGHTED( 0, 1, 1); ADD_WEIGHTED( 1, 1, 1);
return result;
}
#define ADD_WEIGHTEDX(x,y,z)\
weight = dw[0][(x) + 1] * w[1][(y) + 1] * w[2][(z) + 1];\
result += weight * neighbors[x + 1][y + 1][z + 1];
#define ADD_WEIGHTEDY(x,y,z)\
weight = w[0][(x) + 1] * dw[1][(y) + 1] * w[2][(z) + 1];\
result += weight * neighbors[x + 1][y + 1][z + 1];
#define ADD_WEIGHTEDZ(x,y,z)\
weight = w[0][(x) + 1] * w[1][(y) + 1] * dw[2][(z) + 1];\
result += weight * neighbors[x + 1][y + 1][z + 1];
//////////////////////////////////////////////////////////////////////////////////////////
// compute all derivatives in at once
//////////////////////////////////////////////////////////////////////////////////////////
inline Vec3 WaveletNoiseField::WNoiseVec(const Vec3& p, Real *data)
{
Vec3 final(0.);
Real w[3][3];
Real dw[3][3];
Real result = 0;
int xC, yC, zC;
Real weight;
int midX = (int)ceil(p[0] - 0.5f);
int midY = (int)ceil(p[1] - 0.5f);
int midZ = (int)ceil(p[2] - 0.5f);
Real t0 = midX - (p[0] - 0.5f);
Real t1 = midY - (p[1] - 0.5f);
Real t2 = midZ - (p[2] - 0.5f);
// precache all the neighbors for fast access
Real neighbors[3][3][3];
for (int z = -1; z <=1; z++)
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++)
{
xC = modFast128(midX + (x));
yC = modFast128(midY + (y));
zC = modFast128(midZ + (z));
neighbors[x + 1][y + 1][z + 1] = data[zC * NOISE_TILE_SIZE * NOISE_TILE_SIZE + yC * NOISE_TILE_SIZE + xC];
}
///////////////////////////////////////////////////////////////////////////////////////
// evaluate splines
///////////////////////////////////////////////////////////////////////////////////////
dw[0][0] = -t0;
dw[0][2] = (1.f - t0);
dw[0][1] = 2.0f * t0 - 1.0f;
dw[1][0] = -t1;
dw[1][2] = (1.0f - t1);
dw[1][1] = 2.0f * t1 - 1.0f;
dw[2][0] = -t2;
dw[2][2] = (1.0f - t2);
dw[2][1] = 2.0f * t2 - 1.0f;
w[0][0] = t0 * t0 * 0.5f;
w[0][2] = (1.f - t0) * (1.f - t0) *0.5f;
w[0][1] = 1.f - w[0][0] - w[0][2];
w[1][0] = t1 * t1 * 0.5f;
w[1][2] = (1.f - t1) * (1.f - t1) *0.5f;
w[1][1] = 1.f - w[1][0] - w[1][2];
w[2][0] = t2 * t2 * 0.5f;
w[2][2] = (1.f - t2) * (1.f - t2) *0.5f;
w[2][1] = 1.f - w[2][0] - w[2][2];
///////////////////////////////////////////////////////////////////////////////////////
// x derivative
///////////////////////////////////////////////////////////////////////////////////////
result = 0.0f;
ADD_WEIGHTEDX(-1,-1, -1); ADD_WEIGHTEDX( 0,-1, -1); ADD_WEIGHTEDX( 1,-1, -1);
ADD_WEIGHTEDX(-1, 0, -1); ADD_WEIGHTEDX( 0, 0, -1); ADD_WEIGHTEDX( 1, 0, -1);
ADD_WEIGHTEDX(-1, 1, -1); ADD_WEIGHTEDX( 0, 1, -1); ADD_WEIGHTEDX( 1, 1, -1);
ADD_WEIGHTEDX(-1,-1, 0); ADD_WEIGHTEDX( 0,-1, 0); ADD_WEIGHTEDX( 1,-1, 0);
ADD_WEIGHTEDX(-1, 0, 0); ADD_WEIGHTEDX( 0, 0, 0); ADD_WEIGHTEDX( 1, 0, 0);
ADD_WEIGHTEDX(-1, 1, 0); ADD_WEIGHTEDX( 0, 1, 0); ADD_WEIGHTEDX( 1, 1, 0);
ADD_WEIGHTEDX(-1,-1, 1); ADD_WEIGHTEDX( 0,-1, 1); ADD_WEIGHTEDX( 1,-1, 1);
ADD_WEIGHTEDX(-1, 0, 1); ADD_WEIGHTEDX( 0, 0, 1); ADD_WEIGHTEDX( 1, 0, 1);
ADD_WEIGHTEDX(-1, 1, 1); ADD_WEIGHTEDX( 0, 1, 1); ADD_WEIGHTEDX( 1, 1, 1);
final[0] = result;
///////////////////////////////////////////////////////////////////////////////////////
// y derivative
///////////////////////////////////////////////////////////////////////////////////////
result = 0.0f;
ADD_WEIGHTEDY(-1,-1, -1); ADD_WEIGHTEDY( 0,-1, -1); ADD_WEIGHTEDY( 1,-1, -1);
ADD_WEIGHTEDY(-1, 0, -1); ADD_WEIGHTEDY( 0, 0, -1); ADD_WEIGHTEDY( 1, 0, -1);
ADD_WEIGHTEDY(-1, 1, -1); ADD_WEIGHTEDY( 0, 1, -1); ADD_WEIGHTEDY( 1, 1, -1);
ADD_WEIGHTEDY(-1,-1, 0); ADD_WEIGHTEDY( 0,-1, 0); ADD_WEIGHTEDY( 1,-1, 0);
ADD_WEIGHTEDY(-1, 0, 0); ADD_WEIGHTEDY( 0, 0, 0); ADD_WEIGHTEDY( 1, 0, 0);
ADD_WEIGHTEDY(-1, 1, 0); ADD_WEIGHTEDY( 0, 1, 0); ADD_WEIGHTEDY( 1, 1, 0);
ADD_WEIGHTEDY(-1,-1, 1); ADD_WEIGHTEDY( 0,-1, 1); ADD_WEIGHTEDY( 1,-1, 1);
ADD_WEIGHTEDY(-1, 0, 1); ADD_WEIGHTEDY( 0, 0, 1); ADD_WEIGHTEDY( 1, 0, 1);
ADD_WEIGHTEDY(-1, 1, 1); ADD_WEIGHTEDY( 0, 1, 1); ADD_WEIGHTEDY( 1, 1, 1);
final[1] = result;
///////////////////////////////////////////////////////////////////////////////////////
// z derivative
///////////////////////////////////////////////////////////////////////////////////////
result = 0.0f;
ADD_WEIGHTEDZ(-1,-1, -1); ADD_WEIGHTEDZ( 0,-1, -1); ADD_WEIGHTEDZ( 1,-1, -1);
ADD_WEIGHTEDZ(-1, 0, -1); ADD_WEIGHTEDZ( 0, 0, -1); ADD_WEIGHTEDZ( 1, 0, -1);
ADD_WEIGHTEDZ(-1, 1, -1); ADD_WEIGHTEDZ( 0, 1, -1); ADD_WEIGHTEDZ( 1, 1, -1);
ADD_WEIGHTEDZ(-1,-1, 0); ADD_WEIGHTEDZ( 0,-1, 0); ADD_WEIGHTEDZ( 1,-1, 0);
ADD_WEIGHTEDZ(-1, 0, 0); ADD_WEIGHTEDZ( 0, 0, 0); ADD_WEIGHTEDZ( 1, 0, 0);
ADD_WEIGHTEDZ(-1, 1, 0); ADD_WEIGHTEDZ( 0, 1, 0); ADD_WEIGHTEDZ( 1, 1, 0);
ADD_WEIGHTEDZ(-1,-1, 1); ADD_WEIGHTEDZ( 0,-1, 1); ADD_WEIGHTEDZ( 1,-1, 1);
ADD_WEIGHTEDZ(-1, 0, 1); ADD_WEIGHTEDZ( 0, 0, 1); ADD_WEIGHTEDZ( 1, 0, 1);
ADD_WEIGHTEDZ(-1, 1, 1); ADD_WEIGHTEDZ( 0, 1, 1); ADD_WEIGHTEDZ( 1, 1, 1);
final[2] = result;
//debMsg("FINAL","at "<<p<<" = "<<final); // DEBUG
return final;
}
#undef ADD_WEIGHTEDX
#undef ADD_WEIGHTEDY
#undef ADD_WEIGHTEDZ
inline Real WaveletNoiseField::evaluate(Vec3 pos, int tile) const {
pos[0] *= mGsInvX;
pos[1] *= mGsInvY;
pos[2] *= mGsInvZ;
pos += mSeedOffset;
// time anim
pos += Vec3(getTime());
pos[0] *= mPosScale[0];
pos[1] *= mPosScale[1];
pos[2] *= mPosScale[2];
pos += mPosOffset;
const int n3 = square(NOISE_TILE_SIZE) * NOISE_TILE_SIZE;
Real v = WNoise(pos, &mNoiseTile[tile*n3]);
v += mValOffset;
v *= mValScale;
if (mClamp) {
if (v< mClampNeg) v = mClampNeg;
if (v> mClampPos) v = mClampPos;
}
return v;
}
inline Vec3 WaveletNoiseField::evaluateVec(Vec3 pos, int tile) const {
pos[0] *= mGsInvX;
pos[1] *= mGsInvY;
pos[2] *= mGsInvZ;
pos += mSeedOffset;
// time anim
pos += Vec3(getTime());
pos[0] *= mPosScale[0];
pos[1] *= mPosScale[1];
pos[2] *= mPosScale[2];
pos += mPosOffset;
const int n3 = square(NOISE_TILE_SIZE) * NOISE_TILE_SIZE;
Vec3 v = WNoiseVec(pos, &mNoiseTile[tile*n3]);
v += Vec3(mValOffset);
v *= mValScale;
if (mClamp) {
for(int i=0; i<3; i++) {
if (v[i]< mClampNeg) v[i] = mClampNeg;
if (v[i]> mClampPos) v[i] = mClampPos;
}
}
return v;
}
inline Vec3 WaveletNoiseField::evaluateCurl(Vec3 pos) const {
// gradients of w0-w2
Vec3 d0 = evaluateVec(pos,0),
d1 = evaluateVec(pos,1),
d2 = evaluateVec(pos,2);
return Vec3(d0.y-d1.z, d2.z-d0.x, d1.x-d2.y);
}
} // namespace
#endif