1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
|
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2019, Raspberry Pi (Trading) Limited
*
* pwl.hpp - piecewise linear functions interface
*/
#pragma once
#include <math.h>
#include <vector>
#include <boost/property_tree/ptree.hpp>
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) {}
void Read(boost::property_tree::ptree const ¶ms);
void Append(double x, double y, const double eps = 1e-6);
|