AGX Dynamics  2.35.0.1
Range.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/agxCore_export.h>
20 
21 #include <agx/agx.h>
22 #include <agx/Vec2.h>
23 
24 namespace agx
25 {
26 
35  template <class T>
37  {
38  public:
39 
42 
43  RangeViolation(int sign, const T& v);
44 
45  /***
46  \return a reference to the sign which is +1 when violating the upper limit, -1 when violating the lower limit, and 0 otherwise.
47  */
48  int& sign();
49 
50  /***
51  \return the sign which is +1 when violating the upper limit, -1 when violating the lower limit, and 0 otherwise.
52  */
53  int sign() const;
54 
58  T& excess();
59 
63  T excess() const;
64 
65  private:
66  T m_v;
67  int m_sign;
68 
69  };
70 
78  template <class T>
79  class Range
80  {
81  public:
83  Range( const T& Min, const T& Max );
84 
86  explicit Range( const Vec2& vec );
87 
89  Range();
90 
92  explicit Range( const T& x );
93 
95  Range( const Range& rhs );
96 
97  // Copy constructor with one modified element, for min and max setters.
98  Range( const Range& rhs, const T& value, size_t i );
99 
103  T& lower();
104 
108  T lower() const;
109 
113  T& upper();
114 
118  T upper() const;
119 
124  void setLower(T lower);
125 
130  void setUpper(T upper);
131 
135  bool isInfinite() const;
136 
140  T span() const;
141 
143  Range& operator=( const Range& rhs );
144 
145  template< typename T2 >
146  Range operator * ( const T2& rhs ) const;
147 
154  RangeViolation<T> excess(const T& x) const;
155 
160  int outside(const T& x) const;
161 
163  bool inside(const T& x ) const;
164 
169  bool overlap(const Range<T>& r ) const;
170 
171  private:
172  void sync();
173 
174  private:
175  template <typename T2>
176  friend inline std::ostream& operator << ( std::ostream& stream, const Range<T>& range );
177 
178  T mMin;
179  T mMax;
180  };
181 
182  template <typename T>
183  inline std::ostream& operator << ( std::ostream& stream, const Range<T>& range )
184  {
185  stream << range.lower() << " " << range.upper();
186  return stream;
187  }
188 
193 
194 
199 
200 
201  // Implementation
202  template <class T>
203  inline RangeViolation<T>::RangeViolation() : m_sign(0), m_v(0)
204  {
205  }
206 
207  template <class T>
208  inline RangeViolation<T>::RangeViolation(int sign, const T& v) : m_v(v), m_sign(sign)
209  {
210 
211  }
212 
213  template <class T>
215  {
216  return m_sign;
217  }
218 
219  template <class T>
221  {
222  return m_sign;
223  }
224 
225  template <class T>
227  {
228  return m_v;
229  }
230 
231  template <class T>
233  {
234  return m_v;
235  }
236 
237  template <class T>
238  Range<T>::Range( const T& Min, const T& Max ) : mMin( Min ) , mMax( Max )
239  {
240  sync();
241  }
242 
243  template <class T>
244  inline Range<T>::Range( const Vec2& vec ) : mMin(vec[0]), mMax(vec[1])
245  {
246  sync();
247  }
248 
249  template <class T>
250  inline Range<T>::Range() : mMin( -std::numeric_limits<T>::infinity() ),
251  mMax( std::numeric_limits<T>::infinity() ) {}
252 
253  template <class T>
254  inline Range<T>::Range( const T& x ) : mMin( -x ),
255  mMax( x )
256  {
257  sync();
258  }
259 
260  template <class T>
261  inline Range<T>::Range( const Range& rhs )
262  {
263  *this = rhs;
264  }
265 
266  template <class T>
267  inline Range<T>::Range( const Range& rhs, const T& value, size_t i )
268  {
269  if (i == 0) {
270  mMin = value;
271  mMax = rhs.mMax;
272  } else {
273  mMin = rhs.mMin;
274  mMax = value;
275  }
276 
277  sync();
278  }
279 
280  template <class T>
282  {
283  return mMin;
284  }
285 
286  template <class T>
288  {
289  return mMin;
290  }
291 
292  template <class T>
294  {
295  return mMax;
296  }
297 
298  template <class T>
300  {
301  return mMax;
302  }
303 
304  template <class T>
306  {
307  mMin = lower;
308  sync();
309  }
310 
311  template <class T>
313  {
314  mMax = upper;
315  sync();
316  }
317 
318  template <class T>
320  {
321  return mMin == -std::numeric_limits<T>::infinity() && mMax == std::numeric_limits<T>::infinity();
322  }
323 
324  template <class T>
326  {
327  return mMax - mMin;
328  }
329 
330  template <class T>
331  inline Range<T>& Range<T>::operator=( const Range& rhs )
332  {
333  this->mMax = rhs.mMax;
334 
335  this->mMin = rhs.mMin;
336 
337  sync();
338 
339  return *this;
340  }
341 
342  template <class T>
343  template< typename T2 >
345  {
346  return Range< T >( mMin * (T)rhs, mMax * (T)rhs );
347  }
348 
349  template <class T>
350  inline RangeViolation<T> Range<T>::excess(const T& x) const
351  {
352  RangeViolation<T> ret(0, static_cast<T>(0));
353  Range<T> lhs = *this;
354  if ( x <= lhs.mMin ) {
355  ret.sign() = -1;
356  ret.excess() = lhs.mMin - x;
357  } else if ( x >= lhs.mMax ) {
358  ret.sign() = 1;
359  ret.excess() = x - lhs.mMax;
360  }
361  return ret;
362  }
363 
364  template <class T>
365  inline int Range<T>::outside(const T& x) const
366  {
367  int ret = 0;
368  Range<T> lhs = *this;
369  if ( x < lhs.mMin ) {
370  ret = -1;
371  } else if ( x > lhs.mMax ) {
372  ret = 1;
373  }
374  return ret;
375  }
376 
377  template <class T>
378  AGX_FORCE_INLINE bool Range<T>::inside(const T& x ) const
379  {
380  Range<T> lhs = *this;
381  return (x >= lhs.mMin) && (x <= lhs.mMax) ;
382  }
383 
384  template <class T>
386  {
387  return ( inside(r.lower()) || inside(r.upper())
388  || r.inside(mMin) || r.inside(mMax) ) ;
389  }
390 
391  template <class T>
392  inline void Range<T>::sync()
393  {
394  if (mMax < mMin ) {
395  std::swap(mMax, mMin);
396  }
397  }
398 } // namespace agx
This class will store a computed violation of a range.
Definition: Range.h:37
RangeViolation()
Default constructor. sign=0, v=0.
Definition: Range.h:203
int & sign()
Definition: Range.h:214
A range object has a min and max value of a given type and provides services to tell whether a value ...
Definition: Range.h:80
bool isInfinite() const
Definition: Range.h:319
Range(const Vec2 &vec)
Cast operator from Vec2.
Definition: Range.h:244
Range(const T &Min, const T &Max)
Standard creation: set min and max.
Definition: Range.h:238
Range(const Range &rhs)
Copy constructor.
Definition: Range.h:261
bool inside(const T &x) const
Definition: Range.h:378
Range & operator=(const Range &rhs)
Assignment operator.
Definition: Range.h:331
T span() const
Definition: Range.h:325
Range(const T &x)
Set min=-x and max=x.
Definition: Range.h:254
T & lower()
Definition: Range.h:281
Range(const Range &rhs, const T &value, size_t i)
Definition: Range.h:267
RangeViolation< T > excess(const T &x) const
Compute all the details of range violation: parity is +1 when violating the upper limit,...
Definition: Range.h:350
T upper() const
Definition: Range.h:299
Range operator*(const T2 &rhs) const
int outside(const T &x) const
Definition: Range.h:365
T & upper()
Definition: Range.h:293
void setUpper(T upper)
Set the upper bound for the range.
Definition: Range.h:312
friend std::ostream & operator<<(std::ostream &stream, const Range< T > &range)
Definition: Range.h:183
void setLower(T lower)
Set the lower bound for the range.
Definition: Range.h:305
T lower() const
Definition: Range.h:287
Range()
Default constructor. Set min/max to -/+ infinity.
Definition: Range.h:250
bool overlap(const Range< T > &r) const
Definition: Range.h:385
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
T sign(T v)
Definition: Math.h:328
Range< UInt > RangeUInt
A short hand for ranges of unsigned integer numbers.
Definition: Range.h:197
RangeViolation< Real > RangeViolationReal
Definition: Range.h:192
RangeViolation< UInt > RangeViolationUInt
Definition: Range.h:198
Range< Real > RangeReal
A short hand for ranges of floating point numbers.
Definition: Range.h:191
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
Definition: AtomicValue.h:109