+
+ fftw_execute(m_fftwPlan);
+
+ for (int y = 0; y < m_surface->size(); ++y) {
+ for (int x = 0; x < m_surface->size(); ++x) {
+ m_surface->at(x, y)
+ .setHeight(m_fftwOut[y + x * m_surface->size()][0]);
+ }
+ }
+}
+
+double Sea::phillipsSpectrum(double k_x, double k_y) const
+{
+ const double k = sqrt(pow(k_x, 2) + pow(k_y, 2));
+ const double L = pow(m_windSpeed, 2) / GRAVITATIONAL_CONSTANT;
+
+ const double cosineFactor = pow((k_x / k) * m_windDirection[0] +
+ (k_y / k) * m_windDirection[1], 2);
+
+ return PHILLIPS_CONSTANT * exp(-1 / pow(k * L, 2)) / pow(k, 4) *
+ cosineFactor;
+}
+
+std::complex<double>& Sea::fourierAmplitudeAt(int n, int m)
+{
+ return m_fourierAmplitudes.at
+ (n + m_surface->size()/2 +
+ (m + m_surface->size()/2) * m_surface->size());
+}
+
+double Sea::spatialFrequencyForIndex(int n) const
+{
+ return 2 * M_PI * n / m_surface->size();
+}
+
+void Sea::generateFourierAmplitudes()
+{
+ for (int m = -m_surface->size()/2; m < m_surface->size()/2; ++m) {
+ const double k_y = spatialFrequencyForIndex(m);
+
+ for (int n = -m_surface->size()/2; n < m_surface->size()/2; ++n) {
+ const double k_x = spatialFrequencyForIndex(n);
+
+ std::complex<double> cDist(m_normalDistribution(m_randomGenerator),
+ m_normalDistribution(m_randomGenerator));
+
+ fourierAmplitudeAt(n, m) =
+ cDist * sqrt(phillipsSpectrum(k_x, k_y)) / sqrt(2);
+ }
+ }
+
+ for (int n = -m_surface->size()/2; n < m_surface->size()/2; ++n) {
+ fourierAmplitudeAt(n, m_surface->size()/2) =
+ fourierAmplitudeAt(n, -m_surface->size()/2);
+ }
+
+ for (int m = -m_surface->size()/2; m < m_surface->size()/2; ++m) {
+ fourierAmplitudeAt(m_surface->size()/2, m) =
+ fourierAmplitudeAt(-m_surface->size()/2, m);
+ }
+
+ fourierAmplitudeAt(0, 0) = {0, 0};