Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

record struct Size #1613

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 8 additions & 80 deletions src/OpenCvSharp/Modules/core/Struct/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,29 @@ namespace OpenCvSharp;
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
public struct Size : IEquatable<Size>
public record struct Size(int Width, int Height)
{
/// <summary>
///
/// </summary>
public int Width;
public int Width = Width;

/// <summary>
///
/// </summary>
public int Height;

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size(int width, int height)
{
Width = width;
Height = height;
}

public int Height = Height;

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size(double width, double height)
: this((int)width, (int)height)
{
Width = (int)width;
Height = (int)height;
}

/// <summary>
/// Zero size
/// </summary>
public static readonly Size Zero;

#region Operators

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are equal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are equal; otherwise, false.</returns>
public static bool operator ==(Size lhs, Size rhs)
=> lhs.Equals(rhs);

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are unequal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are unequal; otherwise, false.</returns>
public static bool operator !=(Size lhs, Size rhs)
=> !lhs.Equals(rhs);


#pragma warning disable CA2225
/// <summary>
/// </summary>
/// <param name="size"></param>
Expand All @@ -80,41 +44,5 @@ public Size(double width, double height)
/// <param name="size"></param>
public static explicit operator Size(Size2f size)
=> new(size.Width, size.Height);

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size other)
{
return Width == other.Width && Height == other.Height;
}

/// <inheritdoc />
public override readonly bool Equals(object? obj)
{
return obj is Size other && Equals(other);
}

/// <inheritdoc />
public override readonly int GetHashCode()
{
#if DOTNET_FRAMEWORK || NETSTANDARD2_0
unchecked
{
return (Width * 397) ^ Height;
}
#else
return HashCode.Combine(Width, Height);
#endif
}

/// <inheritdoc />
public override readonly string ToString()
{
return $"(width:{Width} height:{Height})";
}
#endregion

#pragma warning restore CA2225
}
87 changes: 15 additions & 72 deletions src/OpenCvSharp/Modules/core/Struct/Size2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,19 @@ namespace OpenCvSharp;
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Size2d : IEquatable<Size2d>
public record struct Size2d(double Width, double Height)
{
/// <summary>
///
/// </summary>
public double Width;
public double Width = Width;

/// <summary>
///
/// </summary>
public double Height;

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size2d(float width, float height)
{
Width = width;
Height = height;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size2d(double width, double height)
{
Width = width;
Height = height;
}

#region Operators

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are equal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are equal; otherwise, false.</returns>
public static bool operator ==(Size2d lhs, Size2d rhs) => lhs.Equals(rhs);

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are unequal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are unequal; otherwise, false.</returns>
public static bool operator !=(Size2d lhs, Size2d rhs)
=> !lhs.Equals(rhs);

public double Height = Height;

#pragma warning disable CA2225
/// <summary>
/// </summary>
/// <param name="size"></param>
Expand All @@ -74,33 +34,16 @@ public Size2d(double width, double height)
/// <param name="size"></param>
public static implicit operator Size2d(Size2f size)
=> new(size.Width, size.Height);
#pragma warning restore CA2225

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size2d other) => Width.Equals(other.Width) && Height.Equals(other.Height);

/// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is Size2d other && Equals(other);

/// <inheritdoc />
public override readonly int GetHashCode()
{
#if DOTNET_FRAMEWORK || NETSTANDARD2_0
unchecked
{
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
}
#else
return HashCode.Combine(Width, Height);
#endif
}

/// <inheritdoc />
public override readonly string ToString() => $"(width:{Width} height:{Height})";

#endregion
/// <summary>
/// </summary>
/// <returns></returns>
public readonly Size ToSize() => new (Width, Height);

/// <summary>
/// </summary>
/// <returns></returns>
// ReSharper disable once InconsistentNaming
public readonly Size2f ToSize2f() => new (Width, Height);
}
77 changes: 14 additions & 63 deletions src/OpenCvSharp/Modules/core/Struct/Size2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,47 @@
[Serializable]
[StructLayout(LayoutKind.Sequential)]
// ReSharper disable once InconsistentNaming
public struct Size2f : IEquatable<Size2f>
public record struct Size2f(float Width, float Height)
{
/// <summary>
///
/// </summary>
public float Width;
public float Width = Width;

/// <summary>
///
/// </summary>
public float Height;

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size2f(float width, float height)
{
Width = width;
Height = height;
}

public float Height = Height;

/// <summary>
/// Constructor
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public Size2f(double width, double height)
: this((float)width, (float)height)
{
Width = (float) width;
Height = (float) height;
}

#region Operators

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are equal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are equal; otherwise, false.</returns>
public static bool operator ==(Size2f lhs, Size2f rhs) => lhs.Equals(rhs);

/// <summary>
/// Compares two CvPoint objects. The result specifies whether the members of each object are unequal.
/// </summary>
/// <param name="lhs">A Point to compare.</param>
/// <param name="rhs">A Point to compare.</param>
/// <returns>This operator returns true if the members of left and right are unequal; otherwise, false.</returns>
public static bool operator !=(Size2f lhs, Size2f rhs)
=> !lhs.Equals(rhs);

/// <summary>
/// </summary>
/// <param name="size"></param>
public static implicit operator Size2f(Size size)

Check warning on line 39 in src/OpenCvSharp/Modules/core/Struct/Size2f.cs

View workflow job for this annotation

GitHub Actions / build

Provide a method named 'ToSize2f' or 'FromSize' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 39 in src/OpenCvSharp/Modules/core/Struct/Size2f.cs

View workflow job for this annotation

GitHub Actions / build

Provide a method named 'ToSize2f' or 'FromSize' as an alternate for operator op_Implicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)
=> new(size.Width, size.Height);

