36 : data_(
std::move(data))
43 const std::string &key_name,
44 const std::string &value_name)
47 lastInterpolatedKey_.reset();
54 for (
const auto &value : yaml.
asList()) {
55 unsigned int ct = std::stoul(value[key_name].get<std::string>(
""));
56 std::optional<T> data =
57 value[value_name].get<T>();
65 if (data_.size() < 1) {
78 void setData(std::map<unsigned int, T> &&data)
80 data_ = std::move(data);
81 lastInterpolatedKey_.reset();
88 if (quantization_ > 0)
89 key = std::lround(key / static_cast<double>(quantization_)) * quantization_;
94 if (lastInterpolatedKey_.has_value() &&
95 *lastInterpolatedKey_ == key)
96 return lastInterpolatedValue_;
98 auto it = data_.lower_bound(key);
100 if (it == data_.begin())
103 if (it == data_.end())
104 return std::prev(it)->second;
106 if (it->first == key)
109 auto it2 = std::prev(it);
110 double lambda = (key - it2->first) / static_cast<double>(it->first - it2->first);
111 interpolate(it2->second, it->second, lastInterpolatedValue_, lambda);
112 lastInterpolatedKey_ = key;
114 return lastInterpolatedValue_;
119 dest = a * (1.0 - lambda) + b * lambda;
123 std::map<unsigned int, T> data_;
124 T lastInterpolatedValue_;
125 std::optional<unsigned int> lastInterpolatedKey_;
126 unsigned int quantization_ = 0;
#define LOG(category, severity)
Log a message.
Interpolator()=default
Construct an empty interpolator.
Top-level libcamera namespace.
Definition: backtrace.h:17
Class for storing, retrieving, and interpolating objects.
Definition: interpolator.h:27
Definition: v4l2_pixelformat.h:60
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition: yaml_parser.h:206
const T & getInterpolated(unsigned int key, unsigned int *quantizedKey=nullptr)
Retrieve an interpolated value for the given key.
Definition: interpolator.h:84
#define ASSERT(condition)
Abort program execution if assertion fails.
Interpolator(std::map< unsigned int, T > &&data)
Construct an interpolator from a map of objects.
Definition: interpolator.h:35
bool isList() const
Return whether the YamlObject is a list.
Definition: yaml_parser.h:155
Interpolator(const std::map< unsigned int, T > &data)
Construct an interpolator from a map of objects.
Definition: interpolator.h:31
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
void setQuantization(const unsigned int q)
Set the quantization value.
Definition: interpolator.h:73
void interpolate(const T &a, const T &b, T &dest, double lambda)
Interpolate between two instances of T.
Definition: interpolator.h:117
A class representing the tree structure of the YAML content.
Definition: yaml_parser.h:27
void setData(std::map< unsigned int, T > &&data)
Set the internal map.
Definition: interpolator.h:78
int readYaml(const libcamera::YamlObject &yaml, const std::string &key_name, const std::string &value_name)
Initialize an Interpolator instance from yaml.
Definition: interpolator.h:42