/// <summary> /// /// </summary> /// <param name="MhNr"></param> public void calcDMXRatios(int MhNr) { Log.Debug("calcDMXRatios"); Log.DebugFormat("calcDMXRatios: MhNrSetupComplete.ContainsKey({0}: {1}", MhNr, MhNrSetupComplete.ContainsKey(MhNr)); Log.DebugFormat("calcDMXRatios: MhNrSetupComplete[{0}]: {1}", MhNr, MhNrSetupComplete[MhNr]); Log.DebugFormat("calcDMXRatios: DMXSetupValues.ContainsKey: {0}", DMXSetupValues.ContainsKey(MhNr)); if (MhNrSetupComplete.ContainsKey(MhNr) && MhNrSetupComplete[MhNr] && DMXSetupValues.ContainsKey(MhNr)) { Log.DebugFormat("of moving head: {0}, Model.Map: {1}", MhNr, Model.Map); //calc epsilon = 5% of avg of height and with of map int epsilon = (int)Math.Round(((Model.Map.GetHeight() + Model.Map.GetWidth()) / 2) * 0.05); //get the relevant sample-points List<DMXImgSetupMappingValue> points = DMXSetupValues[MhNr]; if (!MhNrIsVirtual[MhNr]) { //values in points.VirtualVal are currently image positions //convert them into virtual positions Log.Debug("calcDMXRatios: convert DMXSetupValues.VirtualVal from image to virtual positions"); List<DMXImgSetupMappingValue> virtualPoints = new List<DMXImgSetupMappingValue>(); DMXImgSetupMappingValue val = new DMXImgSetupMappingValue(); foreach (DMXImgSetupMappingValue entry in points) { VPosition pos = new VPosition(entry.VirtualVal.X, entry.VirtualVal.Y); Log.DebugFormat("calcDMXRatios: image pos: {0}, {1}", pos.X, pos.Y); Point virtualPos = getToPosition(pos); Log.DebugFormat("calcDMXRatios: virtual pos: {0}, {1}", virtualPos.X, virtualPos.Y); val.DMXImgVal = entry.DMXImgVal; val.VirtualVal = virtualPos; virtualPoints.Add(val); } DMXSetupValues[MhNr] = virtualPoints; Log.DebugFormat("calcDMXRatios: DMXSetupValues[0].Count: {0}", DMXSetupValues[0].Count); Log.DebugFormat("calcDMXRatios: DMXSetupValues[0][0].VirtualVal: {0}, {1}", DMXSetupValues[0][0].VirtualVal.X, DMXSetupValues[0][0].VirtualVal.Y); MhNrIsVirtual[MhNr] = true; } Log.DebugFormat("calcDMXRatios: points.Count: {0}", points.Count); Tuple<ValueMapping, ValueMapping> valueMappings = null; //Tuple<ValueMapping, ValueMapping> valueMappings = calcRatios(epsilon, points); if (valueMappings != null) { VirtualToDMXMapping[MhNr] = valueMappings.Item1; Log.DebugFormat("calcDMXRatios: VirtualToDMXMapping.GetStartY.K: {0}", VirtualToDMXMapping[MhNr].GetStartY.K); Log.DebugFormat("calcDMXRatios: VirtualToDMXMapping.GetStartY.D: {0}", VirtualToDMXMapping[MhNr].GetStartY.D); DMXToVirtualMapping[MhNr] = valueMappings.Item2; Log.DebugFormat("calcDMXRatios: DMXToVirtualMapping.GetStartY.K: {0}", DMXToVirtualMapping[MhNr].GetStartY.K); Log.DebugFormat("calcDMXRatios: DMXToVirtualMapping.GetStartY.D: {0}", DMXToVirtualMapping[MhNr].GetStartY.D); } // else: Warning from DEBUG-mode is already logged! } }
/// <summary> /// At this time, all setup-values are available and are now processed to the correct VM datastructures /// </summary> public void doImageSetup() { Log.Debug("perform image setup"); if (ImgSetupPositions.Count >= 4) { Log.Debug(" enough positions to perform image setup"); List<Point> points = new List<Point>(); Point p = new Point(); List<Position> positions = new List<Position>(); Log.Debug(" image setup log 1"); foreach (KeyValuePair<int, Position> entry in ImgSetupPositions) { positions.Add(entry.Value); } Log.Debug(" image setup log 2"); foreach (Position pos in positions) { int distanceX = (int)Math.Pow(pos.X - pos.X_Direction, 2); int distanceY = (int)Math.Pow(pos.Y - pos.Y_Direction, 2); int radius = (int)Math.Round(Math.Sqrt(distanceX + distanceY)); p.X = pos.X - radius; p.Y = pos.Y - radius; points.Add(p); } int maxValueX = (int)Math.Sqrt(int.MaxValue); int maxValueY = maxValueX; if (ImgSetup.Width < maxValueX) { maxValueX = ImgSetup.Width; } if (ImgSetup.Height < maxValueY) { maxValueY = ImgSetup.Height; } Log.Debug(" image setup log 3"); p.X = 0; p.Y = 0; int topLeft = getClosestToPoint(p, points); Log.Debug(" image setup log 4"); p.X = maxValueX; int topRight = getClosestToPoint(p, points); Log.Debug(" image setup log 5"); p.Y = maxValueY; int bottomRight = getClosestToPoint(p, points); Log.Debug(" image setup log 6"); p.X = 0; int bottomLeft = getClosestToPoint(p, points); Log.Debug(" image setup log 7"); DMXImgSetupMappingValue val = new DMXImgSetupMappingValue(); val.DMXImgVal = points[topLeft]; p.Y = 0; val.VirtualVal = p; ImgSetupValues.Add(val); Log.Debug(" image setup log 8"); val.DMXImgVal = points[topRight]; p.X = Model.Map.GetWidth(); val.VirtualVal = p; ImgSetupValues.Add(val); Log.Debug(" image setup log 9"); val.DMXImgVal = points[bottomRight]; p.Y = Model.Map.GetHeight(); val.VirtualVal = p; ImgSetupValues.Add(val); Log.Debug(" image setup log 10"); val.DMXImgVal = points[bottomLeft]; p.X = 0; val.VirtualVal = p; ImgSetupValues.Add(val); Log.Debug(" image setup log 11"); ImgSetupDone = true; calcImgRatios(); Log.Debug("image setup complete"); } else { Log.WarnFormat("Found only {0} setup-positions", ImgSetupPositions.Count); } try { // Just a very optimistic idea in diagnostics using (StreamWriter sout = new StreamWriter(@"C:\Temp\fhv_game.test.txt")) { sout.Write("Test at " + System.DateTime.Now); } } catch (Exception e) { Log.Fatal(e); } }
private void SetupPositionReached(MsgSetupPositionReached obj) { Log.Debug("SetupPositionReached"); if (!MhNrSetupComplete.ContainsKey(obj.Pos.MhNr)) { Log.DebugFormat("SetupPositionReached: set mh: {0}", obj.Pos.MhNr); MhNrSetupComplete[obj.Pos.MhNr] = false; MhNrIsVirtual[obj.Pos.MhNr] = false; } if (MhNrSetupComplete[obj.Pos.MhNr]) { // new setup -> reset all old values Log.Debug("SetupPositionReached: reset all old values"); DMXSetupValues.Clear(); MhNrEntityIdMapping.Clear(); MhNrSetupComplete.Clear(); MhNrSetupComplete[obj.Pos.MhNr] = false; MhNrIsVirtual.Clear(); MhNrIsVirtual[obj.Pos.MhNr] = false; } if (!DMXSetupValues.ContainsKey(obj.Pos.MhNr)) { Log.DebugFormat("SetupPositionReached: create a DMXSetupValue for mh: {0}", obj.Pos.MhNr); DMXSetupValues[obj.Pos.MhNr] = new List<DMXImgSetupMappingValue>(); } DMXImgSetupMappingValue val = new DMXImgSetupMappingValue(); Log.DebugFormat("SetupPositionReached: dmx pos: {0}, {1}", obj.Pos.Pos.X, obj.Pos.Pos.Y); val.DMXImgVal = obj.Pos.Pos; if (!MhNrEntityIdMapping.ContainsKey(obj.Pos.MhNr)) { // get next free entityId Log.Debug("SetupPositionReached: get next free entityId"); foreach (KeyValuePair<int, VImageParseResultEntity> entry in ImgParseResults) { if (!MhNrEntityIdMapping.ContainsValue(entry.Key)) { Log.DebugFormat("SetupPositionReached: entityId: {0}", entry.Key); MhNrEntityIdMapping[obj.Pos.MhNr] = entry.Key; break; } } } // TODO: maybe throw exception if no imageParseResult is available if (MhNrEntityIdMapping.ContainsKey(obj.Pos.MhNr)) { VPosition imgPos = ImgParseResults[MhNrEntityIdMapping[obj.Pos.MhNr]].Pos; Log.DebugFormat("SetupPositionReached: img pos: {0}, {1}", imgPos.X, imgPos.Y); //use virtual pos //Point virtualPos = getToPosition(imgPos); //Log.DebugFormat("SetupPositionReached: virtual pos: {0}, {1}", virtualPos.X, virtualPos.Y); //val.VirtualVal = virtualPos; //use image pos (later mapping necessary!) Point pos = new Point(imgPos.X, imgPos.Y); val.VirtualVal = pos; } Log.DebugFormat("SetupPositionReached: old DMXSetupValues.Count: {0}", DMXSetupValues[obj.Pos.MhNr].Count); DMXSetupValues[obj.Pos.MhNr].Add(val); Log.DebugFormat("SetupPositionReached: new DMXSetupValues.Count: {0}", DMXSetupValues[obj.Pos.MhNr].Count); if (obj.IsEndPosition) { Log.Debug("SetupPositionReached: is end pos"); MhNrSetupComplete[obj.Pos.MhNr] = true; } //Console.WriteLine(obj.Pos); }