AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Range.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
24namespace 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
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
125
131
135 bool isInfinite() const;
136
140 bool hasInfinite() const;
141
145 T span() const;
146
148 Range& operator=( const Range& rhs );
149
150 template< typename T2 >
151 Range operator * ( const T2& rhs ) const;
152
159 RangeViolation<T> excess(const T& x) const;
160
165 int outside(const T& x) const;
166
168 bool inside(const T& x ) const;
169
174 bool overlap(const Range<T>& r ) const;
175
176 private:
177 void sync();
178
179 private:
180 template <typename T2>
181 friend inline std::ostream& operator << ( std::ostream& stream, const Range<T>& range );
182
183 T mMin;
184 T mMax;
185 };
186
187 template <typename T>
188 inline std::ostream& operator << ( std::ostream& stream, const Range<T>& range )
189 {
190 stream << range.lower() << " " << range.upper();
191 return stream;
192 }
193
198
199
204
205
206 // Implementation
207 template <class T>
208 inline RangeViolation<T>::RangeViolation() : m_sign(0), m_v(0)
209 {
210 }
211
212 template <class T>
213 inline RangeViolation<T>::RangeViolation(int sign, const T& v) : m_v(v), m_sign(sign)
214 {
215
216 }
217
218 template <class T>
220 {
221 return m_sign;
222 }
223
224 template <class T>
226 {
227 return m_sign;
228 }
229
230 template <class T>
232 {
233 return m_v;
234 }
235
236 template <class T>
238 {
239 return m_v;
240 }
241
242 template <class T>
243 Range<T>::Range( const T& Min, const T& Max ) : mMin( Min ) , mMax( Max )
244 {
245 sync();
246 }
247
248 template <class T>
249 inline Range<T>::Range( const Vec2& vec ) : mMin(vec[0]), mMax(vec[1])
250 {
251 sync();
252 }
253
254 template <class T>
255 inline Range<T>::Range() : mMin( -std::numeric_limits<T>::infinity() ),
256 mMax( std::numeric_limits<T>::infinity() ) {}
257
258 template <class T>
259 inline Range<T>::Range( const T& x ) : mMin( -x ),
260 mMax( x )
261 {
262 sync();
263 }
264
265 template <class T>
266 inline Range<T>::Range( const Range& rhs )
267 {
268 *this = rhs;
269 }
270
271 template <class T>
272 inline Range<T>::Range( const Range& rhs, const T& value, size_t i )
273 {
274 if (i == 0) {
275 mMin = value;
276 mMax = rhs.mMax;
277 } else {
278 mMin = rhs.mMin;
279 mMax = value;
280 }
281
282 sync();
283 }
284
285 template <class T>
287 {
288 return mMin;
289 }
290
291 template <class T>
293 {
294 return mMin;
295 }
296
297 template <class T>
299 {
300 return mMax;
301 }
302
303 template <class T>
305 {
306 return mMax;
307 }
308
309 template <class T>
311 {
312 mMin = lower;
313 sync();
314 }
315
316 template <class T>
318 {
319 mMax = upper;
320 sync();
321 }
322
323 template <class T>
325 {
326 return mMin == -std::numeric_limits<T>::infinity() && mMax == std::numeric_limits<T>::infinity();
327 }
328
329 template <class T>
331 {
332 return std::isinf(mMin) || std::isinf(mMax);
333 }
334
335 template <class T>
337 {
338 return mMax - mMin;
339 }
340
341 template <class T>
342 inline Range<T>& Range<T>::operator=( const Range& rhs )
343 {
344 this->mMax = rhs.mMax;
345
346 this->mMin = rhs.mMin;
347
348 sync();
349
350 return *this;
351 }
352
353 template <class T>
354 template< typename T2 >
356 {
357 return Range< T >( mMin * (T)rhs, mMax * (T)rhs );
358 }
359
360 template <class T>
361 inline RangeViolation<T> Range<T>::excess(const T& x) const
362 {
363 RangeViolation<T> ret(0, static_cast<T>(0));
364 Range<T> lhs = *this;
365 if ( x <= lhs.mMin ) {
366 ret.sign() = -1;
367 ret.excess() = lhs.mMin - x;
368 } else if ( x >= lhs.mMax ) {
369 ret.sign() = 1;
370 ret.excess() = x - lhs.mMax;
371 }
372 return ret;
373 }
374
375 template <class T>
376 inline int Range<T>::outside(const T& x) const
377 {
378 int ret = 0;
379 Range<T> lhs = *this;
380 if ( x < lhs.mMin ) {
381 ret = -1;
382 } else if ( x > lhs.mMax ) {
383 ret = 1;
384 }
385 return ret;
386 }
387
388 template <class T>
389 AGX_FORCE_INLINE bool Range<T>::inside(const T& x ) const
390 {
391 Range<T> lhs = *this;
392 return (x >= lhs.mMin) && (x <= lhs.mMax) ;
393 }
394
395 template <class T>
397 {
398 return ( inside(r.lower()) || inside(r.upper())
399 || r.inside(mMin) || r.inside(mMax) ) ;
400 }
401
402 template <class T>
403 inline void Range<T>::sync()
404 {
405 if (mMax < mMin ) {
406 std::swap(mMax, mMin);
407 }
408 }
409} // 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:208
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:324
Range(const Vec2 &vec)
Cast operator from Vec2.
Definition Range.h:249
Range(const T &Min, const T &Max)
Standard creation: set min and max.
Definition Range.h:243
Range(const Range &rhs)
Copy constructor.
Definition Range.h:266
bool inside(const T &x) const
Definition Range.h:389
Range & operator=(const Range &rhs)
Assignment operator.
Definition Range.h:342
T span() const
Definition Range.h:336
friend std::ostream & operator<<(std::ostream &stream, const Range< T > &range)
Definition Range.h:188
Range(const T &x)
Set min=-x and max=x.
Definition Range.h:259
T & lower()
Definition Range.h:286
bool hasInfinite() const
Definition Range.h:330
Range(const Range &rhs, const T &value, size_t i)
Definition Range.h:272
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:361
T upper() const
Definition Range.h:304
Range operator*(const T2 &rhs) const
int outside(const T &x) const
Definition Range.h:376
T & upper()
Definition Range.h:298
void setUpper(T upper)
Set the upper bound for the range.
Definition Range.h:317
void setLower(T lower)
Set the lower bound for the range.
Definition Range.h:310
T lower() const
Definition Range.h:292
Range()
Default constructor. Set min/max to -/+ infinity.
Definition Range.h:255
bool overlap(const Range< T > &r) const
Definition Range.h:396
#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:202
RangeViolation< Real > RangeViolationReal
Definition Range.h:197
RangeViolation< UInt > RangeViolationUInt
Definition Range.h:203
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
Range< Real > RangeReal
A short hand for ranges of floating point numbers.
Definition Range.h:196
STL namespace.
void swap(agx::Name &lhs, agx::Name &rhs)
Definition Name.h:323