77 lines
2.7 KiB
C#

namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Mapping
{
internal static (string?, string?, string?, bool?) GetSegments(string facesFileNameExtension, string fileName)
{
string[] segments = fileName.Split('.');
string? id;
string? extensionLowered;
bool? needsFacesFileNameExtension;
string? normalizedRectangle;
if (segments.Length < 4 || $".{segments[3]}" != facesFileNameExtension)
{
id = null;
extensionLowered = null;
normalizedRectangle = null;
needsFacesFileNameExtension = null;
}
else
{
id = segments[0];
extensionLowered = $".{segments[2]}";
normalizedRectangle = segments[1];
needsFacesFileNameExtension = segments.Length == 3;
}
return new(id, normalizedRectangle, extensionLowered, needsFacesFileNameExtension);
}
private static (int?, int?) GetConvertedFromSegments(string facesFileNameExtension, string fileName)
{
int? id;
int? normalizedRectangle;
(string? Id, string? NormalizedRectangle, string? ExtensionLowered, bool? Check) segments = GetSegments(facesFileNameExtension, fileName);
if (string.IsNullOrEmpty(segments.Id) || string.IsNullOrEmpty(segments.NormalizedRectangle) || string.IsNullOrEmpty(segments.ExtensionLowered) || segments.Check is null)
{
id = null;
normalizedRectangle = null;
}
else if (!int.TryParse(segments.Id, out int idValue) || !int.TryParse(segments.NormalizedRectangle, out int normalizedRectangleValue))
{
id = null;
normalizedRectangle = null;
}
else
{
id = idValue;
normalizedRectangle = normalizedRectangleValue;
}
return new(id, normalizedRectangle);
}
internal static (int?, int?) GetConverted(string facesFileNameExtension, string file)
{
int? id;
int? normalizedRectangle;
string fileName = Path.GetFileName(file);
if (fileName.Length >= 2 && !fileName[1..].Contains('-'))
(id, normalizedRectangle) = GetConvertedFromSegments(facesFileNameExtension, fileName);
else
{
id = null;
normalizedRectangle = null;
}
return new(id, normalizedRectangle);
}
internal static int GetAreaPermille(int faceAreaPermille, int bottom, int height, int left, int right, int top, int width)
{
int result;
double area = width * height;
double locationArea = (right - left) * (bottom - top);
result = (int)Math.Round(locationArea / area * faceAreaPermille, 0);
return result;
}
}