forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgaussian_filter.cpp
More file actions
63 lines (48 loc) · 1.44 KB
/
gaussian_filter.cpp
File metadata and controls
63 lines (48 loc) · 1.44 KB
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
#include "../include/gaussian_filter.h"
#include <cmath>
GaussianFilter::GaussianFilter() {
m_cache = nullptr;
m_cacheSteps = 0;
m_radius = 0.0;
m_alpha = 0.0;
m_exp_s = 0.0;
m_inv_r = 0.0;
}
GaussianFilter::~GaussianFilter() {
if (m_cache != nullptr) delete[] m_cache;
}
void GaussianFilter::initialize(double alpha, double radius, int cacheSteps) {
m_cacheSteps = cacheSteps;
m_alpha = alpha;
m_radius = radius;
m_exp_s = std::exp(-m_alpha * m_radius * m_radius);
m_inv_r = 1 / m_radius;
generateCache();
}
double GaussianFilter::evaluate(double s) const {
const int actualSteps = m_cacheSteps - 32;
const double s_sample = actualSteps * std::abs(s) * m_inv_r;
const double s0 = std::floor(s_sample);
const double s1 = std::ceil(s_sample);
const double d = s_sample - s0;
return
(1 - d) * m_cache[(int)s0]
+ d * m_cache[(int)s1];
}
double GaussianFilter::calculate(double s) const {
return std::max(
0.0,
std::exp(-m_alpha * s * s) - m_exp_s);
}
void GaussianFilter::generateCache() {
const int actualSteps = m_cacheSteps - 32;
const double step = 1.0 / actualSteps;
m_cache = new double[m_cacheSteps];
for (int i = 0; i <= actualSteps; ++i) {
const double s = i * step * m_radius;
m_cache[i] = calculate(s);
}
for (int i = actualSteps + 1; i < m_cacheSteps; ++i) {
m_cache[i] = 0.0;
}
}