HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Interval.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #ifndef __UT_Interval_h__
9 #define __UT_Interval_h__
10 
11 #include "UT_API.h"
12 #include <SYS/SYS_Types.h>
13 #include <SYS/SYS_Math.h>
14 #include <SYS/SYS_Hash.h>
15 
16 #ifdef WIN32
17  #undef min
18  #undef max
19 #endif
20 
21 template <typename T>
23 {
24 public:
25  typedef T value_type;
27 
28  explicit UT_IntervalT(T a = 0);
29  UT_IntervalT(T a, T b, bool order=false);
30 
31  UT_IntervalT & operator+=(const UT_IntervalT &rhs);
32  UT_IntervalT & operator-=(const UT_IntervalT &rhs);
33  UT_IntervalT & operator*=(const UT_IntervalT &rhs);
34  UT_IntervalT & operator/=(const UT_IntervalT &rhs);
35  UT_IntervalT & operator|=(const UT_IntervalT &rhs);
36  UT_IntervalT & operator&=(const UT_IntervalT &rhs);
37  UT_IntervalT & operator+=(T rhs);
38  UT_IntervalT & operator-=(T rhs);
39  UT_IntervalT & operator*=(T rhs);
40  bool operator==(const UT_IntervalT &rhs) const;
41  bool operator!=(const UT_IntervalT &rhs) const;
42 
43  void assign(T Min, T Max, bool order_them=false)
44  {
45  min = Min;
46  max = Max;
47  if (order_them && min > max)
48  {
49  T tmp = min;
50  min = max;
51  max = tmp;
52  }
53  }
54  void extendToContain( T a );
55  void minWith(const UT_IntervalT &rhs);
56  void maxWith(const UT_IntervalT &rhs);
57  void order();
58  T closest(T val) const;
59  T closest(T val, T wrap) const;
60  T avg() const { return (max+min)*0.5; }
61  T delta() const { return max - min; }
62  void square();
63 
64  UT_IntervalT sqr() const;
65  UT_IntervalT abs() const;
66  UT_IntervalT pow(T arg) const;
67 
68  int contains(T arg) const;
69  int isValid(T tol = 0.f) const;
70  void display() const;
71 
72  int equalZero(T tol = 0.00001f) const
73  {
74  return ((min>=-tol) && (min <= tol) && (max >=-tol) && (max <= tol));
75  }
76 
77  int isEqual(const UT_IntervalT &v, T tol = 0.00001f) const
78  {
79  return ((min>=v.min-tol) && (min<=v.min+tol) &&
80  (max>=v.max-tol) && (max<=v.max+tol));
81  }
82 
83  void clampZero(T tol = 0.00001f)
84  {
85  if (min>=-tol && min<= tol) min = 0;
86  if (max>=-tol && max<= tol) max = 0;
87  }
88 
89  void negate()
90  {
91  T tmp = min;
92  min = -max;
93  max = -tmp;
94  }
95  void invert(UT_IntervalT &v) const
96  {
97  v.min = min;
98  v.max = max;
99  if (!v.min)
100  v.min+=0.00001f;
101  if(!v.max)
102  v.max+=0.00001f;
103  T tmp = v.min;
104  v.min = 1/v.max;
105  v.max = 1/tmp;
106 
107  }
108  void invert()
109  {
110  if (!min)
111  min+=0.00001f;
112  if(!max)
113  max+=0.00001f;
114  T tmp = min;
115  min = 1/max;
116  max = 1/tmp;
117  }
118  /// @{
119  /// Compute a hash
121  {
122  SYS_HashType h = SYShash(min);
123  SYShashCombine(h, max);
124  return h;
125  }
126  friend std::size_t hash_value(const this_type &t) { return t.hash(); }
127  /// @}
128  public:
130 };
131 
135 typedef UT_IntervalT<float> UT_Interval; // deprecated
136 
137 template <typename T> UT_IntervalT<T> operator+(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
138 template <typename T> UT_IntervalT<T> operator+(T lhs, const UT_IntervalT<T> &rhs);
139 template <typename T> UT_IntervalT<T> operator+(const UT_IntervalT<T> &lhs, T rhs);
140 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
141 template <typename T> UT_IntervalT<T> operator-(T lhs, const UT_IntervalT<T> &rhs);
142 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &lhs, T rhs);
143 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &rhs);
144 template <typename T> UT_IntervalT<T> operator*(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
145 template <typename T> UT_IntervalT<T> operator*(T lhs, const UT_IntervalT<T> &rhs);
146 template <typename T> UT_IntervalT<T> operator*(const UT_IntervalT<T> &lhs, T rhs);
147 template <typename T> UT_IntervalT<T> operator/(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
148 template <typename T> UT_IntervalT<T> operator/(T lhs, const UT_IntervalT<T> &rhs);
149 template <typename T> UT_IntervalT<T> operator/(const UT_IntervalT<T> &lhs, T rhs);
150 template <typename T> UT_IntervalT<T> operator|(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
151 template <typename T> UT_IntervalT<T> operator&(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
152 template <typename T> UT_IntervalT<T> maxOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1);
153 template <typename T> UT_IntervalT<T> minOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1);
154 
155 //////////////////////////////////////////////////////////////////////////
156 //////////////// Inline Implementations //////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////
158 
159 template <typename T>
161 template <typename T>
162 UT_IntervalT<T>::UT_IntervalT(T a, T b, bool order_them)
163  : min(a),
164  max(b)
165 {
166  if (order_them && min > max)
167  {
168  T tmp = min;
169  min = max;
170  max = tmp;
171  }
172 }
173 
174 template <typename T>
177 {
178  min += rhs.min;
179  max += rhs.max;
180  return *this;
181 }
182 
183 template <typename T>
186 {
187  min += rhs;
188  max += rhs;
189  return *this;
190 }
191 
192 template <typename T>
195 {
196  T t = rhs.min;
197  min -= rhs.max;
198  max -= t;
199  return *this;
200 }
201 
202 template <typename T>
205 {
206  min -= rhs;
207  max -= rhs;
208  return *this;
209 }
210 
211 template <typename T>
214 {
215  T t1 = min*rhs.min;
216  T t2 = max*rhs.min;
217  T t3 = min*rhs.max;
218  T t4 = max*rhs.max;
219  T tmin, tmax;
220 
221  if (t1 < t2) { tmin = t1; tmax = t2; }
222  else { tmin = t2; tmax = t1; }
223  if (tmax < t3) { tmax = t3; }
224  if (tmin > t3) { tmin = t3; }
225  if (tmax < t4) { tmax = t4; }
226  if (tmin > t4) { tmin = t4; }
227  min = tmin;
228  max = tmax;
229  return *this;
230 }
231 
232 template <typename T>
235 {
236  UT_IntervalT<T> inverted;
237  rhs.invert(inverted);
238  (*this) *= inverted;
239  return *this;
240 }
241 
242 
243 
244 template <typename T>
247 {
248  if (rhs >= 0.0F) { min *= rhs; max *= rhs; }
249  else { T t = min * rhs; min = max * rhs; max = t; }
250  return *this;
251 }
252 template <typename T>
253 bool
255 {
256  return min == rhs.min && max == rhs.max;
257 }
258 template <typename T>
259 bool
261 {
262  return min != rhs.min || max != rhs.max;
263 }
264 
265 
266 template <typename T>
267 void
269 {
270  if (min < rhs.min) min = rhs.min;
271  if (max < rhs.max) max = rhs.max;
272 }
273 
274 template <typename T>
275 void
277 {
278  if( min>a )
279  min = a;
280  else if( max<a )
281  max = a;
282 }
283 
284 template <typename T>
285 void
287 {
288  if (min > rhs.min) min = rhs.min;
289  if (max > rhs.max) max = rhs.max;
290 }
291 
292 template <typename T>
295 {
296  if (min > rhs.min) min = rhs.min;
297  if (max < rhs.max) max = rhs.max;
298  return *this;
299 }
300 
301 template <typename T>
304 {
305  if (min < rhs.min) min = rhs.min;
306  if (max > rhs.max) max = rhs.max;
307  return *this;
308 }
309 
310 
311 template <typename T>
314 {
315  T tmin, tmax;
316  if (min < 0.0F) {
317  if (max < 0.0F) {
318  tmin = max*max;
319  tmax = min*min;
320  }
321  else {
322  tmin = min*min;
323  tmax = max*max;
324  tmax = (tmin < tmax) ? tmax : tmin;
325  tmin = 0.0F;
326  }
327  }
328  else {
329  tmin = min*min;
330  tmax = max*max;
331  }
332  return UT_IntervalT<T>(tmin, tmax);
333 }
334 
335 template
336 <typename T>
337 void
339 {
340  T tmin, tmax;
341  if (min < 0.0F) {
342  if (max < 0.0F) {
343  tmin = max*max;
344  tmax = min*min;
345  }
346  else {
347  tmin = min*min;
348  tmax = max*max;
349  tmax = (tmin < tmax) ? tmax : tmin;
350  tmin = 0.0F;
351  }
352  }
353  else {
354  tmin = min*min;
355  tmax = max*max;
356  }
357  min = tmin;
358  max = tmax;
359 }
360 
361 
362 template <typename T>
365 {
366  if (max < 0.0F) {
367  return UT_IntervalT<T>(-max, -min);
368  }
369  else if (min < 0.0F) {
370  T tmax;
371  if (-min > max) tmax = -min;
372  else tmax = max;
373  return UT_IntervalT<T>(0.0F, tmax);
374  }
375  return *this;
376 }
377 
378 template <typename T>
381 {
382  if (arg > 0) return UT_IntervalT<T>(SYSpow(min, arg), SYSpow(max, arg));
383  else return UT_IntervalT<T>(SYSpow(max, arg), SYSpow(min, arg));
384 }
385 
386 template <typename T>
387 T
389 {
390  T dmin, dmax;
391 
392  dmin = val - min;
393  dmax = max - val;
394  if (dmin < 0.0F)
395  return min;
396  else if (dmax < 0.0F)
397  return max;
398  return val;
399 }
400 
401 template <typename T>
402 T
404 {
405  T dmin, dmax;
406 
407  dmin = val - min;
408  dmax = max - val;
409  if (dmin < 0.0F)
410  {
411  if (dmin < dmax - wrap)
412  return max;
413  else
414  return min;
415  }
416  else if (dmax < 0.0F)
417  {
418  if (dmin - wrap < dmax)
419  return max;
420  else
421  return min;
422  }
423  return val;
424 }
425 
426 #if 0
427 // WARNING! assumes that the interval is positive.
428 //
429 template <typename T>
431 UT_IntervalT<T>::pow(T arg) const
432 {
433  if (arg > 0) return UT_IntervalT<T>(powf(min, arg), powf(max, arg));
434  else return UT_IntervalT<T>(powf(max, arg), powf(min, arg));
435 }
436 #endif
437 
438 template <typename T>
439 int
441 {
442  return ((arg >= min) && (arg <= max));
443 }
444 
445 template <typename T>
446 int
448 {
449  return SYSisLessOrEqual(min, max, tol);
450 }
451 
452 template <typename T>
453 void
455 {
456  if (min > max)
457  {
458  T tmp = min;
459  min = max;
460  max = tmp;
461  }
462 }
463 
464 // Free functions
465 
466 template <typename T>
469 {
470  return UT_IntervalT<T>(lhs.min + rhs.min, lhs.max + rhs.max);
471 }
472 
473 template <typename T>
475 operator+(T lhs, const UT_IntervalT<T> &rhs)
476 {
477  return UT_IntervalT<T>(rhs.min + lhs, rhs.max + lhs);
478 }
479 
480 
481 template <typename T>
483 operator+(const UT_IntervalT<T> &lhs, T rhs)
484 {
485  return UT_IntervalT<T>(lhs.min + rhs, lhs.max + rhs);
486 }
487 
488 template <typename T>
491 {
492  return UT_IntervalT<T>(lhs.min - rhs.max, lhs.max - rhs.min);
493 }
494 
495 template <typename T>
497 operator-(T lhs, const UT_IntervalT<T> &rhs)
498 {
499  return UT_IntervalT<T>(lhs - rhs.max, lhs - rhs.min);
500 }
501 
502 template <typename T>
504 operator-(const UT_IntervalT<T> &lhs, T rhs)
505 {
506  return UT_IntervalT<T>(lhs.min - rhs, lhs.max - rhs);
507 }
508 
509 template <typename T>
512 {
513  return UT_IntervalT<T>(-rhs.max, -rhs.min);
514 }
515 
516 template <typename T>
519 {
520  T t1 = lhs.min*rhs.min;
521  T t2 = lhs.max*rhs.min;
522  T t3 = lhs.min*rhs.max;
523  T t4 = lhs.max*rhs.max;
524  T tmin, tmax;
525 
526  if (t1 < t2) { tmin = t1; tmax = t2; }
527  else { tmin = t2; tmax = t1; }
528  if (tmax < t3) { tmax = t3; }
529  if (tmin > t3) { tmin = t3; }
530  if (tmax < t4) { tmax = t4; }
531  if (tmin > t4) { tmin = t4; }
532  return UT_IntervalT<T>(tmin, tmax);
533 }
534 
535 template <typename T>
537 operator*(T lhs, const UT_IntervalT<T> &rhs)
538 {
539  T tmin, tmax;
540  if (lhs > 0.0F) { tmin = lhs*rhs.min; tmax = lhs*rhs.max; }
541  else { tmin = lhs*rhs.max; tmax = lhs*rhs.min; }
542  return UT_IntervalT<T>(tmin, tmax);
543 }
544 
545 template <typename T>
547 operator*(const UT_IntervalT<T> &lhs, T rhs)
548 {
549  T tmin, tmax;
550  if (rhs > 0.0F) { tmin = rhs*lhs.min; tmax = rhs*lhs.max; }
551  else { tmin = rhs*lhs.max; tmax = rhs*lhs.min; }
552  return UT_IntervalT<T>(tmin, tmax);
553 }
554 
555 template <typename T>
558 {
559  // extra parentheses to workaround bad g++ template parsing
560  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? lhs.min : rhs.min,
561  ((lhs.max) < (rhs.max))? rhs.max : lhs.max);
562 }
563 
564 template <typename T>
567 {
568  // extra parentheses to workaround bad g++ template parsing
569  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? rhs.min : lhs.min,
570  ((lhs.max) < (rhs.max))? lhs.max : rhs.max);
571 }
572 template <typename T>
575 {
577  rhs.invert(v);
578  return lhs * v;
579 }
580 
581 template <typename T>
583 operator/(T lhs, const UT_IntervalT<T> &rhs)
584 {
586  rhs.invert(v);
587  v *= lhs;
588  return v;
589 }
590 
591 template <typename T>
593 operator/(const UT_IntervalT<T> &lhs, T rhs)
594 {
595  if (!rhs) rhs += 0.00001f;
596  return UT_IntervalT<T>(lhs.min / rhs, lhs.max / rhs);
597 }
598 
599 
600 template <typename T>
602 maxOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
603 {
604  // extra parentheses to workaround bad g++ template parsing
605  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg1.min : arg0.min,
606  ((arg0.max) < (arg1.max))? arg1.max : arg0.max);
607 }
608 
609 template <typename T>
611 minOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
612 {
613  // extra parentheses to workaround bad g++ template parsing
614  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg0.min : arg1.min,
615  ((arg0.max) < (arg1.max))? arg0.max : arg1.max);
616 }
617 
618 #include <stdio.h>
619 
620 template <typename T>
621 void
623 {
624  printf( "[%g, %g]", min, max );
625 }
626 
627 #endif // __UT_Interval_h__
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Returns M, where for .
Definition: Mat3.h:615
void maxWith(const UT_IntervalT &rhs)
Definition: UT_Interval.h:268
UT_IntervalT & operator+=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:176
friend std::size_t hash_value(const this_type &t)
Definition: UT_Interval.h:126
int isValid(T tol=0.f) const
Definition: UT_Interval.h:447
int equalZero(T tol=0.00001f) const
Definition: UT_Interval.h:72
UT_IntervalT sqr() const
Definition: UT_Interval.h:313
UT_IntervalT pow(T arg) const
Definition: UT_Interval.h:380
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
UT_IntervalT< T > minOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:611
const GLdouble * v
Definition: glcorearb.h:836
bool operator!=(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:260
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:631
UT_IntervalT & operator-=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:194
SYS_HashType hash() const
Definition: UT_Interval.h:120
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
int isEqual(const UT_IntervalT &v, T tol=0.00001f) const
Definition: UT_Interval.h:77
UT_IntervalT(T a=0)
Definition: UT_Interval.h:160
SYS_API float powf(float x, float y)
void negate()
Definition: UT_Interval.h:89
UT_IntervalT< T > operator&(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:566
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
T closest(T val) const
Definition: UT_Interval.h:388
UT_IntervalT abs() const
Definition: UT_Interval.h:364
UT_IntervalT< fpreal64 > UT_IntervalD
Definition: UT_Interval.h:134
GLfloat f
Definition: glcorearb.h:1925
bool operator==(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:254
UT_IntervalT & operator&=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:303
UT_IntervalT< T > operator|(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:557
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:641
void minWith(const UT_IntervalT &rhs)
Definition: UT_Interval.h:286
UT_IntervalT< T > maxOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:602
void invert()
Definition: UT_Interval.h:108
UT_IntervalT & operator*=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:213
UT_IntervalT< T > operator/(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:574
T avg() const
Definition: UT_Interval.h:60
void square()
Definition: UT_Interval.h:338
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
UT_IntervalT & operator|=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:294
void clampZero(T tol=0.00001f)
Definition: UT_Interval.h:83
UT_IntervalT< T > this_type
Definition: UT_Interval.h:26
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
void order()
Definition: UT_Interval.h:454
UT_IntervalT< fpreal > UT_IntervalR
Definition: UT_Interval.h:132
GLuint GLfloat * val
Definition: glcorearb.h:1607
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:622
void display() const
Definition: UT_Interval.h:622
UT_IntervalT & operator/=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:234
void assign(T Min, T Max, bool order_them=false)
Definition: UT_Interval.h:43
UT_IntervalT< float > UT_Interval
Definition: UT_Interval.h:135
T delta() const
Definition: UT_Interval.h:61
UT_IntervalT< fpreal32 > UT_IntervalF
Definition: UT_Interval.h:133
int contains(T arg) const
Definition: UT_Interval.h:440
void invert(UT_IntervalT &v) const
Definition: UT_Interval.h:95
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition: Math.h:561
void extendToContain(T a)
Definition: UT_Interval.h:276