summaryrefslogtreecommitdiff
path: root/src/ipa/libipa
ModeNameSize
-rw-r--r--algorithm.cpp6618logplain
-rw-r--r--algorithm.h2456logplain
-rw-r--r--camera_sensor_helper.cpp15850logplain
-rw-r--r--camera_sensor_helper.h2098logplain
-rw-r--r--fc_queue.cpp5574logplain
-rw-r--r--fc_queue.h2802logplain
-rw-r--r--histogram.cpp4437logplain
-rw-r--r--histogram.h807logplain
-rw-r--r--meson.build536logplain
-rw-r--r--module.cpp3899logplain
-rw-r--r--module.h2697logplain
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * piecewise linear functions
 */

#include <cassert>
#include <cmath>
#include <stdexcept>

#include "pwl.h"

using namespace RPiController;

int Pwl::read(const libcamera::YamlObject &params)
{
	if (!params.size() || params.size() % 2)
		return -EINVAL;

	const auto &list = params.asList();

	for (auto it = list.begin(); it != list.end(); it++) {
		auto x = it->get<double>();
		if (!x)
			return -EINVAL;
		if (it != list.begin() && *x <= points_.back().x)
			return -EINVAL;

		auto y = (++it)->get<double>();
		if (!y)
			return -EINVAL;

		points_.push_back(Point(*x, *y));
	}

	return 0;
}

void Pwl::append(double x, double y, const double eps)
{
	if (points_.empty() || points_.back().x + eps < x)
		points_.push_back(Point(x, y));
}

void Pwl::prepend(double x, double y, const double eps)
{
	if (points_.empty() || points_.front().x - eps > x)
		points_.insert(points_.begin(), Point(x, y));
}

Pwl::Interval Pwl::domain() const
{
	return Interval(points_[0].x, points_[points_.size() - 1].x);
}

Pwl::Interval Pwl::range() const
{
	double lo = points_[0].y, hi = lo;
	for (auto &p : points_)
		lo = std::min(lo, p.y), hi = std::max(hi, p.y);
	return Interval(lo, hi);
}

bool Pwl::empty() const
{
	return points_.empty();
}

double Pwl::eval(double x, int *spanPtr, bool updateSpan) const
{
	int span = findSpan(x, spanPtr && *spanPtr != -1 ? *spanPtr : points_.size() / 2 - 1);
	if (spanPtr && updateSpan)
		*spanPtr = span;
	return points_[span].y +
	       (x - points_[span].x) * (points_[span + 1].y - points_[span].y) /
		       (points_[span + 1].x - points_[span].x);
}

int Pwl::findSpan(double x, int span) const
{
	/*
	 * Pwls are generally small, so linear search may well be faster than
	 * binary, though could review this if large PWls start turning up.
	 */
	int lastSpan = points_.size() - 2;
	/*
	 * some algorithms may call us with span pointing directly at the last
	 * control point
	 */
	span = std::max(0, std::min(lastSpan, span));
	while (span < lastSpan && x >= points_[span + 1].x)
		span++;
	while (span && x < points_[span].x)
		span--;
	return span;
}

Pwl::PerpType Pwl::invert(Point const &xy, Point &perp, int &span,
			  const double eps) const
{
	assert(span >= -1);
	bool prevOffEnd = false;
	for (span = span + 1; span < (int)points_.size() - 1; span++) {
		Point spanVec = points_[span + 1] - points_[span];
		double t = ((xy - points_[span]) % spanVec) / spanVec.len2();
		if (t < -eps) /* off the start of this span */
		{
			if (span == 0) {
				perp = points_[span];
				return PerpType::Start;
			} else if (prevOffEnd) {
				perp = points_[span];
				return PerpType::Vertex;
			}
		} else if (t > 1 + eps) /* off the end of this span */
		{
			if (span == (int)points_.size() - 2) {
				perp = points_[span + 1];
				return PerpType::End;
			}
			prevOffEnd = true;
		} else /* a true perpendicular */
		{
			perp = points_[span] + spanVec * t;
			return PerpType::Perpendicular;
		}
	}
	return PerpType::None;
}

Pwl Pwl::inverse(bool *trueInverse, const double eps) const
{
	bool appended = false, prepended = false, neither = false;
	Pwl inverse;

	for (Point const &p : points_) {
		if (inverse.empty())
			inverse.append(p.y, p.x, eps);
		else if (std::abs(inverse.points_.back().x - p.y) <= eps ||
			 std::abs(inverse.points_.front().x - p.y) <= eps)
			/* do nothing */;
		else if (p.y > inverse.points_.back().x) {
			inverse.append(p.y, p.x, eps);
			appended = true;
		} else if (p.y < inverse.points_.front().x) {
			inverse.prepend(p.y, p.x, eps);
			prepended = true;
		} else
			neither = true;
	}

	/*
	 * This is not a proper inverse if we found ourselves putting points
	 * onto both ends of the inverse, or if there were points that couldn't
	 * go on either.
	 */
	if (trueInverse)