Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/common/unit_interval.hpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

97 lines
2.5 KiB
C++

#ifndef UNIT_INTERVAL_HPP_BG1EK7QX
#define UNIT_INTERVAL_HPP_BG1EK7QX
#include <boost/operators.hpp>
#include <iterator>
#include <type_traits>
namespace vanetza
{
/**
* UnitInterval represents a number within the unit interval [0.0, 1.0]
*
* UnitInterval is not an interval on its own but limits all numbers to this interval.
* Mantissa (positive fractional part of a real number) behaves differently, thus:
* - Mantissa(42.1234) = 0.1234
* - UnitInterval(42.1234) = 1.0
* UnitInterval is also related to "(proper) decimal fraction" but latter does not include 1.0.
*/
class UnitInterval :
boost::arithmetic<UnitInterval>,
boost::arithmetic<UnitInterval, double>,
boost::totally_ordered<UnitInterval>
{
public:
constexpr UnitInterval() : UnitInterval(0.0) {}
constexpr explicit UnitInterval(double v) : m_value(clamp(v)) {}
UnitInterval(const UnitInterval&) = default;
UnitInterval& operator=(const UnitInterval&) = default;
// arithmetic
UnitInterval& operator+=(const UnitInterval&);
UnitInterval& operator-=(const UnitInterval&);
UnitInterval& operator*=(const UnitInterval&);
UnitInterval& operator/=(const UnitInterval&);
UnitInterval& operator+=(double);
UnitInterval& operator-=(double);
UnitInterval& operator*=(double);
UnitInterval& operator/=(double);
// partially ordered
bool operator<(const UnitInterval& other) const;
bool operator==(const UnitInterval& other) const;
double value() const { return m_value; }
UnitInterval complement() const;
private:
constexpr static double clamp(double v)
{
return (v > 1.0 ? 1.0 : (v < 0.0 ? 0.0 : v));
}
UnitInterval& clamp();
double m_value;
};
/**
* Calculate mean value of two unit intervals
* \param lhs
* \param rhs
* \return mean unit interval
*/
UnitInterval mean(UnitInterval lhs, UnitInterval rhs);
/**
* Calculate mean of a range of unit intervals
* \param begin of range
* \params end of range
* \reutrn mean unit interval
*/
template<
typename Iterator,
typename std::enable_if<
std::is_convertible<typename std::iterator_traits<Iterator>::value_type, UnitInterval>::value,
int>::type = 0
>
UnitInterval mean(Iterator begin, Iterator end)
{
unsigned count = 0;
double accu = 0.0;
for (Iterator it = begin; it != end; ++it)
{
accu += it->value();
++count;
}
return UnitInterval { count > 1 ? (accu / count) : accu };
}
} // namespace vanetza
#endif /* UNIT_INTERVAL_HPP_BG1EK7QX */