// Use this for initialization void Start() { audioSource = GetComponent <AudioSource>(); lineCreation = GetComponent <LineCreation>(); followMouse = false; mousePosition = Vector2.zero; lastPosition = mousePosition; cannonPosition = GetComponent <Transform>().position; }
public static void AddPointToDrawing(ref PCad.Model model, Action saveToHistory) { CoordinateCreation.BakePosition(model.InteractionState.focusPosition); model.Sketch.coordinateSystem.SetAnchorPosition(MouseInput.RaycastPosition); switch (model.Tool.CurrentGeometryType) { case GeometryType.Point: model.Sketch.geometries.Add(PointCreation.NewPoint(model.InteractionState.focusPosition)); saveToHistory(); break; case GeometryType.Line: if (!(model.InteractionState.incompleteGeometry is LineModel)) { model.InteractionState.incompleteGeometry = LineCreation.StartNewLine(model.InteractionState.focusPosition); model.Sketch.geometries.Add(model.InteractionState.incompleteGeometry); } else { LineCreation.CompleteLine(model.InteractionState.incompleteGeometry as LineModel, model.InteractionState.focusPosition); model.InteractionState.incompleteGeometry = null; saveToHistory(); } break; case GeometryType.Rectangle: if (!(model.InteractionState.incompleteGeometry is RectangleModel)) { model.InteractionState.incompleteGeometry = RectangleCreation.StartNewRectangle(model.InteractionState.focusPosition, model.Tool.CurrentGeometryColor); model.Sketch.geometries.Add(model.InteractionState.incompleteGeometry); } else { RectangleCreation.CompleteRectangle(model.InteractionState.incompleteGeometry as RectangleModel, model.InteractionState.focusPosition); model.InteractionState.incompleteGeometry = null; saveToHistory(); } break; default: throw new ArgumentOutOfRangeException(); } // reset input model.InteractionState.keyboardInputModel = new NumpadInput.Model(); }
private static void UpdateGeometry(ref GeometryModel incompleteGeometry, Vec <Coordinate> focusPosition) { switch (incompleteGeometry) { case RectangleModel rectangleModel: RectangleCreation.UpdateRectangle(rectangleModel, focusPosition); break; case LineModel lineModel: LineCreation.UpdateLine(lineModel, focusPosition); break; } }
public void LineCreationTest() { foreach (string image in A_ImageManipulationTests.Images) { Bitmap bitmap = new Bitmap($"Images/Out/{image}/5 thinned.png"); Color[] pixels = bitmap.ToColorArray(); //Create bool array based on black and white bool[][] boolArray = new bool[bitmap.Height][]; for (int y = 0; y < bitmap.Height; y++) { boolArray[y] = new bool[bitmap.Width]; for (int x = 0; x < bitmap.Width; x++) { if (pixels[y * bitmap.Width + x].GetBrightness() < 0.1) { boolArray[y][x] = true; } } } //Creates part of lines List <List <PointF> > lineParts = boolArray.CreateLineParts(); //Cuts out points in the lines lineParts.ReduceLines(); //Finds loops List <List <PointF> > loops = lineParts.CreateLoops(); //Connects remaining lines lineParts.ConnectLines(); //Creates line objects List <Line> lines = LineCreation.CreateLineObjects(lineParts, loops, new Bitmap($"Images/Out/{image}/3 balanced.png")); //Creates a map Map map = new Map { Id = 3, Lines = lines, Ratio = 1.414 }; //Saves it File.WriteAllText($"Images/Out/{image}/3.json", JsonConvert.SerializeObject(map).Replace("\"IsEmpty\":false,", "")); } }
public void LineCreationSingleTest() { Bitmap bitmap = new Bitmap("Images/Thin.png"); Color[] pixels = bitmap.ToColorArray(); //Create bool array based on black and white bool[][] boolArray = new bool[bitmap.Height][]; for (int y = 0; y < bitmap.Height; y++) { boolArray[y] = new bool[bitmap.Width]; for (int x = 0; x < bitmap.Width; x++) { if (pixels[y * bitmap.Width + x].GetBrightness() < 0.1) { boolArray[y][x] = true; } } } Random rnd = new Random(123456); //Creates part of lines List <List <PointF> > lineParts = boolArray.CreateLineParts(); Bitmap bitall = new Bitmap(bitmap.Width, bitmap.Height); bitall.SetPixel(0, 0, Color.Black); bitall.SetPixel(bitall.Width - 1, bitall.Height - 1, Color.Black); for (int index = 0; index < lineParts.Count; index++) { List <PointF> pointFs = lineParts[index]; Bitmap bitout = new Bitmap(bitmap.Width, bitmap.Height); Color randomColor = Color.FromArgb(rnd.Next(50, 200), rnd.Next(50, 200), rnd.Next(50, 200)); bitout.SetPixel(0, 0, Color.Black); bitout.SetPixel(bitout.Width - 1, bitout.Height - 1, Color.Black); foreach (PointF pointF in pointFs) { bitout.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); bitall.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); } if (!Directory.Exists("Images/Out/Line")) { Directory.CreateDirectory("Images/Out/Line"); } bitout.Save($"Images/Out/Line/{index}.png", ImageFormat.Png); } bitall.Save("Images/Out/Line/_1.png"); //Cuts out points in the lines lineParts.ReduceLines(); bitall = new Bitmap(bitmap.Width, bitmap.Height); bitall.SetPixel(0, 0, Color.Black); bitall.SetPixel(bitall.Width - 1, bitall.Height - 1, Color.Black); for (int index = 0; index < lineParts.Count; index++) { List <PointF> pointFs = lineParts[index]; Bitmap bitout = new Bitmap(bitmap.Width, bitmap.Height); Color randomColor = Color.FromArgb(rnd.Next(50, 200), rnd.Next(50, 200), rnd.Next(50, 200)); bitout.SetPixel(0, 0, Color.Black); bitout.SetPixel(bitout.Width - 1, bitout.Height - 1, Color.Black); foreach (PointF pointF in pointFs) { bitout.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); bitall.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); } bitout.Save($"Images/Out/Line/{index} - reduced.png", ImageFormat.Png); } bitall.Save("Images/Out/Line/_2.png"); //Finds loops List <List <PointF> > loops = lineParts.CreateLoops(); bitall = new Bitmap(bitmap.Width, bitmap.Height); bitall.SetPixel(0, 0, Color.Black); bitall.SetPixel(bitall.Width - 1, bitall.Height - 1, Color.Black); for (int index = 0; index < loops.Count; index++) { List <PointF> pointFs = loops[index]; Bitmap bitout = new Bitmap(bitmap.Width, bitmap.Height); Color randomColor = Color.FromArgb(rnd.Next(50, 200), rnd.Next(50, 200), rnd.Next(50, 200)); bitout.SetPixel(0, 0, Color.Black); bitout.SetPixel(bitout.Width - 1, bitout.Height - 1, Color.Black); foreach (PointF pointF in pointFs) { bitout.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); bitall.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); } bitout.Save($"Images/Out/Line/loop - {index}.png", ImageFormat.Png); } bitall.Save("Images/Out/Line/_3.png"); //Connects remaining lines lineParts.ConnectLines(); bitall = new Bitmap(bitmap.Width, bitmap.Height); bitall.SetPixel(0, 0, Color.Black); bitall.SetPixel(bitall.Width - 1, bitall.Height - 1, Color.Black); for (int index = 0; index < lineParts.Count; index++) { List <PointF> pointFs = lineParts[index]; Bitmap bitout = new Bitmap(bitmap.Width, bitmap.Height); Color randomColor = Color.FromArgb(rnd.Next(50, 200), rnd.Next(50, 200), rnd.Next(50, 200)); bitout.SetPixel(0, 0, Color.Black); bitout.SetPixel(bitout.Width - 1, bitout.Height - 1, Color.Black); foreach (PointF pointF in pointFs) { bitout.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); bitall.SetPixel((int)pointF.X, (int)pointF.Y, randomColor); } bitout.Save($"Images/Out/Line/line - {index}.png", ImageFormat.Png); } bitall.Save("Images/Out/Line/_4.png"); //Creates line objects List <Line> lines = LineCreation.CreateLineObjects(lineParts, loops, bitmap); //Creates a map Map map = new Map { Id = 3, Lines = lines, Ratio = 1.414 }; //Saves it File.WriteAllText("Images/Out/Line/_json.json", JsonConvert.SerializeObject(map).Replace("\"IsEmpty\":false,", "")); }
public IActionResult Create(IFormCollection form) { //Start of working directory string workingDirectory = Path.Combine(_workingDirectory, "Working "); // ReSharper disable once RedundantAssignment, needs to be there for non debug compile Bitmap initialImage = null; #if !DEBUG try { #endif #region Setup //Checks there is a file if (form.Files.Count == 0) { return(new BadRequestResult()); } //Gets the id for this upload, locked so only one thread can enter at a time int id = SetupId(ref workingDirectory); //Cleans up Working folders that are leftover for some reason on every 20th pass if (id % 20 == 0) { Thread cleanWorkingDirectories = new Thread(CleanWorkingDirectories); cleanWorkingDirectories.Start(); } //Saves the file sent and get if transformation is needed bool transform = ProcessFormData(form, workingDirectory); //Tries to load file sent as image and will return an UnsupportedMediaTypeResult if file can't be loaded to a bitmap try { //Tries to load the image initialImage = LoadImage(form, workingDirectory); } catch (Exception) { Directory.Delete(workingDirectory, true); return(new UnsupportedMediaTypeResult()); } #endregion #region Image Manipulation //Scales image to be less than a certain number of pixels Bitmap scaledImage = ScaleImage(workingDirectory, initialImage, transform); Bitmap perspectiveImage; //Will only run this if transform flag has been checked if (transform) { //Finds possible rectangles with OpenCV Point[][] rectangles = OpenCVWrapper.IdentifyRectangles($"\"{Path.Combine(workingDirectory, "scaled.png")}\""); if (rectangles == null || rectangles.Length == 0) { initialImage.Dispose(); Directory.Delete(workingDirectory, true); return(new StatusCodeResult((int)HttpStatusCode.InternalServerError)); } #if DEBUG Bitmap temp = Debug.DrawPoints(scaledImage, rectangles); temp.Save(Path.Combine(_workingDirectory, "Debug", "3 Rectangles.png"), ImageFormat.Png); #endif //Finds the correct rectangle Point[] paper = scaledImage.IdentifyPaperCorners(rectangles); if (paper == null || paper.Length != 4) { initialImage.Dispose(); Directory.Delete(workingDirectory, true); return(new StatusCodeResult((int)HttpStatusCode.InternalServerError)); } #if DEBUG temp = Debug.DrawPoints(scaledImage, paper); temp.Save(Path.Combine(_workingDirectory, "Debug", "4 Paper.png"), ImageFormat.Png); #endif perspectiveImage = TransformImage(scaledImage, paper); } else { perspectiveImage = scaledImage; } #endregion #region Color Identification //Gets threshold array for image bool[][] threshold = CreateThresholds(perspectiveImage); //Thins lines ThinLines(threshold); #endregion #region Line Identification //Finds lines List <List <PointF> > lineParts = threshold.CreateLineParts(); //Reduces number of points in lines lineParts.ReduceLines(); //Finds loops List <List <PointF> > loops = lineParts.CreateLoops(); //Joins remaining lines lineParts.ConnectLines(); //Create line objects List <Line> lines = LineCreation.CreateLineObjects(lineParts, loops, perspectiveImage); #endregion #region Map Creation //Creates a map Map map = new Map { Id = id, Lines = lines, Ratio = (double)perspectiveImage.Width / perspectiveImage.Height }; //Converts map to json string json = JsonConvert.SerializeObject(map).Replace("\"IsEmpty\":false,", ""); SaveMap(id, json); #endregion initialImage.Dispose(); Directory.Delete(workingDirectory, true); //Returns map return(new ObjectResult(json) { //Sets the media type to be json instead of string ContentTypes = new MediaTypeCollection { "application/json", "charset=utf-8" } }); #if !DEBUG } catch { initialImage?.Dispose(); if (Directory.Exists(workingDirectory)) { Directory.Delete(workingDirectory, true); } return(new StatusCodeResult((int)HttpStatusCode.InternalServerError)); } #endif }