Mike Phares f72fcee1db Save Closest is ready again with
all Deterministic Hash Code Key
2022-08-20 00:57:48 -07:00

92 lines
3.4 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)
{
if (normalizedPixelPercentage < 0)
normalizedPixelPercentage = 3;
_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)
{
int result;
double value;
double xCenter = left + ((right - left) / 2);
double yCenter = top + ((bottom - top) / 2);
value = ((yCenter * width) + xCenter) / (width * height);
if (value < 0)
value = 3;
result = (int)(Math.Round((decimal)value, 4) * Stateless.Methods.ILocation.Factor);
return result;
}
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);
}