• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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_SIMPLE_ENCLOSED_REGION_H_
6 #define CC_BASE_SIMPLE_ENCLOSED_REGION_H_
7 
8 #include <string>
9 
10 #include "cc/base/cc_export.h"
11 #include "ui/gfx/rect.h"
12 
13 namespace cc {
14 
15 class Region;
16 
17 // A constant-sized approximation of a Region. The SimpleEnclosedRegion may
18 // exclude points in its approximation (may have false negatives) but will never
19 // include a point that would not be in the actual Region (no false positives).
20 class CC_EXPORT SimpleEnclosedRegion {
21  public:
SimpleEnclosedRegion()22   SimpleEnclosedRegion() : rect_() {}
SimpleEnclosedRegion(const SimpleEnclosedRegion & region)23   SimpleEnclosedRegion(const SimpleEnclosedRegion& region)
24       : rect_(region.rect_) {}
SimpleEnclosedRegion(const gfx::Rect & rect)25   explicit SimpleEnclosedRegion(const gfx::Rect& rect) : rect_(rect) {}
SimpleEnclosedRegion(int x,int y,int w,int h)26   SimpleEnclosedRegion(int x, int y, int w, int h) : rect_(x, y, w, h) {}
SimpleEnclosedRegion(int w,int h)27   SimpleEnclosedRegion(int w, int h) : rect_(w, h) {}
28   explicit SimpleEnclosedRegion(const Region& region);
29   ~SimpleEnclosedRegion();
30 
31   const SimpleEnclosedRegion& operator=(const gfx::Rect& rect) {
32     rect_ = rect;
33     return *this;
34   }
35   const SimpleEnclosedRegion& operator=(const SimpleEnclosedRegion& region) {
36     rect_ = region.rect_;
37     return *this;
38   }
39 
IsEmpty()40   bool IsEmpty() const { return rect_.IsEmpty(); }
Clear()41   void Clear() { rect_ = gfx::Rect(); }
GetRegionComplexity()42   size_t GetRegionComplexity() const { return rect_.IsEmpty() ? 0 : 1; }
43 
Contains(const gfx::Point & point)44   bool Contains(const gfx::Point& point) const { return rect_.Contains(point); }
Contains(const gfx::Rect & rect)45   bool Contains(const gfx::Rect& rect) const { return rect_.Contains(rect); }
Contains(const SimpleEnclosedRegion & region)46   bool Contains(const SimpleEnclosedRegion& region) const {
47     return rect_.Contains(region.rect_);
48   }
49 
Intersects(const gfx::Rect & rect)50   bool Intersects(const gfx::Rect& rect) const {
51     return rect_.Intersects(rect);
52   }
Intersects(const SimpleEnclosedRegion & region)53   bool Intersects(const SimpleEnclosedRegion& region) const {
54     return rect_.Intersects(region.rect_);
55   }
56 
57   void Subtract(const gfx::Rect& sub_rect);
Subtract(const SimpleEnclosedRegion & sub_region)58   void Subtract(const SimpleEnclosedRegion& sub_region) {
59     Subtract(sub_region.rect_);
60   }
61   void Union(const gfx::Rect& new_rect);
Union(const SimpleEnclosedRegion & new_region)62   void Union(const SimpleEnclosedRegion& new_region) {
63     Union(new_region.rect_);
64   }
Intersect(const gfx::Rect & in_rect)65   void Intersect(const gfx::Rect& in_rect) { return rect_.Intersect(in_rect); }
Intersect(const SimpleEnclosedRegion & in_region)66   void Intersect(const SimpleEnclosedRegion& in_region) {
67     Intersect(in_region.rect_);
68   }
69 
Equals(const SimpleEnclosedRegion & other)70   bool Equals(const SimpleEnclosedRegion& other) const {
71     bool both_empty = rect_.IsEmpty() && other.rect_.IsEmpty();
72     return both_empty || rect_ == other.rect_;
73   }
74 
bounds()75   gfx::Rect bounds() const { return rect_; }
76 
77   // The value of |i| must be less than GetRegionComplexity().
78   gfx::Rect GetRect(size_t i) const;
79 
ToString()80   std::string ToString() const { return rect_.ToString(); }
81 
82  private:
83   gfx::Rect rect_;
84 };
85 
86 inline bool operator==(const SimpleEnclosedRegion& a,
87                        const SimpleEnclosedRegion& b) {
88   return a.Equals(b);
89 }
90 
91 inline bool operator!=(const SimpleEnclosedRegion& a,
92                        const SimpleEnclosedRegion& b) {
93   return !(a == b);
94 }
95 
SubtractSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)96 inline SimpleEnclosedRegion SubtractSimpleEnclosedRegions(
97     const SimpleEnclosedRegion& a,
98     const SimpleEnclosedRegion& b) {
99   SimpleEnclosedRegion result = a;
100   result.Subtract(b);
101   return result;
102 }
103 
IntersectSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)104 inline SimpleEnclosedRegion IntersectSimpleEnclosedRegions(
105     const SimpleEnclosedRegion& a,
106     const SimpleEnclosedRegion& b) {
107   SimpleEnclosedRegion result = a;
108   result.Intersect(b);
109   return result;
110 }
111 
UnionSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)112 inline SimpleEnclosedRegion UnionSimpleEnclosedRegions(
113     const SimpleEnclosedRegion& a,
114     const SimpleEnclosedRegion& b) {
115   SimpleEnclosedRegion result = a;
116   result.Union(b);
117   return result;
118 }
119 
120 }  // namespace cc
121 
122 #endif  // CC_BASE_SIMPLE_ENCLOSED_REGION_H_
123