Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FFT_NOISE.h « intern « smoke « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a087b4e139140a367ffb1e387cd0f838b68259f5 (plain)
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
/** \file smoke/intern/FFT_NOISE.h
 *  \ingroup smoke
 */
//////////////////////////////////////////////////////////////////////
// This file is part of Wavelet Turbulence.
// 
// Wavelet Turbulence is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Wavelet Turbulence is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Wavelet Turbulence.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright 2008 Theodore Kim and Nils Thuerey
// 
/////////////////////////////////////////////////////////////////////////
//

#ifndef FFT_NOISE_H_
#define FFT_NOISE_H_

#ifdef WITH_FFTW3
#include <iostream>
#include <fftw3.h>
#include <MERSENNETWISTER.h>

#include "WAVELET_NOISE.h"

#ifndef M_PI
#define M_PI 3.14159265
#endif

/////////////////////////////////////////////////////////////////////////
// shift spectrum to the format that FFTW expects
/////////////////////////////////////////////////////////////////////////
static void shift3D(float*& field, int xRes, int yRes, int zRes)
{
  int xHalf = xRes / 2;
  int yHalf = yRes / 2;
  int zHalf = zRes / 2;
 // int slabSize = xRes * yRes;
  for (int z = 0; z < zHalf; z++)
    for (int y = 0; y < yHalf; y++)
      for (int x = 0; x < xHalf; x++)
      {
        int index = x + y * xRes + z * xRes * yRes;
        float temp;
        int xSwap = xHalf;
        int ySwap = yHalf * xRes;
        int zSwap = zHalf * xRes * yRes;
        
        // [0,0,0] to [1,1,1]
        temp = field[index];
        field[index] = field[index + xSwap + ySwap + zSwap];
        field[index + xSwap + ySwap + zSwap] = temp;

        // [1,0,0] to [0,1,1]
        temp = field[index + xSwap];
        field[index + xSwap] = field[index + ySwap + zSwap];
        field[index + ySwap + zSwap] = temp;

        // [0,1,0] to [1,0,1]
        temp = field[index + ySwap];
        field[index + ySwap] = field[index + xSwap + zSwap];
        field[index + xSwap + zSwap] = temp;
        
        // [0,0,1] to [1,1,0]
        temp = field[index + zSwap];
        field[index + zSwap] = field[index + xSwap + ySwap];
        field[index + xSwap + ySwap] = temp;
      }
}

static void generatTile_FFT(float* const noiseTileData, std::string filename)
{
	if (loadTile(noiseTileData, filename)) return;
	
	int res = NOISE_TILE_SIZE;
	int xRes = res;
	int yRes = res;
	int zRes = res;
	int totalCells = xRes * yRes * zRes;
	
	// create and shift the filter
	float* filter = new float[totalCells];
	for (int z = 0; z < zRes; z++)
		for (int y = 0; y < yRes; y++)
			for (int x = 0; x < xRes; x++)
			{
				int index = x + y * xRes + z * xRes * yRes;
				float diff[] = {abs(x - xRes/2), 
				abs(y - yRes/2), 
				abs(z - zRes/2)};
				float radius = sqrtf(diff[0] * diff[0] + 
				diff[1] * diff[1] + 
				diff[2] * diff[2]) / (xRes / 2);
				radius *= M_PI;
				float H = cos((M_PI / 2.0f) * log(4.0f * radius / M_PI) / log(2.0f));
				H = H * H;
				float filtered = H;
				
				// clamp everything outside the wanted band
				if (radius >= M_PI / 2.0f)
					filtered = 0.0f;
				
				// make sure to capture all low frequencies
				if (radius <= M_PI / 4.0f)
					filtered = 1.0f;
				
				filter[index] = filtered;
			}
	shift3D(filter, xRes, yRes, zRes);
	
	// create the noise
	float* noise = new float[totalCells];
	int index = 0;
	MTRand twister;
	for (int z = 0; z < zRes; z++)
	for (int y = 0; y < yRes; y++)
	  for (int x = 0; x < xRes; x++, index++)
		noise[index] = twister.randNorm();
	
	// create padded field
	fftw_complex* forward = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * totalCells);
	
	// init padded field
	index = 0;
	for (int z = 0; z < zRes; z++)
	for (int y = 0; y < yRes; y++)
	  for (int x = 0; x < xRes; x++, index++)
	  {
		forward[index][0] = noise[index];
		forward[index][1] = 0.0f;
	  }
	
	// forward FFT 
	fftw_complex* backward = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * totalCells);
	fftw_plan forwardPlan = fftw_plan_dft_3d(xRes, yRes, zRes, forward, backward, FFTW_FORWARD, FFTW_ESTIMATE);  
	fftw_execute(forwardPlan);
	fftw_destroy_plan(forwardPlan);
	
	// apply filter
	index = 0;
	for (int z = 0; z < zRes; z++)
		for (int y = 0; y < yRes; y++)
			for (int x = 0; x < xRes; x++, index++)
			{
				backward[index][0] *= filter[index];
				backward[index][1] *= filter[index];
			}
	
	// backward FFT
	fftw_plan backwardPlan = fftw_plan_dft_3d(xRes, yRes, zRes, backward, forward, FFTW_BACKWARD, FFTW_ESTIMATE);  
	fftw_execute(backwardPlan);
	fftw_destroy_plan(backwardPlan);
	
	// subtract out the low frequency components
	index = 0;
	for (int z = 0; z < zRes; z++)
		for (int y = 0; y < yRes; y++)
			for (int x = 0; x < xRes; x++, index++)
				noise[index] -= forward[index][0] / totalCells;

	// save out the noise tile
	saveTile(noise, filename);
	
	fftw_free(forward);
	fftw_free(backward);
	delete[] filter;
	delete[] noise;
}

#endif

#endif /* FFT_NOISE_H_ */