HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  : min(a)
162  , max(a)
163 {
164 }
165 
166 template <typename T>
167 UT_IntervalT<T>::UT_IntervalT(T a, T b, bool order_them)
168  : min(a),
169  max(b)
170 {
171  if (order_them && min > max)
172  {
173  T tmp = min;
174  min = max;
175  max = tmp;
176  }
177 }
178 
179 template <typename T>
182 {
183  min += rhs.min;
184  max += rhs.max;
185  return *this;
186 }
187 
188 template <typename T>
191 {
192  min += rhs;
193  max += rhs;
194  return *this;
195 }
196 
197 template <typename T>
200 {
201  T t = rhs.min;
202  min -= rhs.max;
203  max -= t;
204  return *this;
205 }
206 
207 template <typename T>
210 {
211  min -= rhs;
212  max -= rhs;
213  return *this;
214 }
215 
216 template <typename T>
219 {
220  T t1 = min*rhs.min;
221  T t2 = max*rhs.min;
222  T t3 = min*rhs.max;
223  T t4 = max*rhs.max;
224  T tmin, tmax;
225 
226  if (t1 < t2) { tmin = t1; tmax = t2; }
227  else { tmin = t2; tmax = t1; }
228  if (tmax < t3) { tmax = t3; }
229  if (tmin > t3) { tmin = t3; }
230  if (tmax < t4) { tmax = t4; }
231  if (tmin > t4) { tmin = t4; }
232  min = tmin;
233  max = tmax;
234  return *this;
235 }
236 
237 template <typename T>
240 {
241  UT_IntervalT<T> inverted;
242  rhs.invert(inverted);
243  (*this) *= inverted;
244  return *this;
245 }
246 
247 
248 
249 template <typename T>
252 {
253  if (rhs >= 0.0F) { min *= rhs; max *= rhs; }
254  else { T t = min * rhs; min = max * rhs; max = t; }
255  return *this;
256 }
257 template <typename T>
258 bool
260 {
261  return min == rhs.min && max == rhs.max;
262 }
263 template <typename T>
264 bool
266 {
267  return min != rhs.min || max != rhs.max;
268 }
269 
270 
271 template <typename T>
272 void
274 {
275  if (min < rhs.min) min = rhs.min;
276  if (max < rhs.max) max = rhs.max;
277 }
278 
279 template <typename T>
280 void
282 {
283  if( min>a )
284  min = a;
285  else if( max<a )
286  max = a;
287 }
288 
289 template <typename T>
290 void
292 {
293  if (min > rhs.min) min = rhs.min;
294  if (max > rhs.max) max = rhs.max;
295 }
296 
297 template <typename T>
300 {
301  if (min > rhs.min) min = rhs.min;
302  if (max < rhs.max) max = rhs.max;
303  return *this;
304 }
305 
306 template <typename T>
309 {
310  if (min < rhs.min) min = rhs.min;
311  if (max > rhs.max) max = rhs.max;
312  return *this;
313 }
314 
315 
316 template <typename T>
319 {
320  T tmin, tmax;
321  if (min < 0.0F) {
322  if (max < 0.0F) {
323  tmin = max*max;
324  tmax = min*min;
325  }
326  else {
327  tmin = min*min;
328  tmax = max*max;
329  tmax = (tmin < tmax) ? tmax : tmin;
330  tmin = 0.0F;
331  }
332  }
333  else {
334  tmin = min*min;
335  tmax = max*max;
336  }
337  return UT_IntervalT<T>(tmin, tmax);
338 }
339 
340 template
341 <typename T>
342 void
344 {
345  T tmin, tmax;
346  if (min < 0.0F) {
347  if (max < 0.0F) {
348  tmin = max*max;
349  tmax = min*min;
350  }
351  else {
352  tmin = min*min;
353  tmax = max*max;
354  tmax = (tmin < tmax) ? tmax : tmin;
355  tmin = 0.0F;
356  }
357  }
358  else {
359  tmin = min*min;
360  tmax = max*max;
361  }
362  min = tmin;
363  max = tmax;
364 }
365 
366 
367 template <typename T>
370 {
371  if (max < 0.0F) {
372  return UT_IntervalT<T>(-max, -min);
373  }
374  else if (min < 0.0F) {
375  T tmax;
376  if (-min > max) tmax = -min;
377  else tmax = max;
378  return UT_IntervalT<T>(0.0F, tmax);
379  }
380  return *this;
381 }
382 
383 template <typename T>
386 {
387  if (arg > 0) return UT_IntervalT<T>(SYSpow(min, arg), SYSpow(max, arg));
388  else return UT_IntervalT<T>(SYSpow(max, arg), SYSpow(min, arg));
389 }
390 
391 template <typename T>
392 T
394 {
395  T dmin, dmax;
396 
397  dmin = val - min;
398  dmax = max - val;
399  if (dmin < 0.0F)
400  return min;
401  else if (dmax < 0.0F)
402  return max;
403  return val;
404 }
405 
406 template <typename T>
407 T
409 {
410  T dmin, dmax;
411 
412  dmin = val - min;
413  dmax = max - val;
414  if (dmin < 0.0F)
415  {
416  if (dmin < dmax - wrap)
417  return max;
418  else
419  return min;
420  }
421  else if (dmax < 0.0F)
422  {
423  if (dmin - wrap < dmax)
424  return max;
425  else
426  return min;
427  }
428  return val;
429 }
430 
431 #if 0
432 // WARNING! assumes that the interval is positive.
433 //
434 template <typename T>
436 UT_IntervalT<T>::pow(T arg) const
437 {
438  if (arg > 0) return UT_IntervalT<T>(powf(min, arg), powf(max, arg));
439  else return UT_IntervalT<T>(powf(max, arg), powf(min, arg));
440 }
441 #endif
442 
443 template <typename T>
444 int
446 {
447  return ((arg >= min) && (arg <= max));
448 }
449 
450 template <typename T>
451 int
453 {
454  return SYSisLessOrEqual(min, max, tol);
455 }
456 
457 template <typename T>
458 void
460 {
461  if (min > max)
462  {
463  T tmp = min;
464  min = max;
465  max = tmp;
466  }
467 }
468 
469 // Free functions
470 
471 template <typename T>
474 {
475  return UT_IntervalT<T>(lhs.min + rhs.min, lhs.max + rhs.max);
476 }
477 
478 template <typename T>
480 operator+(T lhs, const UT_IntervalT<T> &rhs)
481 {
482  return UT_IntervalT<T>(rhs.min + lhs, rhs.max + lhs);
483 }
484 
485 
486 template <typename T>
488 operator+(const UT_IntervalT<T> &lhs, T rhs)
489 {
490  return UT_IntervalT<T>(lhs.min + rhs, lhs.max + rhs);
491 }
492 
493 template <typename T>
496 {
497  return UT_IntervalT<T>(lhs.min - rhs.max, lhs.max - rhs.min);
498 }
499 
500 template <typename T>
502 operator-(T lhs, const UT_IntervalT<T> &rhs)
503 {
504  return UT_IntervalT<T>(lhs - rhs.max, lhs - rhs.min);
505 }
506 
507 template <typename T>
509 operator-(const UT_IntervalT<T> &lhs, T rhs)
510 {
511  return UT_IntervalT<T>(lhs.min - rhs, lhs.max - rhs);
512 }
513 
514 template <typename T>
517 {
518  return UT_IntervalT<T>(-rhs.max, -rhs.min);
519 }
520 
521 template <typename T>
524 {
525  T t1 = lhs.min*rhs.min;
526  T t2 = lhs.max*rhs.min;
527  T t3 = lhs.min*rhs.max;
528  T t4 = lhs.max*rhs.max;
529  T tmin, tmax;
530 
531  if (t1 < t2) { tmin = t1; tmax = t2; }
532  else { tmin = t2; tmax = t1; }
533  if (tmax < t3) { tmax = t3; }
534  if (tmin > t3) { tmin = t3; }
535  if (tmax < t4) { tmax = t4; }
536  if (tmin > t4) { tmin = t4; }
537  return UT_IntervalT<T>(tmin, tmax);
538 }
539 
540 template <typename T>
542 operator*(T lhs, const UT_IntervalT<T> &rhs)
543 {
544  T tmin, tmax;
545  if (lhs > 0.0F) { tmin = lhs*rhs.min; tmax = lhs*rhs.max; }
546  else { tmin = lhs*rhs.max; tmax = lhs*rhs.min; }
547  return UT_IntervalT<T>(tmin, tmax);
548 }
549 
550 template <typename T>
552 operator*(const UT_IntervalT<T> &lhs, T rhs)
553 {
554  T tmin, tmax;
555  if (rhs > 0.0F) { tmin = rhs*lhs.min; tmax = rhs*lhs.max; }
556  else { tmin = rhs*lhs.max; tmax = rhs*lhs.min; }
557  return UT_IntervalT<T>(tmin, tmax);
558 }
559 
560 template <typename T>
563 {
564  // extra parentheses to workaround bad g++ template parsing
565  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? lhs.min : rhs.min,
566  ((lhs.max) < (rhs.max))? rhs.max : lhs.max);
567 }
568 
569 template <typename T>
572 {
573  // extra parentheses to workaround bad g++ template parsing
574  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? rhs.min : lhs.min,
575  ((lhs.max) < (rhs.max))? lhs.max : rhs.max);
576 }
577 template <typename T>
580 {
582  rhs.invert(v);
583  return lhs * v;
584 }
585 
586 template <typename T>
588 operator/(T lhs, const UT_IntervalT<T> &rhs)
589 {
591  rhs.invert(v);
592  v *= lhs;
593  return v;
594 }
595 
596 template <typename T>
598 operator/(const UT_IntervalT<T> &lhs, T rhs)
599 {
600  if (!rhs) rhs += 0.00001f;
601  return UT_IntervalT<T>(lhs.min / rhs, lhs.max / rhs);
602 }
603 
604 
605 template <typename T>
607 maxOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
608 {
609  // extra parentheses to workaround bad g++ template parsing
610  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg1.min : arg0.min,
611  ((arg0.max) < (arg1.max))? arg1.max : arg0.max);
612 }
613 
614 template <typename T>
616 minOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
617 {
618  // extra parentheses to workaround bad g++ template parsing
619  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg0.min : arg1.min,
620  ((arg0.max) < (arg1.max))? arg0.max : arg1.max);
621 }
622 
623 #include <stdio.h>
624 
625 template <typename T>
626 void
628 {
629  printf( "[%g, %g]", min, max );
630 }
631 
632 #endif // __UT_Interval_h__
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:561
void maxWith(const UT_IntervalT &rhs)
Definition: UT_Interval.h:273
UT_IntervalT & operator+=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:181
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:452
int equalZero(T tol=0.00001f) const
Definition: UT_Interval.h:72
UT_IntervalT sqr() const
Definition: UT_Interval.h:318
UT_IntervalT pow(T arg) const
Definition: UT_Interval.h:385
Max
Definition: ImathEuler.h:174
UT_IntervalT< T > minOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:616
const GLdouble * v
Definition: glcorearb.h:837
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
bool operator!=(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:265
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:577
UT_IntervalT & operator-=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:199
SYS_HashType hash() const
Definition: UT_Interval.h:120
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
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)
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
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:571
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
T closest(T val) const
Definition: UT_Interval.h:393
Min
Definition: ImathEuler.h:173
UT_IntervalT abs() const
Definition: UT_Interval.h:369
UT_IntervalT< fpreal64 > UT_IntervalD
Definition: UT_Interval.h:134
GLfloat f
Definition: glcorearb.h:1926
bool operator==(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:259
UT_IntervalT & operator&=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:308
UT_IntervalT< T > operator|(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:562
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:587
void minWith(const UT_IntervalT &rhs)
Definition: UT_Interval.h:291
UT_IntervalT< T > maxOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:607
void invert()
Definition: UT_Interval.h:108
UT_IntervalT & operator*=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:218
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
UT_IntervalT< T > operator/(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:579
T avg() const
Definition: UT_Interval.h:60
void square()
Definition: UT_Interval.h:343
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
UT_IntervalT & operator|=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:299
void clampZero(T tol=0.00001f)
Definition: UT_Interval.h:83
GLdouble t
Definition: glad.h:2397
UT_IntervalT< T > this_type
Definition: UT_Interval.h:26
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
void order()
Definition: UT_Interval.h:459
UT_IntervalT< fpreal > UT_IntervalR
Definition: UT_Interval.h:132
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat * val
Definition: glcorearb.h:1608
void display() const
Definition: UT_Interval.h:627
UT_IntervalT & operator/=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:239
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:445
void invert(UT_IntervalT &v) const
Definition: UT_Interval.h:95
void extendToContain(T a)
Definition: UT_Interval.h:281