summaryrefslogtreecommitdiff
path: root/test/signal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/signal.cpp')
0 files changed, 0 insertions, 0 deletions
='n56' href='#n56'>56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * pwl.h - piecewise linear functions interface
 */
#pragma once

#include <functional>
#include <math.h>
#include <vector>

#include "libcamera/internal/yaml_parser.h"

namespace RPiController {

class Pwl
{
public:
	struct Interval {
		Interval(double _start, double _end)
			: start(_start), end(_end)
		{
		}
		double start, end;
		bool contains(double value)
		{
			return value >= start && value <= end;
		}
		double clip(double value)
		{
			return value < start ? start
					     : (value > end ? end : value);
		}
		double len() const { return end - start; }
	};
	struct Point {
		Point() : x(0), y(0) {}
		Point(double _x, double _y)
			: x(_x), y(_y) {}
		double x, y;
		Point operator-(Point const &p) const
		{
			return Point(x - p.x, y - p.y);
		}
		Point operator+(Point const &p) const
		{
			return Point(x + p.x, y + p.y);
		}
		double operator%(Point const &p) const
		{
			return x * p.x + y * p.y;
		}
		Point operator*(double f) const { return Point(x * f, y * f); }
		Point operator/(double f) const { return Point(x / f, y / f); }
		double len2() const { return x * x + y * y; }
		double len() const { return sqrt(len2()); }
	};
	Pwl() {}
	Pwl(std::vector<Point> const &points) : points_(points) {}
	int read(const libcamera::YamlObject &params);
	void append(double x, double y, const double eps = 1e-6);
	void prepend(double x, double y, const double eps = 1e-6);
	Interval domain() const;
	Interval range() const;
	bool empty() const;
	/*
	 * Evaluate Pwl, optionally supplying an initial guess for the
	 * "span". The "span" may be optionally be updated.  If you want to know
	 * the "span" value but don't have an initial guess you can set it to
	 * -1.
	 */
	double eval(double x, int *spanPtr = nullptr,
		    bool updateSpan = true) const;
	/*
	 * Find perpendicular closest to xy, starting from span+1 so you can
	 * call it repeatedly to check for multiple closest points (set span to
	 * -1 on the first call). Also returns "pseudo" perpendiculars; see
	 * PerpType enum.