sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_ |
| 12 | #define MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_ |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/constructormagic.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 15 | #include "typedefs.h" // NOLINT(build/include) |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // A vector in the 2D integer space. E.g. can be used to represent screen DPI. |
| 20 | class DesktopVector { |
| 21 | public: |
| 22 | DesktopVector() : x_(0), y_(0) {} |
| 23 | DesktopVector(int32_t x, int32_t y) : x_(x), y_(y) {} |
| 24 | |
| 25 | int32_t x() const { return x_; } |
| 26 | int32_t y() const { return y_; } |
| 27 | bool is_zero() const { return x_ == 0 && y_ == 0; } |
| 28 | |
| 29 | bool equals(const DesktopVector& other) const { |
| 30 | return x_ == other.x_ && y_ == other.y_; |
| 31 | } |
| 32 | |
| 33 | void set(int32_t x, int32_t y) { |
| 34 | x_ = x; |
| 35 | y_ = y; |
| 36 | } |
| 37 | |
sergeyu@chromium.org | e6e749d | 2013-10-16 02:48:41 +0000 | [diff] [blame] | 38 | DesktopVector add(const DesktopVector& other) const { |
| 39 | return DesktopVector(x() + other.x(), y() + other.y()); |
| 40 | } |
| 41 | DesktopVector subtract(const DesktopVector& other) const { |
| 42 | return DesktopVector(x() - other.x(), y() - other.y()); |
| 43 | } |
| 44 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 45 | private: |
| 46 | int32_t x_; |
| 47 | int32_t y_; |
| 48 | }; |
| 49 | |
| 50 | // Type used to represent screen/window size. |
| 51 | class DesktopSize { |
| 52 | public: |
| 53 | DesktopSize() : width_(0), height_(0) {} |
| 54 | DesktopSize(int32_t width, int32_t height) |
| 55 | : width_(width), height_(height) { |
| 56 | } |
| 57 | |
| 58 | int32_t width() const { return width_; } |
| 59 | int32_t height() const { return height_; } |
| 60 | |
sergeyu@chromium.org | 74f6074 | 2014-04-09 01:04:22 +0000 | [diff] [blame] | 61 | bool is_empty() const { return width_ <= 0 || height_ <= 0; } |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 62 | |
| 63 | bool equals(const DesktopSize& other) const { |
| 64 | return width_ == other.width_ && height_ == other.height_; |
| 65 | } |
| 66 | |
| 67 | void set(int32_t width, int32_t height) { |
| 68 | width_ = width; |
| 69 | height_ = height; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | int32_t width_; |
| 74 | int32_t height_; |
| 75 | }; |
| 76 | |
| 77 | // Represents a rectangle on the screen. |
| 78 | class DesktopRect { |
| 79 | public: |
| 80 | static DesktopRect MakeSize(const DesktopSize& size) { |
| 81 | return DesktopRect(0, 0, size.width(), size.height()); |
| 82 | } |
| 83 | static DesktopRect MakeWH(int32_t width, int32_t height) { |
| 84 | return DesktopRect(0, 0, width, height); |
| 85 | } |
| 86 | static DesktopRect MakeXYWH(int32_t x, int32_t y, |
| 87 | int32_t width, int32_t height) { |
| 88 | return DesktopRect(x, y, x + width, y + height); |
| 89 | } |
| 90 | static DesktopRect MakeLTRB(int32_t left, int32_t top, |
| 91 | int32_t right, int32_t bottom) { |
| 92 | return DesktopRect(left, top, right, bottom); |
| 93 | } |
sergeyu@chromium.org | 5d85819 | 2013-11-19 02:15:47 +0000 | [diff] [blame] | 94 | static DesktopRect MakeOriginSize(const DesktopVector& origin, |
| 95 | const DesktopSize& size) { |
| 96 | return MakeXYWH(origin.x(), origin.y(), size.width(), size.height()); |
| 97 | } |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 98 | |
| 99 | DesktopRect() : left_(0), top_(0), right_(0), bottom_(0) {} |
| 100 | |
| 101 | int32_t left() const { return left_; } |
| 102 | int32_t top() const { return top_; } |
| 103 | int32_t right() const { return right_; } |
| 104 | int32_t bottom() const { return bottom_; } |
| 105 | int32_t width() const { return right_ - left_; } |
| 106 | int32_t height() const { return bottom_ - top_; } |
| 107 | |
Zijie He | 4fe6607 | 2017-08-30 16:58:14 -0700 | [diff] [blame] | 108 | void set_width(int32_t width) { right_ = left_ + width; } |
| 109 | void set_height(int32_t height) { bottom_ = top_ + height; } |
| 110 | |
sergeyu@chromium.org | e6e749d | 2013-10-16 02:48:41 +0000 | [diff] [blame] | 111 | DesktopVector top_left() const { return DesktopVector(left_, top_); } |
| 112 | DesktopSize size() const { return DesktopSize(width(), height()); } |
| 113 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 114 | bool is_empty() const { return left_ >= right_ || top_ >= bottom_; } |
| 115 | |
| 116 | bool equals(const DesktopRect& other) const { |
| 117 | return left_ == other.left_ && top_ == other.top_ && |
| 118 | right_ == other.right_ && bottom_ == other.bottom_; |
| 119 | } |
| 120 | |
sergeyu@chromium.org | e6e749d | 2013-10-16 02:48:41 +0000 | [diff] [blame] | 121 | // Returns true if |point| lies within the rectangle boundaries. |
| 122 | bool Contains(const DesktopVector& point) const; |
| 123 | |
sergeyu@chromium.org | 5d85819 | 2013-11-19 02:15:47 +0000 | [diff] [blame] | 124 | // Returns true if |rect| lies within the boundaries of this rectangle. |
| 125 | bool ContainsRect(const DesktopRect& rect) const; |
| 126 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 127 | // Finds intersection with |rect|. |
| 128 | void IntersectWith(const DesktopRect& rect); |
| 129 | |
zijiehe | 73c1234 | 2017-05-16 16:50:32 -0700 | [diff] [blame] | 130 | // Extends the rectangle to cover |rect|. If |this| is empty, replaces |this| |
| 131 | // with |rect|; if |rect| is empty, this function takes no effect. |
| 132 | void UnionWith(const DesktopRect& rect); |
| 133 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 134 | // Adds (dx, dy) to the position of the rectangle. |
| 135 | void Translate(int32_t dx, int32_t dy); |
sergeyu@chromium.org | e6e749d | 2013-10-16 02:48:41 +0000 | [diff] [blame] | 136 | void Translate(DesktopVector d) { Translate(d.x(), d.y()); }; |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 137 | |
zijiehe | fef8653 | 2016-09-05 15:26:32 -0700 | [diff] [blame] | 138 | // Enlarges current DesktopRect by subtracting |left_offset| and |top_offset| |
| 139 | // from |left_| and |top_|, and adding |right_offset| and |bottom_offset| to |
| 140 | // |right_| and |bottom_|. This function does not normalize the result, so |
| 141 | // |left_| and |top_| may be less than zero or larger than |right_| and |
| 142 | // |bottom_|. |
| 143 | void Extend(int32_t left_offset, |
| 144 | int32_t top_offset, |
| 145 | int32_t right_offset, |
| 146 | int32_t bottom_offset); |
| 147 | |
Zijie He | af5686a | 2017-08-25 11:36:52 -0700 | [diff] [blame] | 148 | // Scales current DesktopRect. This function does not impact the |top_| and |
| 149 | // |left_|. |
| 150 | void Scale(double horizontal, double vertical); |
| 151 | |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 152 | private: |
| 153 | DesktopRect(int32_t left, int32_t top, int32_t right, int32_t bottom) |
| 154 | : left_(left), top_(top), right_(right), bottom_(bottom) { |
| 155 | } |
| 156 | |
| 157 | int32_t left_; |
| 158 | int32_t top_; |
| 159 | int32_t right_; |
| 160 | int32_t bottom_; |
| 161 | }; |
| 162 | |
| 163 | } // namespace webrtc |
| 164 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 165 | #endif // MODULES_DESKTOP_CAPTURE_DESKTOP_GEOMETRY_H_ |
sergeyu@chromium.org | 15e32cc | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 166 | |