Brick
Loading...
Searching...
No Matches
util.h
1#pragma once
2
3#include <math.h>
4
5#include <vector>
6
7#include "Eigen/Dense"
8#include "core/math/geometry/translation2d.h"
9
10/**
11 * Constrain the input between a minimum and a maximum value
12 *
13 * @param val the value to be restrained
14 * @param low the minimum value that will be returned
15 * @param high the maximum value that will be returned
16 **/
17inline constexpr double clamp(double value, double low, double high) {
18 return (value < low) ? low : (value > high) ? high : value;
19}
20
21/**
22 * Returns the sign of a number
23 * @param x
24 *
25 * returns the sign +/-1 of x. 0 if x is 0
26 **/
27inline constexpr double sign(double x) { return (x > 0) - (x < 0); }
28
29/**
30 * Calculates the variance of a set of numbers (needed for linear regression)
31 * https://en.wikipedia.org/wiki/Variance
32 * @param values the values for which the variance is taken
33 * @param mean the average of values
34 */
35double variance(std::vector<double> const& values, double mean);
36
37/**
38 * Calculates the average of a vector of doubles
39 * @param values the list of values for which the average is taken
40 */
41double mean(std::vector<double> const& values);
42
43/**
44 * Calculates the covariance of a set of points (needed for linear regression)
45 * https://en.wikipedia.org/wiki/Covariance
46 * @param points the points for which the covariance is taken
47 * @param meanx the mean value of all x coordinates in points
48 * @param meany the mean value of all y coordinates in points
49 */
50double covariance(std::vector<std::pair<double, double>> const& points, double meanx, double meany);
51
52/**
53 * Calculates the slope and y intercept of the line of best fit for the data
54 * @param points the points for the data
55 * @return a pair containing the slope and y intercept
56 */
57std::pair<double, double> calculate_linear_regression(
58 std::vector<std::pair<double, double>> const& points
59);
60
61double estimate_path_length(const std::vector<Translation2d>& points);