146 lines
5.8 KiB
C#
146 lines
5.8 KiB
C#
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
internal abstract class Named
|
|
{
|
|
|
|
private static double GetDeterministicHashCodeFileName(int id, int normalizedPixelPercentage)
|
|
=> double.Parse($"{id}.{normalizedPixelPercentage}");
|
|
|
|
internal static double GetDeterministicHashCodeKey(Models.Item item, Models.Closest closest)
|
|
{
|
|
double 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;
|
|
}
|
|
|
|
internal static double GetDeterministicHashCodeKey(Models.Item item, Models.Face face)
|
|
{
|
|
double 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;
|
|
}
|
|
|
|
private static double? GetReversedDeterministicHashCodeB(string fileName)
|
|
{
|
|
double? result;
|
|
string[] segments = fileName.Split('.');
|
|
if (segments.Length < 2)
|
|
throw new Exception();
|
|
string id = segments[0];
|
|
string normalizedPixelPercentage = segments[1];
|
|
if (!double.TryParse(string.Concat(id, '.', normalizedPixelPercentage), out double resultValue))
|
|
result = null;
|
|
else
|
|
result = resultValue;
|
|
return result;
|
|
}
|
|
|
|
internal static double? GetReversedDeterministicHashCode(string fileName)
|
|
{
|
|
double? result;
|
|
if (fileName.Length < 2 || fileName[1..].Contains('-'))
|
|
result = null;
|
|
else
|
|
result = GetReversedDeterministicHashCodeB(fileName);
|
|
return result;
|
|
}
|
|
|
|
private static (string? check, int? normalizedPixelPercentage) GetReversedDeterministicHashCodeFromFace(string file, int idValue, int locationIndexValue, List<Models.Face> faces)
|
|
{
|
|
string? check;
|
|
int? normalizedPixelPercentage;
|
|
Models.Face face = faces[locationIndexValue];
|
|
if (face.Location?.NormalizedPixelPercentage is null)
|
|
{
|
|
check = null;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
else
|
|
{
|
|
string extensionLowered = Path.GetExtension(file).ToLower();
|
|
normalizedPixelPercentage = face.Location.NormalizedPixelPercentage.Value;
|
|
double deterministicHashCodeKey = GetDeterministicHashCodeFileName(idValue, normalizedPixelPercentage.Value);
|
|
check = Path.Combine(string.Concat(Path.GetDirectoryName(file)), $"{deterministicHashCodeKey}{extensionLowered}");
|
|
}
|
|
return (check, normalizedPixelPercentage);
|
|
}
|
|
|
|
private static (string? check, int? normalizedPixelPercentage) GetReversedDeterministicHashCodeAfterParse(Dictionary<int, List<Models.Face>> keyValuePairs, string file, int idValue, int locationIndexValue)
|
|
{
|
|
string? check;
|
|
int? normalizedPixelPercentage;
|
|
List<Models.Face> faces = keyValuePairs[idValue];
|
|
if (faces.Count > locationIndexValue)
|
|
(check, normalizedPixelPercentage) = GetReversedDeterministicHashCodeFromFace(file, idValue, locationIndexValue, faces);
|
|
else
|
|
{
|
|
check = null;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
return (check, normalizedPixelPercentage);
|
|
}
|
|
|
|
private static (string? check, string id, int? normalizedPixelPercentage) GetReversedDeterministicHashCodeFromCollection(Dictionary<int, List<Models.Face>> keyValuePairs, string file, string fileName)
|
|
{
|
|
string id;
|
|
string? check;
|
|
int? normalizedPixelPercentage;
|
|
string[] segments = fileName.Split(' ');
|
|
if (segments.Length < 3)
|
|
throw new Exception();
|
|
id = segments[2].Split('.')[0];
|
|
string locationIdex = segments[0];
|
|
if (int.TryParse(id, out int idValue) && int.TryParse(locationIdex, out int locationIndexValue) && keyValuePairs.ContainsKey(idValue))
|
|
(check, normalizedPixelPercentage) = GetReversedDeterministicHashCodeAfterParse(keyValuePairs, file, idValue, locationIndexValue);
|
|
else
|
|
{
|
|
check = null;
|
|
id = string.Empty;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
if (normalizedPixelPercentage is null)
|
|
id = string.Empty;
|
|
return new(check, id, normalizedPixelPercentage);
|
|
}
|
|
|
|
private static (string?, double?) GetReversedDeterministicHashCode(Dictionary<int, List<Models.Face>> keyValuePairs, string file, string fileName)
|
|
{
|
|
double? result;
|
|
string? check;
|
|
string id;
|
|
int? normalizedPixelPercentage;
|
|
if (keyValuePairs.Any())
|
|
(check, id, normalizedPixelPercentage) = GetReversedDeterministicHashCodeFromCollection(keyValuePairs, file, fileName);
|
|
else
|
|
{
|
|
check = null;
|
|
id = string.Empty;
|
|
normalizedPixelPercentage = null;
|
|
}
|
|
if (normalizedPixelPercentage is null || !double.TryParse(string.Concat(id, '.', normalizedPixelPercentage.Value), out double resultValue))
|
|
result = null;
|
|
else
|
|
result = resultValue;
|
|
return new(check, result);
|
|
}
|
|
|
|
internal static (string?, double?) GetReversedDeterministicHashCode(Dictionary<int, List<Models.Face>> keyValuePairs, string file)
|
|
{
|
|
double? result;
|
|
string? check;
|
|
string fileName = Path.GetFileName(file);
|
|
if (fileName.Contains('-'))
|
|
(check, result) = GetReversedDeterministicHashCode(keyValuePairs, file, fileName);
|
|
else
|
|
{
|
|
check = null;
|
|
result = null;
|
|
}
|
|
return new(check, result);
|
|
}
|
|
|
|
} |