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.
5 #ifndef CC_BASE_SIMPLE_ENCLOSED_REGION_H_
6 #define CC_BASE_SIMPLE_ENCLOSED_REGION_H_
10 #include "cc/base/cc_export.h"
11 #include "ui/gfx/geometry/rect.h"
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 {
22 SimpleEnclosedRegion() : rect_() {}
23 SimpleEnclosedRegion(const SimpleEnclosedRegion& region)
24 : rect_(region.rect_) {}
25 explicit SimpleEnclosedRegion(const gfx::Rect& rect) : rect_(rect) {}
26 SimpleEnclosedRegion(int x, int y, int w, int h) : rect_(x, y, w, h) {}
27 SimpleEnclosedRegion(int w, int h) : rect_(w, h) {}
28 explicit SimpleEnclosedRegion(const Region& region);
29 ~SimpleEnclosedRegion();
31 const SimpleEnclosedRegion& operator=(const gfx::Rect& rect) {
35 const SimpleEnclosedRegion& operator=(const SimpleEnclosedRegion& region) {
40 bool IsEmpty() const { return rect_.IsEmpty(); }
41 void Clear() { rect_ = gfx::Rect(); }
42 size_t GetRegionComplexity() const { return rect_.IsEmpty() ? 0 : 1; }
44 bool Contains(const gfx::Point& point) const { return rect_.Contains(point); }
45 bool Contains(const gfx::Rect& rect) const { return rect_.Contains(rect); }
46 bool Contains(const SimpleEnclosedRegion& region) const {
47 return rect_.Contains(region.rect_);
50 bool Intersects(const gfx::Rect& rect) const {
51 return rect_.Intersects(rect);
53 bool Intersects(const SimpleEnclosedRegion& region) const {
54 return rect_.Intersects(region.rect_);
57 void Subtract(const gfx::Rect& sub_rect);
58 void Subtract(const SimpleEnclosedRegion& sub_region) {
59 Subtract(sub_region.rect_);
61 void Union(const gfx::Rect& new_rect);
62 void Union(const SimpleEnclosedRegion& new_region) {
63 Union(new_region.rect_);
65 void Intersect(const gfx::Rect& in_rect) { return rect_.Intersect(in_rect); }
66 void Intersect(const SimpleEnclosedRegion& in_region) {
67 Intersect(in_region.rect_);
70 bool Equals(const SimpleEnclosedRegion& other) const {
71 bool both_empty = rect_.IsEmpty() && other.rect_.IsEmpty();
72 return both_empty || rect_ == other.rect_;
75 gfx::Rect bounds() const { return rect_; }
77 // The value of |i| must be less than GetRegionComplexity().
78 gfx::Rect GetRect(size_t i) const;
80 std::string ToString() const { return rect_.ToString(); }
86 inline bool operator==(const SimpleEnclosedRegion& a,
87 const SimpleEnclosedRegion& b) {
91 inline bool operator!=(const SimpleEnclosedRegion& a,
92 const SimpleEnclosedRegion& b) {
96 inline SimpleEnclosedRegion SubtractSimpleEnclosedRegions(
97 const SimpleEnclosedRegion& a,
98 const SimpleEnclosedRegion& b) {
99 SimpleEnclosedRegion result = a;
104 inline SimpleEnclosedRegion IntersectSimpleEnclosedRegions(
105 const SimpleEnclosedRegion& a,
106 const SimpleEnclosedRegion& b) {
107 SimpleEnclosedRegion result = a;
112 inline SimpleEnclosedRegion UnionSimpleEnclosedRegions(
113 const SimpleEnclosedRegion& a,
114 const SimpleEnclosedRegion& b) {
115 SimpleEnclosedRegion result = a;
122 #endif // CC_BASE_SIMPLE_ENCLOSED_REGION_H_