/// <summary>
/// </summary>
/// <param name="size"></param>
public static explicit operator Size2f(Size2d size)

Check warning on line 45 in src/OpenCvSharp/Modules/core/Struct/Size2f.cs

View workflow job for this annotation

GitHub Actions / build

Provide a method named 'ToSize2f' or 'FromSize2d' as an alternate for operator op_Explicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)

Check warning on line 45 in src/OpenCvSharp/Modules/core/Struct/Size2f.cs

View workflow job for this annotation

GitHub Actions / build

Provide a method named 'ToSize2f' or 'FromSize2d' as an alternate for operator op_Explicit (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2225)
=> new(size.Width, size.Height);

/// <summary>
/// </summary>
/// <returns></returns>
public readonly Size ToSize() => new (Width, Height);

#endregion

#region Override

/// <inheritdoc />
public readonly bool Equals(Size2f other) => Width.Equals(other.Width) && Height.Equals(other.Height);

/// <inheritdoc />
public override readonly bool Equals(object? obj) => obj is Size2f other && Equals(other);

/// <inheritdoc />
public override readonly int GetHashCode()
{
#if DOTNET_FRAMEWORK || NETSTANDARD2_0
unchecked
{
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
}
#else
return HashCode.Combine(Width, Height);
#endif
}

/// <inheritdoc />
public override readonly string ToString() => $"(width:{Width} height:{Height})";

#endregion
/// <summary>
/// </summary>
/// <returns></returns>
public readonly Size2d ToSize2d() => new (Width, Height);
}
14 changes: 7 additions & 7 deletions src/OpenCvSharp/Modules/videoio/VideoWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public VideoWriter()
{
FileName = null;
Fps = -1;
FrameSize = Size.Zero;
FrameSize = default;
IsColor = true;
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new1(out ptr));
Expand All @@ -42,7 +42,7 @@ public VideoWriter(string fileName, FourCC fourcc, double fps, Size frameSize, b
FrameSize = frameSize;
IsColor = isColor;
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new2(fileName, (int)fourcc, fps, frameSize, isColor ? 1 : 0, out ptr));
NativeMethods.videoio_VideoWriter_new2(fileName, fourcc, fps, frameSize, isColor ? 1 : 0, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand All @@ -66,7 +66,7 @@ public VideoWriter(string fileName, VideoCaptureAPIs apiPreference, FourCC fourc
FrameSize = frameSize;
IsColor = isColor;
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new3(fileName, (int)apiPreference, (int)fourcc, fps, frameSize, isColor ? 1 : 0, out ptr));
NativeMethods.videoio_VideoWriter_new3(fileName, (int)apiPreference, fourcc, fps, frameSize, isColor ? 1 : 0, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand All @@ -90,7 +90,7 @@ public VideoWriter(string fileName, FourCC fourcc, double fps, Size frameSize, i
Fps = fps;
FrameSize = frameSize;
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new4(fileName, (int)fourcc, fps, frameSize, prms, prms.Length, out ptr));
NativeMethods.videoio_VideoWriter_new4(fileName, fourcc, fps, frameSize, prms, prms.Length, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand All @@ -114,7 +114,7 @@ public VideoWriter(string fileName, FourCC fourcc, double fps, Size frameSize, V
FrameSize = frameSize;
var p = prms.GetParameters();
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new4(fileName, (int)fourcc, fps, frameSize, p, p.Length, out ptr));
NativeMethods.videoio_VideoWriter_new4(fileName, fourcc, fps, frameSize, p, p.Length, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand All @@ -140,7 +140,7 @@ public VideoWriter(string fileName, VideoCaptureAPIs apiPreference, FourCC fourc
Fps = fps;
FrameSize = frameSize;
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new5(fileName, (int)apiPreference, (int)fourcc, fps, frameSize, prms, prms.Length, out ptr));
NativeMethods.videoio_VideoWriter_new5(fileName, (int)apiPreference, fourcc, fps, frameSize, prms, prms.Length, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand All @@ -166,7 +166,7 @@ public VideoWriter(string fileName, VideoCaptureAPIs apiPreference, FourCC fourc
FrameSize = frameSize;
var p = prms.GetParameters();
NativeMethods.HandleException(
NativeMethods.videoio_VideoWriter_new5(fileName, (int)apiPreference, (int)fourcc, fps, frameSize, p, p.Length, out ptr));
NativeMethods.videoio_VideoWriter_new5(fileName, (int)apiPreference, fourcc, fps, frameSize, p, p.Length, out ptr));
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Failed to create VideoWriter");
}
Expand Down