summary refs log tree commit diff
path: root/src/Util/Sampler.hpp
blob: 3a4ff4f82c69a9f1576aade8ad0f61e1e9e035c4 (plain)
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
#pragma once

#include <functional>
#include "../Math/Vector.hpp"
#include "../Math/Grid.hpp"

namespace MC::Util {

template<size_t D, typename T>
class Sampler {
    using Pos = Vector<D>;
    using Sample = std::function<T(Pos)>;
    using Interpolate = std::function<T(Pos, Sample)>;
public:
    Sampler(
        Sample sample,
        Pos offset = {},
        float scale = 1.0f,
        Interpolate interpolate = nearest_interpolation
    ) : m_sample([=](Sampler::Pos pos) -> T {
        return interpolate(pos, [=](Sampler::Pos p) -> T { return sample(p * scale + offset); });
    }) {}

    T sample(Pos at) {
        return m_sample(at);
    }

    static T nearest_interpolation(Pos pos, Sample sample) {
        return sample(pos);
    }

private:
    Sample m_sample;
};

}