Speeduino
Loading...
Searching...
No Matches
table3d_interpolate.h
Go to the documentation of this file.
1#pragma once
2
3#include <iterator>
4#include "table3d_typedefs.h"
5#include "maths.h"
6#include "table2d.h"
7
15{
16 uint16_t x;
17 uint16_t y;
18
20 friend bool operator==(const xy_pair_t& lhs, const xy_pair_t& rhs)
21 {
22 return lhs.x==rhs.x && lhs.y==rhs.y;
23 }
24};
25
34
37 // Store the upper *index* of the X and Y axis bins that were last hit.
38 // This is used to make the next check faster since very likely the x & y values have
39 // only changed by a small amount & are in the same bin (or an adjacent bin).
40 //
41 // It's implicit that the other bin index is max bin index - 1 (a single axis
42 // value can't span 2 axis bins). This saves 1 byte.
43 //
44 // E.g. 6 element x-axis contents:
45 // [ 8| 9|12|15|18|21]
46 // indices:
47 // 0, 1, 2, 3, 4, 5
48 // If lastXBinMax==3, the min index must be 2. I.e. the last X value looked
49 // up was between 12<X<=15.
50 xy_coord2d lastBinMax = { 1U, 1U };
51
52 //Store the last input and output values, again for caching purposes
53 xy_pair_t last_lookup = { UINT16_MAX, UINT16_MAX };
55};
56
58static inline void invalidate_cache(table3DGetValueCache *pCache)
59{
60 pCache->last_lookup.x = UINT16_MAX;
61}
62
64// private to table3D implementation
65using table3d_bin_t = _table2d_detail::Bin<table3d_axis_t>;
66
67extern table3d_value_t interpolate_3d_value(const xy_pair_t &lookUpValues,
68 const table3d_dim_t &axisSize,
69 const table3d_value_t *pValues,
70 const table3d_bin_t &xBin,
71 const uint16_t xMultiplier,
72 const table3d_bin_t &yBin,
73 const uint16_t yMultiplier);
74
76
118template <uint16_t xFactor, uint16_t yFactor, typename TValues, typename TXAxis, typename TYAxis>
120 const TValues &values,
121 const TXAxis &xAxis,
122 const TYAxis &yAxis,
123 const xy_pair_t &lookupValues) {
124
125#if !defined(UNIT_TEST) // No caching during unit testing
126 // Check if the lookup values are the same as the last time we looked up a value
127 // If they are, we can return the cached value
128 if( lookupValues == pValueCache->last_lookup)
129 {
130 return pValueCache->lastOutput;
131 }
132#endif
133
134 constexpr table3d_dim_t axisSize = std::tuple_size<TXAxis>::value;
135
136 // Figure out where on the axes the incoming coord are
137 auto xBin = _table2d_detail::findCachedBin(pValueCache->lastBinMax.x, std::begin(xAxis), std::end(xAxis), (table3d_dim_t)div_round_closest_u16<xFactor>(lookupValues.x));
138 auto yBin = _table2d_detail::findCachedBin(pValueCache->lastBinMax.y, std::begin(yAxis), std::end(yAxis), (table3d_dim_t)div_round_closest_u16<yFactor>(lookupValues.y));
139
140 // Interpolate based on the bin positions
141 pValueCache->lastOutput = interpolate_3d_value(lookupValues, axisSize, values.data(), xBin, xFactor, yBin, yFactor);
142 // Store the last lookup values so we can check them next time
143 pValueCache->last_lookup = lookupValues;
144 pValueCache->lastBinMax.x = xBin.upperIndex;
145 pValueCache->lastBinMax.y = yBin.upperIndex;
146
147 return pValueCache->lastOutput;
148
149}
150
uint8_t table3d_value_t
The type of each table value.
Definition table3d_typedefs.h:25
uint8_t table3d_dim_t
Encodes the length of the axes.
Definition table3d_typedefs.h:22
constexpr uint16_t yFactor
Definition table3d.h:109
Row and column coordinates in a 2D table.
Definition table3d_interpolate.h:152
table3d_dim_t row
Definition table3d_interpolate.h:153
table3d_dim_t col
Definition table3d_interpolate.h:154
Cache structure for 3D table value lookups.
Definition table3d_interpolate.h:36
xy_pair_t last_lookup
Definition table3d_interpolate.h:53
xy_coord2d lastBinMax
Definition table3d_interpolate.h:50
table3d_value_t lastOutput
Definition table3d_interpolate.h:54
2D coordinate structure for table lookups
Definition table3d_interpolate.h:28
table3d_dim_t y
Y axis coordinate.
Definition table3d_interpolate.h:32
table3d_dim_t x
X axis coordinate.
Definition table3d_interpolate.h:30
A pair of x and y values used for lookups in 3D tables.
Definition table3d_interpolate.h:15
friend bool operator==(const xy_pair_t &lhs, const xy_pair_t &rhs)
Equality operator for xy_pair_t.
Definition table3d_interpolate.h:20
uint16_t y
Definition table3d_interpolate.h:17
uint16_t x
Definition table3d_interpolate.h:16
table3d_value_t interpolate_3d_value(const xy_pair_t &lookUpValues, const table3d_dim_t &axisSize, const table3d_value_t *pValues, const table3d_bin_t &xBin, const uint16_t xMultiplier, const table3d_bin_t &yBin, const uint16_t yMultiplier)
Interpolate a table value from axis bins & values.
Definition table3d_interpolate.cpp:162
table3d_value_t get3DTableValue(struct table3DGetValueCache *pValueCache, const TValues &values, const TXAxis &xAxis, const TYAxis &yAxis, const xy_pair_t &lookupValues)
Get a value from a 3D table using the specified lookup values.
Definition table3d_interpolate.h:119
static void invalidate_cache(table3DGetValueCache *pCache)
Invalidate the cache by resetting the last lookup values.
Definition table3d_interpolate.h:58
Typedefs for primitive 3D table elements.