Parasol Planning Library (PPL)
Range.h
Go to the documentation of this file.
1 #ifndef RANGE_TYPE_H_
2 #define RANGE_TYPE_H_
3 
4 #include <cctype>
5 #include <iostream>
6 #include <limits>
7 #include <type_traits>
8 #include <utility>
9 
10 #include "Utilities/MPUtils.h"
11 
12 
16 template <typename T>
17 struct Range final {
18 
22 
23  T min;
24  T max;
25 
29 
31  Range() noexcept;
32 
36  Range(const T _min, const T _max) noexcept;
37 
40  Range(const std::pair<T, T>& _bounds) noexcept;
41 
45 
47  T Length() const noexcept;
48 
51  T Center() const noexcept;
52 
59  template <typename U>
60  bool Contains(const U& _val, const double _tolerance = 1e-8)
61  const noexcept;
62 
68  template <typename U>
69  T Clearance(const U& _val) const noexcept;
70 
74  template <typename U>
75  T ClearancePoint(const U& _val) const noexcept;
76 
78  T Sample() const noexcept;
79 
81  bool operator==(const Range<T>& _other) const {
82  return min == _other.min && max == _other.max;
83  }
84 
86  bool operator<(const Range<T>& _other) const {
87  if(min != _other.min)
88  return min < _other.min;
89 
90  return max < _other.max;
91  }
92 
96 
100  void Resize(const T _min, const T _max) noexcept;
101 
104  void Translate(const T _t) noexcept;
105 
108  void SetCenter(const T _t) noexcept;
109 
112  void ExpandToInclude(const T _t) noexcept;
113 
115 
116 };
117 
118 /*------------------------------- Construction -------------------------------*/
119 
120 template <typename T>
121 inline
123 Range() noexcept :
124  min(T(0)), max(T(0)) { }
125 
126 
127 template <typename T>
128 inline
130 Range(const T _min, const T _max) noexcept :
131  min(_min), max(_max) { }
132 
133 
134 template <typename T>
135 inline
137 Range(const std::pair<T, T>& _bounds) noexcept :
138  min(_bounds.first), max(_bounds.second) { }
139 
140 /*-------------------------------- Queries -----------------------------------*/
141 
142 template <typename T>
143 inline
144 T
146 Length() const noexcept {
147  return max - min;
148 }
149 
150 
151 template <typename T>
152 inline
153 T
155 Center() const noexcept {
156  return (max + min) / T(2);
157 }
158 
159 
160 template <typename T>
161 template <typename U>
162 inline
163 bool
165 Contains(const U& _val, const double _tolerance) const noexcept {
166  // James: Tolerance is causing issues with abs compilation.
167  // There is also an imbalance with the amount of tolerance at either end of the
168  // range with this implementation.
169  // An alternative is to just offer tolerance as a constant amount to extend the
170  // range in either direction "min - _tolernace, max + _tolerance".
171 
172  //return (min - std::abs(min) * _tolerance) <= _val
173  // and _val <= (max + std::abs(max) * _tolerance);
174 
175  return min <= _val and _val <= max;
176 
177 }
178 
179 
180 template <typename T>
181 template <typename U>
182 inline
183 T
185 Clearance(const U& _val) const noexcept {
186  static_assert(!std::is_unsigned<T>::value, "Can't compute clearance for "
187  "unsigned ranges as the return would be negative if the test value "
188  "lies outside the range.");
189  return std::min(_val - min, max - _val);
190 }
191 
192 
193 template <typename T>
194 template <typename U>
195 inline
196 T
198 ClearancePoint(const U& _val) const noexcept {
199  static_assert(!std::is_unsigned<T>::value, "Can't compute clearance for "
200  "unsigned ranges as the return would be negative if the test value "
201  "lies outside the range.");
202  const T distToMin = std::abs(min - _val),
203  distToMax = std::abs(max - _val);
204  return distToMin < distToMax ? min : max;
205 }
206 
207 
208 template <typename T>
209 inline
210 T
212 Sample() const noexcept {
213  return min + (max - min) * DRand();
214 }
215 
216 /*-------------------------------- Modifiers ---------------------------------*/
217 
218 template <typename T>
219 inline
220 void
222 Resize(const T _min, const T _max) noexcept {
223  min = _min;
224  max = _max;
225 }
226 
227 
228 template <typename T>
229 inline
230 void
232 Translate(const T _t) noexcept {
233  min += _t;
234  max += _t;
235 }
236 
237 
238 template <typename T>
239 inline
240 void
242 SetCenter(const T _t) noexcept {
243  Translate(_t - Center());
244 }
245 
246 
247 template <typename T>
248 inline
249 void
251 ExpandToInclude(const T _t) noexcept {
252  min = std::min(min, _t);
253  max = std::max(max, _t);
254 }
255 
256 /*---------------------------------- I/O -------------------------------------*/
257 
258 template <typename T>
259 std::ostream&
260 operator<<(std::ostream& _os, const Range<T>& _r) {
261  return _os << _r.min << ":" << _r.max;
262 }
263 
264 
265 template <typename T>
266 std::istream&
267 operator>>(std::istream& _is, Range<T>& _r) {
268  char delim;
269  return _is >> _r.min >> std::skipws >> delim >> _r.max;
270 }
271 
272 
273 /*----------------------------------------------------------------------------*/
274 
275 #endif
double DRand()
Definition: MPUtils.cpp:8
std::ostream & operator<<(std::ostream &_os, const Range< T > &_r)
Definition: Range.h:260
std::istream & operator>>(std::istream &_is, Range< T > &_r)
Definition: Range.h:267
A range of numeric values.
Definition: Range.h:17
T ClearancePoint(const U &_val) const noexcept
Definition: Range.h:198
Range() noexcept
Construct a range over all values of T.
Definition: Range.h:123
void SetCenter(const T _t) noexcept
Definition: Range.h:242
T Center() const noexcept
Definition: Range.h:155
bool Contains(const U &_val, const double _tolerance=1e-8) const noexcept
Definition: Range.h:165
bool operator<(const Range< T > &_other) const
This only for placing in sets.
Definition: Range.h:86
T Sample() const noexcept
Sample the range for a random contained value with uniform probability.
Definition: Range.h:212
void Resize(const T _min, const T _max) noexcept
Definition: Range.h:222
void ExpandToInclude(const T _t) noexcept
Definition: Range.h:251
void Translate(const T _t) noexcept
Definition: Range.h:232
T Clearance(const U &_val) const noexcept
Definition: Range.h:185
T min
The lower bound on this range.
Definition: Range.h:23
T max
The upper bound on this range.
Definition: Range.h:24
T Length() const noexcept
Compute the length of this range.
Definition: Range.h:146