Rect.h
浏览该文件的文档.
//
// Rect.h
// MNN
//
// Modified by jiangxiaotang on 2018/09/19.
// Copyright © 2018, Alibaba Group Holding Limited
//
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Generated by tools/bookmaker from include/core/Rect.h and docs/SkRect_Reference.bmh
on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
https://skia.org/user/api/SkRect_Reference
You may edit either file directly. Structural changes to public interfaces require
editing both files. After editing docs/SkRect_Reference.bmh, run:
bookmaker -b docs -i include/core/Rect.h -p
to create an updated version of this file.
*/
#ifndef SkRect_DEFINED
#define SkRect_DEFINED
#include <math.h>
#include <algorithm>
#include <utility>
#include "MNNDefine.h"
namespace MNN {
namespace CV {
struct Point {
float fX;
float fY;
void set(float x, float y) {
fX = x;
fY = y;
}
};
struct MNN_PUBLIC Rect {
float fLeft;
float fTop;
float fRight;
float fBottom;
static constexpr Rect MakeEmpty() {
return Rect{0, 0, 0, 0};
}
#ifdef SK_SUPPORT_LEGACY_RECTMAKELARGEST
static Rect MakeLargest() {
return {SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax};
}
#endif
static constexpr Rect MakeWH(float w, float h) {
return Rect{0, 0, w, h};
}
static Rect MakeIWH(int w, int h) {
Rect r;
r.set(0, 0, (float)(w), (float)(h));
return r;
}
static constexpr Rect MakeLTRB(float l, float t, float r, float b) {
return Rect{l, t, r, b};
}
static constexpr Rect MakeXYWH(float x, float y, float w, float h) {
return Rect{x, y, x + w, y + h};
}
bool isEmpty() const {
// We write it as the NOT of a non-empty rect, so we will return true if any values
// are NaN.
return !(fLeft < fRight && fTop < fBottom);
}
bool isSorted() const {
return fLeft <= fRight && fTop <= fBottom;
}
float x() const {
return fLeft;
}
float y() const {
return fTop;
}
float left() const {
return fLeft;
}
float top() const {
return fTop;
}
float right() const {
return fRight;
}
float bottom() const {
return fBottom;
}
float width() const {
return fRight - fLeft;
}
float height() const {
return fBottom - fTop;
}
float centerX() const {
// don't use floatHalf(fLeft + fBottom) as that might overflow before the 0.5
return 0.5f * (fLeft) + 0.5f * (fRight);
}
float centerY() const {
// don't use floatHalf(fTop + fBottom) as that might overflow before the 0.5
return 0.5f * (fTop) + 0.5f * (fBottom);
}
void setEmpty() {
*this = MakeEmpty();
}
void set(float left, float top, float right, float bottom) {
fLeft = left;
fTop = top;
fRight = right;
fBottom = bottom;
}
void setLTRB(float left, float top, float right, float bottom) {
this->set(left, top, right, bottom);
}
void iset(int left, int top, int right, int bottom) {
fLeft = (float)(left);
fTop = (float)(top);
fRight = (float)(right);
fBottom = (float)(bottom);
}
void isetWH(int width, int height) {
fLeft = fTop = 0;
fRight = (float)(width);
fBottom = (float)(height);
}
void setXYWH(float x, float y, float width, float height) {
fLeft = x;
fTop = y;
fRight = x + width;
fBottom = y + height;
}
void setWH(float width, float height) {
fLeft = 0;
fTop = 0;
fRight = width;
fBottom = height;
}
Rect makeOffset(float dx, float dy) const {
return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
}
Rect makeInset(float dx, float dy) const {
return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
}
Rect makeOutset(float dx, float dy) const {
return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
}
void offset(float dx, float dy) {
fLeft += dx;
fTop += dy;
fRight += dx;
fBottom += dy;
}
void offsetTo(float newX, float newY) {
fRight += newX - fLeft;
fBottom += newY - fTop;
fLeft = newX;
fTop = newY;
}
void inset(float dx, float dy) {
fLeft += dx;
fTop += dy;
fRight -= dx;
fBottom -= dy;
}
void outset(float dx, float dy) {
this->inset(-dx, -dy);
}
bool intersect(const Rect& r);
bool intersect(float left, float top, float right, float bottom);
bool intersect(const Rect& a, const Rect& b);
private:
static bool Intersects(float al, float at, float ar, float ab, float bl, float bt, float br, float bb) {
float L = std::max(al, bl);
float R = std::min(ar, br);
float T = std::max(at, bt);
float B = std::min(ab, bb);
return L < R && T < B;
}
public:
bool intersects(float left, float top, float right, float bottom) const {
return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
}
bool intersects(const Rect& r) const {
return Intersects(fLeft, fTop, fRight, fBottom, r.fLeft, r.fTop, r.fRight, r.fBottom);
}
static bool Intersects(const Rect& a, const Rect& b) {
return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
}
void join(float left, float top, float right, float bottom);
void join(const Rect& r) {
this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
}
void joinNonEmptyArg(const Rect& r) {
MNN_ASSERT(!r.isEmpty());
// if we are empty, just assign
if (fLeft >= fRight || fTop >= fBottom) {
*this = r;
} else {
this->joinPossiblyEmptyRect(r);
}
}
void joinPossiblyEmptyRect(const Rect& r) {
fLeft = std::min(fLeft, r.left());
fTop = std::min(fTop, r.top());
fRight = std::max(fRight, r.right());
fBottom = std::max(fBottom, r.bottom());
}
bool contains(float x, float y) const {
return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
}
void sort() {
using std::swap;
if (fLeft > fRight) {
swap(fLeft, fRight);
}
if (fTop > fBottom) {
swap(fTop, fBottom);
}
}
Rect makeSorted() const {
return MakeLTRB(std::min(fLeft, fRight), std::min(fTop, fBottom), std::max(fLeft, fRight),
std::max(fTop, fBottom));
}
const float* asScalars() const {
return &fLeft;
}
};
} // namespace CV
} // namespace MNN
#endif
MNN::CV::Rect::makeOutset
Rect makeOutset(float dx, float dy) const
Definition: Rect.h:387
MNN::CV::Rect::MakeXYWH
static constexpr Rect MakeXYWH(float x, float y, float w, float h)
Definition: Rect.h:131
MNN::CV::Rect::contains
bool contains(float x, float y) const
Definition: Rect.h:601
MNN::CV::Rect::join
void join(const Rect &r)
Definition: Rect.h:558
MNN::CV::Rect::width
float width() const
Definition: Rect.h:216
MNN::CV::Rect::MakeWH
static constexpr Rect MakeWH(float w, float h)
Definition: Rect.h:89
MNN::CV::Rect::fTop
float fTop
smaller y-axis bounds
Definition: Rect.h:56
MNN::CV::Rect::fBottom
float fBottom
larger y-axis bounds
Definition: Rect.h:58
MNN_ASSERT
#define MNN_ASSERT(x)
Definition: MNNDefine.h:41
MNN::CV::Rect
Definition: Rect.h:54
MNN::CV::Rect::Intersects
static bool Intersects(const Rect &a, const Rect &b)
Definition: Rect.h:532
MNN::CV::Rect::isetWH
void isetWH(int width, int height)
Definition: Rect.h:312
MNN::CV::Rect::fRight
float fRight
larger x-axis bounds
Definition: Rect.h:57
MNN::CV::Rect::offset
void offset(float dx, float dy)
Definition: Rect.h:401
MNN::CV::Rect::fLeft
float fLeft
smaller x-axis bounds
Definition: Rect.h:55
MNN::CV::Rect::isSorted
bool isSorted() const
Definition: Rect.h:153
MNN::CV::Rect::iset
void iset(int left, int top, int right, int bottom)
Definition: Rect.h:298
MNN::CV::Point::set
void set(float x, float y)
Definition: Rect.h:41
MNN::CV::Rect::joinNonEmptyArg
void joinNonEmptyArg(const Rect &r)
Definition: Rect.h:571
MNN::CV::Rect::MakeIWH
static Rect MakeIWH(int w, int h)
Definition: Rect.h:103
MNN::CV::Rect::isEmpty
bool isEmpty() const
Definition: Rect.h:141
MNN::CV::Rect::y
float y() const
Definition: Rect.h:171
MNN::CV::Point::fY
float fY
Definition: Rect.h:39
MNN::CV::Rect::sort
void sort()
Definition: Rect.h:609
MNN::CV::Rect::inset
void inset(float dx, float dy)
Definition: Rect.h:431
MNN::CV::Rect::set
void set(float left, float top, float right, float bottom)
Definition: Rect.h:268
MNN::CV::Rect::offsetTo
void offsetTo(float newX, float newY)
Definition: Rect.h:414
MNN::CV::Point::fX
float fX
Definition: Rect.h:38
MNN::CV::Rect::makeOffset
Rect makeOffset(float dx, float dy) const
Definition: Rect.h:357
MNN::CV::Rect::joinPossiblyEmptyRect
void joinPossiblyEmptyRect(const Rect &r)
Definition: Rect.h:587
MNN::CV::Rect::setWH
void setWH(float width, float height)
Definition: Rect.h:339
MNN::CV::Rect::top
float top() const
Definition: Rect.h:189
MNN_PUBLIC
#define MNN_PUBLIC
Definition: MNNDefine.h:53
MNN::CV::Rect::asScalars
const float * asScalars() const
Definition: Rect.h:636
MNN::CV::Rect::outset
void outset(float dx, float dy)
Definition: Rect.h:448
MNN::CV::Rect::MakeEmpty
static constexpr Rect MakeEmpty()
Definition: Rect.h:67
MNN::CV::Rect::MakeLTRB
static constexpr Rect MakeLTRB(float l, float t, float r, float b)
Definition: Rect.h:118
MNN
Definition: AutoTime.hpp:16
MNN::CV::Rect::setEmpty
void setEmpty()
Definition: Rect.h:255
MNN::CV::Rect::height
float height() const
Definition: Rect.h:225
MNN::CV::Rect::bottom
float bottom() const
Definition: Rect.h:207
MNN::CV::Rect::x
float x() const
Definition: Rect.h:162
MNN::CV::Point
Definition: Rect.h:37
MNN::CV::Rect::setXYWH
void setXYWH(float x, float y, float width, float height)
Definition: Rect.h:326
MNN::CV::Rect::setLTRB
void setLTRB(float left, float top, float right, float bottom)
Definition: Rect.h:284
MNN::CV::Rect::makeSorted
Rect makeSorted() const
Definition: Rect.h:626
MNN::CV::Rect::intersects
bool intersects(const Rect &r) const
Definition: Rect.h:521
MNN::CV::Rect::right
float right() const
Definition: Rect.h:198
MNN::CV::Rect::intersects
bool intersects(float left, float top, float right, float bottom) const
Definition: Rect.h:511
MNN::CV::Rect::centerX
float centerX() const
Definition: Rect.h:234
MNN::CV::Rect::centerY
float centerY() const
Definition: Rect.h:244
MNN::CV::Rect::makeInset
Rect makeInset(float dx, float dy) const
Definition: Rect.h:372
MNNDefine.h
MNN::CV::Rect::left
float left() const
Definition: Rect.h:180