cgv
Loading...
Searching...
No Matches
piecewise_linear_function.h
1#pragma once
2
3#include <vector>
4
5#include "interval.h"
6
7namespace cgv {
8namespace math {
9
11template<typename T>
14 std::vector<T> values;
16 interval<T> domain = { T(0), T(1) };
17
19 T evaluate(T x) const {
20 x = (x - domain.lower_bound) / domain.size();
21
22 if(x <= T(0))
23 return values.front();
24
25 if(x >= T(1))
26 return values.back();
27
28 T num_segments = static_cast<T>(values.size() - 1);
29 int64_t index = static_cast<int64_t>(x * num_segments);
30
31 T step = T(1) / num_segments;
32 T prev = static_cast<T>(index) * step;
33 T curr = static_cast<T>(index + 1) * step;
34 T t = (x - prev) / (curr - prev);
35 return interpolate_linear(values[index], values[index + 1], t);
36 }
37};
38
39} // namespace math
40} // namespace cgv
The interval template represents a closed interval of two numbers, i.e.
Definition interval.h:27
the cgv namespace
Definition print.h:11
Template class representing a regularly (equidistant breakpoints) sampled piecewise linear function/a...
interval< T > domain
The domain (range of input values) of the function.
T evaluate(T x) const
Return the function value at position x.
std::vector< T > values
The sample values of the function evenly spread over the domain.