using System.Text.Json; using System.Text.Json.Serialization; namespace View_by_Distance.Shared.Models; public class Location : Properties.ILocation, IEquatable { protected int _Bottom; protected double _Confidence; protected int _Left; protected readonly int? _NormalizedPixelPercentage; protected int _Right; protected int _Top; public double Confidence => _Confidence; public int Bottom => _Bottom; 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; Check(bottom, left, normalizedPixelPercentage, right, top, zCount: 1); } public Location(double confidence, int height, Location location, int width, int zCount) : this(location.Bottom, confidence, location.Left, GetNormalizedPixelPercentage(location.Bottom, height, location.Left, location.Right, location.Top, width, zCount), location.Right, location.Top) => Check(_Bottom, _Left, _NormalizedPixelPercentage, _Right, _Top, zCount); public Location(int bottom, double confidence, int height, int left, int right, int top, int width, int zCount) : this(bottom, confidence, left, GetNormalizedPixelPercentage(bottom, height, left, right, top, width, zCount), right, top) => Check(_Bottom, height, _Left, _NormalizedPixelPercentage, _Right, _Top, width, zCount); public Location(double confidence, int factor, int height, Location location, int width, int zCount) { int x = (location.Right - location.Left) / factor; int y = (location.Bottom - location.Top) / factor; int bottom = Math.Min(location.Bottom + y, height); int left = Math.Max(location.Left - x, 0); int right = Math.Min(location.Right + x, width); int top = Math.Max(location.Top - y, 0); int normalizedPixelPercentage = GetNormalizedPixelPercentage(location.Bottom, height, location.Left, location.Right, location.Top, width, zCount); Check(bottom, left, _NormalizedPixelPercentage, right, top, zCount); _Confidence = confidence; _Bottom = bottom; _Left = left; _NormalizedPixelPercentage = normalizedPixelPercentage; _Right = right; _Top = 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; } private static void Check(int bottom, int left, int right, int top, int zCount) { if (left < 0) throw new Exception(); if (right < 0) throw new Exception(); if (right < left) throw new Exception(); if (top < 0) throw new Exception(); if (bottom < 0) throw new Exception(); if (bottom < top) throw new Exception(); if (zCount < 0) throw new Exception(); } private static void Check(int bottom, int height, int left, int right, int top, int width, int zCount) { if (bottom > height) throw new Exception(); if (left > width) throw new Exception(); if (right > width) throw new Exception(); if (top > height) throw new Exception(); if (zCount < 0) throw new Exception(); } private static void Check(int bottom, int left, int? normalizedPixelPercentage, int right, int top, int zCount) { Check(bottom, left, right, top, zCount); if (normalizedPixelPercentage.HasValue && normalizedPixelPercentage.Value < 0) throw new Exception(); } private static void Check(int bottom, int height, int left, int? normalizedPixelPercentage, int right, int top, int width, int zCount) { Check(bottom, left, right, top, zCount); Check(bottom, height, left, right, top, width, zCount); if (normalizedPixelPercentage.HasValue && normalizedPixelPercentage.Value < 0) throw new Exception(); } 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 zCount) { int result; double value; double total = width * height; Check(bottom, left, right, top, zCount); double xCenter = left + ((right - left) / 2); double yCenter = top + ((bottom - top) / 2); double at = ((yCenter - 1) * width) + xCenter; value = at / total; if (value < 0) value = 3; result = (int)(Math.Round(value, Stateless.ILocation.Digits) * Stateless.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.Default.Equals(location1, location2); public static bool operator !=(Location location1, Location location2) => !(location1 == location2); }