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  bool contains(T arg) const;
69  bool intersects(const UT_IntervalT &arg) const;
70  int isValid(T tol = 0.f) const;
71  void display() const;
72 
73  int equalZero(T tol = 0.00001f) const
74  {
75  return ((min>=-tol) && (min <= tol) && (max >=-tol) && (max <= tol));
76  }
77 
78  int isEqual(const UT_IntervalT &v, T tol = 0.00001f) const
79  {
80  return ((min>=v.min-tol) && (min<=v.min+tol) &&
81  (max>=v.max-tol) && (max<=v.max+tol));
82  }
83 
84  void clampZero(T tol = 0.00001f)
85  {
86  if (min>=-tol && min<= tol) min = 0;
87  if (max>=-tol && max<= tol) max = 0;
88  }
89 
90  void negate()
91  {
92  T tmp = min;
93  min = -max;
94  max = -tmp;
95  }
96  void invert(UT_IntervalT &v) const
97  {
98  v.min = min;
99  v.max = max;
100  if (!v.min)
101  v.min+=0.00001f;
102  if(!v.max)
103  v.max+=0.00001f;
104  T tmp = v.min;
105  v.min = 1/v.max;
106  v.max = 1/tmp;
107 
108  }
109  void invert()
110  {
111  if (!min)
112  min+=0.00001f;
113  if(!max)
114  max+=0.00001f;
115  T tmp = min;
116  min = 1/max;
117  max = 1/tmp;
118  }
119  /// @{
120  /// Compute a hash
122  {
124  SYShashCombine(h, max);
125  return h;
126  }
127  friend std::size_t hash_value(const this_type &t) { return t.hash(); }
128  /// @}
129  public:
131 };
132 
136 typedef UT_IntervalT<float> UT_Interval; // deprecated
137 
138 template <typename T> UT_IntervalT<T> operator+(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
139 template <typename T> UT_IntervalT<T> operator+(T lhs, const UT_IntervalT<T> &rhs);
140 template <typename T> UT_IntervalT<T> operator+(const UT_IntervalT<T> &lhs, T rhs);
141 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
142 template <typename T> UT_IntervalT<T> operator-(T lhs, const UT_IntervalT<T> &rhs);
143 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &lhs, T rhs);
144 template <typename T> UT_IntervalT<T> operator-(const UT_IntervalT<T> &rhs);
145 template <typename T> UT_IntervalT<T> operator*(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
146 template <typename T> UT_IntervalT<T> operator*(T lhs, const UT_IntervalT<T> &rhs);
147 template <typename T> UT_IntervalT<T> operator*(const UT_IntervalT<T> &lhs, T rhs);
148 template <typename T> UT_IntervalT<T> operator/(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
149 template <typename T> UT_IntervalT<T> operator/(T lhs, const UT_IntervalT<T> &rhs);
150 template <typename T> UT_IntervalT<T> operator/(const UT_IntervalT<T> &lhs, 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> operator&(const UT_IntervalT<T> &lhs, const UT_IntervalT<T> &rhs);
153 template <typename T> UT_IntervalT<T> maxOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1);
154 template <typename T> UT_IntervalT<T> minOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1);
155 
156 //////////////////////////////////////////////////////////////////////////
157 //////////////// Inline Implementations //////////////////////////////////
158 //////////////////////////////////////////////////////////////////////////
159 
160 template <typename T>
162  : min(a)
163  , max(a)
164 {
165 }
166 
167 template <typename T>
168 UT_IntervalT<T>::UT_IntervalT(T a, T b, bool order_them)
169  : min(a),
170  max(b)
171 {
172  if (order_them && min > max)
173  {
174  T tmp = min;
175  min = max;
176  max = tmp;
177  }
178 }
179 
180 template <typename T>
183 {
184  min += rhs.min;
185  max += rhs.max;
186  return *this;
187 }
188 
189 template <typename T>
192 {
193  min += rhs;
194  max += rhs;
195  return *this;
196 }
197 
198 template <typename T>
201 {
202  T t = rhs.min;
203  min -= rhs.max;
204  max -= t;
205  return *this;
206 }
207 
208 template <typename T>
211 {
212  min -= rhs;
213  max -= rhs;
214  return *this;
215 }
216 
217 template <typename T>
220 {
221  T t1 = min*rhs.min;
222  T t2 = max*rhs.min;
223  T t3 = min*rhs.max;
224  T t4 = max*rhs.max;
225  T tmin, tmax;
226 
227  if (t1 < t2) { tmin = t1; tmax = t2; }
228  else { tmin = t2; tmax = t1; }
229  if (tmax < t3) { tmax = t3; }
230  if (tmin > t3) { tmin = t3; }
231  if (tmax < t4) { tmax = t4; }
232  if (tmin > t4) { tmin = t4; }
233  min = tmin;
234  max = tmax;
235  return *this;
236 }
237 
238 template <typename T>
241 {
242  UT_IntervalT<T> inverted;
243  rhs.invert(inverted);
244  (*this) *= inverted;
245  return *this;
246 }
247 
248 
249 
250 template <typename T>
253 {
254  if (rhs >= 0.0F) { min *= rhs; max *= rhs; }
255  else { T t = min * rhs; min = max * rhs; max = t; }
256  return *this;
257 }
258 template <typename T>
259 bool
261 {
262  return min == rhs.min && max == rhs.max;
263 }
264 template <typename T>
265 bool
267 {
268  return min != rhs.min || max != rhs.max;
269 }
270 
271 
272 template <typename T>
273 void
275 {
276  if (min < rhs.min) min = rhs.min;
277  if (max < rhs.max) max = rhs.max;
278 }
279 
280 template <typename T>
281 void
283 {
284  if( min>a )
285  min = a;
286  else if( max<a )
287  max = a;
288 }
289 
290 template <typename T>
291 void
293 {
294  if (min > rhs.min) min = rhs.min;
295  if (max > rhs.max) max = rhs.max;
296 }
297 
298 template <typename T>
301 {
302  if (min > rhs.min) min = rhs.min;
303  if (max < rhs.max) max = rhs.max;
304  return *this;
305 }
306 
307 template <typename T>
310 {
311  if (min < rhs.min) min = rhs.min;
312  if (max > rhs.max) max = rhs.max;
313  return *this;
314 }
315 
316 
317 template <typename T>
320 {
321  T tmin, tmax;
322  if (min < 0.0F) {
323  if (max < 0.0F) {
324  tmin = max*max;
325  tmax = min*min;
326  }
327  else {
328  tmin = min*min;
329  tmax = max*max;
330  tmax = (tmin < tmax) ? tmax : tmin;
331  tmin = 0.0F;
332  }
333  }
334  else {
335  tmin = min*min;
336  tmax = max*max;
337  }
338  return UT_IntervalT<T>(tmin, tmax);
339 }
340 
341 template
342 <typename T>
343 void
345 {
346  T tmin, tmax;
347  if (min < 0.0F) {
348  if (max < 0.0F) {
349  tmin = max*max;
350  tmax = min*min;
351  }
352  else {
353  tmin = min*min;
354  tmax = max*max;
355  tmax = (tmin < tmax) ? tmax : tmin;
356  tmin = 0.0F;
357  }
358  }
359  else {
360  tmin = min*min;
361  tmax = max*max;
362  }
363  min = tmin;
364  max = tmax;
365 }
366 
367 
368 template <typename T>
371 {
372  if (max < 0.0F) {
373  return UT_IntervalT<T>(-max, -min);
374  }
375  else if (min < 0.0F) {
376  T tmax;
377  if (-min > max) tmax = -min;
378  else tmax = max;
379  return UT_IntervalT<T>(0.0F, tmax);
380  }
381  return *this;
382 }
383 
384 template <typename T>
387 {
388  if (arg > 0) return UT_IntervalT<T>(SYSpow(min, arg), SYSpow(max, arg));
389  else return UT_IntervalT<T>(SYSpow(max, arg), SYSpow(min, arg));
390 }
391 
392 template <typename T>
393 T
395 {
396  T dmin, dmax;
397 
398  dmin = val - min;
399  dmax = max - val;
400  if (dmin < 0.0F)
401  return min;
402  else if (dmax < 0.0F)
403  return max;
404  return val;
405 }
406 
407 template <typename T>
408 T
410 {
411  T dmin, dmax;
412 
413  dmin = val - min;
414  dmax = max - val;
415  if (dmin < 0.0F)
416  {
417  if (dmin < dmax - wrap)
418  return max;
419  else
420  return min;
421  }
422  else if (dmax < 0.0F)
423  {
424  if (dmin - wrap < dmax)
425  return max;
426  else
427  return min;
428  }
429  return val;
430 }
431 
432 #if 0
433 // WARNING! assumes that the interval is positive.
434 //
435 template <typename T>
437 UT_IntervalT<T>::pow(T arg) const
438 {
439  if (arg > 0) return UT_IntervalT<T>(powf(min, arg), powf(max, arg));
440  else return UT_IntervalT<T>(powf(max, arg), powf(min, arg));
441 }
442 #endif
443 
444 template <typename T>
445 bool
447 {
448  return ((arg >= min) && (arg <= max));
449 }
450 
451 template <typename T>
452 bool
454 {
455  if (max < arg.min)
456  return false;
457 
458  if (arg.max < min)
459  return false;
460 
461  return true;
462 }
463 
464 template <typename T>
465 int
467 {
468  return SYSisLessOrEqual(min, max, tol);
469 }
470 
471 template <typename T>
472 void
474 {
475  if (min > max)
476  {
477  T tmp = min;
478  min = max;
479  max = tmp;
480  }
481 }
482 
483 // Free functions
484 
485 template <typename T>
488 {
489  return UT_IntervalT<T>(lhs.min + rhs.min, lhs.max + rhs.max);
490 }
491 
492 template <typename T>
494 operator+(T lhs, const UT_IntervalT<T> &rhs)
495 {
496  return UT_IntervalT<T>(rhs.min + lhs, rhs.max + lhs);
497 }
498 
499 
500 template <typename T>
502 operator+(const UT_IntervalT<T> &lhs, T rhs)
503 {
504  return UT_IntervalT<T>(lhs.min + rhs, lhs.max + rhs);
505 }
506 
507 template <typename T>
510 {
511  return UT_IntervalT<T>(lhs.min - rhs.max, lhs.max - rhs.min);
512 }
513 
514 template <typename T>
516 operator-(T lhs, const UT_IntervalT<T> &rhs)
517 {
518  return UT_IntervalT<T>(lhs - rhs.max, lhs - rhs.min);
519 }
520 
521 template <typename T>
523 operator-(const UT_IntervalT<T> &lhs, T rhs)
524 {
525  return UT_IntervalT<T>(lhs.min - rhs, lhs.max - rhs);
526 }
527 
528 template <typename T>
531 {
532  return UT_IntervalT<T>(-rhs.max, -rhs.min);
533 }
534 
535 template <typename T>
538 {
539  T t1 = lhs.min*rhs.min;
540  T t2 = lhs.max*rhs.min;
541  T t3 = lhs.min*rhs.max;
542  T t4 = lhs.max*rhs.max;
543  T tmin, tmax;
544 
545  if (t1 < t2) { tmin = t1; tmax = t2; }
546  else { tmin = t2; tmax = t1; }
547  if (tmax < t3) { tmax = t3; }
548  if (tmin > t3) { tmin = t3; }
549  if (tmax < t4) { tmax = t4; }
550  if (tmin > t4) { tmin = t4; }
551  return UT_IntervalT<T>(tmin, tmax);
552 }
553 
554 template <typename T>
556 operator*(T lhs, const UT_IntervalT<T> &rhs)
557 {
558  T tmin, tmax;
559  if (lhs > 0.0F) { tmin = lhs*rhs.min; tmax = lhs*rhs.max; }
560  else { tmin = lhs*rhs.max; tmax = lhs*rhs.min; }
561  return UT_IntervalT<T>(tmin, tmax);
562 }
563 
564 template <typename T>
566 operator*(const UT_IntervalT<T> &lhs, T rhs)
567 {
568  T tmin, tmax;
569  if (rhs > 0.0F) { tmin = rhs*lhs.min; tmax = rhs*lhs.max; }
570  else { tmin = rhs*lhs.max; tmax = rhs*lhs.min; }
571  return UT_IntervalT<T>(tmin, tmax);
572 }
573 
574 template <typename T>
577 {
578  // extra parentheses to workaround bad g++ template parsing
579  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? lhs.min : rhs.min,
580  ((lhs.max) < (rhs.max))? rhs.max : lhs.max);
581 }
582 
583 template <typename T>
586 {
587  // extra parentheses to workaround bad g++ template parsing
588  return UT_IntervalT<T>(((lhs.min) < (rhs.min))? rhs.min : lhs.min,
589  ((lhs.max) < (rhs.max))? lhs.max : rhs.max);
590 }
591 template <typename T>
594 {
596  rhs.invert(v);
597  return lhs * v;
598 }
599 
600 template <typename T>
602 operator/(T lhs, const UT_IntervalT<T> &rhs)
603 {
605  rhs.invert(v);
606  v *= lhs;
607  return v;
608 }
609 
610 template <typename T>
612 operator/(const UT_IntervalT<T> &lhs, T rhs)
613 {
614  if (!rhs) rhs += 0.00001f;
615  return UT_IntervalT<T>(lhs.min / rhs, lhs.max / rhs);
616 }
617 
618 
619 template <typename T>
621 maxOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
622 {
623  // extra parentheses to workaround bad g++ template parsing
624  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg1.min : arg0.min,
625  ((arg0.max) < (arg1.max))? arg1.max : arg0.max);
626 }
627 
628 template <typename T>
630 minOf(const UT_IntervalT<T> &arg0, const UT_IntervalT<T> &arg1)
631 {
632  // extra parentheses to workaround bad g++ template parsing
633  return UT_IntervalT<T>(((arg0.min) < (arg1.min))? arg0.min : arg1.min,
634  ((arg0.max) < (arg1.max))? arg0.max : arg1.max);
635 }
636 
637 #include <stdio.h>
638 
639 template <typename T>
640 void
642 {
643  printf( "[%g, %g]", min, max );
644 }
645 
646 #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:274
UT_IntervalT & operator+=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:182
friend std::size_t hash_value(const this_type &t)
Definition: UT_Interval.h:127
int isValid(T tol=0.f) const
Definition: UT_Interval.h:466
int equalZero(T tol=0.00001f) const
Definition: UT_Interval.h:73
UT_IntervalT sqr() const
Definition: UT_Interval.h:319
UT_IntervalT pow(T arg) const
Definition: UT_Interval.h:386
constexpr size_t SYShash(const SYS_Flicks f)
Definition: SYS_Flicks.h:46
Max
Definition: ImathEuler.h:174
UT_IntervalT< T > minOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:630
const GLdouble * v
Definition: glcorearb.h:837
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:670
bool operator!=(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:266
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:200
SYS_HashType hash() const
Definition: UT_Interval.h:121
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:78
UT_IntervalT(T a=0)
Definition: UT_Interval.h:161
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void negate()
Definition: UT_Interval.h:90
UT_IntervalT< T > operator&(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:585
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1859
bool intersects(const UT_IntervalT &arg) const
Definition: UT_Interval.h:453
T closest(T val) const
Definition: UT_Interval.h:394
Min
Definition: ImathEuler.h:173
UT_IntervalT abs() const
Definition: UT_Interval.h:370
UT_IntervalT< fpreal64 > UT_IntervalD
Definition: UT_Interval.h:135
GLfloat f
Definition: glcorearb.h:1926
bool operator==(const UT_IntervalT &rhs) const
Definition: UT_Interval.h:260
UT_IntervalT & operator&=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:309
UT_IntervalT< T > operator|(const UT_IntervalT< T > &lhs, const UT_IntervalT< T > &rhs)
Definition: UT_Interval.h:576
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:292
UT_IntervalT< T > maxOf(const UT_IntervalT< T > &arg0, const UT_IntervalT< T > &arg1)
Definition: UT_Interval.h:621
void invert()
Definition: UT_Interval.h:109
UT_IntervalT & operator*=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:219
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:593
T avg() const
Definition: UT_Interval.h:60
void square()
Definition: UT_Interval.h:344
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
UT_IntervalT & operator|=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:300
void clampZero(T tol=0.00001f)
Definition: UT_Interval.h:84
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
bool contains(T arg) const
Definition: UT_Interval.h:446
void order()
Definition: UT_Interval.h:473
UT_IntervalT< fpreal > UT_IntervalR
Definition: UT_Interval.h:133
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:641
UT_IntervalT & operator/=(const UT_IntervalT &rhs)
Definition: UT_Interval.h:240
void assign(T Min, T Max, bool order_them=false)
Definition: UT_Interval.h:43
UT_IntervalT< float > UT_Interval
Definition: UT_Interval.h:136
T delta() const
Definition: UT_Interval.h:61
UT_IntervalT< fpreal32 > UT_IntervalF
Definition: UT_Interval.h:134
void invert(UT_IntervalT &v) const
Definition: UT_Interval.h:96
void extendToContain(T a)
Definition: UT_Interval.h:282