Skip to content

Commit d8c70e2

Browse files
committed
Merge pull request opencv#9286 from alalek:rect_size_empty
2 parents 03bf0a8 + 321c0ec commit d8c70e2

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

modules/core/include/opencv2/core/types.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ template<typename _Tp> class Size_
301301
Size_& operator = (const Size_& sz);
302302
//! the area (width*height)
303303
_Tp area() const;
304+
//! true if empty
305+
bool empty() const;
304306

305307
//! conversion of another data type.
306308
template<typename _Tp2> operator Size_<_Tp2>() const;
@@ -400,6 +402,8 @@ template<typename _Tp> class Rect_
400402
Size_<_Tp> size() const;
401403
//! area (width*height) of the rectangle
402404
_Tp area() const;
405+
//! true if empty
406+
bool empty() const;
403407

404408
//! conversion to another data type
405409
template<typename _Tp2> operator Rect_<_Tp2>() const;
@@ -1599,6 +1603,13 @@ _Tp Size_<_Tp>::area() const
15991603
return result;
16001604
}
16011605

1606+
template<typename _Tp> inline
1607+
bool Size_<_Tp>::empty() const
1608+
{
1609+
return width <= 0 || height <= 0;
1610+
}
1611+
1612+
16021613
template<typename _Tp> static inline
16031614
Size_<_Tp>& operator *= (Size_<_Tp>& a, _Tp b)
16041615
{
@@ -1741,6 +1752,12 @@ _Tp Rect_<_Tp>::area() const
17411752
return result;
17421753
}
17431754

1755+
template<typename _Tp> inline
1756+
bool Rect_<_Tp>::empty() const
1757+
{
1758+
return width <= 0 || height <= 0;
1759+
}
1760+
17441761
template<typename _Tp> template<typename _Tp2> inline
17451762
Rect_<_Tp>::operator Rect_<_Tp2>() const
17461763
{
@@ -1803,10 +1820,10 @@ Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
18031820
template<typename _Tp> static inline
18041821
Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
18051822
{
1806-
if (!a.area()) {
1823+
if (a.empty()) {
18071824
a = b;
18081825
}
1809-
else if (b.area()) {
1826+
else if (!b.empty()) {
18101827
_Tp x1 = std::min(a.x, b.x);
18111828
_Tp y1 = std::min(a.y, b.y);
18121829
a.width = std::max(a.x + a.width, b.x + b.width) - x1;

modules/core/misc/java/src/java/core+Rect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public double area() {
6565
return width * height;
6666
}
6767

68+
public boolean empty() {
69+
return width <= 0 || height <= 0;
70+
}
71+
6872
public boolean contains(Point p) {
6973
return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
7074
}

modules/core/misc/java/src/java/core+Rect2d.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public double area() {
6565
return width * height;
6666
}
6767

68+
public boolean empty() {
69+
return width <= 0 || height <= 0;
70+
}
71+
6872
public boolean contains(Point p) {
6973
return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
7074
}

modules/core/misc/java/src/java/core+Size.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public double area() {
3737
return width * height;
3838
}
3939

40+
public boolean empty() {
41+
return width <= 0 || height <= 0;
42+
}
43+
4044
public Size clone() {
4145
return new Size(width, height);
4246
}

0 commit comments

Comments
 (0)