range_f.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_RANGE_RANGE_F_H_
  5. #define UI_GFX_RANGE_RANGE_F_H_
  6. #include <limits>
  7. #include <ostream>
  8. #include <string>
  9. #include "ui/gfx/range/gfx_range_export.h"
  10. #include "ui/gfx/range/range.h"
  11. namespace gfx {
  12. // A float version of Range. RangeF is made of a start and end position; when
  13. // they are the same, the range is empty. Note that |start_| can be greater
  14. // than |end_| to respect the directionality of the range.
  15. class GFX_RANGE_EXPORT RangeF {
  16. public:
  17. // Creates an empty range {0,0}.
  18. constexpr RangeF() : RangeF(0.f) {}
  19. // Initializes the range with a start and end.
  20. constexpr RangeF(float start, float end) : start_(start), end_(end) {}
  21. // Initializes the range with the same start and end positions.
  22. constexpr explicit RangeF(float position) : RangeF(position, position) {}
  23. // Returns a range that is invalid, which is {float_max,float_max}.
  24. static constexpr RangeF InvalidRange() {
  25. return RangeF(std::numeric_limits<float>::max());
  26. }
  27. // Checks if the range is valid through comparison to InvalidRange().
  28. constexpr bool IsValid() const { return *this != InvalidRange(); }
  29. // Getters and setters.
  30. constexpr float start() const { return start_; }
  31. void set_start(float start) { start_ = start; }
  32. constexpr float end() const { return end_; }
  33. void set_end(float end) { end_ = end; }
  34. // Returns the absolute value of the length.
  35. constexpr float length() const { return GetMax() - GetMin(); }
  36. constexpr bool is_reversed() const { return start() > end(); }
  37. constexpr bool is_empty() const { return start() == end(); }
  38. // Returns the minimum and maximum values.
  39. constexpr float GetMin() const { return start() < end() ? start() : end(); }
  40. constexpr float GetMax() const { return start() > end() ? start() : end(); }
  41. constexpr bool operator==(const RangeF& other) const {
  42. return start() == other.start() && end() == other.end();
  43. }
  44. constexpr bool operator!=(const RangeF& other) const {
  45. return !(*this == other);
  46. }
  47. constexpr bool EqualsIgnoringDirection(const RangeF& other) const {
  48. return GetMin() == other.GetMin() && GetMax() == other.GetMax();
  49. }
  50. // Returns true if this range intersects the specified |range|.
  51. constexpr bool Intersects(const RangeF& range) const {
  52. return Intersect(range).IsValid();
  53. }
  54. // Returns true if this range is contained by the specified |range| or it is
  55. // an empty range and ending the range |range|. (copied from gfx::Range)
  56. constexpr bool IsBoundedBy(const RangeF& range) const {
  57. return IsValid() && range.IsValid() && GetMin() >= range.GetMin() &&
  58. GetMax() <= range.GetMax();
  59. }
  60. // Returns true if this range contains the specified |range|.
  61. constexpr bool Contains(const RangeF& range) const {
  62. return range.IsBoundedBy(*this) &&
  63. // A non-empty range doesn't contain the range [max, max).
  64. (range.GetMax() != GetMax() || range.is_empty() == is_empty());
  65. }
  66. // Computes the intersection of this range with the given |range|.
  67. // If they don't intersect, it returns an InvalidRange().
  68. // The returned range is always empty or forward (never reversed).
  69. constexpr RangeF Intersect(const RangeF& range) const {
  70. const float min = std::max(GetMin(), range.GetMin());
  71. const float max = std::min(GetMax(), range.GetMax());
  72. return (min < max || Contains(range) || range.Contains(*this))
  73. ? RangeF(min, max)
  74. : InvalidRange();
  75. }
  76. RangeF Intersect(const Range& range) const;
  77. // Floor/Ceil/Round the start and end values of the given RangeF.
  78. Range Floor() const;
  79. Range Ceil() const;
  80. Range Round() const;
  81. std::string ToString() const;
  82. private:
  83. float start_;
  84. float end_;
  85. };
  86. GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os,
  87. const RangeF& range);
  88. } // namespace gfx
  89. #endif // UI_GFX_RANGE_RANGE_F_H_