blob: c35289427d113419eb6cbec6050e53da1688844d [file] [log] [blame]
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrRect_DEFINED
#define GrRect_DEFINED
#include "include/core/SkMatrix.h"
#include "include/core/SkRect.h"
#include "include/core/SkTypes.h"
/** Returns true if the rectangles have a nonzero area of overlap. It assumed that rects can be
infinitely small but not "inverted". */
static inline bool GrRectsOverlap(const SkRect& a, const SkRect& b) {
// See skbug.com/6607 about the isFinite() checks.
SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
return a.fRight > b.fLeft && a.fBottom > b.fTop && b.fRight > a.fLeft && b.fBottom > a.fTop;
}
/** Returns true if the rectangles overlap or share an edge or corner. It assumed that rects can be
infinitely small but not "inverted". */
static inline bool GrRectsTouchOrOverlap(const SkRect& a, const SkRect& b) {
// See skbug.com/6607 about the isFinite() checks.
SkASSERT(!a.isFinite() || (a.fLeft <= a.fRight && a.fTop <= a.fBottom));
SkASSERT(!b.isFinite() || (b.fLeft <= b.fRight && b.fTop <= b.fBottom));
return a.fRight >= b.fLeft && a.fBottom >= b.fTop && b.fRight >= a.fLeft && b.fBottom >= a.fTop;
}
/**
* Apply the transform from 'inRect' to 'outRect' to each point in 'inPts', storing the mapped point
* into the parallel index of 'outPts'.
*/
static inline void GrMapRectPoints(const SkRect& inRect, const SkRect& outRect,
const SkPoint inPts[], SkPoint outPts[], int ptCount) {
SkMatrix::RectToRect(inRect, outRect).mapPoints(outPts, inPts, ptCount);
}
/**
* Clips the srcRect and the dstPoint to the bounds of the srcSize and dstSize respectively. Returns
* true if the srcRect and dstRect intersect the srcRect and dst rect (dstPoint with srcRect
* width/height). Returns false otherwise. The clipped values are stored back into 'dstPoint'
* and 'srcRect'
*/
static inline bool GrClipSrcRectAndDstPoint(const SkISize& dstSize,
SkIPoint* dstPoint,
const SkISize& srcSize,
SkIRect* srcRect) {
// clip the left edge to src and dst bounds, adjusting dstPoint if necessary
if (srcRect->fLeft < 0) {
dstPoint->fX -= srcRect->fLeft;
srcRect->fLeft = 0;
}
if (dstPoint->fX < 0) {
srcRect->fLeft -= dstPoint->fX;
dstPoint->fX = 0;
}
// clip the top edge to src and dst bounds, adjusting dstPoint if necessary
if (srcRect->fTop < 0) {
dstPoint->fY -= srcRect->fTop;
srcRect->fTop = 0;
}
if (dstPoint->fY < 0) {
srcRect->fTop -= dstPoint->fY;
dstPoint->fY = 0;
}
// clip the right edge to the src and dst bounds.
if (srcRect->fRight > srcSize.width()) {
srcRect->fRight = srcSize.width();
}
if (dstPoint->fX + srcRect->width() > dstSize.width()) {
srcRect->fRight = srcRect->fLeft + dstSize.width() - dstPoint->fX;
}
// clip the bottom edge to the src and dst bounds.
if (srcRect->fBottom > srcSize.height()) {
srcRect->fBottom = srcSize.height();
}
if (dstPoint->fY + srcRect->height() > dstSize.height()) {
srcRect->fBottom = srcRect->fTop + dstSize.height() - dstPoint->fY;
}
// The above clipping steps may have inverted the rect if it didn't intersect either the src or
// dst bounds.
return !srcRect->isEmpty();
}
#endif