xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/desktop_geometry.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "modules/desktop_capture/desktop_geometry.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker #include <cmath>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
17*d9f75844SAndroid Build Coastguard Worker 
Contains(const DesktopVector & point) const18*d9f75844SAndroid Build Coastguard Worker bool DesktopRect::Contains(const DesktopVector& point) const {
19*d9f75844SAndroid Build Coastguard Worker   return point.x() >= left() && point.x() < right() && point.y() >= top() &&
20*d9f75844SAndroid Build Coastguard Worker          point.y() < bottom();
21*d9f75844SAndroid Build Coastguard Worker }
22*d9f75844SAndroid Build Coastguard Worker 
ContainsRect(const DesktopRect & rect) const23*d9f75844SAndroid Build Coastguard Worker bool DesktopRect::ContainsRect(const DesktopRect& rect) const {
24*d9f75844SAndroid Build Coastguard Worker   return rect.left() >= left() && rect.right() <= right() &&
25*d9f75844SAndroid Build Coastguard Worker          rect.top() >= top() && rect.bottom() <= bottom();
26*d9f75844SAndroid Build Coastguard Worker }
27*d9f75844SAndroid Build Coastguard Worker 
IntersectWith(const DesktopRect & rect)28*d9f75844SAndroid Build Coastguard Worker void DesktopRect::IntersectWith(const DesktopRect& rect) {
29*d9f75844SAndroid Build Coastguard Worker   left_ = std::max(left(), rect.left());
30*d9f75844SAndroid Build Coastguard Worker   top_ = std::max(top(), rect.top());
31*d9f75844SAndroid Build Coastguard Worker   right_ = std::min(right(), rect.right());
32*d9f75844SAndroid Build Coastguard Worker   bottom_ = std::min(bottom(), rect.bottom());
33*d9f75844SAndroid Build Coastguard Worker   if (is_empty()) {
34*d9f75844SAndroid Build Coastguard Worker     left_ = 0;
35*d9f75844SAndroid Build Coastguard Worker     top_ = 0;
36*d9f75844SAndroid Build Coastguard Worker     right_ = 0;
37*d9f75844SAndroid Build Coastguard Worker     bottom_ = 0;
38*d9f75844SAndroid Build Coastguard Worker   }
39*d9f75844SAndroid Build Coastguard Worker }
40*d9f75844SAndroid Build Coastguard Worker 
UnionWith(const DesktopRect & rect)41*d9f75844SAndroid Build Coastguard Worker void DesktopRect::UnionWith(const DesktopRect& rect) {
42*d9f75844SAndroid Build Coastguard Worker   if (is_empty()) {
43*d9f75844SAndroid Build Coastguard Worker     *this = rect;
44*d9f75844SAndroid Build Coastguard Worker     return;
45*d9f75844SAndroid Build Coastguard Worker   }
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker   if (rect.is_empty()) {
48*d9f75844SAndroid Build Coastguard Worker     return;
49*d9f75844SAndroid Build Coastguard Worker   }
50*d9f75844SAndroid Build Coastguard Worker 
51*d9f75844SAndroid Build Coastguard Worker   left_ = std::min(left(), rect.left());
52*d9f75844SAndroid Build Coastguard Worker   top_ = std::min(top(), rect.top());
53*d9f75844SAndroid Build Coastguard Worker   right_ = std::max(right(), rect.right());
54*d9f75844SAndroid Build Coastguard Worker   bottom_ = std::max(bottom(), rect.bottom());
55*d9f75844SAndroid Build Coastguard Worker }
56*d9f75844SAndroid Build Coastguard Worker 
Translate(int32_t dx,int32_t dy)57*d9f75844SAndroid Build Coastguard Worker void DesktopRect::Translate(int32_t dx, int32_t dy) {
58*d9f75844SAndroid Build Coastguard Worker   left_ += dx;
59*d9f75844SAndroid Build Coastguard Worker   top_ += dy;
60*d9f75844SAndroid Build Coastguard Worker   right_ += dx;
61*d9f75844SAndroid Build Coastguard Worker   bottom_ += dy;
62*d9f75844SAndroid Build Coastguard Worker }
63*d9f75844SAndroid Build Coastguard Worker 
Extend(int32_t left_offset,int32_t top_offset,int32_t right_offset,int32_t bottom_offset)64*d9f75844SAndroid Build Coastguard Worker void DesktopRect::Extend(int32_t left_offset,
65*d9f75844SAndroid Build Coastguard Worker                          int32_t top_offset,
66*d9f75844SAndroid Build Coastguard Worker                          int32_t right_offset,
67*d9f75844SAndroid Build Coastguard Worker                          int32_t bottom_offset) {
68*d9f75844SAndroid Build Coastguard Worker   left_ -= left_offset;
69*d9f75844SAndroid Build Coastguard Worker   top_ -= top_offset;
70*d9f75844SAndroid Build Coastguard Worker   right_ += right_offset;
71*d9f75844SAndroid Build Coastguard Worker   bottom_ += bottom_offset;
72*d9f75844SAndroid Build Coastguard Worker }
73*d9f75844SAndroid Build Coastguard Worker 
Scale(double horizontal,double vertical)74*d9f75844SAndroid Build Coastguard Worker void DesktopRect::Scale(double horizontal, double vertical) {
75*d9f75844SAndroid Build Coastguard Worker   right_ += static_cast<int>(std::round(width() * (horizontal - 1)));
76*d9f75844SAndroid Build Coastguard Worker   bottom_ += static_cast<int>(std::round(height() * (vertical - 1)));
77*d9f75844SAndroid Build Coastguard Worker }
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
80