|
| 1 | +#include "fft_normal_integration.h" |
| 2 | + |
| 3 | +#include "pocketfft.h" |
| 4 | +#include <Eigen/Dense> |
| 5 | + |
| 6 | +#include <vector> |
| 7 | +#include <complex> |
| 8 | +#include <cmath> |
| 9 | + |
| 10 | + |
| 11 | +using namespace std; |
| 12 | +using namespace Eigen; |
| 13 | + |
| 14 | +typedef Matrix<std::complex<double>, Dynamic, Dynamic> ComplexMatrix; |
| 15 | + |
| 16 | +// Helper function to generate meshgrid-like matrices |
| 17 | +void meshgrid(MatrixXd& wx, MatrixXd& wy, int cols, int rows) { |
| 18 | + wx.resize(rows, cols); |
| 19 | + wy.resize(rows, cols); |
| 20 | + |
| 21 | + double colMid = (cols / 2) + 1; |
| 22 | + double rowMid = (rows / 2) + 1; |
| 23 | + double colDiv = cols - (cols % 2); |
| 24 | + double rowDiv = rows - (rows % 2); |
| 25 | + |
| 26 | + for (int i = 0; i < rows; ++i) { |
| 27 | + for (int j = 0; j < cols; ++j) { |
| 28 | + wx(i, j) = (j + 1 - colMid) / colDiv; |
| 29 | + wy(i, j) = (i + 1 - rowMid) / rowDiv; |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +MatrixXd ifftshift(const MatrixXd& input) { |
| 35 | + int rows = input.rows(); |
| 36 | + int cols = input.cols(); |
| 37 | + MatrixXd shifted(rows, cols); |
| 38 | + |
| 39 | + int rowShift = rows / 2; |
| 40 | + int colShift = cols / 2; |
| 41 | + |
| 42 | + for (int i = 0; i < rows; ++i) { |
| 43 | + for (int j = 0; j < cols; ++j) { |
| 44 | + int newRow = (i + rowShift) % rows; |
| 45 | + int newCol = (j + colShift) % cols; |
| 46 | + shifted(newRow, newCol) = input(i, j); |
| 47 | + } |
| 48 | + } |
| 49 | + return shifted; |
| 50 | +} |
| 51 | +void fft2(const MatrixXd& input, ComplexMatrix& output) { |
| 52 | + int rows = input.rows(); |
| 53 | + int cols = input.cols(); |
| 54 | + |
| 55 | + // Prepare data |
| 56 | + std::vector<std::complex<double>> data(rows * cols); |
| 57 | + for (int y = 0; y < rows; ++y) { |
| 58 | + for (int x = 0; x < cols; ++x) { |
| 59 | + data[y * cols + x] = input(y, x); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Perform FFT |
| 64 | + size_t element_size = sizeof(std::complex<double>); |
| 65 | + pocketfft::shape_t shape = {size_t(cols), size_t(rows)}; |
| 66 | + pocketfft::stride_t stride = { element_size, size_t(cols)*element_size }; |
| 67 | + pocketfft::shape_t axes{0, 1}; |
| 68 | + |
| 69 | + pocketfft::c2c(shape, stride, stride, axes, pocketfft::FORWARD, data.data(), data.data(), 1.0); |
| 70 | + |
| 71 | + // Fill output |
| 72 | + output.resize(rows, cols); |
| 73 | + for (int y = 0; y < rows; ++y) |
| 74 | + for (int x = 0; x < cols; ++x) { |
| 75 | + output(y, x) = data[y * cols + x]; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Function to compute 2D IFFT using PocketFFT |
| 80 | +void ifft2(const ComplexMatrix& input, MatrixXd& output) { |
| 81 | + int rows = input.rows(); |
| 82 | + int cols = input.cols(); |
| 83 | + |
| 84 | + // Prepare data |
| 85 | + std::vector<std::complex<double>> data(rows * cols); |
| 86 | + for (int i = 0; i < rows; ++i) |
| 87 | + for (int j = 0; j < cols; ++j) { |
| 88 | + data[i * cols + j] = input(i, j); |
| 89 | + } |
| 90 | + |
| 91 | + // Perform IFFT |
| 92 | + size_t element_size = sizeof(std::complex<double>); |
| 93 | + pocketfft::shape_t shape = {size_t(cols), size_t(rows)}; |
| 94 | + pocketfft::stride_t stride = { element_size, size_t(cols)*element_size }; |
| 95 | + pocketfft::shape_t axes{0, 1}; |
| 96 | + pocketfft::c2c(shape, stride, stride, axes, pocketfft::BACKWARD, data.data(), data.data(), 1.0/(4*sqrt(2)* rows * cols)); |
| 97 | + |
| 98 | + // Fill output and normalize |
| 99 | + output.resize(rows, cols); |
| 100 | + for (int i = 0; i < rows; ++i) |
| 101 | + for (int j = 0; j < cols; ++j) { |
| 102 | + output(i, j) = data[i * cols + j].real(); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void fft_integrate(std::function<bool(QString s, int n)> progressed, |
| 107 | + int cols, int rows, std::vector<float> &normals, std::vector<float> &heights) { |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + MatrixXd dzdx(rows, cols); |
| 112 | + MatrixXd dzdy(rows, cols); |
| 113 | + for (int i = 0; i < rows; ++i) { |
| 114 | + for (int j = 0; j < cols; ++j) { |
| 115 | + float *normal = &normals[3*(i * cols + j)]; |
| 116 | + dzdx(i, j) = normal[0] / normal[2]; // dz/dx = -nx/nz |
| 117 | + dzdy(i, j) = -normal[1] / normal[2]; // dz/dy = -ny/nz |
| 118 | + assert(!isnan(dzdx(i, j))); |
| 119 | + assert(!isnan(dzdy(i, j))); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + MatrixXd wx, wy; |
| 124 | + meshgrid(wx, wy, cols, rows); |
| 125 | + |
| 126 | + wx = ifftshift(wx); |
| 127 | + wy = ifftshift(wy); |
| 128 | + |
| 129 | + // Fourier Transforms of gradients |
| 130 | + ComplexMatrix DZDX, DZDY; |
| 131 | + fft2(dzdx, DZDX); |
| 132 | + fft2(dzdy, DZDY); |
| 133 | + |
| 134 | + // Frequency domain integration |
| 135 | + ComplexMatrix Z(rows, cols); |
| 136 | + std::complex<double> j(0, 1); // Imaginary unit |
| 137 | + |
| 138 | + for (int y = 0; y < rows; ++y) { |
| 139 | + for (int x = 0; x < cols; ++x) { |
| 140 | + double wx2_wy2 = wx(y, x) * wx(y, x) + wy(y, x) * wy(y, x) + 1e-12; // Avoid division by zero |
| 141 | + Z(y, x) = (-j * wx(y, x) * DZDX(y, x) - j * wy(y, x) * DZDY(y, x)) / wx2_wy2; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Inverse FFT to reconstruct z |
| 146 | + MatrixXd z; |
| 147 | + ifft2(Z, z); |
| 148 | + |
| 149 | + heights.resize(rows* cols); |
| 150 | + for (int i = 0; i < rows; ++i) { |
| 151 | + for (int j = 0; j < cols; ++j) { |
| 152 | + heights[i * cols + j] = static_cast<float>(z(i, j)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /* |
| 157 | + [wx, wy] = meshgrid(([1:cols]-(fix(cols/2)+1))/(cols-mod(cols,2)), ... |
| 158 | + ([1:rows]-(fix(rows/2)+1))/(rows-mod(rows,2))); |
| 159 | +
|
| 160 | + % Quadrant shift to put zero frequency at the appropriate edge |
| 161 | + wx = ifftshift(wx); wy = ifftshift(wy); |
| 162 | +
|
| 163 | + DZDX = fft2(dzdx); % Fourier transforms of gradients |
| 164 | + DZDY = fft2(dzdy); |
| 165 | +
|
| 166 | + % Integrate in the frequency domain by phase shifting by pi/2 and |
| 167 | + % weighting the Fourier coefficients by their frequencies in x and y and |
| 168 | + % then dividing by the squared frequency. eps is added to the |
| 169 | + % denominator to avoid division by 0. |
| 170 | +
|
| 171 | + Z = (-j*wx.*DZDX -j*wy.*DZDY)./(wx.^2 + wy.^2 + eps); % Equation 21 |
| 172 | +
|
| 173 | + z = real(ifft2(Z)); % Reconstruction |
| 174 | + */ |
| 175 | +} |
0 commit comments