Skip to content

Commit

Permalink
Merge pull request #980 from shimat/fix_Mat
Browse files Browse the repository at this point in the history
fix Mat right, bottom and Contains
  • Loading branch information
shimat authored Jul 1, 2020
2 parents 40c9e6c + 7ed88fb commit 206eba0
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/macos10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: actions/cache@v1
with:
path: opencv_macos/
key: opencv-4.3.0-macos-rev3
key: opencv-4.3.0-macos-rev4

- name: Build OpenCV
if: steps.opencv-cache.outputs.cache-hit != 'true'
Expand Down
8 changes: 4 additions & 4 deletions src/OpenCvSharp/Modules/core/Struct/Rect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public int Top
#endif
public int Bottom
{
get { return Y + Height - 1; }
get { return Y + Height; }
}

#if LANG_JP
Expand Down Expand Up @@ -333,7 +333,7 @@ public int Left
#endif
public int Right
{
get { return X + Width - 1; }
get { return X + Width; }
}

#if LANG_JP
Expand Down Expand Up @@ -399,7 +399,7 @@ public Point TopLeft
#endif
public Point BottomRight
{
get { return new Point(X + Width - 1, Y + Height - 1); }
get { return new Point(X + Width, Y + Height); }
}

#endregion
Expand All @@ -423,7 +423,7 @@ public Point BottomRight
#endif
public readonly bool Contains(int x, int y)
{
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
}

#if LANG_JP
Expand Down
8 changes: 4 additions & 4 deletions src/OpenCvSharp/Modules/core/Struct/Rect2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public double Top
#endif
public double Bottom
{
get { return Y + Height - 1; }
get { return Y + Height; }
}
#if LANG_JP
/// <summary>
Expand All @@ -329,7 +329,7 @@ public double Left
#endif
public double Right
{
get { return X + Width - 1; }
get { return X + Width; }
}

#if LANG_JP
Expand Down Expand Up @@ -393,7 +393,7 @@ public Point2d TopLeft
#endif
public Point2d BottomRight
{
get { return new Point2d(X + Width - 1, Y + Height - 1); }
get { return new Point2d(X + Width, Y + Height); }
}
#endregion

Expand Down Expand Up @@ -425,7 +425,7 @@ public readonly Rect ToRect()
#endif
public readonly bool Contains(double x, double y)
{
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
}

#if LANG_JP
Expand Down
8 changes: 4 additions & 4 deletions src/OpenCvSharp/Modules/core/Struct/Rect2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public float Top
#endif
public float Bottom
{
get { return Y + Height - 1; }
get { return Y + Height; }
}

#if LANG_JP
Expand Down Expand Up @@ -327,7 +327,7 @@ public float Left
#endif
public float Right
{
get { return X + Width - 1; }
get { return X + Width; }
}

#if LANG_JP
Expand Down Expand Up @@ -393,7 +393,7 @@ public Point2f TopLeft
#endif
public Point2f BottomRight
{
get { return new Point2f(X + Width - 1, Y + Height - 1); }
get { return new Point2f(X + Width, Y + Height); }
}
#endregion

Expand All @@ -416,7 +416,7 @@ public Point2f BottomRight
#endif
public readonly bool Contains(float x, float y)
{
return (X <= x && Y <= y && X + Width - 1 > x && Y + Height - 1 > y);
return (X <= x && Y <= y && X + Width > x && Y + Height > y);
}

#if LANG_JP
Expand Down
137 changes: 137 additions & 0 deletions test/OpenCvSharp.Tests/core/Rect2dTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using Xunit;
using Xunit.Abstractions;

namespace OpenCvSharp.Tests
{
public class Rect2dTest
{
private readonly ITestOutputHelper testOutputHelper;

public Rect2dTest(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}

[Fact]
public void TopLeft()
{
var rect = new Rect2d(10, 10, 100, 100);

Assert.Equal(10, rect.Top);
Assert.Equal(10, rect.Left);
Assert.Equal(new Point2d(10, 10), rect.TopLeft);
}

[Fact]
public void BottomRight()
{
var rect = new Rect2d(10, 10, 100, 100);

Assert.Equal(110, rect.Bottom);
Assert.Equal(110, rect.Right);
Assert.Equal(new Point2d(110, 110), rect.BottomRight);
}

[Fact]
public void Contains()
{
var rect = new Rect2d(new Point2d(0, 0), new Size2d(3,3));

// OpenCV typically assumes that the top and left boundary of the rectangle are inclusive,
// while the right and bottom boundaries are not. https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=rect

Assert.False(rect.Contains(0, -1));
Assert.False(rect.Contains(-1, 0));
Assert.False(rect.Contains(-1, -1));

Assert.True(rect.Contains(0, 0));
Assert.True(rect.Contains(0, 1));
Assert.True(rect.Contains(1, 0));
Assert.True(rect.Contains(1, 1));

Assert.True(rect.Contains(2, 0));
Assert.True(rect.Contains(2, 1));
Assert.True(rect.Contains(2, 2));
Assert.True(rect.Contains(0, 2));
Assert.True(rect.Contains(1, 2));
Assert.True(rect.Contains(2, 2));

Assert.False(rect.Contains(0, 3));
Assert.False(rect.Contains(1, 3));
Assert.False(rect.Contains(2, 3));
Assert.False(rect.Contains(3, 3));
Assert.False(rect.Contains(3, 0));
Assert.False(rect.Contains(3, 1));
Assert.False(rect.Contains(3, 2));
Assert.False(rect.Contains(3, 3));
}

// https://github.com/shimat/opencvsharp/issues/74
// https://github.com/shimat/opencvsharp/issues/75
[Fact]
public void ContainsTopLeft()
{
var rect = new Rect2d(10, 10, 100, 100);

Assert.True(rect.Contains(rect.TopLeft));
Assert.True(rect.Contains(rect.Left, rect.Top));
}

[Fact]
public void DoNotContainsBottomRight()
{
var rect = new Rect2d(10, 10, 100, 100);

Assert.False(rect.Contains(rect.BottomRight));
Assert.False(rect.Contains(rect.Right, rect.Bottom));
}

[Fact]
public void ContainsRect()
{
var rect = new Rect2d(10, 10, 100, 100);

Assert.True(rect.Contains(rect));
}

[Fact]
public void IntersectsWith()
{
var rect1 = new Rect2d(0, 0, 100, 100);
var rect2 = new Rect2d(0, 0, 100, 100);
Assert.True(rect1.IntersectsWith(rect2));

rect2 = new Rect2d(50, 0, 100, 100);
Assert.True(rect1.IntersectsWith(rect2));

rect2 = new Rect2d(100, 0, 100, 100);
Assert.False(rect1.IntersectsWith(rect2));
}

[Fact]
public void Intersect()
{
var rect1 = new Rect2d(0, 0, 100, 100);
var rect2 = new Rect2d(0, 0, 100, 100);

var intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2d(0, 0, 100, 100), intersect);

rect2 = new Rect2d(50, 0, 100, 100);
intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2d(50, 0, 50, 100), intersect);

rect2 = new Rect2d(100, 0, 100, 100);
intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2d(100, 0, 0, 100), intersect);
}

