196 lines
7.2 KiB
C#
196 lines
7.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Test_Merge
|
|
{
|
|
public class Zone
|
|
{
|
|
private Rectangle _bounds;
|
|
private List<Zone> _zones;
|
|
private List<Window> _windows;
|
|
private Bitmap _image;
|
|
|
|
public Bitmap ZoneImage
|
|
{
|
|
get
|
|
{
|
|
//This little trickery lets you have the image that the zone sees
|
|
Bitmap sample = new Bitmap(Bounds.Width, Bounds.Height);
|
|
Graphics g = Graphics.FromImage(sample);
|
|
g.DrawImage(Image, new Rectangle(0, 0, sample.Width, sample.Height), Bounds, GraphicsUnit.Pixel);
|
|
return sample;
|
|
}
|
|
}
|
|
public Bitmap Image
|
|
{
|
|
get { return _image; }
|
|
set
|
|
{
|
|
//It automatically sets the image for the contained windows and zones
|
|
_image = Image;
|
|
foreach (Window w in Windows)
|
|
{
|
|
w.Image = ZoneImage;
|
|
}
|
|
foreach (Zone z in Zones)
|
|
{
|
|
z.Image = Image;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Rectangle Bounds { get => _bounds; protected set => _bounds = value; }
|
|
public List<Zone> Zones { get => _zones; protected set => _zones = value; }
|
|
public List<Window> Windows { get => _windows; protected set => _windows = value; }
|
|
|
|
public Zone(Bitmap image, Rectangle bounds)
|
|
{
|
|
Windows = new List<Window>();
|
|
Zones = new List<Zone>();
|
|
|
|
//You cant set the image in the CTOR because the processing is impossible at first initiation
|
|
_image = image;
|
|
Bounds = bounds;
|
|
}
|
|
/// <summary>
|
|
/// Adds a zone to the list of zones
|
|
/// </summary>
|
|
/// <param name="zone">The zone you want to add</param>
|
|
public virtual void AddZone(Zone zone)
|
|
{
|
|
Zones.Add(zone);
|
|
}
|
|
/// <summary>
|
|
/// Add a window to the list of windows
|
|
/// </summary>
|
|
/// <param name="window">the window you want to add</param>
|
|
public virtual void AddWindow(Window window)
|
|
{
|
|
Windows.Add(window);
|
|
}
|
|
/// <summary>
|
|
/// Calls all the windows to do OCR and to give back the results so we can send them to the model
|
|
/// </summary>
|
|
/// <param name="driverList">A list of all the driver in the race to help with text recognition</param>
|
|
/// <returns>A driver data object that contains all the infos about a driver</returns>
|
|
public virtual async Task<DriverData> Decode(List<string> driverList)
|
|
{
|
|
int sectorCount = 0;
|
|
DriverData result = new DriverData();
|
|
Parallel.ForEach(Windows, async w =>
|
|
{
|
|
// A switch would be prettier but I dont think its supported in this C# version
|
|
if (w is DriverNameWindow)
|
|
result.Name = (string)await (w as DriverNameWindow).DecodePng(driverList);
|
|
if (w is DriverDrsWindow)
|
|
result.DRS = (bool)await (w as DriverDrsWindow).DecodePng();
|
|
if (w is DriverGapToLeaderWindow)
|
|
result.GapToLeader = (int)await (w as DriverGapToLeaderWindow).DecodePng();
|
|
if (w is DriverLapTimeWindow)
|
|
result.LapTime = (int)await (w as DriverLapTimeWindow).DecodePng();
|
|
if (w is DriverPositionWindow)
|
|
result.Position = (int)await (w as DriverPositionWindow).DecodePng();
|
|
if (w is DriverSectorWindow)
|
|
{
|
|
sectorCount++;
|
|
if (sectorCount == 1)
|
|
result.Sector1 = (int)await (w as DriverSectorWindow).DecodePng();
|
|
if (sectorCount == 2)
|
|
result.Sector2 = (int)await (w as DriverSectorWindow).DecodePng();
|
|
if (sectorCount == 3)
|
|
result.Sector3 = (int)await (w as DriverSectorWindow).DecodePng();
|
|
}
|
|
if (w is DriverTyresWindow)
|
|
result.CurrentTyre = (Tyre)await (w as DriverTyresWindow).DecodePng();
|
|
});
|
|
return result;
|
|
}
|
|
public virtual Bitmap Draw()
|
|
{
|
|
Image img = ZoneImage;
|
|
Graphics g = Graphics.FromImage(img);
|
|
foreach (Window w in Windows)
|
|
{
|
|
g.DrawRectangle(Pens.Blue, w.Bounds);
|
|
}
|
|
return (Bitmap)img;
|
|
}
|
|
public virtual string ToJSON()
|
|
{
|
|
string result = "";
|
|
result += "\"" + "Zone" + "\":{" + Environment.NewLine;
|
|
result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine;
|
|
result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine;
|
|
result += "\t" + "\"width\":" + Bounds.Width + "," + Environment.NewLine;
|
|
result += "\t" + "\"height\":" + Bounds.Height;
|
|
|
|
if (Windows.Count != 0)
|
|
{
|
|
result += "," + Environment.NewLine;
|
|
|
|
result += "\t" + "\"Windows\":[" + Environment.NewLine;
|
|
result += "\t\t{" + Environment.NewLine;
|
|
int Wcount = 0;
|
|
foreach (Window w in Windows)
|
|
{
|
|
result += "\t\t" + w.ToJSON();
|
|
Wcount++;
|
|
if (Wcount != Windows.Count)
|
|
result += ",";
|
|
}
|
|
result += "\t\t}" + Environment.NewLine;
|
|
result += "\t" + "]" + Environment.NewLine;
|
|
}
|
|
else
|
|
{
|
|
result += Environment.NewLine;
|
|
}
|
|
if (Zones.Count != 0)
|
|
{
|
|
result += "," + Environment.NewLine;
|
|
|
|
result += "\t" + "\"Zones\":[" + Environment.NewLine;
|
|
result += "\t\t{" + Environment.NewLine;
|
|
int Zcount = 0;
|
|
//foreach (Zone z in Zones)
|
|
//{
|
|
result += "\t\t" + Zones[0].ToJSON();
|
|
Zcount++;
|
|
if (Zcount != Zones.Count)
|
|
//result += ",";
|
|
//}
|
|
result += "\t\t}" + Environment.NewLine;
|
|
result += "\t" + "]" + Environment.NewLine;
|
|
}
|
|
else
|
|
{
|
|
result += Environment.NewLine;
|
|
}
|
|
|
|
result += "}";
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// Checks if the given Rectangle fits in the current zone
|
|
/// </summary>
|
|
/// <param name="InputRectangle">The Rectangle you want to check the fittment</param>
|
|
/// <returns></returns>
|
|
protected bool Fits(Rectangle inputRectangle)
|
|
{
|
|
if (inputRectangle.X + inputRectangle.Width > Bounds.Width || inputRectangle.Y + inputRectangle.Height > Bounds.Height || inputRectangle.X < 0 || inputRectangle.Y < 0)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|