Loading...
Searching...
No Matches
interpolator.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
*
* Helper class for interpolating maps of objects
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
#include <tuple>
#include <libcamera/base/log.h>
#include "libcamera/internal/yaml_parser.h"
namespace libcamera {
LOG_DECLARE_CATEGORY(Interpolator)
namespace ipa {
template<typename T>
class Interpolator
{
public:
Interpolator() = default;
Interpolator(const std::map<unsigned int, T> &data)
: data_(data)
{
}
Interpolator(std::map<unsigned int, T> &&data)
: data_(std::move(data))
{
}
~Interpolator() = default;
int readYaml(const libcamera::YamlObject &yaml,
const std::string &key_name,
const std::string &value_name)
{
data_.clear();
lastInterpolatedKey_.reset();
if (!yaml.isList()) {
LOG(Interpolator, Error) << "yaml object must be a list";
return -EINVAL;
}
for (const auto &value : yaml.asList()) {
unsigned int ct = std::stoul(value[key_name].get<std::string>(""));
std::optional<T> data =
value[value_name].get<T>();
if (!data) {
return -EINVAL;
}
data_[ct] = *data;
}
if (data_.size() < 1) {
LOG(Interpolator, Error) << "Need at least one element";
return -EINVAL;
}
return 0;
}
void setData(std::map<unsigned int, T> &&data)
{
data_ = std::move(data);
lastInterpolatedKey_.reset();
}
const std::map<unsigned int, T> &data() const
{
return data_;
}
const T &getInterpolated(unsigned int key)
{
ASSERT(data_.size() > 0);
if (lastInterpolatedKey_.has_value() &&
*lastInterpolatedKey_ == key)
return lastInterpolatedValue_;
auto it = data_.lower_bound(key);
if (it == data_.begin())
return it->second;
if (it == data_.end())
return std::prev(it)->second;
if (it->first == key)
return it->second;
auto it2 = std::prev(it);
double lambda = (key - it2->first) / static_cast<double>(it->first - it2->first);
interpolate(it2->second, it->second, lastInterpolatedValue_, lambda);
lastInterpolatedKey_ = key;
return lastInterpolatedValue_;
}
void interpolate(const T &a, const T &b, T &dest, double lambda)
{
dest = a * (1.0 - lambda) + b * lambda;
}
private:
std::map<unsigned int, T> data_;
T lastInterpolatedValue_;
std::optional<unsigned int> lastInterpolatedKey_;
};
} /* namespace ipa */
} /* namespace libcamera */
libcamera::YamlObject
A class representing the tree structure of the YAML content.
Definition yaml_parser.h:28
libcamera::YamlObject::asList
ListAdapter asList() const
Wrap a list YamlObject in an adapter that exposes iterators.
Definition yaml_parser.h:206
libcamera::YamlObject::isList
bool isList() const
Return whether the YamlObject is a list.
Definition yaml_parser.h:155
libcamera::ipa::Interpolator
Class for storing, retrieving, and interpolating objects.
Definition interpolator.h:28
libcamera::ipa::Interpolator::getInterpolated
const T & getInterpolated(unsigned int key)
Retrieve an interpolated value for the given key.
Definition interpolator.h:84
libcamera::ipa::Interpolator::Interpolator
Interpolator(const std::map< unsigned int, T > &data)
Construct an interpolator from a map of objects.
Definition interpolator.h:31
libcamera::ipa::Interpolator::setData
void setData(std::map< unsigned int, T > &&data)
Set the internal map.
Definition interpolator.h:73
libcamera::ipa::Interpolator::Interpolator
Interpolator()=default
Construct an empty interpolator.
libcamera::ipa::Interpolator::data
const std::map< unsigned int, T > & data() const
Access the internal map.
Definition interpolator.h:79
libcamera::ipa::Interpolator::Interpolator
Interpolator(std::map< unsigned int, T > &&data)
Construct an interpolator from a map of objects.
Definition interpolator.h:35
libcamera::ipa::Interpolator::readYaml
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
libcamera::ipa::Interpolator::interpolate
void interpolate(const T &a, const T &b, T &dest, double lambda)
Interpolate between two instances of T.
Definition interpolator.h:111
log.h
Logging infrastructure.
LOG_DECLARE_CATEGORY
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
LOG
#define LOG(category, severity)
Log a message.
Definition log.h:129
ASSERT
#define ASSERT(condition)
Abort program execution if assertion fails.
Definition log.h:133
libcamera::ipa
The IPA (Image Processing Algorithm) namespace.
Definition af.cpp:58
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
yaml_parser.h
A YAML parser helper.