Mike Phares 8863fd763f Removed old GetRectangle
RectangleIntersectMinimums
AddUserSecrets
2023-06-19 21:33:04 -07:00

319 lines
14 KiB
C#

using System.Drawing;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Location
{
internal static bool Check(int bottom, int left, int right, int top, int zCount, bool throwException)
{
bool result = true;
if (left < 0)
result = false;
if (right < 0)
result = false;
if (right < left)
result = false;
if (top < 0)
result = false;
if (bottom < 0)
result = false;
if (bottom < top)
result = false;
if (zCount < 0)
result = false;
if (throwException && !result)
throw new Exception();
return result;
}
internal static bool Check(int bottom, int height, int left, int right, int top, int width, int zCount, bool throwException)
{
bool result = true;
if (bottom > height)
result = false;
if (left > width)
result = false;
if (right > width)
result = false;
if (top > height)
result = false;
if (zCount < 0)
result = false;
if (result)
result = Check(bottom, left, right, top, zCount, throwException);
if (throwException && !result)
throw new Exception();
return result;
}
internal static string GetLeftPadded(int locationDigits, string value)
{
string result;
if (value.Length == locationDigits)
result = value;
else if (value.Length > locationDigits)
result = value[..locationDigits];
else
result = value.PadLeft(locationDigits, '0');
return result;
}
internal static (decimal?, decimal?, decimal?, decimal?) GetHeightLeftTopWidth(int bottom, int height, int left, int right, int top, int width, int zCount)
{
(decimal?, decimal?, decimal?, decimal?) result;
bool verified = Check(bottom, height, left, right, top, width, zCount, throwException: false);
decimal t = top;
decimal l = left;
decimal w = right - l;
decimal h = bottom - t;
decimal xHeightPercentageFactored = h / height;
decimal xLeftPercentageFactored = l / width;
decimal xTopPercentageFactored = t / height;
decimal xWidthPercentageFactored = w / width;
if (!verified)
result = new(null, null, null, null);
else
result = new(xHeightPercentageFactored, xLeftPercentageFactored, xTopPercentageFactored, xWidthPercentageFactored);
return result;
}
internal static int GetNormalizedRectangle(int bottom, int height, int left, int locationDigits, int right, int top, int width, int zCount)
{
int result;
string check;
bool verified = Check(bottom, height, left, right, top, width, zCount, throwException: false);
int checksum = left > top ? 4 : 8;
if (!verified)
check = string.Concat(checksum, new string('0', locationDigits - 1));
else
{
decimal factor = 100;
int factorMinusOne = (int)factor - 1;
int length = (locationDigits - 1) / 4;
decimal x = left / (decimal)width * factor;
decimal y = top / (decimal)height * factor;
decimal w = (right - left) / (decimal)width * factor;
decimal h = (bottom - top) / (decimal)height * factor;
string xPadded = x < factor ? ILocation.GetLeftPadded(length, (int)x) : ILocation.GetLeftPadded(length, factorMinusOne);
string yPadded = y < factor ? ILocation.GetLeftPadded(length, (int)y) : ILocation.GetLeftPadded(length, factorMinusOne);
string widthPadded = w < factor ? ILocation.GetLeftPadded(length, (int)w) : ILocation.GetLeftPadded(length, factorMinusOne);
string heightPadded = h < factor ? ILocation.GetLeftPadded(length, (int)h) : ILocation.GetLeftPadded(length, factorMinusOne);
check = string.Concat(checksum, xPadded, yPadded, widthPadded, heightPadded);
}
long value = long.Parse(check);
if (value > int.MaxValue)
throw new Exception();
result = (int)value;
return result;
}
internal static int GetConfidencePercent(int faceConfidencePercent, float[] rangeFaceConfidence, double confidence)
{
int result = (int)(confidence / rangeFaceConfidence[1] * faceConfidencePercent);
return result;
}
internal static Rectangle? GetNormalizedRectangle(int locationDigits, string normalizedRectangle)
{
Rectangle? result;
int length = (locationDigits - 1) / 4;
string[] segments = new string[]
{
normalizedRectangle[..1],
normalizedRectangle.Substring(1, length),
normalizedRectangle.Substring(3, length),
normalizedRectangle.Substring(5, length),
normalizedRectangle.Substring(7, length)
};
if (string.Join(string.Empty, segments) != normalizedRectangle)
result = null;
else
{
if (!int.TryParse(segments[1], out int xNormalized) || !int.TryParse(segments[2], out int yNormalized) || !int.TryParse(segments[3], out int wNormalized) || !int.TryParse(segments[4], out int hNormalized))
result = null;
else
result = new(xNormalized, yNormalized, wNormalized, hNormalized);
}
return result;
}
internal static Rectangle? GetRectangle(int height, int locationDigits, string normalizedRectangle, int width)
{
Rectangle? result;
Rectangle? rectangle = GetNormalizedRectangle(locationDigits, normalizedRectangle);
if (rectangle is null)
result = null;
else
{
decimal factor = 100;
result = new((int)(rectangle.Value.X / factor * width), (int)(rectangle.Value.Y / factor * height), (int)(rectangle.Value.Width / factor * width), (int)(rectangle.Value.Height / factor * height));
}
return result;
}
internal static Rectangle? GetRectangle(int locationDigits, string normalizedRectangle, Models.OutputResolution outputResolution)
{
Rectangle? result;
if (normalizedRectangle.Length != locationDigits || normalizedRectangle[0] is not '4' and not '8')
throw new NotSupportedException("Old way has been removed!");
(int width, int height) = OutputResolution.Get(outputResolution);
result = GetRectangle(height, locationDigits, normalizedRectangle, width);
if (result is null)
throw new NullReferenceException(nameof(result));
return result;
}
private static bool Matches(Models.OutputResolution outputResolution, DatabaseFile databaseFile)
{
bool result = outputResolution.Height == databaseFile.FileHeight && outputResolution.Width == databaseFile.FileWidth;
return result;
}
internal static Rectangle? GetRectangle(DatabaseFile databaseFile, Marker marker, Models.OutputResolution outputResolution)
{
Rectangle? result;
bool matches = Matches(outputResolution, databaseFile);
if (!matches)
result = null;
else
result = new((int)Math.Ceiling(marker.X * databaseFile.FileWidth), (int)Math.Ceiling(marker.Y * databaseFile.FileHeight), (int)Math.Ceiling(marker.W * databaseFile.FileWidth), (int)Math.Ceiling(marker.H * databaseFile.FileHeight));
return result;
}
private static Models.Location? GetLocation(DatabaseFile databaseFile, Marker marker, Rectangle rectangle)
{
Models.Location? result;
bool verified = Check(rectangle.Bottom, databaseFile.FileHeight, rectangle.Left, rectangle.Right, rectangle.Top, databaseFile.FileWidth, zCount: 1, throwException: false);
if (!verified)
result = null;
else
result = new(rectangle.Bottom, marker.Score / 100, rectangle.Left, rectangle.Right, rectangle.Top);
return result;
}
internal static Models.Location? GetLocation(int height, Rectangle rectangle, int width)
{
Models.Location? result;
double confidence = 0;
bool verified = Check(rectangle.Bottom, height, rectangle.Left, rectangle.Right, rectangle.Top, width, zCount: 1, throwException: false);
if (!verified)
result = null;
else
result = new(rectangle.Bottom, confidence, rectangle.Left, rectangle.Right, rectangle.Top);
return result;
}
internal static Models.Location? GetLocation(DatabaseFile databaseFile, Marker marker, Models.OutputResolution outputResolution)
{
Models.Location? result;
Rectangle? rectangle = GetRectangle(databaseFile, marker, outputResolution);
if (rectangle is null)
result = null;
else
result = GetLocation(databaseFile, marker, rectangle.Value);
return result;
}
internal static List<Models.Location> GetLocations<T>(List<MappingFromPhotoPrism> mappingFromPhotoPrismCollection, List<Models.Face> faces, List<LocationContainer<T>> containers)
{
List<Models.Location> results = new();
bool any;
bool matches;
Rectangle dlibRectangle;
Rectangle? prismRectangle;
Models.Location? location;
Rectangle intersectRectangle;
Models.OutputResolution? outputResolution = null;
foreach (Models.Face face in faces)
{
if (face.Location is null || face.OutputResolution is null)
continue;
results.Add(face.Location);
outputResolution ??= face.OutputResolution;
}
int before = results.Count;
foreach (LocationContainer<T> locationContainer in containers)
{
if (locationContainer.Location is null)
continue;
results.Add(locationContainer.Location);
}
foreach (MappingFromPhotoPrism mappingFromPhotoPrism in mappingFromPhotoPrismCollection)
{
if (outputResolution is null)
break;
matches = Matches(outputResolution, mappingFromPhotoPrism.DatabaseFile);
if (!matches)
break;
foreach (Marker marker in mappingFromPhotoPrism.Markers)
{
any = false;
prismRectangle = GetRectangle(mappingFromPhotoPrism.DatabaseFile, marker, outputResolution);
if (prismRectangle is null)
break;
location = GetLocation(mappingFromPhotoPrism.DatabaseFile, marker, prismRectangle.Value);
if (location is null)
break;
foreach (LocationContainer<T> locationContainer in containers)
{
if (any)
continue;
if (locationContainer.Location is null)
continue;
dlibRectangle = new(locationContainer.Location.Left, locationContainer.Location.Top, locationContainer.Location.Right - locationContainer.Location.Left, locationContainer.Location.Bottom - locationContainer.Location.Top);
intersectRectangle = Rectangle.Intersect(prismRectangle.Value, dlibRectangle);
if (intersectRectangle.Width == 0 && intersectRectangle.Height == 0)
continue;
any = true;
break;
}
foreach (Models.Face face in faces)
{
if (any)
continue;
if (face.Location is null || face.OutputResolution is null)
continue;
dlibRectangle = new(face.Location.Left, face.Location.Top, face.Location.Right - face.Location.Left, face.Location.Bottom - face.Location.Top);
intersectRectangle = Rectangle.Intersect(prismRectangle.Value, dlibRectangle);
if (intersectRectangle.Width == 0 && intersectRectangle.Height == 0)
continue;
any = true;
break;
}
if (!any)
results.Add(location);
}
}
if (before == results.Count)
results.Clear();
return results;
}
internal static List<Models.Face> FilterByIntersect(Models.Face[] faces, int normalizedRectangle, float rectangleIntersectMinimum)
{
List<Models.Face> results = new();
double? percent;
Rectangle checkRectangle;
Rectangle? sourceRectangle;
Rectangle intersectRectangle;
foreach (Models.Face face in faces)
{
if (face.Location is null || face.OutputResolution is null)
continue;
checkRectangle = new(face.Location.Left, face.Location.Top, face.Location.Right - face.Location.Left, face.Location.Bottom - face.Location.Top);
sourceRectangle = GetRectangle(Stateless.ILocation.Digits, normalizedRectangle.ToString(), face.OutputResolution);
if (sourceRectangle is null)
continue;
intersectRectangle = Rectangle.Intersect(checkRectangle, sourceRectangle.Value);
if (intersectRectangle.Width == 0 || intersectRectangle.Height == 0)
continue;
percent = (double)intersectRectangle.Width * intersectRectangle.Height / (checkRectangle.Width * checkRectangle.Height);
if (percent < rectangleIntersectMinimum)
continue;
results.Add(face);
}
return results;
}
}