diff --git a/src/OpenCvSharp/Modules/core/Struct/DMatch.cs b/src/OpenCvSharp/Modules/core/Struct/DMatch.cs index f065202f4..d2724afbe 100644 --- a/src/OpenCvSharp/Modules/core/Struct/DMatch.cs +++ b/src/OpenCvSharp/Modules/core/Struct/DMatch.cs @@ -1,42 +1,40 @@ -using System.Diagnostics.CodeAnalysis; +namespace OpenCvSharp; -namespace OpenCvSharp; #pragma warning disable CA1051 /// /// Struct for matching: query descriptor index, train descriptor index, train image index and distance between descriptors. /// -[SuppressMessage("Microsoft.Design", "CA1815: Override equals and operator equals on value types")] -public struct DMatch +/// +/// Constructor +/// +public record struct DMatch(int QueryIdx, int TrainIdx, int ImgIdx, float Distance) { /// /// query descriptor index /// - public int QueryIdx; + public int QueryIdx = QueryIdx; /// /// train descriptor index /// - public int TrainIdx; + public int TrainIdx = TrainIdx; /// /// train image index /// - public int ImgIdx; + public int ImgIdx = ImgIdx; /// /// /// - public float Distance; + public float Distance = Distance; /// /// /// /// - public static DMatch Empty() - { - return new (-1, -1, -1, float.MaxValue); - } + public static DMatch Empty() => new(-1, -1, -1, float.MaxValue); /// /// Constructor @@ -49,21 +47,6 @@ public DMatch(int queryIdx, int trainIdx, float distance) : { } - /// - /// Constructor - /// - /// - /// - /// - /// - public DMatch(int queryIdx, int trainIdx, int imgIdx, float distance) - { - QueryIdx = queryIdx; - TrainIdx = trainIdx; - ImgIdx = imgIdx; - Distance = distance; - } - /// /// Compares by distance (less is better) /// @@ -106,11 +89,4 @@ public DMatch(int queryIdx, int trainIdx, int imgIdx, float distance) public static DMatch FromVec4f(Vec4f v) => new ((int)v.Item0, (int)v.Item1, (int)v.Item2, v.Item3); #pragma warning restore 1591 - - /// - public override readonly string ToString() - { - // ReSharper disable once UseStringInterpolation - return $"DMatch (QueryIdx:{QueryIdx}, TrainIdx:{TrainIdx}, ImgIdx:{ImgIdx}, Distance:{Distance})"; - } } diff --git a/src/OpenCvSharp/Modules/core/Struct/KeyPoint.cs b/src/OpenCvSharp/Modules/core/Struct/KeyPoint.cs index e26246441..742f6b09f 100644 --- a/src/OpenCvSharp/Modules/core/Struct/KeyPoint.cs +++ b/src/OpenCvSharp/Modules/core/Struct/KeyPoint.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Globalization; using System.Runtime.InteropServices; namespace OpenCvSharp; @@ -7,66 +6,50 @@ namespace OpenCvSharp; /// /// Data structure for salient point detectors /// +/// +/// Complete constructor +/// +/// Coordinate of the point +/// Feature size +/// Feature orientation in degrees (has negative value if the orientation is not defined/not computed) +/// Feature strength (can be used to select only the most prominent key points) +/// Scale-space octave in which the feature has been found; may correlate with the size +/// Point class (can be used by feature classifiers or object detectors) [Serializable] [StructLayout(LayoutKind.Sequential)] [SuppressMessage("Design", "CA1051: Do not declare visible instance fields")] -public struct KeyPoint : IEquatable +public record struct KeyPoint( + Point2f Pt, float Size, float Angle = -1, float Response = 0, int Octave = 0, int ClassId = -1) { - #region Properties - /// /// Coordinate of the point /// - public Point2f Pt; + public Point2f Pt = Pt; /// /// Feature size /// - public float Size; + public float Size = Size; /// /// Feature orientation in degrees (has negative value if the orientation is not defined/not computed) /// - public float Angle; + public float Angle = Angle; /// /// Feature strength (can be used to select only the most prominent key points) /// - public float Response; + public float Response = Response; /// /// Scale-space octave in which the feature has been found; may correlate with the size /// - public int Octave; + public int Octave = Octave; /// /// Point class (can be used by feature classifiers or object detectors) /// - public int ClassId; - - #endregion - - #region Constructors - - /// - /// Complete constructor - /// - /// Coordinate of the point - /// Feature size - /// Feature orientation in degrees (has negative value if the orientation is not defined/not computed) - /// Feature strength (can be used to select only the most prominent key points) - /// Scale-space octave in which the feature has been found; may correlate with the size - /// Point class (can be used by feature classifiers or object detectors) - public KeyPoint(Point2f pt, float size, float angle = -1, float response = 0, int octave = 0, - int classId = -1) - { - Pt = pt; - Size = size; - Angle = angle; - Response = response; - Octave = octave; - ClassId = classId; - } + public int ClassId = ClassId; /// /// Complete constructor @@ -78,82 +61,9 @@ public KeyPoint(Point2f pt, float size, float angle = -1, float response = 0, in /// Feature strength (can be used to select only the most prominent key points) /// Scale-space octave in which the feature has been found; may correlate with the size /// Point class (can be used by feature classifiers or object detectors) - public KeyPoint(float x, float y, float size, float angle = -1, float response = 0, int octave = 0, - int classId = -1) + public KeyPoint( + float x, float y, float size, float angle = -1, float response = 0, int octave = 0, int classId = -1) : this(new Point2f(x, y), size, angle, response, octave, classId) { } - - #endregion - - #region Operators - - /// - /// Compares two CvPoint objects. The result specifies whether the members of each object are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the members of left and right are equal; otherwise, false. - public static bool operator ==(KeyPoint lhs, KeyPoint rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint objects. The result specifies whether the members of each object are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the members of left and right are unequal; otherwise, false. - public static bool operator !=(KeyPoint lhs, KeyPoint rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region Overrided Methods - - /// - public readonly bool Equals(KeyPoint other) - { - return Pt.Equals(other.Pt) && Size.Equals(other.Size) && Angle.Equals(other.Angle) && Response.Equals(other.Response) && Octave == other.Octave && ClassId == other.ClassId; - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is KeyPoint other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if NET48 || NETSTANDARD2_0 - unchecked - { - var hashCode = Pt.GetHashCode(); - hashCode = (hashCode * 397) ^ Size.GetHashCode(); - hashCode = (hashCode * 397) ^ Angle.GetHashCode(); - hashCode = (hashCode * 397) ^ Response.GetHashCode(); - hashCode = (hashCode * 397) ^ Octave; - hashCode = (hashCode * 397) ^ ClassId; - return hashCode; - } -#else - return HashCode.Combine(Pt, Size, Angle, Response, Octave, ClassId); -#endif - } - - /// - public override readonly string ToString() - { - // ReSharper disable once UseStringInterpolation - return string.Format( - CultureInfo.InvariantCulture, - "[Pt:{0}, Size:{1}, Angle:{2}, Response:{3}, Octave:{4}, ClassId:{5}]", - Pt, Size, Angle, Response, Octave, ClassId); - } - - #endregion } diff --git a/src/OpenCvSharp/Modules/core/Struct/MatType.cs b/src/OpenCvSharp/Modules/core/Struct/MatType.cs index a08560900..3cfab3e1b 100644 --- a/src/OpenCvSharp/Modules/core/Struct/MatType.cs +++ b/src/OpenCvSharp/Modules/core/Struct/MatType.cs @@ -5,27 +5,18 @@ /// /// Matrix data type (depth and number of channels) /// -public readonly struct MatType : IEquatable, IEquatable +public readonly record struct MatType(int value) : IEquatable { /// /// Entity value /// - private readonly int value; + private readonly int value = value; /// /// Entity value /// public int Value => value; - /// - /// - /// - /// - public MatType(int value) - { - this.value = value; - } - /// /// /// @@ -76,35 +67,11 @@ public static MatType FromInt32(int value) /// public int Channels => (Value >> CV_CN_SHIFT) + 1; - public bool Equals(MatType other) - { - return value == other.value; - } - public bool Equals(int other) { return value == other; } - public override bool Equals(object? obj) - { - if (obj is null) - return false; - if (obj.GetType() != typeof (MatType)) - return false; - return obj is MatType mt && Equals(mt); - } - - public static bool operator ==(MatType self, MatType other) - { - return self.Equals(other); - } - - public static bool operator !=(MatType self, MatType other) - { - return !self.Equals(other); - } - public static bool operator ==(MatType self, int other) { return self.Equals(other); @@ -115,11 +82,6 @@ public override bool Equals(object? obj) return !self.Equals(other); } - public override int GetHashCode() - { - return value.GetHashCode(); - } - /// public override string ToString() { diff --git a/src/OpenCvSharp/Modules/core/Struct/Point.cs b/src/OpenCvSharp/Modules/core/Struct/Point.cs index 7cfb61157..9dced2758 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point.cs @@ -9,28 +9,17 @@ namespace OpenCvSharp; /// [Serializable] [StructLayout(LayoutKind.Sequential)] -public struct Point : IEquatable +public record struct Point(int X, int Y) { /// /// /// - public int X; + public int X = X; /// /// /// - public int Y; - - /// - /// - /// - /// - /// - public Point(int x, int y) - { - X = x; - Y = y; - } + public int Y = Y; /// /// @@ -38,9 +27,8 @@ public Point(int x, int y) /// /// public Point(double x, double y) + : this((int)x, (int)y) { - X = (int) x; - Y = (int) y; } #region Cast @@ -63,34 +51,6 @@ public Point(double x, double y) #region Operators - #region == / != - - /// - /// Compares two Point objects. The result specifies whether the values of the X and Y properties of the two Point objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point lhs, Point rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two Point objects. The result specifies whether the values of the X or Y properties of the two Point objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point lhs, Point rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - /// /// Unary plus operator /// @@ -164,43 +124,6 @@ public Point(double x, double y) #endregion - #endregion - - #region Override - - /// - public readonly bool Equals(Point other) - { - return X == other.X && Y == other.Y; - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - return (X.GetHashCode() * 397) ^ Y.GetHashCode(); - } -#else - return HashCode.Combine(X, Y); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y})"; - } - - #endregion - #region Methods /// diff --git a/src/OpenCvSharp/Modules/core/Struct/Point2d.cs b/src/OpenCvSharp/Modules/core/Struct/Point2d.cs index 1a15b7388..67c252d9e 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point2d.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point2d.cs @@ -4,33 +4,21 @@ namespace OpenCvSharp; -/// -/// +/// /// /// [Serializable] [StructLayout(LayoutKind.Sequential)] -public struct Point2d : IEquatable +public record struct Point2d(double X, double Y) { /// /// /// - public double X; + public double X = X; /// /// /// - public double Y; - - /// - /// - /// - /// - /// - public Point2d(double x, double y) - { - X = x; - Y = y; - } + public double Y = Y; #region Cast @@ -61,34 +49,6 @@ public Point2d(double x, double y) #region Operators - #region == / != - - /// - /// Compares two CvPoint objects. The result specifies whether the values of the X and Y properties of the two CvPoint objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point2d lhs, Point2d rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint2D32f objects. The result specifies whether the values of the X or Y properties of the two CvPoint2D32f objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point2d lhs, Point2d rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - /// /// Unary plus operator /// @@ -162,43 +122,6 @@ public Point2d(double x, double y) #endregion - #endregion - - #region Override - - /// - public readonly bool Equals(Point2d other) - { - return X.Equals(other.X) && Y.Equals(other.Y); - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point2d other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - return (X.GetHashCode() * 397) ^ Y.GetHashCode(); - } -#else - return HashCode.Combine(X, Y); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y})"; - } - - #endregion - #region Methods /// diff --git a/src/OpenCvSharp/Modules/core/Struct/Point2f.cs b/src/OpenCvSharp/Modules/core/Struct/Point2f.cs index b2d044ccd..9cf0b3159 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point2f.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point2f.cs @@ -10,28 +10,17 @@ namespace OpenCvSharp; [Serializable] [StructLayout(LayoutKind.Sequential)] // ReSharper disable once InconsistentNaming -public struct Point2f : IEquatable +public record struct Point2f(float X, float Y) { /// /// /// - public float X; + public float X = X; /// /// /// - public float Y; - - /// - /// - /// - /// - /// - public Point2f(float x, float y) - { - X = x; - Y = y; - } + public float Y = Y; #region Cast @@ -62,34 +51,6 @@ public Point2f(float x, float y) #region Operators - #region == / != - - /// - /// Compares two CvPoint objects. The result specifies whether the values of the X and Y properties of the two CvPoint objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point2f lhs, Point2f rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint2D32f objects. The result specifies whether the values of the X or Y properties of the two CvPoint2D32f objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point2f lhs, Point2f rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - /// /// Unary plus operator /// @@ -166,43 +127,6 @@ public Point2f(float x, float y) #endregion - #endregion - - #region Override - - /// - public readonly bool Equals(Point2f other) - { - return X.Equals(other.X) && Y.Equals(other.Y); - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point2f other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - return (X.GetHashCode() * 397) ^ Y.GetHashCode(); - } -#else - return HashCode.Combine(X, Y); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y})"; - } - - #endregion - #region Methods /// diff --git a/src/OpenCvSharp/Modules/core/Struct/Point3d.cs b/src/OpenCvSharp/Modules/core/Struct/Point3d.cs index ec78860c6..48536847e 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point3d.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point3d.cs @@ -4,40 +4,26 @@ namespace OpenCvSharp; -/// -/// +/// /// /// [Serializable] [StructLayout(LayoutKind.Sequential)] -public struct Point3d : IEquatable +public record struct Point3d(double X, double Y, double Z) { /// /// /// - public double X; + public double X = X; /// /// /// - public double Y; + public double Y = Y; /// /// /// - public double Z; - - /// - /// - /// - /// - /// - /// - public Point3d(double x, double y, double z) - { - X = x; - Y = y; - Z = z; - } + public double Z = Z; #region Cast @@ -69,34 +55,6 @@ public Point3d(double x, double y, double z) #region Operators - #region == / != - - /// - /// Compares two CvPoint objects. The result specifies whether the values of the X and Y properties of the two CvPoint objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point3d lhs, Point3d rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint2D32f objects. The result specifies whether the values of the X or Y properties of the two CvPoint2D32f objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point3d lhs, Point3d rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - /// /// Unary plus operator /// @@ -169,44 +127,4 @@ public Point3d(double x, double y, double z) public readonly Point3d Multiply(double scale) => new(X * scale, Y * scale, Z * scale); #endregion - - #endregion - - #region Override - - /// - public readonly bool Equals(Point3d other) - { - return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z); - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point3d other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = X.GetHashCode(); - hashCode = (hashCode * 397) ^ Y.GetHashCode(); - hashCode = (hashCode * 397) ^ Z.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine(X, Y, Z); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y} z:{Z})"; - } - - #endregion } diff --git a/src/OpenCvSharp/Modules/core/Struct/Point3f.cs b/src/OpenCvSharp/Modules/core/Struct/Point3f.cs index 0024984fd..76423a7c8 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point3f.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point3f.cs @@ -3,42 +3,28 @@ namespace OpenCvSharp; -/// -/// +/// /// /// [Serializable] [StructLayout(LayoutKind.Sequential)] [SuppressMessage("Design", "CA1051: Do not declare visible instance fields")] // ReSharper disable once InconsistentNaming -public struct Point3f : IEquatable +public record struct Point3f(float X, float Y, float Z) { /// /// /// - public float X; + public float X = X; /// /// /// - public float Y; + public float Y = Y; /// /// /// - public float Z; - - /// - /// Constructor - /// - /// - /// - /// - public Point3f(float x, float y, float z) - { - X = x; - Y = y; - Z = z; - } + public float Z = Z; #region Cast @@ -69,35 +55,7 @@ public Point3f(float x, float y, float z) #endregion #region Operators - - #region == / != - - /// - /// Compares two CvPoint objects. The result specifies whether the values of the X and Y properties of the two CvPoint objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point3f lhs, Point3f rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint2D32f objects. The result specifies whether the values of the X or Y properties of the two CvPoint2D32f objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point3f lhs, Point3f rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - + /// /// Unary plus operator /// @@ -171,45 +129,4 @@ public readonly Point3f Multiply(double scale) public static Point3f operator *(Point3f pt, double scale) => pt.Multiply(scale); #endregion - - #endregion - - #region Override - - /// - public readonly bool Equals(Point3f other) - { - return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z); - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point3f other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = X.GetHashCode(); - hashCode = (hashCode * 397) ^ Y.GetHashCode(); - hashCode = (hashCode * 397) ^ Z.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine(X, Y, Z); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y} z:{Z})"; - } - - #endregion - } diff --git a/src/OpenCvSharp/Modules/core/Struct/Point3i.cs b/src/OpenCvSharp/Modules/core/Struct/Point3i.cs index d5a556b04..6dd154357 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Point3i.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Point3i.cs @@ -10,35 +10,22 @@ namespace OpenCvSharp; [Serializable] [StructLayout(LayoutKind.Sequential)] // ReSharper disable once InconsistentNaming -public struct Point3i : IEquatable +public record struct Point3i(int X, int Y, int Z) { /// /// /// - public int X; + public int X = X; /// /// /// - public int Y; + public int Y = Y; /// /// /// - public int Z; - - /// - /// - /// - /// - /// - /// - public Point3i(int x, int y, int z) - { - X = x; - Y = y; - Z = z; - } + public int Z = Z; #region Cast @@ -60,34 +47,6 @@ public Point3i(int x, int y, int z) #region Operators - #region == / != - - /// - /// Compares two CvPoint objects. The result specifies whether the values of the X and Y properties of the two CvPoint objects are equal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the X and Y values of left and right are equal; otherwise, false. - public static bool operator ==(Point3i lhs, Point3i rhs) - { - return lhs.Equals(rhs); - } - - /// - /// Compares two CvPoint2D32f objects. The result specifies whether the values of the X or Y properties of the two CvPoint2D32f objects are unequal. - /// - /// A Point to compare. - /// A Point to compare. - /// This operator returns true if the values of either the X properties or the Y properties of left and right differ; otherwise, false. - public static bool operator !=(Point3i lhs, Point3i rhs) - { - return !lhs.Equals(rhs); - } - - #endregion - - #region + / - - /// /// Unary plus operator /// @@ -163,44 +122,4 @@ public Point3i(int x, int y, int z) public readonly Point3i Multiply(double scale) => new ((int)(X * scale), (int)(Y * scale), (int)(Z * scale)); #endregion - - #endregion - - #region Override - - /// - public readonly bool Equals(Point3i other) - { - return X == other.X && Y == other.Y && Z == other.Z; - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Point3i other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = X; - hashCode = (hashCode * 397) ^ Y; - hashCode = (hashCode * 397) ^ Z; - return hashCode; - } -#else - return HashCode.Combine(X, Y, Z); -#endif - } - - /// - public override readonly string ToString() - { - return $"(x:{X} y:{Y} z:{Z})"; - } - - #endregion } diff --git a/src/OpenCvSharp/Modules/core/Struct/Range.cs b/src/OpenCvSharp/Modules/core/Struct/Range.cs index db22362f4..7e7a69c5d 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Range.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Range.cs @@ -9,74 +9,20 @@ namespace OpenCvSharp; /// [Serializable] [StructLayout(LayoutKind.Sequential)] -public readonly struct Range : IEquatable +public readonly record struct Range(int Start, int End) { /// /// /// - public readonly int Start; + public readonly int Start = Start; /// /// /// - public readonly int End; - - /// - /// - /// - /// - /// - public Range(int start, int end) - { - Start = start; - End = end; - } + public readonly int End = End; /// /// /// public static Range All => new Range(int.MinValue, int.MaxValue); - - /// - public readonly bool Equals(Range other) - { - return Start == other.Start && End == other.End; - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Range other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { - unchecked - { - return (Start * 397) ^ End; - } - } - - /// - /// - /// - /// - /// - /// - public static bool operator ==(Range left, Range right) - { - return left.Equals(right); - } - - /// - /// - /// - /// - /// - /// - public static bool operator !=(Range left, Range right) - { - return !(left == right); - } } diff --git a/src/OpenCvSharp/Modules/core/Struct/Rangef.cs b/src/OpenCvSharp/Modules/core/Struct/Rangef.cs index d54b692c8..ee0ed903f 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Rangef.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Rangef.cs @@ -10,29 +10,17 @@ namespace OpenCvSharp; [StructLayout(LayoutKind.Sequential)] [SuppressMessage("Design", "CA1051: Do not declare visible instance fields")] // ReSharper disable once IdentifierTypo -public readonly struct Rangef : IEquatable +public readonly record struct Rangef(float Start, float End) { /// /// /// - public readonly float Start; + public readonly float Start = Start; /// /// /// - public readonly float End; - - /// - /// Constructor - /// - /// - /// - // ReSharper disable once IdentifierTypo - public Rangef(float start, float end) - { - Start = start; - End = end; - } + public readonly float End = End; /// /// Convert to Range @@ -51,40 +39,4 @@ public Rangef(float start, float end) /// Range(int.MinValue, int.MaxValue) /// public static Range All => new (int.MinValue, int.MaxValue); - -#pragma warning disable CS1591 - public bool Equals(Rangef other) - { - return Start.Equals(other.Start) && End.Equals(other.End); - } - - public override bool Equals(object? obj) - { - return obj is Rangef other && Equals(other); - } - - public override int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = Start.GetHashCode(); - hashCode = (hashCode * 397) ^ End.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine(Start, End); -#endif - } - - public static bool operator ==(Rangef left, Rangef right) - { - return left.Equals(right); - } - - public static bool operator !=(Rangef left, Rangef right) - { - return !left.Equals(right); - } -#pragma warning restore CS1591 } diff --git a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs index 84f61a355..c203c6ef8 100644 --- a/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs +++ b/src/OpenCvSharp/Modules/core/Struct/RotatedRect.cs @@ -10,7 +10,7 @@ namespace OpenCvSharp; /// [StructLayout(LayoutKind.Sequential)] [SuppressMessage("Design", "CA1051: Do not declare visible instance fields")] -public struct RotatedRect : IEquatable +public record struct RotatedRect { /// /// the rectangle mass center @@ -145,43 +145,4 @@ public readonly Rect BoundingRect() r.Height -= r.Y - 1; return r; } - -#pragma warning disable CS1591 - - public bool Equals(RotatedRect other) - { - return Center.Equals(other.Center) && Size.Equals(other.Size) && Angle.Equals(other.Angle); - } - - public override bool Equals(object? obj) - { - return obj is RotatedRect other && Equals(other); - } - - public override int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = Center.GetHashCode(); - hashCode = (hashCode * 397) ^ Size.GetHashCode(); - hashCode = (hashCode * 397) ^ Angle.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine(Center, Size, Angle); -#endif - } - - public static bool operator ==(RotatedRect left, RotatedRect right) - { - return left.Equals(right); - } - - public static bool operator !=(RotatedRect left, RotatedRect right) - { - return !left.Equals(right); - } - -#pragma warning restore CS1591 } diff --git a/src/OpenCvSharp/Modules/core/Struct/Scalar.cs b/src/OpenCvSharp/Modules/core/Struct/Scalar.cs index b7989f414..2a4b36d25 100644 --- a/src/OpenCvSharp/Modules/core/Struct/Scalar.cs +++ b/src/OpenCvSharp/Modules/core/Struct/Scalar.cs @@ -10,29 +10,29 @@ namespace OpenCvSharp; /// [Serializable] [StructLayout(LayoutKind.Sequential)] -public struct Scalar : IEquatable +public record struct Scalar(double Val0, double Val1, double Val2, double Val3) { #region Field /// /// /// - public double Val0; + public double Val0 = Val0; /// /// /// - public double Val1; + public double Val1 = Val1; /// /// /// - public double Val2; + public double Val2 = Val2; /// /// /// - public double Val3; + public double Val3 = Val3; /// /// @@ -79,48 +79,33 @@ public double this[int i] /// /// /// - /// - public Scalar(double v0) - : this(v0, 0, 0, 0) + /// + public Scalar(double val0) + : this(val0, 0, 0, 0) { } /// /// /// - /// - /// - public Scalar(double v0, double v1) - : this(v0, v1, 0, 0) + /// + /// + public Scalar(double val0, double val1) + : this(val0, val1, 0, 0) { } /// /// /// - /// - /// - /// - public Scalar(double v0, double v1, double v2) - : this(v0, v1, v2, 0) + /// + /// + /// + public Scalar(double val0, double val1, double val2) + : this(val0, val1, val2, 0) { } - /// - /// - /// - /// - /// - /// - /// - public Scalar(double v0, double v1, double v2, double v3) - { - Val0 = v0; - Val1 = v1; - Val2 = v2; - Val3 = v3; - } - /// /// /// @@ -205,71 +190,6 @@ public static Scalar RandomColor(RandomNumberGenerator rng) #endregion - #region Override - - /// - public readonly bool Equals(Scalar other) - { - return Val0.Equals(other.Val0) && Val1.Equals(other.Val1) && Val2.Equals(other.Val2) && Val3.Equals(other.Val3); - } - - /// - public override readonly bool Equals(object? obj) - { - return obj is Scalar other && Equals(other); - } - - /// - public override readonly int GetHashCode() - { -#if NET48 || NETSTANDARD2_0 - unchecked - { - var hashCode = Val0.GetHashCode(); - hashCode = (hashCode * 397) ^ Val1.GetHashCode(); - hashCode = (hashCode * 397) ^ Val2.GetHashCode(); - hashCode = (hashCode * 397) ^ Val3.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine(Val0, Val1, Val2, Val3); -#endif - } - - /// - public override readonly string ToString() - { - return $"[{Val0}, {Val1}, {Val2}, {Val3}]"; - } - - #endregion - - #region Operators - - /// - /// - /// - /// - /// - /// - public static bool operator ==(Scalar s1, Scalar s2) - { - return s1.Equals(s2); - } - - /// - /// - /// - /// - /// - /// - public static bool operator !=(Scalar s1, Scalar s2) - { - return !s1.Equals(s2); - } - - #endregion - #region Methods /// diff --git a/src/OpenCvSharp/Modules/core/Struct/TermCriteria.cs b/src/OpenCvSharp/Modules/core/Struct/TermCriteria.cs index 026f176de..527a29948 100644 --- a/src/OpenCvSharp/Modules/core/Struct/TermCriteria.cs +++ b/src/OpenCvSharp/Modules/core/Struct/TermCriteria.cs @@ -5,35 +5,22 @@ namespace OpenCvSharp; /// /// The class defining termination criteria for iterative algorithms. /// -public readonly struct TermCriteria : IEquatable +public readonly record struct TermCriteria(CriteriaTypes Type, int MaxCount, double Epsilon) { /// /// the type of termination criteria: COUNT, EPS or COUNT + EPS /// - public readonly CriteriaTypes Type; + public readonly CriteriaTypes Type = Type; /// /// the maximum number of iterations/elements /// - public readonly int MaxCount; + public readonly int MaxCount = MaxCount; /// /// the desired accuracy /// - public readonly double Epsilon; - - /// - /// full constructor - /// - /// - /// - /// - public TermCriteria(CriteriaTypes type, int maxCount, double epsilon) - { - Type = type; - MaxCount = maxCount; - Epsilon = epsilon; - } + public readonly double Epsilon = Epsilon; /// /// full constructor with both type (count | epsilon) @@ -43,47 +30,8 @@ public TermCriteria(CriteriaTypes type, int maxCount, double epsilon) public static TermCriteria Both(int maxCount, double epsilon) { return new ( - type: CriteriaTypes.Count | CriteriaTypes.Eps, - maxCount: maxCount, - epsilon: epsilon); - } - -#pragma warning disable CS1591 - - public bool Equals(TermCriteria other) - { - return Type == other.Type && MaxCount == other.MaxCount && Epsilon.Equals(other.Epsilon); - } - - public override bool Equals(object? obj) - { - return obj is TermCriteria other && Equals(other); - } - - public override int GetHashCode() - { -#if DOTNET_FRAMEWORK || NETSTANDARD2_0 - unchecked - { - var hashCode = Type.GetHashCode(); - hashCode = (hashCode * 397) ^ MaxCount.GetHashCode(); - hashCode = (hashCode * 397) ^ Epsilon.GetHashCode(); - return hashCode; - } -#else - return HashCode.Combine((int) Type, MaxCount, Epsilon); -#endif - } - - public static bool operator ==(TermCriteria left, TermCriteria right) - { - return left.Equals(right); - } - - public static bool operator !=(TermCriteria left, TermCriteria right) - { - return !left.Equals(right); + Type: CriteriaTypes.Count | CriteriaTypes.Eps, + MaxCount: maxCount, + Epsilon: epsilon); } - -#pragma warning restore CS1591 } diff --git a/test/OpenCvSharp.Tests/core/RectTest.cs b/test/OpenCvSharp.Tests/core/RectTest.cs index 85b829b8d..b545197c1 100644 --- a/test/OpenCvSharp.Tests/core/RectTest.cs +++ b/test/OpenCvSharp.Tests/core/RectTest.cs @@ -1,9 +1,16 @@ -using Xunit; +using System.Runtime.InteropServices; +using Xunit; namespace OpenCvSharp.Tests.Core; public class RectTest { + [Fact] + public void SizeOf() + { + Assert.Equal(sizeof(int)*4, Marshal.SizeOf()); + } + [Fact] public void TopLeft() { diff --git a/test/OpenCvSharp.Tests/core/ScalarTest.cs b/test/OpenCvSharp.Tests/core/ScalarTest.cs new file mode 100644 index 000000000..a681aa67c --- /dev/null +++ b/test/OpenCvSharp.Tests/core/ScalarTest.cs @@ -0,0 +1,13 @@ +using System.Runtime.InteropServices; +using Xunit; + +namespace OpenCvSharp.Tests.Core; + +public class ScalarTest +{ + [Fact] + public void SizeOf() + { + Assert.Equal(sizeof(double)*4, Marshal.SizeOf()); + } +}