Skip to content

Commit

Permalink
record struct
Browse files Browse the repository at this point in the history
  • Loading branch information
shimat committed Feb 27, 2024
1 parent 1873f4a commit a97e803
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 990 deletions.
44 changes: 10 additions & 34 deletions src/OpenCvSharp/Modules/core/Struct/DMatch.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
using System.Diagnostics.CodeAnalysis;
namespace OpenCvSharp;

namespace OpenCvSharp;
#pragma warning disable CA1051

/// <summary>
/// Struct for matching: query descriptor index, train descriptor index, train image index and distance between descriptors.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1815: Override equals and operator equals on value types")]
public struct DMatch
/// <remarks>
/// Constructor
/// </remarks>
public record struct DMatch(int QueryIdx, int TrainIdx, int ImgIdx, float Distance)
{
/// <summary>
/// query descriptor index
/// </summary>
public int QueryIdx;
public int QueryIdx = QueryIdx;

/// <summary>
/// train descriptor index
/// </summary>
public int TrainIdx;
public int TrainIdx = TrainIdx;

/// <summary>
/// train image index
/// </summary>
public int ImgIdx;
public int ImgIdx = ImgIdx;

/// <summary>
///
/// </summary>
public float Distance;
public float Distance = Distance;

/// <summary>
///
/// </summary>
/// <returns></returns>
public static DMatch Empty()
{
return new (-1, -1, -1, float.MaxValue);
}
public static DMatch Empty() => new(-1, -1, -1, float.MaxValue);

/// <summary>
/// Constructor
Expand All @@ -49,21 +47,6 @@ public DMatch(int queryIdx, int trainIdx, float distance) :
{
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="queryIdx"></param>
/// <param name="trainIdx"></param>
/// <param name="imgIdx"></param>
/// <param name="distance"></param>
public DMatch(int queryIdx, int trainIdx, int imgIdx, float distance)
{
QueryIdx = queryIdx;
TrainIdx = trainIdx;
ImgIdx = imgIdx;
Distance = distance;
}

/// <summary>
/// Compares by distance (less is better)
/// </summary>
Expand Down Expand Up @@ -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

/// <inheritdoc />
public override readonly string ToString()
{
// ReSharper disable once UseStringInterpolation
return $"DMatch (QueryIdx:{QueryIdx}, TrainIdx:{TrainIdx}, ImgIdx:{ImgIdx}, Distance:{Distance})";
}
}
128 changes: 19 additions & 109 deletions src/OpenCvSharp/Modules/core/Struct/KeyPoint.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,55 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;

namespace OpenCvSharp;

/// <summary>
/// Data structure for salient point detectors
/// </summary>
/// <remarks>
/// Complete constructor
/// </remarks>
/// <param name="Pt">Coordinate of the point</param>
/// <param name="Size">Feature size</param>
/// <param name="Angle">Feature orientation in degrees (has negative value if the orientation is not defined/not computed)</param>
/// <param name="Response">Feature strength (can be used to select only the most prominent key points)</param>
/// <param name="Octave">Scale-space octave in which the feature has been found; may correlate with the size</param>
/// <param name="ClassId">Point class (can be used by feature classifiers or object detectors)</param>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
public struct KeyPoint : IEquatable<KeyPoint>
public record struct KeyPoint(
Point2f Pt, float Size, float Angle = -1, float Response = 0, int Octave = 0, int ClassId = -1)
{
#region Properties

/// <summary>
/// Coordinate of the point
/// </summary>
public Point2f Pt;
public Point2f Pt = Pt;

/// <summary>
/// Feature size
/// </summary>
public float Size;
public float Size = Size;

/// <summary>
/// Feature orientation in degrees (has negative value if the orientation is not defined/not computed)
/// </summary>
public float Angle;
public float Angle = Angle;

/// <summary>
/// Feature strength (can be used to select only the most prominent key points)
/// </summary>
public float Response;
public float Response = Response;

/// <summary>
/// Scale-space octave in which the feature has been found; may correlate with the size
/// </summary>
public int Octave;
public int Octave = Octave;

/// <summary>
/// Point class (can be used by feature classifiers or object detectors)
/// </summary>
public int ClassId;

#endregion

#region Constructors

/// <summary>
/// Complete constructor
/// </summary>
/// <param name="pt">Coordinate of the point</param>
/// <param name="size">Feature size</param>
/// <param name="angle">Feature orientation in degrees (has negative value if the orientation is not defined/not computed)</param>
/// <param name="response">Feature strength (can be used to select only the most prominent key points)</param>
/// <param name="octave">Scale-space octave in which the feature has been found; may correlate with the size</param>
/// <param name="classId">Point class (can be used by feature classifiers or object detectors)</param>
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;

/// <summary>
/// Complete constructor
Expand All @@ -78,82 +61,9 @@ public KeyPoint(Point2f pt, float size, float angle = -1, float response = 0, in
/// <param name="response">Feature strength (can be used to select only the most prominent key points)</param>
/// <param name="octave">Scale-space octave in which the feature has been found; may correlate with the size</param>
/// <param name="classId">Point class (can be used by feature classifiers or object detectors)</param>
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

/// <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 ==(KeyPoint lhs, KeyPoint rhs)
{
return 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 !=(KeyPoint lhs, KeyPoint rhs)
{
return !lhs.Equals(rhs);
}

#endregion

#region Overrided Methods

/// <inheritdoc />
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;
}

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

/// <inheritdoc />
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
}

/// <inheritdoc />
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
}
42 changes: 2 additions & 40 deletions src/OpenCvSharp/Modules/core/Struct/MatType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@
/// <summary>
/// Matrix data type (depth and number of channels)
/// </summary>
public readonly struct MatType : IEquatable<MatType>, IEquatable<int>
public readonly record struct MatType(int value) : IEquatable<int>
{
/// <summary>
/// Entity value
/// </summary>
private readonly int value;
private readonly int value = value;

/// <summary>
/// Entity value
/// </summary>
public int Value => value;

/// <summary>
///
/// </summary>
/// <param name="value"></param>
public MatType(int value)
{
this.value = value;
}

/// <summary>
/// </summary>
/// <param name="self"></param>
Expand Down Expand Up @@ -76,35 +67,11 @@ public static MatType FromInt32(int value)
/// </summary>
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);
Expand All @@ -115,11 +82,6 @@ public override bool Equals(object? obj)
return !self.Equals(other);
}

public override int GetHashCode()
{
return value.GetHashCode();
}

/// <inheritdoc />
public override string ToString()
{
Expand Down
Loading

0 comments on commit a97e803

Please sign in to comment.