| 1 |
/* |
| 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. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef GrRect_DEFINED |
| 9 |
#define GrRect_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
|
| 15 |
/** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be |
| 16 |
infinitely small but not "inverted". */ |
| 17 |
static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) { |
| 18 |
// See skbug.com/40037824 about the isFinite() checks. |
| 19 |
SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom)); |
| 20 |
SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom)); |
| 21 |
return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop; |
| 22 |
} |
| 23 |
|
| 24 |
/** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be |
| 25 |
infinitely small but not "inverted". */ |
| 26 |
static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) { |
| 27 |
// See skbug.com/40037824 about the isFinite() checks. |
| 28 |
SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom)); |
| 29 |
SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom)); |
| 30 |
return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point |
| 35 |
* into the parallel index of 'outPts'. |
| 36 |
*/ |
| 37 |
static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect, |
| 38 |
const SkPoint inPts[], SkPoint outPts[], size_t ptCount) { |
| 39 |
SkMatrix::RectToRectOrIdentity(inRect, outRect).mapPoints({outPts, ptCount}, {inPts, ptCount}); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns |
| 44 |
* true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect |
| 45 |
* width/height). Returns false otherwise. The clipped values are stored back into 'dstPoint' |
| 46 |
* and 'srcRect' |
| 47 |
*/ |
| 48 |
static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize, |
| 49 |
SkIPoint* dstPoint, |
| 50 |
const SkISize& srcSize, |
| 51 |
SkIRect* srcRect) { |
| 52 |
// clip the left edge to src and dst bounds, adjusting dstPoint if necessary |
| 53 |
if (srcRect->fLeft < 0) { |
| 54 |
dstPoint->fX -= srcRect->fLeft; |
| 55 |
srcRect->fLeft = 0; |
| 56 |
} |
| 57 |
if (dstPoint->fX < 0) { |
| 58 |
srcRect->fLeft -= dstPoint->fX; |
| 59 |
dstPoint->fX = 0; |
| 60 |
} |
| 61 |
|
| 62 |
// clip the top edge to src and dst bounds, adjusting dstPoint if necessary |
| 63 |
if (srcRect->fTop < 0) { |
| 64 |
dstPoint->fY -= srcRect->fTop; |
| 65 |
srcRect->fTop = 0; |
| 66 |
} |
| 67 |
if (dstPoint->fY < 0) { |
| 68 |
srcRect->fTop -= dstPoint->fY; |
| 69 |
dstPoint->fY = 0; |
| 70 |
} |
| 71 |
|
| 72 |
// clip the right edge to the src and dst bounds. |
| 73 |
if (srcRect->fRight > srcSize.width()) { |
| 74 |
srcRect->fRight = srcSize.width(); |
| 75 |
} |
| 76 |
if (dstPoint->fX + srcRect->width() > dstSize.width()) { |
| 77 |
srcRect->fRight = srcRect->fLeft + dstSize.width() - dstPoint->fX; |
| 78 |
} |
| 79 |
|
| 80 |
// clip the bottom edge to the src and dst bounds. |
| 81 |
if (srcRect->fBottom > srcSize.height()) { |
| 82 |
srcRect->fBottom = srcSize.height(); |
| 83 |
} |
| 84 |
if (dstPoint->fY + srcRect->height() > dstSize.height()) { |
| 85 |
srcRect->fBottom = srcRect->fTop + dstSize.height() - dstPoint->fY; |
| 86 |
} |
| 87 |
|
| 88 |
// The above clipping steps may have inverted the rect if it didn't intersect either the src or |
| 89 |
// dst bounds. |
| 90 |
return !srcRect->isEmpty(); |
| 91 |
} |
| 92 |
|
| 93 |
#endif |