86 lines
3.2 KiB
C#
86 lines
3.2 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using View_by_Distance.Shared.Models.Methods;
|
|
|
|
namespace View_by_Distance.Shared.Models;
|
|
|
|
public class Location : Properties.ILocation, ILocation, IEquatable<Location>
|
|
{
|
|
|
|
protected int _Bottom;
|
|
protected double _Confidence;
|
|
protected int _Left;
|
|
protected readonly int? _NormalizedPixelPercentage;
|
|
protected int _Right;
|
|
protected int _Top;
|
|
public int Bottom => _Bottom;
|
|
public double Confidence => _Confidence;
|
|
public int Left => _Left;
|
|
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
|
|
public int Right => _Right;
|
|
public int Top => _Top;
|
|
|
|
[JsonConstructor]
|
|
public Location(int bottom, double confidence, int left, int? normalizedPixelPercentage, int right, int top)
|
|
{
|
|
_Confidence = confidence;
|
|
_Bottom = bottom;
|
|
_Left = left;
|
|
_NormalizedPixelPercentage = normalizedPixelPercentage;
|
|
_Right = right;
|
|
_Top = top;
|
|
}
|
|
|
|
public Location(double confidence, int bottom, int left, int right, int top, int width, int height) :
|
|
this(bottom, confidence, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width), right, top)
|
|
{ }
|
|
|
|
public Location(double confidence, Location location, int width, int height) :
|
|
this(location.Bottom, confidence, location.Left, GetNormalizedPixelPercentage(location.Bottom, height, location.Left, location.Right, location.Top, width), location.Right, location.Top)
|
|
{ }
|
|
|
|
public Location(int left, int top, int right, int bottom, int width, int height) :
|
|
this(bottom, -1.0d, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width), right, top)
|
|
{ }
|
|
|
|
public override bool Equals(object? obj) => Equals(obj as Location);
|
|
|
|
public override string ToString()
|
|
{
|
|
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int hashCode = -773114317;
|
|
hashCode = hashCode * -1521134295 + _Bottom.GetHashCode();
|
|
hashCode = hashCode * -1521134295 + _Left.GetHashCode();
|
|
hashCode = hashCode * -1521134295 + _Right.GetHashCode();
|
|
hashCode = hashCode * -1521134295 + _Top.GetHashCode();
|
|
return hashCode;
|
|
}
|
|
|
|
public static int GetNormalizedPixelPercentage(int bottom, int height, int left, int right, int top, int width)
|
|
{
|
|
double result;
|
|
double xCenter = left + ((right - left) / 2);
|
|
double yCenter = top + ((bottom - top) / 2);
|
|
result = ((yCenter * width) + xCenter) / (width * height);
|
|
return (int)(Math.Round((decimal)result, 4) * Stateless.Methods.ILocation.Factor);
|
|
}
|
|
|
|
public bool Equals(Location? location)
|
|
{
|
|
return location is not null
|
|
&& _Bottom == location.Bottom
|
|
&& _Left == location.Left
|
|
&& _Right == location.Right
|
|
&& _Top == location.Top;
|
|
}
|
|
|
|
public static bool operator ==(Location location1, Location location2) => EqualityComparer<Location>.Default.Equals(location1, location2);
|
|
|
|
public static bool operator !=(Location location1, Location location2) => !(location1 == location2);
|
|
|
|
} |