private Color ReadColorAtPosition(ReadPosition pos) { if (currentFABitmap.BitmapIsValid()) { int absoluteX = Convert.ToInt32(pos.RelativeX * currentFABitmap.Width); int absoluteY = Convert.ToInt32(pos.RelativeY * currentFABitmap.Height); Color colorAtPos = currentFABitmap.ReadPixel(absoluteX - ReadRadius, absoluteY - ReadRadius, ReadRadius * 2 + 1, ReadRadius * 2 + 1); return(colorAtPos); } return(Color.Empty); }
public static Ellipse CircleByIndicies(int faceIndex, int rowIndex, int colIndex) { for (int i = 0; i < PositionsToReadAt.GetLength(0); i++) { for (int j = 0; j < PositionsToReadAt.GetLength(1); j++) { ReadPosition tempPosition = PositionsToReadAt[i, j]; if (tempPosition.RowIndex == rowIndex && tempPosition.ColIndex == colIndex && tempPosition.FaceIndex == faceIndex) { return(tempPosition.Circle); } } } return(null); }
private static void SortAndValidateColors() { List <ReadPosition> allPositions = AllReadPositions(); // Positions left to assign List <ReadPosition> positions = new List <ReadPosition>(allPositions); // Assign color percentages to each color for (int i = 0; i < positions.Count; i++) { positions[i].Percentages = ColorIdentification.CalculateColorPercentages(positions[i].Color); } for (int i = 0; i < 6; i++) { CubeColor currentCubeColor = (CubeColor)i; int[] maxIndicies = ColorIdentification.Max8Indicies(currentCubeColor, positions); for (int j = 0; j < maxIndicies.Length; j++) { ReadPosition currentPosition = positions[maxIndicies[j]]; // Assign tiles to the cube MainWindow.cube.SetTile((CubeFace)(currentPosition.FaceIndex), currentPosition.RowIndex * 3 + currentPosition.ColIndex, currentCubeColor); currentPosition.AssumedCubeColor = currentCubeColor; // Dye the circle of the readposition in the corresponding color Application.Current.Dispatcher.Invoke(() => { // Change circle color of the current position on the gui CircleByIndicies(currentPosition.FaceIndex, currentPosition.RowIndex, currentPosition.ColIndex).Fill = Helper.ColorBrush(currentCubeColor); }); } // Sort maxIndicies -> highest value at index=0 maxIndicies = maxIndicies.OrderByDescending(c => c).ToArray(); // Remove all positions, that were assigned to the cube in this loop cycle for (int j = 0; j < maxIndicies.Length; j++) { positions.RemoveAt(maxIndicies[j]); } } }
private static Ellipse DrawCircleAtPosition(ReadPosition pos, Canvas canvas) { Ellipse circle = null; Application.Current.Dispatcher.Invoke(() => { circle = GenerateCircle(pos.AssumedCubeColor); circle.ToolTip = string.Format("{0}[{1},{2}]", (CubeColor)pos.FaceIndex, pos.RowIndex, pos.ColIndex); circle.MouseUp += Circle_MouseUp; // Set position of circle on canvas Canvas.SetLeft(circle, pos.RelativeX * canvas.ActualWidth - ReadRadius); Canvas.SetTop(circle, pos.RelativeY * canvas.ActualHeight - ReadRadius); // Add circle to the canvas over the camera stream canvas.Children.Add(circle); }); return(circle); }
public static string AddPosition(ReadPosition readPosition, int cameraIndex) { List <ReadPosition> slice = GetSlice(cameraIndex); if (PositionExists(readPosition.FaceIndex, readPosition.RowIndex, readPosition.ColIndex)) { return("Position not added: Entered Tile is already being scanned"); } else if (slice.Count >= MAXPOSITIONSPERCAMERA) { return(string.Format("Position not added: Camera already has {0} Tiles to scan", MAXPOSITIONSPERCAMERA)); } else { pendingPositions[cameraIndex].Enqueue(readPosition); return("Position added"); } }
private static bool PositionExists(int faceIndex, int rowIndex, int colIndex) { for (int i = 0; i < PositionsToReadAt.GetLength(0); i++) { for (int j = 0; j < PositionsToReadAt.GetLength(1); j++) { ReadPosition pos = PositionsToReadAt[i, j]; if (pos == null) { continue; } else if (pos.FaceIndex == faceIndex && pos.RowIndex == rowIndex && pos.ColIndex == colIndex) { return(true); } } } return(false); }
private void Run() { while (!threadShouldStop) { long loopStart = Helper.CurrentTimeMillis(); long loopEnd = loopStart + 1000 / ticksPerSecond; // Code in while loop // Handle all frameUpdates if (frameToDisplay != null) { currentFABitmap.SetBitmap(new Bitmap(frameToDisplay)); frameToDisplay.Dispose(); frameToDisplay = null; } // Add all pendingPositions to PositionsToReadAt and draw their circles while (pendingPositions[cameraIndex].Count > 0) { ReadPosition pos = pendingPositions[cameraIndex].Dequeue(); for (int i = 0; i < PositionsToReadAt.GetLength(1); i++) { if (PositionsToReadAt[cameraIndex, i] == null) { pos.Circle = DrawCircleAtPosition(pos, drawingCanvas); PositionsToReadAt[cameraIndex, i] = pos; TotalPositionCount++; break; } } } // Read all colors from positions in readPositions for (int i = 0; i < PositionsToReadAt.GetLength(1); i++) { if (PositionsToReadAt[cameraIndex, i] == null) { continue; } PositionsToReadAt[cameraIndex, i].Color = ReadColorAtPosition(PositionsToReadAt[cameraIndex, i]); } // This block will be only executed by the primary webcamcontrol if (cameraIndex == 0) { // If the whole cube is scanned, send the cube-configuration to the cube solver if (CubeIsFullyScanned()) { SortAndValidateColors(); } lastCubeGeneration = Helper.CurrentTimeMillis(); } // Code in while loop if (GC.GetTotalMemory(true) > 500 * Math.Pow(10, 6)) { GC.Collect(); } while (Helper.CurrentTimeMillis() < loopEnd) { Thread.Sleep(Convert.ToInt32(loopEnd - Helper.CurrentTimeMillis())); } } }