libcamera v0.3.1 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
pwl.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2/*
3 * Copyright (C) 2019, Raspberry Pi Ltd
4 *
5 * Piecewise linear functions interface
6 */
7#pragma once
8
9#include <algorithm>
10#include <cmath>
11#include <functional>
12#include <string>
13#include <utility>
14#include <vector>
15
16#include "libcamera/internal/yaml_parser.h"
17
18#include "vector.h"
19
20 namespace libcamera {
21
22 namespace ipa {
23
24 class Pwl
25{
26 public:
27 using Point = Vector<double, 2>;
28
29 struct Interval {
30 Interval(double _start, double _end)
31 : start(_start), end(_end) {}
32
33 bool contains(double value)
34 {
35 return value >= start && value <= end;
36 }
37
38 double clamp(double value)
39 {
40 return std::clamp(value, start, end);
41 }
42
43 double length() const { return end - start; }
44
45 double start, end;
46 };
47
48 Pwl();
49 Pwl(const std::vector<Point> &points);
50 Pwl(std::vector<Point> &&points);
51
52 void append(double x, double y, double eps = 1e-6);
53
54 bool empty() const { return points_.empty(); }
55 size_t size() const { return points_.size(); }
56
57 Interval domain() const;
58 Interval range() const;
59
60 double eval(double x, int *span = nullptr,
61 bool updateSpan = true) const;
62
63 std::pair<Pwl, bool> inverse(double eps = 1e-6) const;
64 Pwl compose(const Pwl &other, double eps = 1e-6) const;
65
66 void map(std::function<void(double x, double y)> f) const;
67
68 static Pwl
69 combine(const Pwl &pwl0, const Pwl &pwl1,
70 std::function<double(double x, double y0, double y1)> f,
71 double eps = 1e-6);
72
73 Pwl &operator*=(double d);
74
75 std::string toString() const;
76
77 private:
78 static void map2(const Pwl &pwl0, const Pwl &pwl1,
79 std::function<void(double x, double y0, double y1)> f);
80 void prepend(double x, double y, double eps = 1e-6);
81 int findSpan(double x, int span) const;
82
83 std::vector<Point> points_;
84};
85
86} /* namespace ipa */
87
88} /* namespace libcamera */
libcamera::ipa::Pwl
Describe a univariate piecewise linear function in two-dimensional real space.
Definition pwl.h:25
libcamera::ipa::Pwl::toString
std::string toString() const
Assemble and return a string describing the piecewise linear function.
Definition pwl.cpp:412
libcamera::ipa::Pwl::append
void append(double x, double y, double eps=1e-6)
Append a point to the end of the piecewise linear function.
Definition pwl.cpp:137
libcamera::ipa::Pwl::map
void map(std::function< void(double x, double y)> f) const
Apply function to (x, y) values at every control point.
Definition pwl.cpp:335
libcamera::ipa::Pwl::combine
static Pwl combine(const Pwl &pwl0, const Pwl &pwl1, std::function< double(double x, double y0, double y1)> f, double eps=1e-6)
Combine two Pwls.
Definition pwl.cpp:385
libcamera::ipa::Pwl::compose
Pwl compose(const Pwl &other, double eps=1e-6) const
Compose two piecewise linear functions together.
Definition pwl.cpp:286
libcamera::ipa::Pwl::empty
bool empty() const
Check if the piecewise linear function is empty.
Definition pwl.h:54
libcamera::ipa::Pwl::eval
double eval(double x, int *span=nullptr, bool updateSpan=true) const
Evaluate the piecewise linear function.
Definition pwl.cpp:204
libcamera::ipa::Pwl::domain
Interval domain() const
Get the domain of the piecewise linear function.
Definition pwl.cpp:174
libcamera::ipa::Pwl::inverse
std::pair< Pwl, bool > inverse(double eps=1e-6) const
Compute the inverse function.
Definition pwl.cpp:245
libcamera::ipa::Pwl::size
size_t size() const
Retrieve the number of points in the piecewise linear function.
Definition pwl.h:55
libcamera::ipa::Pwl::operator*=
Pwl & operator*=(double d)
Multiply the piecewise linear function.
Definition pwl.cpp:401
libcamera::ipa::Pwl::Pwl
Pwl()
Construct an empty piecewise linear function.
Definition pwl.cpp:101
libcamera::ipa::Pwl::range
Interval range() const
Get the range of the piecewise linear function.
Definition pwl.cpp:183
libcamera::ipa::Vector
Vector class.
Definition vector.h:34
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::ipa::Pwl::Interval
Describe an interval in one-dimensional real space.
Definition pwl.h:29
libcamera::ipa::Pwl::Interval::clamp
double clamp(double value)
Clamp a value such that it is within the interval.
Definition pwl.h:38
libcamera::ipa::Pwl::Interval::start
double start
Start of the interval.
Definition pwl.h:45
libcamera::ipa::Pwl::Interval::Interval
Interval(double _start, double _end)
Construct an interval.
Definition pwl.h:30
libcamera::ipa::Pwl::Interval::end
double end
End of the interval.
Definition pwl.h:45
libcamera::ipa::Pwl::Interval::contains
bool contains(double value)
Check if a given value falls within the interval.
Definition pwl.h:33
libcamera::ipa::Pwl::Interval::length
double length() const
Compute the length of the interval.
Definition pwl.h:43
vector.h
Vector class.
yaml_parser.h
A YAML parser helper.