85 lines
3.4 KiB
C#
85 lines
3.4 KiB
C#
using System.Text.Json.Serialization;
|
|
using View_by_Distance.Shared.Models;
|
|
using View_by_Distance.Shared.Models.Properties;
|
|
|
|
namespace View_by_Distance.Property.Models;
|
|
|
|
public class Named
|
|
{
|
|
|
|
protected readonly bool? _IsWrongYear;
|
|
protected readonly DateTime _MinimumDateTime;
|
|
protected readonly int? _NormalizedPixelPercentage;
|
|
protected readonly PersonBirthday? _PersonBirthday;
|
|
public bool? IsWrongYear => _IsWrongYear;
|
|
public DateTime MinimumDateTime => _MinimumDateTime;
|
|
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
|
|
public PersonBirthday? PersonBirthday => _PersonBirthday;
|
|
|
|
[JsonConstructor]
|
|
public Named(bool? isWrongYear, DateTime minimumDateTime, int? normalizedPixelPercentage, PersonBirthday? personBirthday)
|
|
{
|
|
_IsWrongYear = isWrongYear;
|
|
_MinimumDateTime = minimumDateTime;
|
|
_NormalizedPixelPercentage = normalizedPixelPercentage;
|
|
_PersonBirthday = personBirthday;
|
|
}
|
|
|
|
public Named(bool? isWrongYear, DateTime minimumDateTime, PersonBirthday? personBirthday) :
|
|
this(isWrongYear, minimumDateTime, null, personBirthday)
|
|
{ }
|
|
|
|
private static float GetDeterministicHashCodeFileName(int id, int normalizedPixelPercentage)
|
|
=> float.Parse($"{id}.{normalizedPixelPercentage}");
|
|
|
|
public static float GetDeterministicHashCodeKey(Item item, Closest closest)
|
|
{
|
|
float result;
|
|
if (item.Property?.Id is null || item.ImageFileHolder is null || closest.NormalizedPixelPercentage is null)
|
|
throw new NullReferenceException();
|
|
result = GetDeterministicHashCodeFileName(item.Property.Id.Value, closest.NormalizedPixelPercentage.Value);
|
|
return result;
|
|
}
|
|
|
|
public static float GetDeterministicHashCodeKey(Item item, IFace face)
|
|
{
|
|
float result;
|
|
if (item.Property?.Id is null || item.ImageFileHolder is null || face.Location?.NormalizedPixelPercentage is null)
|
|
throw new NullReferenceException();
|
|
result = GetDeterministicHashCodeFileName(item.Property.Id.Value, face.Location.NormalizedPixelPercentage.Value);
|
|
return result;
|
|
}
|
|
|
|
public static float? GetReversedDeterministicHashCode(Dictionary<int, List<IFace>> keyValuePairs, string fileName)
|
|
{
|
|
float? result;
|
|
string[] segments = fileName.Split('.');
|
|
if (segments.Length < 2)
|
|
throw new Exception();
|
|
string id = segments[0];
|
|
string normalizedPixelPercentage;
|
|
if (!id.Contains('-'))
|
|
normalizedPixelPercentage = segments[1];
|
|
else
|
|
{
|
|
segments = fileName.Split(' ');
|
|
if (segments.Length < 3)
|
|
throw new Exception();
|
|
id = segments[2];
|
|
string locationIdex = segments[0];
|
|
if (int.TryParse(id, out int idValue) && int.TryParse(locationIdex, out int locationIndexValue) && keyValuePairs.ContainsKey(idValue) && keyValuePairs[idValue].Count > locationIndexValue)
|
|
normalizedPixelPercentage = string.Concat(keyValuePairs[idValue][locationIndexValue].Location.NormalizedPixelPercentage);
|
|
else
|
|
{
|
|
id = string.Empty;
|
|
normalizedPixelPercentage = string.Empty;
|
|
}
|
|
}
|
|
if (!float.TryParse(string.Concat(id, '.', normalizedPixelPercentage), out float resultValue))
|
|
result = null;
|
|
else
|
|
result = resultValue;
|
|
return result;
|
|
}
|
|
|
|
} |