reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrRect_DEFINED |
| 9 | #define GrRect_DEFINED |
| 10 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 13 | #include "SkRect.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | struct GrIRect16 { |
| 16 | int16_t fLeft, fTop, fRight, fBottom; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 2f56998 | 2014-04-10 18:36:19 +0000 | [diff] [blame] | 18 | static GrIRect16 SK_WARN_UNUSED_RESULT MakeEmpty() { |
| 19 | GrIRect16 r; |
| 20 | r.setEmpty(); |
| 21 | return r; |
| 22 | } |
| 23 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 24 | static GrIRect16 SK_WARN_UNUSED_RESULT MakeWH(int16_t w, int16_t h) { |
| 25 | GrIRect16 r; |
| 26 | r.set(0, 0, w, h); |
| 27 | return r; |
| 28 | } |
| 29 | |
| 30 | static GrIRect16 SK_WARN_UNUSED_RESULT MakeXYWH(int16_t x, int16_t y, int16_t w, int16_t h) { |
| 31 | GrIRect16 r; |
| 32 | r.set(x, y, x + w, y + h); |
| 33 | return r; |
| 34 | } |
| 35 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | int width() const { return fRight - fLeft; } |
| 37 | int height() const { return fBottom - fTop; } |
| 38 | int area() const { return this->width() * this->height(); } |
| 39 | bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | 2f56998 | 2014-04-10 18:36:19 +0000 | [diff] [blame] | 41 | void setEmpty() { memset(this, 0, sizeof(*this)); } |
| 42 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 43 | void set(int16_t left, int16_t top, int16_t right, int16_t bottom) { |
| 44 | fLeft = left; |
| 45 | fTop = top; |
| 46 | fRight = right; |
| 47 | fBottom = bottom; |
| 48 | } |
| 49 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 50 | void set(const SkIRect& r) { |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 51 | fLeft = SkToS16(r.fLeft); |
| 52 | fTop = SkToS16(r.fTop); |
| 53 | fRight = SkToS16(r.fRight); |
| 54 | fBottom = SkToS16(r.fBottom); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 55 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 58 | /** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be |
| 59 | infinitely small but not "inverted". */ |
| 60 | static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) { |
Brian Salomon | 1ffda04 | 2017-05-09 18:09:28 -0400 | [diff] [blame] | 61 | // See skbug.com/6607 about the isFinite() checks. |
| 62 | SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom)); |
| 63 | SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom)); |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 64 | return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop; |
| 65 | } |
| 66 | |
| 67 | /** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be |
| 68 | infinitely small but not "inverted". */ |
| 69 | static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) { |
Brian Salomon | 1ffda04 | 2017-05-09 18:09:28 -0400 | [diff] [blame] | 70 | // See skbug.com/6607 about the isFinite() checks. |
| 71 | SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom)); |
| 72 | SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom)); |
Brian Salomon | a4677b5 | 2017-05-04 12:39:56 -0400 | [diff] [blame] | 73 | return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop; |
| 74 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 75 | #endif |