doc/API/html/_rect_8h_source.html
| 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
40
41void set(float x, float y) {
42fX = x;
43fY = y;
44 }
45 };
46
54 struct MNN_PUBLIC Rect {
59
67static constexpr Rect MakeEmpty() {
68return Rect{0, 0, 0, 0};
69 }
70
71 #ifdef SK_SUPPORT_LEGACY_RECTMAKELARGEST
72
74static Rect MakeLargest() {
75return {SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax};
76 }
77 #endif
78
89static constexpr Rect MakeWH(float w, float h) {
90return Rect{0, 0, w, h};
91 }
92
103static Rect MakeIWH(int w, int h) {
104Rect r;
105 r.set(0, 0, (float)(w), (float)(h));
106return r;
107 }
108
118static constexpr Rect MakeLTRB(float l, float t, float r, float b) {
119return Rect{l, t, r, b};
120 }
121
131static constexpr Rect MakeXYWH(float x, float y, float w, float h) {
132return Rect{x, y, x + w, y + h};
133 }
134
142// We write it as the NOT of a non-empty rect, so we will return true if any values
143// are NaN.
144return !(fLeft < fRight && fTop < fBottom);
145 }
146
154return fLeft <= fRight && fTop <= fBottom;
155 }
156
163return fLeft;
164 }
165
172return fTop;
173 }
174
181return fLeft;
182 }
183
190return fTop;
191 }
192
199return fRight;
200 }
201
208return fBottom;
209 }
210
217return fRight - fLeft;
218 }
219
226return fBottom - fTop;
227 }
228
235// don't use floatHalf(fLeft + fBottom) as that might overflow before the 0.5
236return 0.5f * (fLeft) + 0.5f * (fRight);
237 }
238
245// don't use floatHalf(fTop + fBottom) as that might overflow before the 0.5
246return 0.5f * (fTop) + 0.5f * (fBottom);
247 }
248
256 *this = MakeEmpty();
257 }
258
268void set(float left, float top, float right, float bottom) {
269 fLeft = left;
270 fTop = top;
271 fRight = right;
272 fBottom = bottom;
273 }
274
284void setLTRB(float left, float top, float right, float bottom) {
285 this->set(left, top, right, bottom);
286 }
287
298void 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
312void isetWH(int width, int height) {
313 fLeft = fTop = 0;
314 fRight = (float)(width);
315 fBottom = (float)(height);
316 }
317
326void 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
339void setWH(float width, float height) {
340 fLeft = 0;
341 fTop = 0;
342 fRight = width;
343 fBottom = height;
344 }
345
357Rect makeOffset(float dx, float dy) const {
358return MakeLTRB(fLeft + dx, fTop + dy, fRight + dx, fBottom + dy);
359 }
360
372Rect makeInset(float dx, float dy) const {
373return MakeLTRB(fLeft + dx, fTop + dy, fRight - dx, fBottom - dy);
374 }
375
387Rect makeOutset(float dx, float dy) const {
388return MakeLTRB(fLeft - dx, fTop - dy, fRight + dx, fBottom + dy);
389 }
390
401void offset(float dx, float dy) {
402 fLeft += dx;
403 fTop += dy;
404 fRight += dx;
405 fBottom += dy;
406 }
407
414void offsetTo(float newX, float newY) {
415 fRight += newX - fLeft;
416 fBottom += newY - fTop;
417 fLeft = newX;
418 fTop = newY;
419 }
420
431void inset(float dx, float dy) {
432 fLeft += dx;
433 fTop += dy;
434 fRight -= dx;
435 fBottom -= dy;
436 }
437
448void outset(float dx, float dy) {
449 this->inset(-dx, -dy);
450 }
451
460bool intersect(const Rect& r);
461
476bool intersect(float left, float top, float right, float bottom);
477
487bool intersect(const Rect& a, const Rect& b);
488
489 private:
490static bool Intersects(float al, float at, float ar, float ab, float bl, float bt, float br, float bb) {
491float L = std::max(al, bl);
492float R = std::min(ar, br);
493float T = std::max(at, bt);
494float B = std::min(ab, bb);
495return L < R && T < B;
496 }
497
498 public:
511bool intersects(float left, float top, float right, float bottom) const {
512return Intersects(fLeft, fTop, fRight, fBottom, left, top, right, bottom);
513 }
514
521bool intersects(const Rect& r) const {
522return Intersects(fLeft, fTop, fRight, fBottom, r.fLeft, r.fTop, r.fRight, r.fBottom);
523 }
524
532static bool Intersects(const Rect& a, const Rect& b) {
533return Intersects(a.fLeft, a.fTop, a.fRight, a.fBottom, b.fLeft, b.fTop, b.fRight, b.fBottom);
534 }
535
549void join(float left, float top, float right, float bottom);
550
559 this->join(r.fLeft, r.fTop, r.fRight, r.fBottom);
560 }
561
571void joinNonEmptyArg(const Rect& r) {
572MNN_ASSERT(!r.isEmpty());
573// if we are empty, just assign
574if (fLeft >= fRight || fTop >= fBottom) {
575 *this = r;
576 } else {
577 this->joinPossiblyEmptyRect(r);
578 }
579 }
580
587void 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
601bool contains(float x, float y) const {
602return x >= fLeft && x < fRight && y >= fTop && y < fBottom;
603 }
604
610using std::swap;
611if (fLeft > fRight) {
612 swap(fLeft, fRight);
613 }
614
615if (fTop > fBottom) {
616 swap(fTop, fBottom);
617 }
618 }
619
626Rect makeSorted() const {
627return MakeLTRB(std::min(fLeft, fRight), std::min(fTop, fBottom), std::max(fLeft, fRight),
628 std::max(fTop, fBottom));
629 }
630
636const float* asScalars() const {
637return &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
MNN::CV::Rect::joinNonEmptyArg
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
MNN::CV::Rect::joinPossiblyEmptyRect
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