Helios++
Helios software for LiDAR simulations
LinearPiecesFunction.h
1 #pragma once
2 
3 #include <fluxionum/Function.h>
4 
5 #include <armadillo>
6 
7 namespace fluxionum{
8 
27 template <typename A, typename B>
28 class LinearPiecesFunction : public Function<A, B>{
29 protected:
30  // *** ATTRIBUTES *** //
31  // ******************** //
36  arma::Col<A> const &start;
40  arma::Col<B> const &slope;
44  arma::Col<B> const &intercept;
45 
46 public:
47  // *** CONSTRUCTION / DESTRUCTION *** //
48  // ************************************ //
56  Col<A> const &start,
57  Col<B> const &slope,
58  Col<B> const &intercept
59  ) :
60  start(start),
61  slope(slope),
63  {}
64  virtual ~LinearPiecesFunction() = default;
65 
66  // *** FUNCTION METHODS *** //
67  // ************************** //
79  B eval(A const &x) override{
80  size_t const xIdx = findIndex(x);
81  return (x-getStart(xIdx))*getSlope(xIdx) + getIntercept(xIdx);
82  }
83 
84 
85  // *** LINEAR FUNCTION *** //
86  // ************************* //
93  inline A getStart(size_t const i=0) const {return start.at(i);}
100  inline B getSlope(size_t const i=0) const {return slope.at(i);}
107  inline B getIntercept(size_t const i=0) const {return intercept.at(i);}
115  inline size_t findIndex(A const &x) const{return findIndex(x, start);}
120  static inline size_t findIndex(
121  A const &x,
122  arma::Col<A> const &start
123  ) {
124  size_t const m = start.n_elem-1;
125  for(size_t i = m ; i > 0 ; --i) if(x >= start.at(i)) return i;
126  return 0;
127  }
128 };
129 
130 }
Abstract class representing a function.
Definition: Function.h:27
Linear pieces function.
Definition: LinearPiecesFunction.h:28
B getIntercept(size_t const i=0) const
Obtain the -th intercept of the linear function.
Definition: LinearPiecesFunction.h:107
B eval(A const &x) override
Calculate the image of by assuming a linear behavior where is the slope and is the intercept.
Definition: LinearPiecesFunction.h:79
arma::Col< A > const & start
The set of sorted start points.
Definition: LinearPiecesFunction.h:36
static size_t findIndex(A const &x, arma::Col< A > const &start)
Assist the LinearPiecesFunction::findIndex(A const &)
Definition: LinearPiecesFunction.h:120
B getSlope(size_t const i=0) const
Obtain the -th slope of the linear function.
Definition: LinearPiecesFunction.h:100
size_t findIndex(A const &x) const
Obtain the index identifying the interval where belongs to. It is, find such that .
Definition: LinearPiecesFunction.h:115
LinearPiecesFunction(Col< A > const &start, Col< B > const &slope, Col< B > const &intercept)
LinearPiecesFunction default constructor.
Definition: LinearPiecesFunction.h:55
arma::Col< B > const & slope
The slopes for each piece .
Definition: LinearPiecesFunction.h:40
A getStart(size_t const i=0) const
Obtain the -th start point of the linear function.
Definition: LinearPiecesFunction.h:93
arma::Col< B > const & intercept
The intercepts for each piece .
Definition: LinearPiecesFunction.h:44