range.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_RANGE_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_RANGE_H_
27 
28 #include <corbo-core/console.h>
29 
30 #include <array>
31 #include <cmath>
32 #include <limits>
33 #include <vector>
34 
35 namespace corbo {
36 
37 class Range
38 {
39  public:
40  explicit Range(double single_val, bool force_include_end = false)
41  : _start(single_val), _step(0.0), _end(single_val), _include_end(force_include_end)
42  {
43  update();
44  }
45  explicit Range(double start, double step, double end, bool force_include_end = false)
46  : _start(start), _step(step), _end(end), _include_end(force_include_end)
47  {
48  update();
49  }
50  explicit Range(double start, double step, int num, bool force_include_end = false)
51  : _start(start), _step(step), _end(start + (double)num * step), _include_end(force_include_end)
52  {
53  _remainder = 0;
54  _n = num;
55  }
56  explicit Range(const std::array<double, 3>& interval, bool force_include_end = false)
57  : _start(interval[0]), _step(interval[1]), _end(interval[2]), _include_end(force_include_end)
58  {
59  update();
60  }
61 
62  inline double getStart() const { return _start; }
63  inline double getStep() const { return _step; }
64  inline double getEnd() const { return _end; }
65 
66  inline bool includeEnd() const { return _include_end && getRemainder() > 1e-8; }
67 
68  inline double getEndPlusEps() const { return _end + std::numeric_limits<double>::epsilon(); }
69  inline double getRemainder() const { return _remainder; }
70 
71  inline int getNumInRange() const { return _n; } // including start
72 
73  inline double getProgressFactor(double t) const { return _step == 0.0 ? 0.0 : (t - _start) / ( _end - _start); }
74 
75  void getGrid(std::vector<double>& values)
76  {
77  int n = getNumInRange();
78  for (int i = 0; i < n; ++i) values.push_back(_start + (double)i * _step);
79  if (includeEnd()) values.push_back(_end);
80  }
81 
82  void getGrid(std::vector<double>& values, double offset)
83  {
84  int n = getNumInRange();
85  for (int i = 0; i < n; ++i) values.push_back(_start + (double)i * _step + offset);
86  if (includeEnd()) values.push_back(_end + offset);
87  }
88 
89  void printGrid()
90  {
91  int n = getNumInRange();
92  PRINT_INFO("=== Grid start ===");
93  for (int i = 0; i < n; ++i) PRINT_INFO(_start + (double)i * _step);
94  if (includeEnd()) PRINT_INFO(_end);
95  PRINT_INFO("=== Grid end ===");
96  }
97 
98  protected:
99  void update()
100  {
101  if (std::abs(_step) < 1e-15)
102  {
103  _n = 1;
104  _remainder = 0;
105  return;
106  }
107  _n = (1 + (int)((_end - _start) / _step));
109  }
110 
111  private:
112  double _start;
113  double _step;
114  double _end;
115  bool _include_end = false;
116 
117  double _remainder;
118  int _n;
119 };
120 
121 // class Slice
122 //{
123 // public:
124 // explicit Slice(int start, int stop, int step) : _start(start), _stop(stop), _step(step) {}
125 // explicit Slice(int start, int num) : _start(start), _stop(start + num), _step(1) {}
126 
127 // private:
128 // int _start;
129 // int _stop;
130 // int _step;
131 //};
132 
133 } // namespace corbo
134 
135 #endif // SRC_CORE_INCLUDE_CORBO_CORE_RANGE_H_
double _end
Definition: range.h:114
int getNumInRange() const
Definition: range.h:71
void getGrid(std::vector< double > &values)
Definition: range.h:75
double getStart() const
Definition: range.h:62
return int(ret)+1
void printGrid()
Definition: range.h:89
int _n
Definition: range.h:118
double getEndPlusEps() const
Definition: range.h:68
void getGrid(std::vector< double > &values, double offset)
Definition: range.h:82
double getEnd() const
Definition: range.h:64
double _step
Definition: range.h:113
Range(const std::array< double, 3 > &interval, bool force_include_end=false)
Definition: range.h:56
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
double getProgressFactor(double t) const
Definition: range.h:73
Range(double start, double step, int num, bool force_include_end=false)
Definition: range.h:50
double _remainder
Definition: range.h:117
double getStep() const
Definition: range.h:63
bool includeEnd() const
Definition: range.h:66
bool _include_end
Definition: range.h:115
double _start
Definition: range.h:112
Range(double start, double step, double end, bool force_include_end=false)
Definition: range.h:45
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
double getRemainder() const
Definition: range.h:69
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T fmod(const T &a, const T &b)
#define PRINT_INFO(msg)
Print msg-stream.
Definition: console.h:117
void update()
Definition: range.h:99
Range(double single_val, bool force_include_end=false)
Definition: range.h:40


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:14