-
Notifications
You must be signed in to change notification settings - Fork 0
/
laplacian_smoothing.c
executable file
·271 lines (240 loc) · 8.62 KB
/
laplacian_smoothing.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
#include "bathymetrictools.h"
/*
* This file contains:
* - Navigationally safe Laplace interpolation implementation to smooth surfaces iteratively
* - Neighborhood is only the immediate neighboring cells, nodata is handled as a missing neighbor
* - Weights are calculated from spatial resolutions in X- and Y-directions
*
* For example: If spatial resolution X = Y, the used neighborhood (convolution kernel) will be like:
*
* |0 1 0|
* 1/4 * |1 0 1|
* |0 1 0|
*/
/*
* Controls the iterative smoothing process.
* - Iterates over surface cells
* - Memory management
*/
void smoothLaplacian(const int iterations, struct FloatSurface *src) {
printf("Laplacian smoothing..");
fflush(stdout);
const double nodata = src->nodata;
// Build extra array to hold smoothed surface (type float**):
float **smooth_array = createFloatArray(src->cols, src->rows);
float **holder = NULL; // Pointer placeholder
// Iterate and smooth surface N times:
for (int i = 0; i < iterations; i++) {
for (int row = 0; row < src->rows; row++) {
for (int col = 0; col < src->cols; col++) {
if (fabs(src->array[row][col] - nodata) > EPSILON) {
smooth_array[row][col] = getSafeSmoothDepth(src, row, col);
} else {
smooth_array[row][col] = nodata;
}
}
}
// Swap surface data array:
holder = src->array; // Store pointer temporarily
src->array = smooth_array; // Change surface struct data array
smooth_array = holder; // Use the same temporary array again
}
// Free memory of the temporary array:
freeFloatArray(smooth_array, src->rows);
printf("Done\n");
fflush(stdout);
}
/*
* Helper function to check if given cell holds a No Data value.
* - Returns 1 if cell value == No Data
* - Returns 0 if cell value != No Data
*/
char isNodata(struct FloatSurface *src, int rowindex, int colindex) {
if (fabs(src->array[rowindex][colindex] - src->nodata) > EPSILON) { // != NO DATA
return 0;
} else {
return 1;
}
}
/*
* Interpolates a value based on immediate neighborhood.
* - A minimum of 2 valid (NoData handled as not valid)
* neighbor cells is needed to interpolate a value
* - Handles all cells, note behaviour in corners and borders
*/
float getInterpolatedDepth(struct FloatSurface *src, int row, int col) {
const double xres = fabs(src->geotransform[1]); // W-E grid cell spatial resolution
const double yres = fabs(src->geotransform[5]); // N-S grid cell spatial resolution (negative value)
// Get kernel weights (Wi = dVi / di)
const double xWeight = yres / xres; // --> X-direction: Yres / Xres
const double yWeight = xres / yres; // --> Y-direction: Xres / Yres
double weightSum = 0.0;
double list[4] = {0.0, 0.0, 0.0, 0.0};
double sum = 0;
int count = 0;
// Top left corner:
if (row == 0 && col == 0) {
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Top right corner:
} else if (row == 0 && col == src->cols - 1) {
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Lower left corner:
} else if (row == src->rows - 1 && col == 0) {
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Lower right corner:
} else if (row == src->rows - 1 && col == src->cols - 1) {
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Left border:
} else if (col == 0) {
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Right border:
} else if (col == src->cols - 1) {
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
// Top row:
} else if (row == 0) {
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
// Bottom row:
} else if (row == src->rows - 1) {
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
// All other cells (in the middle):
} else {
if (isNodata(src, row - 1, col) == 0) {
list[count] = src->array[row - 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row + 1, col) == 0) {
list[count] = src->array[row + 1][col] * yWeight;
weightSum += yWeight;
count ++;
}
if (isNodata(src, row, col - 1) == 0) {
list[count] = src->array[row][col - 1] * xWeight;
weightSum += xWeight;
count ++;
}
if (isNodata(src, row, col + 1) == 0) {
list[count] = src->array[row][col + 1] * xWeight;
weightSum += xWeight;
count ++;
}
}
// If less than 2 valid (!= No Data) neighbors, do not interpolate but return cell value itself:
if (count < 2) {
return src->array[row][col];
} else { // Return weighted mean of neighbours
// Calculate sum of cell values:
for (int i = 0; i < count; i++) {
sum += list[i];
}
return (float)(sum / weightSum);
}
}
/*
* Function to guarantee the safety of the process.
* - Gets an interpolated value based on neighborhood and
* compares it to the original cell value
* - Returns the one that is shoaler
*/
float getSafeSmoothDepth(struct FloatSurface *src, int row, int col) {
const float z = src->array[row][col]; // Original value
const float estimate = getInterpolatedDepth(src, row, col); // Interpolated value
// Return safer value:
if (fabs(estimate) < fabs(z)) {
return estimate;
} else {
return z;
}
}