[Fact]
public void FromLTRB()
{
var rect = Rect2d.FromLTRB(1, 2, 3, 4);

Assert.Equal(new Rect2d(1, 2, 3, 3), rect);
}
}
}
137 changes: 137 additions & 0 deletions test/OpenCvSharp.Tests/core/Rect2fTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using Xunit;
using Xunit.Abstractions;

namespace OpenCvSharp.Tests
{
public class Rect2fTest
{
private readonly ITestOutputHelper testOutputHelper;

public Rect2fTest(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}

[Fact]
public void TopLeft()
{
var rect = new Rect2f(10, 10, 100, 100);

Assert.Equal(10, rect.Top);
Assert.Equal(10, rect.Left);
Assert.Equal(new Point2f(10, 10), rect.TopLeft);
}

[Fact]
public void BottomRight()
{
var rect = new Rect2f(10, 10, 100, 100);

Assert.Equal(110, rect.Bottom);
Assert.Equal(110, rect.Right);
Assert.Equal(new Point2f(110, 110), rect.BottomRight);
}

[Fact]
public void Contains()
{
var rect = new Rect2f(new Point2f(0, 0), new Size2f(3,3));

// OpenCV typically assumes that the top and left boundary of the rectangle are inclusive,
// while the right and bottom boundaries are not. https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=rect

Assert.False(rect.Contains(0, -1));
Assert.False(rect.Contains(-1, 0));
Assert.False(rect.Contains(-1, -1));

Assert.True(rect.Contains(0, 0));
Assert.True(rect.Contains(0, 1));
Assert.True(rect.Contains(1, 0));
Assert.True(rect.Contains(1, 1));

Assert.True(rect.Contains(2, 0));
Assert.True(rect.Contains(2, 1));
Assert.True(rect.Contains(2, 2));
Assert.True(rect.Contains(0, 2));
Assert.True(rect.Contains(1, 2));
Assert.True(rect.Contains(2, 2));

Assert.False(rect.Contains(0, 3));
Assert.False(rect.Contains(1, 3));
Assert.False(rect.Contains(2, 3));
Assert.False(rect.Contains(3, 3));
Assert.False(rect.Contains(3, 0));
Assert.False(rect.Contains(3, 1));
Assert.False(rect.Contains(3, 2));
Assert.False(rect.Contains(3, 3));
}

// https://github.com/shimat/opencvsharp/issues/74
// https://github.com/shimat/opencvsharp/issues/75
[Fact]
public void ContainsTopLeft()
{
var rect = new Rect2f(10, 10, 100, 100);

Assert.True(rect.Contains(rect.TopLeft));
Assert.True(rect.Contains(rect.Left, rect.Top));
}

[Fact]
public void DoNotContainsBottomRight()
{
var rect = new Rect2f(10, 10, 100, 100);

Assert.False(rect.Contains(rect.BottomRight));
Assert.False(rect.Contains(rect.Right, rect.Bottom));
}

[Fact]
public void ContainsRect()
{
var rect = new Rect2f(10, 10, 100, 100);

Assert.True(rect.Contains(rect));
}

[Fact]
public void IntersectsWith()
{
var rect1 = new Rect2f(0, 0, 100, 100);
var rect2 = new Rect2f(0, 0, 100, 100);
Assert.True(rect1.IntersectsWith(rect2));

rect2 = new Rect2f(50, 0, 100, 100);
Assert.True(rect1.IntersectsWith(rect2));

rect2 = new Rect2f(100, 0, 100, 100);
Assert.False(rect1.IntersectsWith(rect2));
}

[Fact]
public void Intersect()
{
var rect1 = new Rect2f(0, 0, 100, 100);
var rect2 = new Rect2f(0, 0, 100, 100);

var intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2f(0, 0, 100, 100), intersect);

rect2 = new Rect2f(50, 0, 100, 100);
intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2f(50, 0, 50, 100), intersect);

rect2 = new Rect2f(100, 0, 100, 100);
intersect = rect1.Intersect(rect2);
Assert.Equal(new Rect2f(100, 0, 0, 100), intersect);
}

[Fact]
public void FromLTRB()
{
var rect = Rect2f.FromLTRB(1, 2, 3, 4);

Assert.Equal(new Rect2f(1, 2, 3, 3), rect);
}
}
}
Loading

0 comments on commit 206eba0

Please sign in to comment.