range.h
Go to the documentation of this file.
1 
7 #ifndef CRANGE_H
8 #define CRANGE_H
9 
10 #include <argos3/core/utility/datatypes/datatypes.h>
11 #include <argos3/core/utility/string_utilities.h>
12 #include <argos3/core/utility/configuration/argos_exception.h>
13 #include <iostream>
14 
15 namespace argos {
16 
17  template<typename T> class CRange {
18 
19  public:
20 
21  CRange(const T& t_min = T(),
22  const T& t_max = T()) :
23  m_tMin(t_min),
24  m_tMax(t_max),
25  m_tSpan(m_tMax - m_tMin) {
26  ARGOS_ASSERT(t_min <= t_max,
27  "Error initializing CRange(" <<
28  t_min << ", " << t_max << "): " <<
29  t_min << " is not <= " << t_max);
30  }
31 
32  inline T GetMin() const {
33  return m_tMin;
34  }
35 
36  inline void SetMin(const T& t_min) {
37  ARGOS_ASSERT(t_min <= m_tMax,
38  "Error setting min CRange bound (" <<
39  t_min << "): " <<
40  t_min << " is not <= " << m_tMax);
41  m_tMin = t_min;
42  /* Same as, but faster than
43  m_tSpan = m_tMax - m_tMin; */
44  m_tSpan = m_tMax;
45  m_tSpan -= m_tMin;
46  }
47 
48  inline T GetMax() const {
49  return m_tMax;
50  }
51 
52  inline void SetMax(const T& t_max) {
53  ARGOS_ASSERT(m_tMin <= t_max,
54  "Error setting max CRange bound (" <<
55  t_max << "): " <<
56  m_tMin << " is not <= " << t_max);
57  m_tMax = t_max;
58  /* Same as, but faster than
59  m_tSpan = m_tMax - m_tMin; */
60  m_tSpan = m_tMax;
61  m_tSpan -= m_tMin;
62  }
63 
64  inline T GetSpan() const {
65  return m_tSpan;
66  }
67 
68  inline void Set(const T& t_min, const T& t_max) {
69  ARGOS_ASSERT(t_min <= t_max,
70  "Error setting CRange bounds (" <<
71  t_min << ", " << t_max << "): " <<
72  t_min << " is not <= " << t_max);
73  m_tMin = t_min;
74  m_tMax = t_max;
75  /* Same as, but faster than
76  m_tSpan = m_tMax - m_tMin; */
77  m_tSpan = m_tMax;
78  m_tSpan -= m_tMin;
79  }
80 
81  inline bool WithinMinBoundIncludedMaxBoundIncluded(const T& t_value) const {
82  return t_value >= m_tMin && t_value <= m_tMax;
83  }
84 
85  inline bool WithinMinBoundIncludedMaxBoundExcluded(const T& t_value) const {
86  return t_value >= m_tMin && t_value < m_tMax;
87  }
88 
89  inline bool WithinMinBoundExcludedMaxBoundIncluded(const T& t_value) const {
90  return t_value > m_tMin && t_value <= m_tMax;
91  }
92 
93  inline bool WithinMinBoundExcludedMaxBoundExcluded(const T& t_value) const {
94  return t_value > m_tMin && t_value < m_tMax;
95  }
96 
97  inline void TruncValue(T& t_value) const {
98  if (t_value > m_tMax) t_value = m_tMax;
99  if (t_value < m_tMin) t_value = m_tMin;
100  }
101 
102  inline Real NormalizeValue(const T& t_value) const {
103  T tTmpValue(t_value);
104  TruncValue(tTmpValue);
105  return static_cast<Real>(tTmpValue - m_tMin) /
106  static_cast<Real>(m_tSpan);
107  }
108 
109  template <typename U> void MapValueIntoRange(U& t_output_value,
110  const T& t_input_value,
111  const CRange<U>& c_range) const {
112  t_output_value = (NormalizeValue(t_input_value) *
113  c_range.GetSpan()) + c_range.GetMin();
114  }
115 
116  inline void WrapValue(T& t_value) const {
117  while(t_value > m_tMax) t_value -= m_tSpan;
118  while(t_value < m_tMin) t_value += m_tSpan;
119  }
120 
121  inline friend std::ostream& operator<<(std::ostream& os,
122  const CRange& c_range) {
123  os << c_range.m_tMin << ":"
124  << c_range.m_tMax;
125  return os;
126  }
127 
128  inline friend std::istream& operator>>(std::istream& is,
129  CRange& c_range) {
130  T tValues[2];
131  ParseValues<T> (is, 2, tValues, ':');
132  c_range.Set(tValues[0], tValues[1]);
133  return is;
134  }
135 
136  private:
137 
138  T m_tMin;
139  T m_tMax;
140  T m_tSpan;
141 
142  };
143 
144 }
145 
146 #endif
void MapValueIntoRange(U &t_output_value, const T &t_input_value, const CRange< U > &c_range) const
Definition: range.h:109
Real NormalizeValue(const T &t_value) const
Definition: range.h:102
T GetMax() const
Definition: range.h:48
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
void WrapValue(T &t_value) const
Definition: range.h:116
void TruncValue(T &t_value) const
Definition: range.h:97
void Set(const T &t_min, const T &t_max)
Definition: range.h:68
void SetMin(const T &t_min)
Definition: range.h:36
CRange(const T &t_min=T(), const T &t_max=T())
Definition: range.h:21
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
bool WithinMinBoundIncludedMaxBoundExcluded(const T &t_value) const
Definition: range.h:85
T GetMin() const
Definition: range.h:32
bool WithinMinBoundExcludedMaxBoundIncluded(const T &t_value) const
Definition: range.h:89
void SetMax(const T &t_max)
Definition: range.h:52
friend std::ostream & operator<<(std::ostream &os, const CRange &c_range)
Definition: range.h:121
friend std::istream & operator>>(std::istream &is, CRange &c_range)
Definition: range.h:128
bool WithinMinBoundExcludedMaxBoundExcluded(const T &t_value) const
Definition: range.h:93
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
T GetSpan() const
Definition: range.h:64
bool WithinMinBoundIncludedMaxBoundIncluded(const T &t_value) const
Definition: range.h:81