Range.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Time TRange class.
  9. #ifndef CRYINCLUDE_CRYCOMMON_RANGE_H
  10. #define CRYINCLUDE_CRYCOMMON_RANGE_H
  11. #pragma once
  12. #include <AzCore/RTTI/TypeInfo.h>
  13. #ifndef MIN
  14. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  15. #endif
  16. #ifndef MAX
  17. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  18. #endif
  19. /*!
  20. Class TRange, can represent anything that is range between two values, mostly used for time ranges.
  21. */
  22. template <class T>
  23. class TRange
  24. {
  25. public:
  26. T start;
  27. T end;
  28. TRange() { start = T(0); end = T(0); };
  29. TRange(const TRange& r) { start = r.start; end = r.end; };
  30. TRange(T s, T e) { start = s; end = e; };
  31. void Set(T s, T e) { start = s; end = e; };
  32. void Clear() { start = 0; end = 0; };
  33. //! Get length of range.
  34. T Length() const { return end - start; };
  35. //! Check if range is empty.
  36. bool IsEmpty() const { return (start == 0 && end == 0); }
  37. //! Check if value is inside range.
  38. bool IsInside(T val) { return val >= start && val <= end; };
  39. void ClipValue(T& val) const
  40. {
  41. if (val < start)
  42. {
  43. val = start;
  44. }
  45. if (val > end)
  46. {
  47. val = end;
  48. }
  49. }
  50. //! Compare two ranges.
  51. bool operator == (const TRange& r) const
  52. {
  53. return start == r.start && end == r.end;
  54. }
  55. bool operator != (const TRange& r) const
  56. {
  57. return !(*this == r);
  58. }
  59. //! Assign operator.
  60. TRange& operator =(const TRange& r)
  61. {
  62. start = r.start;
  63. end = r.end;
  64. return *this;
  65. }
  66. //! Interect two ranges.
  67. TRange operator & (const TRange& r) const
  68. {
  69. return TRange(MAX(start, r.start), MIN(end, r.end));
  70. }
  71. TRange& operator &= (const TRange& r)
  72. {
  73. return (*this = (*this & r));
  74. }
  75. //! Concatent two ranges.
  76. TRange operator | (const TRange& r) const
  77. {
  78. return TRange(MIN(start, r.start), MAX(end, r.end));
  79. }
  80. TRange& operator |= (const TRange& r)
  81. {
  82. return (*this = (*this | r));
  83. }
  84. //! Add new value to range.
  85. TRange operator + (T v) const
  86. {
  87. T s = start, e = end;
  88. if (v < start)
  89. {
  90. s = v;
  91. }
  92. if (v > end)
  93. {
  94. e = v;
  95. }
  96. return TRange(s, e);
  97. }
  98. //! Add new value to range.
  99. TRange& operator += (T v) const
  100. {
  101. if (v < start)
  102. {
  103. start = v;
  104. }
  105. if (v > end)
  106. {
  107. end = v;
  108. }
  109. return *this;
  110. }
  111. };
  112. //! CRange if just TRange for floats..
  113. typedef TRange<float> Range;
  114. namespace AZ
  115. {
  116. AZ_TYPE_INFO_SPECIALIZE(Range, "{515CF4CF-4992-4139-BDE5-42A887432B45}");
  117. }
  118. #endif // CRYINCLUDE_CRYCOMMON_RANGE_H