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
|
#include <mbgl/util/tiny_sdf.hpp>
#include <mbgl/util/math.hpp>
#include <algorithm>
namespace mbgl {
namespace util {
namespace tinysdf {
static const double INF = 1e20;
// 1D squared distance transform
void edt1d(std::vector<double>& f,
std::vector<double>& d,
std::vector<int16_t>& v,
std::vector<double>& z,
uint32_t n) {
v[0] = 0;
z[0] = -INF;
z[1] = +INF;
for (uint32_t q = 1, k = 0; q < n; q++) {
double s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
while (s <= z[k]) {
k--;
s = ((f[q] + q * q) - (f[v[k]] + v[k] * v[k])) / (2 * q - 2 * v[k]);
}
k++;
v[k] = q;
z[k] = s;
z[k + 1] = +INF;
}
for (uint32_t q = 0, k = 0; q < n; q++) {
while (z[k + 1] < q) k++;
d[q] = (q - v[k]) * (q - v[k]) + f[v[k]];
}
}
// 2D Euclidean distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/dt/
void edt(std::vector<double>& data,
uint32_t width,
uint32_t height,
std::vector<double>& f,
std::vector<double>& d,
std::vector<int16_t>& v,
std::vector<double>& z) {
for (uint32_t x = 0; x < width; x++) {
for (uint32_t y = 0; y < height; y++) {
f[y] = data[y * width + x];
}
edt1d(f, d, v, z, height);
for (uint32_t y = 0; y < height; y++) {
data[y * width + x] = d[y];
}
}
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
f[x] = data[y * width + x];
}
edt1d(f, d, v, z, width);
for (uint32_t x = 0; x < width; x++) {
data[y * width + x] = std::sqrt(d[x]);
}
}
}
} // namespace tinysdf
AlphaImage transformRasterToSDF(const AlphaImage& rasterInput, double radius, double cutoff) {
uint32_t size = rasterInput.size.width * rasterInput.size.height;
uint32_t maxDimension = std::max(rasterInput.size.width, rasterInput.size.height);
AlphaImage sdf(rasterInput.size);
// temporary arrays for the distance transform
std::vector<double> gridOuter(size);
std::vector<double> gridInner(size);
std::vector<double> f(maxDimension);
std::vector<double> d(maxDimension);
std::vector<double> z(maxDimension + 1);
std::vector<int16_t> v(maxDimension);
for (uint32_t i = 0; i < size; i++) {
double a = double(rasterInput.data[i]) / 255; // alpha value
gridOuter[i] = a == 1.0 ? 0.0 : a == 0.0 ? tinysdf::INF : std::pow(std::max(0.0, 0.5 - a), 2.0);
gridInner[i] = a == 1.0 ? tinysdf::INF : a == 0.0 ? 0.0 : std::pow(std::max(0.0, a - 0.5), 2.0);
}
tinysdf::edt(gridOuter, rasterInput.size.width, rasterInput.size.height, f, d, v, z);
tinysdf::edt(gridInner, rasterInput.size.width, rasterInput.size.height, f, d, v, z);
for (uint32_t i = 0; i < size; i++) {
double distance = gridOuter[i] - gridInner[i];
sdf.data[i] = std::max(0l, std::min(255l, ::lround(255.0 - 255.0 * (distance / radius + cutoff))));
}
return sdf;
}
} // namespace util
} // namespace mbgl
|