1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124

cc / base / simple_enclosed_region.h [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_BASE_SIMPLE_ENCLOSED_REGION_H_
#define CC_BASE_SIMPLE_ENCLOSED_REGION_H_

#include <stddef.h>

#include <string>

#include "cc/base/base_export.h"
#include "ui/gfx/geometry/rect.h"

namespace cc {

class Region;

// A constant-sized approximation of a Region. The SimpleEnclosedRegion may
// exclude points in its approximation (may have false negatives) but will never
// include a point that would not be in the actual Region (no false positives).
class CC_BASE_EXPORT SimpleEnclosedRegion {
 public:
  SimpleEnclosedRegion() : rect_() {}
  SimpleEnclosedRegion(const SimpleEnclosedRegion& region)
      : rect_(region.rect_) {}
  explicit SimpleEnclosedRegion(const gfx::Rect& rect) : rect_(rect) {}
  SimpleEnclosedRegion(int x, int y, int w, int h) : rect_(x, y, w, h) {}
  SimpleEnclosedRegion(int w, int h) : rect_(w, h) {}
  explicit SimpleEnclosedRegion(const Region& region);
  ~SimpleEnclosedRegion();

  const SimpleEnclosedRegion& operator=(const gfx::Rect& rect) {
    rect_ = rect;
    return *this;
  }
  const SimpleEnclosedRegion& operator=(const SimpleEnclosedRegion& region) {
    rect_ = region.rect_;
    return *this;
  }

  bool IsEmpty() const { return rect_.IsEmpty(); }
  void Clear() { rect_ = gfx::Rect(); }
  size_t GetRegionComplexity() const { return rect_.IsEmpty() ? 0 : 1; }

  bool Contains(const gfx::Point& point) const { return rect_.Contains(point); }
  bool Contains(const gfx::Rect& rect) const { return rect_.Contains(rect); }
  bool Contains(const SimpleEnclosedRegion& region) const {
    return rect_.Contains(region.rect_);
  }

  bool Intersects(const gfx::Rect& rect) const {
    return rect_.Intersects(rect);
  }
  bool Intersects(const SimpleEnclosedRegion& region) const {
    return rect_.Intersects(region.rect_);
  }

  void Subtract(const gfx::Rect& sub_rect);
  void Subtract(const SimpleEnclosedRegion& sub_region) {
    Subtract(sub_region.rect_);
  }
  void Union(const gfx::Rect& new_rect);
  void Union(const SimpleEnclosedRegion& new_region) {
    Union(new_region.rect_);
  }
  void Intersect(const gfx::Rect& in_rect) { return rect_.Intersect(in_rect); }
  void Intersect(const SimpleEnclosedRegion& in_region) {
    Intersect(in_region.rect_);
  }

  bool Equals(const SimpleEnclosedRegion& other) const {
    bool both_empty = rect_.IsEmpty() && other.rect_.IsEmpty();
    return both_empty || rect_ == other.rect_;
  }

  gfx::Rect bounds() const { return rect_; }

  // The value of |i| must be less than GetRegionComplexity().
  gfx::Rect GetRect(size_t i) const;

  std::string ToString() const { return rect_.ToString(); }

 private:
  gfx::Rect rect_;
};

inline bool operator==(const SimpleEnclosedRegion& a,
                       const SimpleEnclosedRegion& b) {
  return a.Equals(b);
}

inline bool operator!=(const SimpleEnclosedRegion& a,
                       const SimpleEnclosedRegion& b) {
  return !(a == b);
}

inline SimpleEnclosedRegion SubtractSimpleEnclosedRegions(
    const SimpleEnclosedRegion& a,
    const SimpleEnclosedRegion& b) {
  SimpleEnclosedRegion result = a;
  result.Subtract(b);
  return result;
}

inline SimpleEnclosedRegion IntersectSimpleEnclosedRegions(
    const SimpleEnclosedRegion& a,
    const SimpleEnclosedRegion& b) {
  SimpleEnclosedRegion result = a;
  result.Intersect(b);
  return result;
}

inline SimpleEnclosedRegion UnionSimpleEnclosedRegions(
    const SimpleEnclosedRegion& a,
    const SimpleEnclosedRegion& b) {
  SimpleEnclosedRegion result = a;
  result.Union(b);
  return result;
}

}  // namespace cc

#endif  // CC_BASE_SIMPLE_ENCLOSED_REGION_H_