rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Map2D.h
Go to the documentation of this file.
1//
2// Created by kifir on 7/29/24.
3//
4
5#pragma once
6
7#include <optional>
8
10public:
11 virtual std::optional<float> getValue(float x) const = 0;
12};
13
14template<int TLength, typename TValue, typename TBin>
15class Map2D : public ValueProvider2D {
16public:
17 Map2D(const char* const name);
18
19 void initTable(const TValue (&values)[TLength], const TBin (&bins)[TLength]);
20
21 virtual std::optional<float> getValue(const float x) const;
22private:
23 const char* const m_name;
24
25 const TValue (*m_values)[TLength] = nullptr;
26 const TBin (*m_bins)[TLength] = nullptr;
27};
28
29template<int TLength, typename TValue, typename TBin>
30Map2D<TLength, TValue, TBin>::Map2D(const char* const name) : m_name(name) {
31}
32
33template<int TLength, typename TValue, typename TBin>
34void Map2D<TLength, TValue, TBin>::initTable(const TValue (&values)[TLength], const TBin (&bins)[TLength]) {
35 m_values = &values;
36 m_bins = &bins;
37}
38
39template<int TLength, typename TValue, typename TBin>
40std::optional<float> Map2D<TLength, TValue, TBin>::getValue(const float x) const {
41 if (!m_bins) {
42 criticalError("Access to uninitialized bins: %s", m_name);
43 return {};
44 } else if (!m_values) {
45 criticalError("Access to uninitialized values: %s", m_name);
46 return {};
47 } else if ((1 < TLength) && ((*m_bins)[0] == 0.0f) && ((*m_bins)[1] == 0.0f)) {
48 // bins are uninitialized, but it isn't critical - maybe old configuration
49 return {};
50 } else {
51 return std::make_optional(interpolate2d(x, *m_bins, *m_values));
52 }
53}
Definition Map2D.h:15
const char *const m_name
Definition Map2D.h:23
virtual std::optional< float > getValue(const float x) const
Definition Map2D.h:40
void initTable(const TValue(&values)[TLength], const TBin(&bins)[TLength])
Definition Map2D.h:34
Map2D(const char *const name)
Definition Map2D.h:30
const TValue(* m_values)[TLength]
Definition Map2D.h:25
const TBin(* m_bins)[TLength]
Definition Map2D.h:26
virtual std::optional< float > getValue(float x) const =0