103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// found in the LICENSE file. 403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#ifndef CC_BASE_SIMPLE_ENCLOSED_REGION_H_ 603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#define CC_BASE_SIMPLE_ENCLOSED_REGION_H_ 703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include <string> 903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 1003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "cc/base/cc_export.h" 1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "ui/gfx/rect.h" 1203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace cc { 1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class Region; 1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 1703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// A constant-sized approximation of a Region. The SimpleEnclosedRegion may 1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// exclude points in its approximation (may have false negatives) but will never 1903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// include a point that would not be in the actual Region (no false positives). 2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class CC_EXPORT SimpleEnclosedRegion { 2103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) public: 2203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion() : rect_() {} 2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion(const SimpleEnclosedRegion& region) 2403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) : rect_(region.rect_) {} 2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) explicit SimpleEnclosedRegion(const gfx::Rect& rect) : rect_(rect) {} 2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion(int x, int y, int w, int h) : rect_(x, y, w, h) {} 2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion(int w, int h) : rect_(w, h) {} 2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) explicit SimpleEnclosedRegion(const Region& region); 2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) ~SimpleEnclosedRegion(); 3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 3103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& operator=(const gfx::Rect& rect) { 3203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) rect_ = rect; 3303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return *this; 3403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 3503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& operator=(const SimpleEnclosedRegion& region) { 3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) rect_ = region.rect_; 3703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return *this; 3803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 3903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 4003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool IsEmpty() const { return rect_.IsEmpty(); } 4103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Clear() { rect_ = gfx::Rect(); } 4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) size_t GetRegionComplexity() const { return rect_.IsEmpty() ? 0 : 1; } 4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Contains(const gfx::Point& point) const { return rect_.Contains(point); } 4503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Contains(const gfx::Rect& rect) const { return rect_.Contains(rect); } 4603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Contains(const SimpleEnclosedRegion& region) const { 4703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return rect_.Contains(region.rect_); 4803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 4903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 5003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Intersects(const gfx::Rect& rect) const { 5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return rect_.Intersects(rect); 5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Intersects(const SimpleEnclosedRegion& region) const { 5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return rect_.Intersects(region.rect_); 5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Subtract(const gfx::Rect& sub_rect); 5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Subtract(const SimpleEnclosedRegion& sub_region) { 5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) Subtract(sub_region.rect_); 6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Union(const gfx::Rect& new_rect); 6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Union(const SimpleEnclosedRegion& new_region) { 6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) Union(new_region.rect_); 6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Intersect(const gfx::Rect& in_rect) { return rect_.Intersect(in_rect); } 6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) void Intersect(const SimpleEnclosedRegion& in_region) { 6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) Intersect(in_region.rect_); 6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool Equals(const SimpleEnclosedRegion& other) const { 7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool both_empty = rect_.IsEmpty() && other.rect_.IsEmpty(); 7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return both_empty || rect_ == other.rect_; 7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) } 7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) gfx::Rect bounds() const { return rect_; } 7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) // The value of |i| must be less than GetRegionComplexity(). 7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) gfx::Rect GetRect(size_t i) const; 7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) std::string ToString() const { return rect_.ToString(); } 8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) private: 8303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) gfx::Rect rect_; 8403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}; 8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)inline bool operator==(const SimpleEnclosedRegion& a, 8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& b) { 8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return a.Equals(b); 8903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 9003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 9103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)inline bool operator!=(const SimpleEnclosedRegion& a, 9203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& b) { 9303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return !(a == b); 9403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 9503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)inline SimpleEnclosedRegion SubtractSimpleEnclosedRegions( 9703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& a, 9803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& b) { 9903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion result = a; 10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) result.Subtract(b); 10103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return result; 10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)inline SimpleEnclosedRegion IntersectSimpleEnclosedRegions( 10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& a, 10603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& b) { 10703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion result = a; 10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) result.Intersect(b); 10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return result; 11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 11103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 11203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)inline SimpleEnclosedRegion UnionSimpleEnclosedRegions( 11303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& a, 11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SimpleEnclosedRegion& b) { 11503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) SimpleEnclosedRegion result = a; 11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) result.Union(b); 11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) return result; 11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} 11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 12003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)} // namespace cc 12103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 12203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#endif // CC_BASE_SIMPLE_ENCLOSED_REGION_H_ 123