summaryrefslogtreecommitdiff
path: root/chromium/cc/base/index_rect.h
blob: d6d2d917828b33d4f2a8b7169c1e7a8b61d4b8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_BASE_INDEX_RECT_H_
#define CC_BASE_INDEX_RECT_H_

#include <string>

#include "cc/base/cc_export.h"

namespace cc {

// This class encapsulates the index boundaries for region on co-ordinate system
// (used for tiling). The delimiting boundaries |left_|, |right_|, |top_| and
// |bottom_| are basically leftmost, rightmost, topmost and bottommost indices
// of the region. These delimiters can span in any quadrants.
//
// If |left_| <= |right_| and |top_| <= |bottom_|, IndexRect is considered to
// hold valid indices and this can be checked using is_valid().
//
// If IndexRect is valid, it has a coverage of all the indices from |left_| to
// |right_| both inclusive and |top_| to |bottom_| both inclusive. So for
// |left_| == |right_|, num_indices_x() is 1, meaning |left_| and |right_| point
// to the same index.
//
// The following diagram shows how indices span in different quadrants and the
// positive quadrant. In the positive quadrant all indices are >= 0. The first
// index in this quadrant is (0, 0). The indices in positive quadrant represent
// the visible region and is_in_positive_quadrant() can be used to check whether
// all indices lie within this quadrant or not.
//
//              │
//              │
//  -ve index_x │  +ve index_x
//  -ve index_y │  -ve index_y
//              │
//  ────────────┼────────────
//              │
//  -ve index_x │  +ve index_x
//  +ve index_y │  +ve index_y
//              │
//              │  (+ve Quadrant)
//
// In the following example, region has |left_| = 0, |right_| = 4, |top_| = 0
// and |bottom_| = 4. Here x indices are 0, 1, 2, 3, 4 and y indices are
// 0, 1, 2, 3, 4.
//
//    x 0   1   2   3   4
//  y ┌───┬───┬───┬───┬───┐
//  0 │   │   │   │   │   │
//    ├───┼───┼───┼───┼───┤
//  1 │   │   │   │   │   │
//    ├───┼───┼───┼───┼───┤
//  2 │   │   │   │   │   │
//    ├───┼───┼───┼───┼───┤
//  3 │   │   │   │   │   │
//    ├───┼───┼───┼───┼───┤
//  4 │   │   │   │   │   │
//    └───┴───┴───┴───┴───┘
class CC_EXPORT IndexRect {
 public:
  constexpr IndexRect(int left, int right, int top, int bottom)
      : left_(left), right_(right), top_(top), bottom_(bottom) {}

  ~IndexRect() = default;

  constexpr int left() const { return left_; }
  constexpr int right() const { return right_; }
  constexpr int top() const { return top_; }
  constexpr int bottom() const { return bottom_; }

  // Returns the number of indices from left to right, including both.
  constexpr int num_indices_x() const { return right_ - left_ + 1; }
  // Returns the number of indices from top to bottom, including both.
  constexpr int num_indices_y() const { return bottom_ - top_ + 1; }

  // Returns true if the index rect has valid indices.
  constexpr bool is_valid() const { return left_ <= right_ && top_ <= bottom_; }

  // Returns true if the index rect has valid indices in positive quadrant.
  constexpr bool is_in_positive_quadrant() const {
    return is_valid() && left_ >= 0 && top_ >= 0;
  }

  // Returns true if the index identified by index_x is valid column.
  bool valid_column(int index_x) const {
    return index_x >= left() && index_x <= right();
  }
  // Returns true if the index identified by index_y is a valid row.
  bool valid_row(int index_y) const {
    return index_y >= top() && index_y <= bottom();
  }

  // Clamp indices to the given IndexRect indices. For non-intersecting rects,
  // it makes this index rect invalid.
  void ClampTo(const IndexRect& other);

  // Returns true if the given index identified by index_x and index_y falls
  // inside this index rectangle, including edge indices.
  bool Contains(int index_x, int index_y) const;

  std::string ToString() const;

 private:
  int left_;
  int right_;
  int top_;
  int bottom_;
};

inline bool operator==(const IndexRect& lhs, const IndexRect& rhs) {
  return lhs.left() == rhs.left() && lhs.right() == rhs.right() &&
         lhs.top() == rhs.top() && lhs.bottom() == rhs.bottom();
}

inline bool operator!=(const IndexRect& lhs, const IndexRect& rhs) {
  return !(lhs == rhs);
}

}  // namespace cc

#endif  // CC_BASE_INDEX_RECT_H_