AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
Range.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. 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
201
202
207
208
209 // Implementation
210 template <class T>
211 inline RangeViolation<T>::RangeViolation() : m_sign(0), m_v(0)
212 {
213 }
214
215 template <class T>
216 inline RangeViolation<T>::RangeViolation(int sign, const T& v) : m_v(v), m_sign(sign)
217 {
218
219 }
220
221 template <class T>
223 {
224 return m_sign;
225 }
226
227 template <class T>
229 {
230 return m_sign;
231 }
232
233 template <class T>
235 {
236 return m_v;
237 }
238
239 template <class T>
241 {
242 return m_v;
243 }
244
245 template <class T>
246 Range<T>::Range( const T& Min, const T& Max ) : mMin( Min ) , mMax( Max )
247 {
248 sync();
249 }
250
251 template <class T>
252 inline Range<T>::Range( const Vec2& vec ) : mMin( static_cast<T>( vec[0] ) ), mMax( static_cast<T>( vec[1] ) )
253 {
254 sync();
255 }
256
257 template <class T>
258 inline Range<T>::Range() : mMin( -std::numeric_limits<T>::infinity() ),
259 mMax( std::numeric_limits<T>::infinity() ) {}
260
261 template <class T>
262 inline Range<T>::Range( const T& x ) : mMin( -x ),
263 mMax( x )
264 {
265 sync();
266 }
267
268 template <class T>
269 inline Range<T>::Range( const Range& rhs )
270 {
271 *this = rhs;
272 }
273
274 template <class T>
275 inline Range<T>::Range( const Range& rhs, const T& value, size_t i )
276 {
277 if (i == 0) {
278 mMin = value;
279 mMax = rhs.mMax;
280 } else {
281 mMin = rhs.mMin;
282 mMax = value;
283 }
284
285 sync();
286 }
287
288 template <class T>
290 {
291 return mMin;
292 }
293
294 template <class T>
296 {
297 return mMin;
298 }
299
300 template <class T>
302 {
303 return mMax;
304 }
305
306 template <class T>
308 {
309 return mMax;
310 }
311
312 template <class T>
314 {
315 mMin = lower;
316 sync();
317 }
318
319 template <class T>
321 {
322 mMax = upper;
323 sync();
324 }
325
326 template <class T>
328 {
329 return mMin == -std::numeric_limits<T>::infinity() && mMax == std::numeric_limits<T>::infinity();
330 }
331
332 template <class T>
334 {
335 return std::isinf(mMin) || std::isinf(mMax);
336 }
337
338 template <class T>
340 {
341 return mMax - mMin;
342 }
343
344 template <class T>
345 inline Range<T>& Range<T>::operator=( const Range& rhs )
346 {
347 this->mMax = rhs.mMax;
348
349 this->mMin = rhs.mMin;
350
351 sync();
352
353 return *this;
354 }
355
356 template <class T>
357 template< typename T2 >
359 {
360 return Range< T >( mMin * (T)rhs, mMax * (T)rhs );
361 }
362
363 template <class T>
364 inline RangeViolation<T> Range<T>::excess(const T& x) const
365 {
366 RangeViolation<T> ret(0, static_cast<T>(0));
367 Range<T> lhs = *this;
368 if ( x <= lhs.mMin ) {
369 ret.sign() = -1;
370 ret.excess() = lhs.mMin - x;
371 } else if ( x >= lhs.mMax ) {
372 ret.sign() = 1;
373 ret.excess() = x - lhs.mMax;
374 }
375 return ret;
376 }
377
378 template <class T>
379 inline int Range<T>::outside(const T& x) const
380 {
381 int ret = 0;
382 Range<T> lhs = *this;
383 if ( x < lhs.mMin ) {
384 ret = -1;
385 } else if ( x > lhs.mMax ) {
386 ret = 1;
387 }
388 return ret;
389 }
390
391 template <class T>
392 AGX_FORCE_INLINE bool Range<T>::inside(const T& x ) const
393 {
394 Range<T> lhs = *this;
395 return (x >= lhs.mMin) && (x <= lhs.mMax) ;
396 }
397
398 template <class T>
400 {
401 return ( inside(r.lower()) || inside(r.upper())
402 || r.inside(mMin) || r.inside(mMax) ) ;
403 }
404
405 template <class T>
406 inline void Range<T>::sync()
407 {
408 if (mMax < mMin ) {
409 std::swap(mMax, mMin);
410 }
411 }
412} // 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:211
int & sign()
Definition: Range.h:222
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:327
Range(const Vec2 &vec)
Cast operator from Vec2.
Definition: Range.h:252
Range(const T &Min, const T &Max)
Standard creation: set min and max.
Definition: Range.h:246
Range(const Range &rhs)
Copy constructor.
Definition: Range.h:269
bool inside(const T &x) const
Definition: Range.h:392
Range & operator=(const Range &rhs)
Assignment operator.
Definition: Range.h:345
T span() const
Definition: Range.h:339
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:262
T & lower()
Definition: Range.h:289
bool hasInfinite() const
Definition: Range.h:333
Range(const Range &rhs, const T &value, size_t i)
Definition: Range.h:275
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:364
T upper() const
Definition: Range.h:307
Range operator*(const T2 &rhs) const
int outside(const T &x) const
Definition: Range.h:379
T & upper()
Definition: Range.h:301
void setUpper(T upper)
Set the upper bound for the range.
Definition: Range.h:320
void setLower(T lower)
Set the lower bound for the range.
Definition: Range.h:313
T lower() const
Definition: Range.h:295
Range()
Default constructor. Set min/max to -/+ infinity.
Definition: Range.h:258
bool overlap(const Range< T > &r) const
Definition: Range.h:399
#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< Real32 > RangeReal32
Definition: Range.h:199
Range< UInt > RangeUInt
A short hand for ranges of unsigned integer numbers.
Definition: Range.h:205
RangeViolation< Real > RangeViolationReal
Definition: Range.h:197
RangeViolation< UInt > RangeViolationUInt
Definition: Range.h:206
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
RangeViolation< Real32 > RangeViolationReal32
Definition: Range.h:200
STL namespace.
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323