GY-63_MS5611/libraries/DistanceTable/DistanceTable.cpp

171 lines
3.7 KiB
C++
Raw Normal View History

//
// FILE: DistanceTable.cpp
// AUTHOR: Rob Tillaart
2021-10-26 06:22:01 -04:00
// VERSION: 0.2.1
2020-11-27 05:10:47 -05:00
// PURPOSE: Arduino library to store a symmetrical distance table in less memory
// URL: https://github.com/RobTillaart/DistanceTable
2021-01-29 06:31:58 -05:00
2021-10-26 06:22:01 -04:00
// HISTORY
// 0.1.00 initial version
// 0.1.01 refactor
// 0.1.2 fix overflow; add some error detection; revert float to float to memory
// 0.1.3 2017-07-27 Fix issue #33
// 0.1.4 2019-01-10 add size()
// 0.1.5 2020-06-07 fix library.json, minor edits
// 0.1.6 2020-12-20 arduino-ci + unit test
2021-01-29 06:31:58 -05:00
// 0.2.0 2021-01-19 refactor
// properly named functions,
// add setAll(), minimum(), maximum() and count()
2021-10-26 06:22:01 -04:00
// 0.2.1 2021-10-26 update build-CI, update readme.md
// default value in constructor
2021-01-29 06:31:58 -05:00
#include "DistanceTable.h"
2021-01-29 06:31:58 -05:00
2021-10-26 06:22:01 -04:00
DistanceTable::DistanceTable(uint8_t dimension, float value)
{
2021-10-26 06:22:01 -04:00
// ATMEL 328 has ~2000 bytes RAM,
2021-01-29 06:31:58 -05:00
// so roughly 30X30 = 900 floats(4Bytes) => 1740 bytes is max feasible
_dimension = 0;
_elements = 0;
if (dimension < 2) return;
_dimension = dimension;
_elements = _dimension;
_elements *= ((_dimension - 1) / 2);
_distanceTable = (float *) malloc(_elements * sizeof(float));
if (_distanceTable == NULL)
{
_dimension = 0;
_elements = 0;
}
2021-10-26 06:22:01 -04:00
setAll(value);
}
2021-01-29 06:31:58 -05:00
DistanceTable::~DistanceTable()
{
2021-01-29 06:31:58 -05:00
if (_distanceTable != NULL)
{
free(_distanceTable);
}
}
2021-01-29 06:31:58 -05:00
void DistanceTable::setAll(float value)
{
2021-01-29 06:31:58 -05:00
for (uint16_t index = 0; index < _elements; index++)
{
_distanceTable[index] = value;
}
};
2021-01-29 06:31:58 -05:00
2017-07-27 10:24:22 -04:00
void DistanceTable::set(uint8_t x, uint8_t y, float value )
{
2021-01-29 06:31:58 -05:00
if ( x == y ) return;
2021-10-26 06:22:01 -04:00
// comment next line to skip range check (squeeze performance)
2021-01-29 06:31:58 -05:00
if ( (x >= _dimension) || (y >= _dimension)) return;
2017-07-27 10:24:22 -04:00
2021-01-29 06:31:58 -05:00
if ( x < y )
{
uint8_t t = x; x = y; y = t; // swap
}
// prevent overflow by moving to 16 bit
uint16_t index = x;
index = (index * (index - 1)) / 2 + y;
_distanceTable[index] = value;
};
2021-01-29 06:31:58 -05:00
2017-07-27 10:24:22 -04:00
float DistanceTable::get (uint8_t x, uint8_t y)
{
2021-01-29 06:31:58 -05:00
if ( x == y ) return 0.0; // TODO even true when x and y are out of range??
2021-10-26 06:22:01 -04:00
// comment next line to skip range check (squeeze performance)
2021-01-29 06:31:58 -05:00
if ( (x >= _dimension) || (y >= _dimension)) return -1; // NAN ?
if ( x < y )
{
uint8_t t = x; x = y; y = t;
}
uint16_t index = x;
index = (index * (index-1))/2 + y;
return _distanceTable[index];
};
2017-07-27 10:24:22 -04:00
2021-01-29 06:31:58 -05:00
// triangular dump
void DistanceTable::dump(Print * stream)
{
stream->println();
uint16_t index = 0;
for (uint8_t i = 0; i < _dimension - 1; i++)
{
for (uint8_t j = 0; j <= i; j++)
{
2021-01-29 06:31:58 -05:00
stream->print(_distanceTable[index++]);
stream->print("\t");
}
2021-01-29 06:31:58 -05:00
stream->println();
}
stream->println();
};
2021-01-29 06:31:58 -05:00
float DistanceTable::minimum(uint8_t &x, uint8_t &y)
{
2021-01-29 06:31:58 -05:00
float mi = _distanceTable[0];
for (uint8_t xx = 1; xx < _dimension; xx++)
{
uint16_t index = (xx * (xx - 1))/2;
for (uint8_t yy = 0; yy < xx; yy++)
{
2021-01-29 06:31:58 -05:00
float value = _distanceTable[index + yy];
if (value < mi)
{
mi = value;
x = xx;
y = yy;
}
}
}
return mi;
}
float DistanceTable::maximum(uint8_t &x, uint8_t &y)
{
float ma = _distanceTable[0];
for (uint8_t xx = 1; xx < _dimension; xx++)
{
uint16_t index = (xx * (xx - 1))/2;
for (uint8_t yy = 0; yy < xx; yy++)
{
float value = _distanceTable[index + yy];
if (value > ma)
{
ma = value;
x = xx;
y = yy;
}
}
}
return ma;
}
uint16_t DistanceTable::count(float value, float epsilon)
{
uint16_t cnt = 0;
for (uint16_t index = 0; index < _elements; index++)
{
if (abs (_distanceTable[index] - value) < epsilon) cnt++;
}
return cnt;
}
2021-01-29 06:31:58 -05:00
// --- END OF FILE ---
2021-12-17 04:50:55 -05:00