OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #ifndef CC_BASE_INDEX_RECT_H_ |
| 6 #define CC_BASE_INDEX_RECT_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "cc/base/cc_export.h" |
| 11 |
| 12 namespace cc { |
| 13 |
| 14 // This class encapsulates the index boundaries for region on co-ordinate system |
| 15 // (used for tiling). The delimiting boundaries |left_|, |right_|, |top_| and |
| 16 // |bottom_| are basically leftmost, rightmost, topmost and bottommost indices |
| 17 // of the region. These delimiters can span in any quadrants. |
| 18 // |
| 19 // If |left_| <= |right_| and |top_| <= |bottom_|, IndexRect is considered to |
| 20 // hold valid indices and this can be checked using is_valid(). |
| 21 // |
| 22 // If IndexRect is valid, it has a coverage of all the indices from |left_| to |
| 23 // |right_| both inclusive and |top_| to |bottom_| both inclusive. So for |
| 24 // |left_| == |right_|, num_indices_x() is 1, meaning |left_| and |right_| point |
| 25 // to the same index. |
| 26 // |
| 27 // The following diagram shows how indices span in different quadrants and the |
| 28 // positive quadrant. In the positive quadrant all indices are >= 0. The first |
| 29 // index in this quadrant is (0, 0). The indices in positive quadrant represent |
| 30 // the visible region and is_in_positive_quadrant() can be used to check whether |
| 31 // all indices lie within this quadrant or not. |
| 32 // |
| 33 // │ |
| 34 // │ |
| 35 // -ve index_x │ +ve index_x |
| 36 // -ve index_y │ -ve index_y |
| 37 // │ |
| 38 // ────────────┼──────────── |
| 39 // │ |
| 40 // -ve index_x │ +ve index_x |
| 41 // +ve index_y │ +ve index_y |
| 42 // │ |
| 43 // │ (+ve Quadrant) |
| 44 // |
| 45 // In the following example, region has |left_| = 0, |right_| = 4, |top_| = 0 |
| 46 // and |bottom_| = 4. Here x indices are 0, 1, 2, 3, 4 and y indices are |
| 47 // 0, 1, 2, 3, 4. |
| 48 // |
| 49 // x 0 1 2 3 4 |
| 50 // y ┌───┬───┬───┬───┬───┐ |
| 51 // 0 │ │ │ │ │ │ |
| 52 // ├───┼───┼───┼───┼───┤ |
| 53 // 1 │ │ │ │ │ │ |
| 54 // ├───┼───┼───┼───┼───┤ |
| 55 // 2 │ │ │ │ │ │ |
| 56 // ├───┼───┼───┼───┼───┤ |
| 57 // 3 │ │ │ │ │ │ |
| 58 // ├───┼───┼───┼───┼───┤ |
| 59 // 4 │ │ │ │ │ │ |
| 60 // └───┴───┴───┴───┴───┘ |
| 61 class CC_EXPORT IndexRect { |
| 62 public: |
| 63 constexpr IndexRect(int left, int right, int top, int bottom) |
| 64 : left_(left), right_(right), top_(top), bottom_(bottom) {} |
| 65 |
| 66 ~IndexRect() = default; |
| 67 |
| 68 constexpr int left() const { return left_; } |
| 69 constexpr int right() const { return right_; } |
| 70 constexpr int top() const { return top_; } |
| 71 constexpr int bottom() const { return bottom_; } |
| 72 |
| 73 // Returns the number of indices from left to right, including both. |
| 74 constexpr int num_indices_x() const { return right_ - left_ + 1; } |
| 75 // Returns the number of indices from top to bottom, including both. |
| 76 constexpr int num_indices_y() const { return bottom_ - top_ + 1; } |
| 77 |
| 78 // Returns true if the index rect has valid indices. |
| 79 constexpr bool is_valid() const { return left_ <= right_ && top_ <= bottom_; } |
| 80 |
| 81 // Returns true if the index rect has valid indices in positive quadrant. |
| 82 constexpr bool is_in_positive_quadrant() const { |
| 83 return is_valid() && left_ >= 0 && top_ >= 0; |
| 84 } |
| 85 |
| 86 // Returns true if the index identified by index_x is valid column. |
| 87 bool valid_column(int index_x) const { |
| 88 return index_x >= left() && index_x <= right(); |
| 89 } |
| 90 // Returns true if the index identified by index_y is a valid row. |
| 91 bool valid_row(int index_y) const { |
| 92 return index_y >= top() && index_y <= bottom(); |
| 93 } |
| 94 |
| 95 // Clamp indices to the given IndexRect indices. For non-intersecting rects, |
| 96 // it makes this index rect invalid. |
| 97 void ClampTo(const IndexRect& other); |
| 98 |
| 99 // Returns true if the given index identified by index_x and index_y falls |
| 100 // inside this index rectangle, including edge indices. |
| 101 bool Contains(int index_x, int index_y) const; |
| 102 |
| 103 std::string ToString() const; |
| 104 |
| 105 private: |
| 106 int left_; |
| 107 int right_; |
| 108 int top_; |
| 109 int bottom_; |
| 110 }; |
| 111 |
| 112 inline bool operator==(const IndexRect& lhs, const IndexRect& rhs) { |
| 113 return lhs.left() == rhs.left() && lhs.right() == rhs.right() && |
| 114 lhs.top() == rhs.top() && lhs.bottom() == rhs.bottom(); |
| 115 } |
| 116 |
| 117 inline bool operator!=(const IndexRect& lhs, const IndexRect& rhs) { |
| 118 return !(lhs == rhs); |
| 119 } |
| 120 |
| 121 } // namespace cc |
| 122 |
| 123 #endif // CC_BASE_INDEX_RECT_H_ |
OLD | NEW |