Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
Interp.hpp
Go to the documentation of this file.
1/**
2 * @file Interp.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Declaration and definition of the LinearInterp class template for linear interpolation
5 * @version 1.2.3
6 * @date 2026-02-09
7 */
8
9#ifndef INTERP_HPP
10#define INTERP_HPP
11
12#include <stdint.h>
13
14/**
15 * @brief Structure representing a point in the interpolation table
16 * @tparam Tin Type of the input value
17 * @tparam Tout Type of the output value
18 */
19template <typename Tin, typename Tout>
21{
22 Tin in; /**< Input (x) value */
23 Tout out; /**< Output (y) value */
24};
25
26/**
27 * @brief Class template for performing linear interpolation using a lookup table
28 * @tparam Tin Type of the input values
29 * @tparam Tout Type of the output values
30 * @tparam Tmid Intermediate type for calculations to prevent overflow
31 * @tparam size Number of points in the interpolation table
32 */
33template <typename Tin, typename Tout, typename Tmid, uint8_t size>
35{
36public:
37 LinearInterp() = delete; /**< Default constructor deleted to prevent instantiation without a table */
38 explicit constexpr LinearInterp(const TablePoint<Tin, Tout> (&table_)[size]) : table(table_) {} /**< Normal constructor */
39
40 /**
41 * @brief Performs linear interpolation for the given input value, using the table
42 * @param input The input value to interpolate
43 * @return The interpolated output value
44 */
45 constexpr Tout interp(Tin input) const
46 {
47 // Clamp below first point
48 if (input <= table[0].in)
49 return table[0].out;
50 // Clamp above last point
51 if (input >= table[size - 1].in)
52 return table[size - 1].out;
53 // Find segment
54 for (uint8_t i = 1; i < size; ++i)
55 {
56 if (input < table[i].in)
57 {
58 const TablePoint<Tin, Tout> &p0 = table[i - 1];
59 const TablePoint<Tin, Tout> &p1 = table[i];
60 // Linear interpolation, all integer math
61 Tmid deltaIn = p1.in - p0.in;
62 Tmid deltaOut = p1.out - p0.out;
63 return p0.out + ((Tmid)(input - p0.in) * deltaOut) / deltaIn; // can omit Tmid if slope is integer, but assume enough performance for now
64 }
65 }
66 // Should never reach here
67 return table[size - 1].out;
68 }
69
70 /**
71 * @brief Returns the starting input value of the interpolation table
72 * @return The starting input value
73 */
74 constexpr Tin start() const
75 {
76 return table[0].in;
77 }
78
79 /**
80 * @brief Returns the input range of the interpolation table (last input - first input)
81 * @return The input range of the table
82 */
83 constexpr Tin range() const
84 {
85 return table[size - 1].in - table[0].in;
86 }
87
88private:
89 const TablePoint<Tin, Tout> (&table)[size]; /**< Reference to the interpolation table */
90};
91
92#endif // INTERP_HPP
Class template for performing linear interpolation using a lookup table.
Definition: Interp.hpp:35
constexpr Tout interp(Tin input) const
Performs linear interpolation for the given input value, using the table.
Definition: Interp.hpp:45
LinearInterp()=delete
constexpr LinearInterp(const TablePoint< Tin, Tout >(&table_)[size])
Definition: Interp.hpp:38
constexpr Tin range() const
Returns the input range of the interpolation table (last input - first input)
Definition: Interp.hpp:83
constexpr Tin start() const
Returns the starting input value of the interpolation table.
Definition: Interp.hpp:74
Structure representing a point in the interpolation table.
Definition: Interp.hpp:21
Tin in
Definition: Interp.hpp:22
Tout out
Definition: Interp.hpp:23