public static Cube AnalyseCube(List<ColorFilter> filters, int faceletMinWidth, int faceletMinHeight, ArduinoChannel channel) { /* 1- Get all images from Arduino 2- Open a thread to each face and call ImageAnalyser.AnalyseImageSample() 3- Wait all 6 threads 4- Sort the positions of the facelets to the 6 faces 5- Create the string of original cube 6- Create new cube */ if (channel == null) { return null; } // inicializa visão do Arduino channel.SendInitVision(); Cube newCube = new Cube(); const int nFaces = 6; ManualResetEvent[] doneEvents = new ManualResetEvent[nFaces]; ImageSample imgSmp = new ImageSample(); imgSmp.FaceletMinWidth = faceletMinWidth; imgSmp.FaceletMinHeight = faceletMinHeight; List<ImageAnalyser> analysers = new List<ImageAnalyser>(); for (int i = 0; i < nFaces; i++) { doneEvents[i] = new ManualResetEvent(false); ImageAnalyser imgAn = new ImageAnalyser(); imgAn.Filters = filters; imgSmp.FaceNumber = i; PositioningFace(channel, i+1); imgSmp.SampleImg = CubeAnalyser.SourceImage; imgAn.Sample = imgSmp.Clone(); analysers.Add(imgAn); ThreadPool.QueueUserWorkItem(imgAn.AnalyseImageSample, doneEvents[i]); } foreach (ManualResetEvent item in doneEvents) { WaitHandle hdl = (WaitHandle)item; hdl.WaitOne(); } int[] faces; Dictionary<CubeFace, CubeFaceType> cubeMap = new Dictionary<CubeFace, CubeFaceType>(); for (int i = 0; i < nFaces; i++) { Dictionary<CubeFaceletType, CubeFaceColor> tmpFacelets = new Dictionary<CubeFaceletType, CubeFaceColor>(); faces = SortFacelets((CubeFaceType)i); for (int j = 0; j < 9; j++) { tmpFacelets.Add((CubeFaceletType)faces[j], GetCubeFaceColor(analysers[i].Sample.Cores[j])); } cubeMap.Add(new CubeFace(tmpFacelets), (CubeFaceType)i); } newCube = new Cube(cubeMap); return newCube; }
private static bool PositioningFace(ArduinoChannel channel, int numberFace) { RotateCubeMovement rotMov = new RotateCubeMovement(); switch (numberFace) { case 1: // não faz nada, é a primeira face break; case 2: rotMov.Axis = CoordinateAxis.Y; rotMov.Type = TurnType.HalfTurnRight; channel.SendVisualizationMovement(rotMov); break; case 3: rotMov.Axis = CoordinateAxis.Y; rotMov.Type = TurnType.HalfTurnRight; channel.SendVisualizationMovement(rotMov); break; case 4: rotMov.Axis = CoordinateAxis.Y; rotMov.Type = TurnType.HalfTurnRight; channel.SendVisualizationMovement(rotMov); break; case 5: rotMov.Axis = CoordinateAxis.X; rotMov.Type = TurnType.HalfTurnRight; channel.SendVisualizationMovement(rotMov); rotMov = new RotateCubeMovement(); rotMov.Axis = CoordinateAxis.Y; rotMov.Type = TurnType.HalfTurnRight; channel.SendVisualizationMovement(rotMov); break; case 6: rotMov.Axis = CoordinateAxis.Y; rotMov.Type = TurnType.FullTurn; channel.SendVisualizationMovement(rotMov); break; default: return false; } return true; }