Speeduino
Loading...
Searching...
No Matches
table3d_interpolate.h
Go to the documentation of this file.
1#pragma once
2
3#include "table3d_typedefs.h"
4#include "maths.h"
5
17
19static inline bool operator==(const xy_values& lhs, const xy_values& rhs)
20{
21 return lhs.x==rhs.x && lhs.y==rhs.y;
22}
23
32
35 // Store the upper *index* of the X and Y axis bins that were last hit.
36 // This is used to make the next check faster since very likely the x & y values have
37 // only changed by a small amount & are in the same bin (or an adjacent bin).
38 //
39 // It's implicit that the other bin index is max bin index - 1 (a single axis
40 // value can't span 2 axis bins). This saves 1 byte.
41 //
42 // E.g. 6 element x-axis contents:
43 // [ 8| 9|12|15|18|21]
44 // indices:
45 // 0, 1, 2, 3, 4, 5
46 // If lastXBinMax==3, the min index must be 2. I.e. the last X value looked
47 // up was between 12<X<=15.
48 xy_coord2d lastBinMax = { 1U, 1U };
49
50 //Store the last input and output values, again for caching purposes
53};
54
57{
58 pCache->last_lookup.x = UINT16_MAX;
59}
60
62// private to table3D implementation
63
65 const table3d_axis_t &value,
66 const table3d_axis_t *pAxis,
68 table3d_dim_t lastBinMax);
69
77 const uint16_t yMultiplier);
78
80
123template <uint16_t xFactor, uint16_t yFactor>
127 const table3d_axis_t *pXAxis,
128 const table3d_axis_t *pYAxis,
129 const xy_values &lookupValues) {
130
131
132 // Check if the lookup values are the same as the last time we looked up a value
133 // If they are, we can return the cached value
134 if( lookupValues == pValueCache->last_lookup)
135 {
136 return pValueCache->lastOutput;
137 }
138
139 // Figure out where on the axes the incoming coord are
142 // Interpolate based on the bin positions
144 // Store the last lookup values so we can check them next time
145 pValueCache->last_lookup = lookupValues;
146
147 return pValueCache->lastOutput;
148
149}
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:348
uint8_t table3d_axis_t
The type of each axis value.
Definition table3d_typedefs.h:28
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
Cache structure for 3D table value lookups.
Definition table3d_interpolate.h:34
xy_values last_lookup
Definition table3d_interpolate.h:51
xy_coord2d lastBinMax
Definition table3d_interpolate.h:48
table3d_value_t lastOutput
Definition table3d_interpolate.h:52
2D coordinate structure for table lookups
Definition table3d_interpolate.h:26
table3d_dim_t y
Y axis coordinate.
Definition table3d_interpolate.h:30
table3d_dim_t x
X axis coordinate.
Definition table3d_interpolate.h:28
A pair of x and y values used for lookups in 3D tables.
Definition table3d_interpolate.h:13
uint16_t y
Definition table3d_interpolate.h:15
uint16_t x
Definition table3d_interpolate.h:14
table3d_dim_t find_bin_max(const table3d_axis_t &value, const table3d_axis_t *pAxis, table3d_dim_t length, table3d_dim_t lastBinMax)
Find the bin that covers the test value.
Definition table3d_interpolate.cpp:75
table3d_value_t interpolate_3d_value(const xy_values &lookUpValues, const xy_coord2d &upperBinIndices, const table3d_dim_t &axisSize, const table3d_value_t *pValues, const table3d_axis_t *pXAxis, const uint16_t xMultiplier, const table3d_axis_t *pYAxis, const uint16_t yMultiplier)
Interpolate a table value from axis bins & values.
Definition table3d_interpolate.cpp:252
static bool operator==(const xy_values &lhs, const xy_values &rhs)
Equality operator for xy_values.
Definition table3d_interpolate.h:19
table3d_value_t get3DTableValue(struct table3DGetValueCache *pValueCache, const table3d_dim_t axisSize, const table3d_value_t *pValues, const table3d_axis_t *pXAxis, const table3d_axis_t *pYAxis, const xy_values &lookupValues)
Get a value from a 3D table using the specified lookup values.
Definition table3d_interpolate.h:124
static void invalidate_cache(table3DGetValueCache *pCache)
Invalidate the cache by resetting the last lookup values.
Definition table3d_interpolate.h:56
Typedefs for primitive 3D table elements.