Moved more to Map library
This commit is contained in:
91
Shared/Models/Stateless/Methods/Mapping.cs
Normal file
91
Shared/Models/Stateless/Methods/Mapping.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
internal abstract class Mapping
|
||||
{
|
||||
|
||||
private static double GetDeterministicHashCodeKey(int id, int normalizedPixelPercentage)
|
||||
=> Math.Round(double.Parse($"{id}.{normalizedPixelPercentage}"), Stateless.ILocation.Digits);
|
||||
|
||||
internal static double GetDeterministicHashCodeKey(Models.Item item, Models.Closest closest)
|
||||
{
|
||||
double result;
|
||||
if (item.Property?.Id is null || item.ImageFileHolder is null)
|
||||
throw new NullReferenceException();
|
||||
result = GetDeterministicHashCodeKey(item.Property.Id.Value, closest.NormalizedPixelPercentage);
|
||||
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 = GetDeterministicHashCodeKey(item.Property.Id.Value, face.Location.NormalizedPixelPercentage.Value);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void UseKeyValuePairsSaveFaceEncoding(Dictionary<int, List<Models.Face>> keyValuePairs, string file, int id, int normalizedPixelPercentageValue, double deterministicHashCodeKey, string extensionLowered)
|
||||
{
|
||||
string json;
|
||||
string checkFile;
|
||||
string? directoryName;
|
||||
List<Models.Face> collection = new();
|
||||
List<Models.Face> faces = keyValuePairs[id];
|
||||
foreach (Models.Face face in faces)
|
||||
{
|
||||
if (face.FaceEncoding is null || face.Location?.NormalizedPixelPercentage is null)
|
||||
continue;
|
||||
if (normalizedPixelPercentageValue != face.Location.NormalizedPixelPercentage.Value && deterministicHashCodeKey != GetDeterministicHashCodeKey(id, face.Location.NormalizedPixelPercentage.Value))
|
||||
continue;
|
||||
collection.Add(face);
|
||||
}
|
||||
if (collection.Count != 1)
|
||||
throw new Exception();
|
||||
foreach (Models.Face face in collection)
|
||||
{
|
||||
directoryName = Path.GetDirectoryName(file);
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
continue;
|
||||
checkFile = Path.Combine(directoryName, $"{deterministicHashCodeKey}{extensionLowered}.json");
|
||||
if (File.Exists(checkFile))
|
||||
continue;
|
||||
json = JsonSerializer.Serialize(face.FaceEncoding);
|
||||
_ = IPath.WriteAllText(checkFile, json, updateDateWhenMatches: false, compareBeforeWrite: true, updateToWhenMatches: null);
|
||||
}
|
||||
}
|
||||
|
||||
private static double? GetReversedDeterministicHashCodeFromSegments(bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file, string[] segments)
|
||||
{
|
||||
double? result;
|
||||
if (segments.Length != 3)
|
||||
throw new Exception();
|
||||
string id = segments[0];
|
||||
string normalizedPixelPercentage = segments[1];
|
||||
if (!int.TryParse(id, out int idValue) || !int.TryParse(normalizedPixelPercentage, out int normalizedPixelPercentageValue))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
result = GetDeterministicHashCodeKey(idValue, normalizedPixelPercentageValue);
|
||||
if (keyValuePairsAny && keyValuePairs.ContainsKey(idValue))
|
||||
UseKeyValuePairsSaveFaceEncoding(keyValuePairs, file, idValue, normalizedPixelPercentageValue, result.Value, $".{segments[2]}");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static double? GetReversedDeterministicHashCodeKey(bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file)
|
||||
{
|
||||
double? result;
|
||||
string fileName = Path.GetFileName(file);
|
||||
if (fileName.Length < 2 || fileName[1..].Contains('-'))
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
string[] segments = fileName.Split('.');
|
||||
result = GetReversedDeterministicHashCodeFromSegments(keyValuePairsAny, keyValuePairs, file, segments);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user