Speeduino
Loading...
Searching...
No Matches
table3d_interpolate.cpp File Reference

Support functions for 3D table interpolation. More...

#include "table3d_interpolate.h"
#include "maths.h"
#include "unit_testing.h"
#include "table2d.h"

Functions

Interpolation
TESTABLE_INLINE_STATIC QU1X8_t compute_bin_position (const uint16_t &value, const table3d_bin_t &bin, const uint16_t &multiplier)
 Compute the % position of a value within a bin.
 
TESTABLE_INLINE_STATIC row_col2d toTopRight (const table3d_bin_t &xBin, const table3d_bin_t &yBin, const table3d_dim_t &axisSize)
 Get the top right corner of the value coordinates in a 3D table, based on x/y axis coords.
 
static row_col2d toBottomLeft (const row_col2d &topRight, const table3d_dim_t &axisSize)
 Get the bottom left corner of the value coordinates in a 3D table, based on top right corner.
 
TESTABLE_INLINE_STATIC table3d_value_t bilinear_interpolation (const table3d_value_t &tl, const table3d_value_t &tr, const table3d_value_t &bl, const table3d_value_t &br, const QU1X8_t &dx, const QU1X8_t &dy)
 2d interpolation, given 4 corner values and x/y percentages
 
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.
 

Fixed point math

typedef uint16_t QU1X8_t
 Unsigned fixed point number type with 1 integer bit & 8 fractional bits.
 
constexpr QU1X8_t QU1X8_INTEGER_SHIFT = 8
 Integer shift to convert to/from QU1X8_t.
 
TESTABLE_CONSTEXPR QU1X8_t QU1X8_ONE = toQU1X8(1U)
 Precomputed value of 1 in QU1X8_t.
 
TESTABLE_CONSTEXPR QU1X8_t QU1X8_HALF = QU1X8_ONE/2U
 Precomputed value of 0.5 in QU1X8_t.
 
static constexpr QU1X8_t toQU1X8 (uint16_t base)
 Unsigned fixed point number type with 1 integer bit & 8 fractional bits.
 
static constexpr uint16_t fromQU1X8 (QU1X8_t base)
 Unsigned fixed point number type with 1 integer bit & 8 fractional bits.
 
TESTABLE_INLINE_STATIC QU1X8_t mulQU1X8 (QU1X8_t a, QU1X8_t b)
 Multiply two QU1X8_t values.
 

Detailed Description

Support functions for 3D table interpolation.

Typedef Documentation

◆ QU1X8_t

typedef uint16_t QU1X8_t

Unsigned fixed point number type with 1 integer bit & 8 fractional bits.

See also
https://en.wikipedia.org/wiki/Q_(number_format).

This is specialised for the number range 0..1 - a generic fixed point class would miss some important optimisations. Specifically, we can avoid type promotion during multiplication.

Function Documentation

◆ bilinear_interpolation()

TESTABLE_INLINE_STATIC table3d_value_t bilinear_interpolation ( const table3d_value_t tl,
const table3d_value_t tr,
const table3d_value_t bl,
const table3d_value_t br,
const QU1X8_t dx,
const QU1X8_t dy 
)

2d interpolation, given 4 corner values and x/y percentages

 tl----------------tr
 |                 |
 |                 |
 |                 |
 |>>>>>dx>>>>?     |
 |           ^     |
 |           dy    |
 |           ^     |
 bl----------------br
Parameters
tlTop left value
trTop right value
blBottom left value
brBottom right value
dxX distance
dyY distance
Returns
table3d_value_t

◆ compute_bin_position()

TESTABLE_INLINE_STATIC QU1X8_t compute_bin_position ( const uint16_t &  value,
const table3d_bin_t &  bin,
const uint16_t &  multiplier 
)

Compute the % position of a value within a bin.

  • 0%==at/below the bin minimum
  • 100%==at/above the bin maximum
  • 50%==in the middle of the bin.
Note
The multiplier is used to scale the axis values to the same scale as the value being checked. This retains the full precision of the axis values, thus the computed position and eventually the final interpolated result
Parameters
valueThe value to check.
binThe axis bin.
multiplierThe multiplier for the axis values.
Returns
QU1X8_t The % position of the value within the bin.

◆ fromQU1X8()

static constexpr uint16_t fromQU1X8 ( QU1X8_t  base)
staticconstexpr

Unsigned fixed point number type with 1 integer bit & 8 fractional bits.

See also
https://en.wikipedia.org/wiki/Q_(number_format).

This is specialised for the number range 0..1 - a generic fixed point class would miss some important optimisations. Specifically, we can avoid type promotion during multiplication.

◆ interpolate_3d_value()

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.

Parameters
lookUpValuesThe x & y axis values we are interpolating
axisSizeThe length of an axis
pValuesThe interpolation source values
xBinThe x-axis bin containing lookUpValues.x
xMultiplierThe x-axis multiplier
yBinThe y-axis bin containing lookUpValues.y
yMultiplierThe y-axis multiplier
Returns
table3d_value_t

◆ mulQU1X8()

TESTABLE_INLINE_STATIC QU1X8_t mulQU1X8 ( QU1X8_t  a,
QU1X8_t  b 
)

Multiply two QU1X8_t values.

◆ toBottomLeft()

static row_col2d toBottomLeft ( const row_col2d topRight,
const table3d_dim_t axisSize 
)
inlinestatic

Get the bottom left corner of the value coordinates in a 3D table, based on top right corner.

◆ toQU1X8()

static constexpr QU1X8_t toQU1X8 ( uint16_t  base)
staticconstexpr

Unsigned fixed point number type with 1 integer bit & 8 fractional bits.

See also
https://en.wikipedia.org/wiki/Q_(number_format).

This is specialised for the number range 0..1 - a generic fixed point class would miss some important optimisations. Specifically, we can avoid type promotion during multiplication.

◆ toTopRight()

TESTABLE_INLINE_STATIC row_col2d toTopRight ( const table3d_bin_t &  xBin,
const table3d_bin_t &  yBin,
const table3d_dim_t axisSize 
)

Get the top right corner of the value coordinates in a 3D table, based on x/y axis coords.

Variable Documentation

◆ QU1X8_HALF

Precomputed value of 0.5 in QU1X8_t.

◆ QU1X8_INTEGER_SHIFT

constexpr QU1X8_t QU1X8_INTEGER_SHIFT = 8
constexpr

Integer shift to convert to/from QU1X8_t.

◆ QU1X8_ONE

Precomputed value of 1 in QU1X8_t.