Speeduino
Loading...
Searching...
No Matches
table3d_values.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "table3d_typedefs.h"
13
14// ========================= INTRA-ROW ITERATION =========================
15
21public:
22
29 : pValue(pRowStart), pEnd(pRowStart+rowWidth) //cppcheck-suppress misra-c2012-10.4
30 {
31 }
32
34 const table3d_value_t* end(void) const { return pEnd; }
36 table3d_value_t* end(void) { return const_cast<table3d_value_t *>(pEnd); }
37
42 {
43 pValue = pValue + steps;
44 return *this;
45 }
46
49 {
50 return advance(1);
51 }
52
54 bool at_end(void) const
55 {
56 return pValue == pEnd;
57 }
58
60 const table3d_value_t& operator*(void) const
61 {
62 return *pValue;
63 }
66 {
67 return *const_cast<table3d_value_t *>(pValue);
68 }
69
71 table3d_dim_t size(void) const { return pEnd-pValue; }
72
73private:
74 const table3d_value_t *pValue;
75 const table3d_value_t *pEnd;
76};
77
78// ========================= INTER-ROW ITERATION =========================
79
82{
83public:
84
91 : pRowsStart(pValues + (axisSize*(axisSize-1U))), //cppcheck-suppress misra-c2012-10.4
92 pRowsEnd(pValues - axisSize),
93 rowWidth(axisSize)
94 {
95 // Table values are not linear in memory - rows are in reverse order
96 // E.g. a 4x4 table with logical element [0][0] at the bottom left
97 // (normal cartesian coordinates) has this layout.
98 // 0 1 2 3
99 // 4 5 6 7
100 // 8 9 10 11
101 // 12 13 14 15
102 // So we start at row 3 (index 12 of the array) and iterate towards
103 // the start of the array
104 //
105 // This all supports fast 3d interpolation.
106 }
107
112 {
113 pRowsStart = pRowsStart - (rowWidth * rows);
114 return *this;
115 }
116
119 {
120 return advance(1U);
121 }
122
125 {
126 return table_row_iterator(pRowsStart, rowWidth);
127 }
130 {
131 return table_row_iterator(pRowsStart, rowWidth);
132 }
133
135 bool at_end(void) const
136 {
137 return pRowsStart == pRowsEnd;
138 }
139
140private:
141 const table3d_value_t *pRowsStart;
142 const table3d_value_t *pRowsEnd;
143 table3d_dim_t rowWidth;
144};
145
146#define TABLE3D_TYPENAME_VALUE(size, xDom, yDom) CONCAT(TABLE3D_TYPENAME_BASE(size, xDom, yDom), _values)
147
148#define TABLE3D_GEN_VALUES(size, xDom, yDom) \
149 \
150 struct TABLE3D_TYPENAME_VALUE(size, xDom, yDom) { \
151 \
152 static constexpr table3d_dim_t row_size = (size); \
153 \
154 static constexpr table3d_dim_t num_rows = (size); \
155 \
162 table3d_value_t values[(uint16_t)row_size*num_rows]; \
163 \
164 \
165 table_value_iterator begin(void) \
166 { \
167 return table_value_iterator(values, row_size); \
168 } \
169 \
170 \
188 table3d_value_t& value_at(table3d_dim_t linear_index) \
189 { \
190 static_assert(row_size<17U, "Table is too big"); \
191 static_assert(num_rows<17U, "Table is too big"); \
192 /* Zero length will mess up unsigned calcs */ \
193 static_assert(row_size>0U, "No zero length rows"); \
194 static_assert(num_rows>0U, "No empty tables"); \
195 constexpr table3d_dim_t first_index = row_size*(table3d_dim_t)(num_rows-1U); \
196 const table3d_dim_t index = (table3d_dim_t)(first_index + (table3d_dim_t)(2U*(linear_index % row_size)) - linear_index); \
197 return values[index]; \
198 } \
199 };
200TABLE3D_GENERATOR(TABLE3D_GEN_VALUES)
201
202
Iterate through a table row. I.e. constant Y, changing X.
Definition table3d_values.h:20
const table3d_value_t * end(void) const
Pointer to the end of the row.
Definition table3d_values.h:34
table_row_iterator & operator++(void)
Increment the iterator by one element.
Definition table3d_values.h:48
table_row_iterator(const table3d_value_t *pRowStart, table3d_dim_t rowWidth)
Construct.
Definition table3d_values.h:28
table3d_value_t * end(void)
Pointer to the end of the row.
Definition table3d_values.h:36
table3d_dim_t size(void) const
Number of elements available.
Definition table3d_values.h:71
table_row_iterator & advance(table3d_dim_t steps)
Advance the iterator.
Definition table3d_values.h:41
table3d_value_t & operator*(void)
Dereference the iterator.
Definition table3d_values.h:65
const table3d_value_t & operator*(void) const
Dereference the iterator.
Definition table3d_values.h:60
bool at_end(void) const
Test for end of iteration.
Definition table3d_values.h:54
Iterate through a tables values, row by row.
Definition table3d_values.h:82
table_row_iterator operator*(void)
Dereference the iterator to access a row of data.
Definition table3d_values.h:129
const table_row_iterator operator*(void) const
Dereference the iterator to access a row of data.
Definition table3d_values.h:124
table_value_iterator & operator++(void)
Increment the iterator by one row.
Definition table3d_values.h:118
table_value_iterator(const table3d_value_t *pValues, table3d_dim_t axisSize)
Construct.
Definition table3d_values.h:90
table_value_iterator & advance(table3d_dim_t rows)
Advance the iterator.
Definition table3d_values.h:111
bool at_end(void) const
Test for end of iteration.
Definition table3d_values.h:135
static uint32_t rshift(uint32_t a)
Bitwise right shift - generic, unoptimized, case.
Definition bit_shifts.h:349
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
Typedefs for primitive 3D table elements.