MNN  1.0
Rect.h
浏览该文件的文档.
1 //
2 // Rect.h
3 // MNN
4 //
5 // Modified by jiangxiaotang on 2018/09/19.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8 
9 /*
10  * Copyright 2006 The Android Open Source Project
11  *
12  * Use of this source code is governed by a BSD-style license that can be
13  * found in the LICENSE file.
14  */
15 
16 /* Generated by tools/bookmaker from include/core/Rect.h and docs/SkRect_Reference.bmh
17  on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
18  https://skia.org/user/api/SkRect_Reference
19 
20  You may edit either file directly. Structural changes to public interfaces require
21  editing both files. After editing docs/SkRect_Reference.bmh, run:
22  bookmaker -b docs -i include/core/Rect.h -p
23  to create an updated version of this file.
24  */
25 
26 #ifndef SkRect_DEFINED
27 #define SkRect_DEFINED
28 
29 #include <math.h>
30 #include <algorithm>
31 #include <utility>
32 #include "MNNDefine.h"
33 
34 namespace MNN {
35 namespace CV {
36 
37 struct Point {
38  float fX;
39  float fY;
40 
41  void set(float x, float y) {
42  fX = x;
43  fY = y;
44  }
45 };
46 
54 struct MNN_PUBLIC Rect {
55  float fLeft;
56  float fTop;
57  float fRight;
58  float fBottom;
59 
67  static constexpr Rect MakeEmpty() {
68  return Rect{0, 0, 0, 0};
69  }
70 
71 #ifdef SK_SUPPORT_LEGACY_RECTMAKELARGEST
72 
74  static Rect MakeLargest() {
75  return {SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax};
76  }
77 #endif
78 
89  static constexpr Rect MakeWH(float w, float h) {
90  return Rect{0, 0, w, h};
91  }
92 
103  static Rect MakeIWH(int w, int h) {
104  Rect r;
105  r.set(0, 0, (float)(w), (float)(h));
106  return r;
107  }
108 
118  static constexpr Rect MakeLTRB(float l, float t, float r, float b) {
119  return Rect{l, t, r, b};
120  }
121 
131  static constexpr Rect MakeXYWH(float x, float y, float w, float h) {
132  return Rect{x, y, x + w, y + h};
133  }
134 
141  bool isEmpty() const {
142  // We write it as the NOT of a non-empty rect, so we will return true if any values
143  // are NaN.
144  return !(fLeft < fRight && fTop < fBottom);
145  }
146 
153  bool isSorted() const {
154  return fLeft <= fRight && fTop <= fBottom;
155  }
156 
162  float x() const {
163  return fLeft;
164  }
165 
171  float y() const {
172  return fTop;
173  }
174 
180  float left() const {
181  return fLeft;
182  }
183 
189  float top() const {
190  return fTop;
191  }
192 
198  float right() const {
199  return fRight;
200  }
201 
207  float bottom() const {
208  return fBottom;
209  }
210 
216  float width() const {
217  return fRight - fLeft;
218  }
219 
225  float height() const {
226  return fBottom - fTop;
227  }
228 
234  float centerX() const {
235  // don't use floatHalf(fLeft + fBottom) as that might overflow before the 0.5
236  return 0.5f * (fLeft) + 0.5f * (fRight);
237  }
238 
244  float centerY() const {
245  // don't use floatHalf(fTop + fBottom) as that might overflow before the 0.5
246  return 0.5f * (fTop) + 0.5f * (fBottom);
247  }
248 
255  void setEmpty() {
256  *this = MakeEmpty();
257  }
258 
268  void set(float left, float top, float right, float bottom) {
269  fLeft = left;
270  fTop = top;
271  fRight = right;
272  fBottom = bottom;
273  }
274 
284  void setLTRB(float left, float top, float right, float bottom) {
285  this->set(left, top, right, bottom);
286  }
287 
298  void iset(int left, int top, int right, int bottom) {
299  fLeft = (float)(left);
300  fTop = (float)(top);
301  fRight = (float)(right);
302  fBottom = (float)(bottom);
303  }
304 
312  void isetWH(int width, int height) {
313  fLeft = fTop = 0;
314  fRight = (float)(width);
315  fBottom = (float)(height);
316  }
317 
326  void setXYWH(float x, float y, float width, float height) {
327  fLeft = x;
328  fTop = y;
329  fRight = x + width;
330  fBottom = y + height;
331  }
332 
339  void setWH(float width, float height) {
340  fLeft = 0;
341  fTop = 0;
342  fRight = width;
343  fBottom = height;
344  }
345 
357  Rect makeOffset(float dx, float dy) const {
358  return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
359  }
360 
372  Rect makeInset(float dx, float dy) const {
373  return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
374  }
375 
387  Rect makeOutset(float dx, float dy) const {
388  return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
389  }
390 
401  void offset(float dx, float dy) {
402  fLeft += dx;
403  fTop += dy;
404  fRight += dx;
405  fBottom += dy;
406  }
407 
414  void offsetTo(float newX, float newY) {
415  fRight += newX - fLeft;
416  fBottom += newY - fTop;
417  fLeft = newX;
418  fTop = newY;
419  }
420 
431  void inset(float dx, float dy) {
432  fLeft += dx;
433  fTop += dy;
434  fRight -= dx;
435  fBottom -= dy;
436  }
437 
448  void outset(float dx, float dy) {
449  this->inset(-dx, -dy);
450  }
451 
460  bool intersect(const Rect& r);
461 
476  bool intersect(float left, float top, float right, float bottom);
477 
487  bool intersect(const Rect& a, const Rect& b);
488 
489 private:
490  static bool Intersects(float al, float at, float ar, float ab, float bl, float bt, float br, float bb) {
491  float L = std::max(al, bl);
492  float R = std::min(ar, br);
493  float T = std::max(at, bt);
494  float B = std::min(ab, bb);
495  return L < R && T < B;
496  }
497 
498 public:
511  bool intersects(float left, float top, float right, float bottom) const {
512  return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
513  }
514 
521  bool intersects(const Rect& r) const {
522  return Intersects(fLeft, fTop, fRight, fBottom, r.fLeft, r.fTop, r.fRight, r.fBottom);
523  }
524 
532  static bool Intersects(const Rect& a, const Rect& b) {
533  return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
534  }
535 
549  void join(float left, float top, float right, float bottom);
550 
558  void join(const Rect& r) {
559  this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
560  }
561 
571  void joinNonEmptyArg(const Rect& r) {
572  MNN_ASSERT(!r.isEmpty());
573  // if we are empty, just assign
574  if (fLeft >= fRight || fTop >= fBottom) {
575  *this = r;
576  } else {
577  this->joinPossiblyEmptyRect(r);
578  }
579  }
580 
587  void joinPossiblyEmptyRect(const Rect& r) {
588  fLeft = std::min(fLeft, r.left());
589  fTop = std::min(fTop, r.top());
590  fRight = std::max(fRight, r.right());
591  fBottom = std::max(fBottom, r.bottom());
592  }
593 
601  bool contains(float x, float y) const {
602  return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
603  }
604 
609  void sort() {
610  using std::swap;
611  if (fLeft > fRight) {
612  swap(fLeft, fRight);
613  }
614 
615  if (fTop > fBottom) {
616  swap(fTop, fBottom);
617  }
618  }
619 
626  Rect makeSorted() const {
627  return MakeLTRB(std::min(fLeft, fRight), std::min(fTop, fBottom), std::max(fLeft, fRight),
628  std::max(fTop, fBottom));
629  }
630 
636  const float* asScalars() const {
637  return &fLeft;
638  }
639 };
640 
641 } // namespace CV
642 } // namespace MNN
643 #endif
Rect makeOutset(float dx, float dy) const
Definition: Rect.h:387
static constexpr Rect MakeXYWH(float x, float y, float w, float h)
Definition: Rect.h:131
bool contains(float x, float y) const
Definition: Rect.h:601
void join(const Rect &r)
Definition: Rect.h:558
float width() const
Definition: Rect.h:216
static constexpr Rect MakeWH(float w, float h)
Definition: Rect.h:89
float fTop
smaller y-axis bounds
Definition: Rect.h:56
float fBottom
larger y-axis bounds
Definition: Rect.h:58
#define MNN_ASSERT(x)
Definition: MNNDefine.h:41
Definition: Rect.h:54
static bool Intersects(const Rect &a, const Rect &b)
Definition: Rect.h:532
void isetWH(int width, int height)
Definition: Rect.h:312
float fRight
larger x-axis bounds
Definition: Rect.h:57
void offset(float dx, float dy)
Definition: Rect.h:401
float fLeft
smaller x-axis bounds
Definition: Rect.h:55
bool isSorted() const
Definition: Rect.h:153
void iset(int left, int top, int right, int bottom)
Definition: Rect.h:298
void set(float x, float y)
Definition: Rect.h:41
void joinNonEmptyArg(const Rect &r)
Definition: Rect.h:571
static Rect MakeIWH(int w, int h)
Definition: Rect.h:103
bool isEmpty() const
Definition: Rect.h:141
float y() const
Definition: Rect.h:171
float fY
Definition: Rect.h:39
void sort()
Definition: Rect.h:609
void inset(float dx, float dy)
Definition: Rect.h:431
void set(float left, float top, float right, float bottom)
Definition: Rect.h:268
void offsetTo(float newX, float newY)
Definition: Rect.h:414
float fX
Definition: Rect.h:38
Rect makeOffset(float dx, float dy) const
Definition: Rect.h:357
void joinPossiblyEmptyRect(const Rect &r)
Definition: Rect.h:587
void setWH(float width, float height)
Definition: Rect.h:339
float top() const
Definition: Rect.h:189
#define MNN_PUBLIC
Definition: MNNDefine.h:53
const float * asScalars() const
Definition: Rect.h:636
void outset(float dx, float dy)
Definition: Rect.h:448
static constexpr Rect MakeEmpty()
Definition: Rect.h:67
static constexpr Rect MakeLTRB(float l, float t, float r, float b)
Definition: Rect.h:118
Definition: AutoTime.hpp:16
void setEmpty()
Definition: Rect.h:255
float height() const
Definition: Rect.h:225
float bottom() const
Definition: Rect.h:207
float x() const
Definition: Rect.h:162
Definition: Rect.h:37
void setXYWH(float x, float y, float width, float height)
Definition: Rect.h:326
void setLTRB(float left, float top, float right, float bottom)
Definition: Rect.h:284
Rect makeSorted() const
Definition: Rect.h:626
bool intersects(const Rect &r) const
Definition: Rect.h:521
float right() const
Definition: Rect.h:198
bool intersects(float left, float top, float right, float bottom) const
Definition: Rect.h:511
float centerX() const
Definition: Rect.h:234
float centerY() const
Definition: Rect.h:244
Rect makeInset(float dx, float dy) const
Definition: Rect.h:372
float left() const
Definition: Rect.h:180