public void Add(RTrack rtr) { TempStream.Position = 32 * Count; TempWrite.Write(rtr.Slope.X); TempWrite.Write(rtr.Slope.Y); TempWrite.Write(rtr.Position.X); TempWrite.Write(rtr.Position.Y); Count++; Sum.X += rtr.Position.X; Sum.Y += rtr.Position.Y; Average.X = Sum.X / Count; Average.Y = Sum.Y / Count; }
public RTrack Get(int i) { if (i < 0 || i >= Count) { throw new Exception("Wrong index " + i + " (Count = " + Count + ")"); } TempStream.Position = 32 * i; RTrack rtr = new RTrack(); rtr.Slope.X = TempRead.ReadDouble(); rtr.Slope.Y = TempRead.ReadDouble(); rtr.Position.X = TempRead.ReadDouble(); rtr.Position.Y = TempRead.ReadDouble(); return(rtr); }
static void Main(string[] args) { if (args.Length != 13 && args.Length != 4 && args.Length != 2) { Console.WriteLine("usage: FlattenTLG.exe <cell map> <TLG to be flattened> <output TLG> <min matches>"); Console.WriteLine(" or"); Console.WriteLine("usage: FlattenTLG.exe <cell map> <min matches>"); Console.WriteLine(" (opens a console to query the transformation map generator)"); Console.WriteLine(" or"); Console.WriteLine("usage: FlattenTLG.exe <reference TLG (supposed flat)> <TLG to be flattened> <output TLG> <cell size> <slope tol> <pos tol> <pos sweep> <z projection> <selection string> <min matches> <z adjust> <z step> <parallel (true|false)>"); Console.WriteLine("Selection function variables:"); foreach (SelFunc sf in KnownFunctions) { Console.WriteLine(sf.Name + " -> " + sf.Desc); } return; } bool usereadymap = (args.Length < 13); bool useconsole = (args.Length == 2); string reftlg = args[0]; string worktlg = useconsole ? "" : args[1]; string outtlg = useconsole ? "" : args[2]; uint MinMatches = 0; int xcells = 0; int ycells = 0; double cellsize = 0.0; int ix, iy; int i, j, k; SySal.BasicTypes.Vector2 Center = new SySal.BasicTypes.Vector2(); SySal.BasicTypes.Rectangle WorkRect; SySal.Scanning.Plate.IO.OPERA.LinkedZone worklz = null; if (useconsole == false) { worklz = SySal.DataStreams.OPERALinkedZone.FromFile(worktlg); WorkRect = worklz.Extents; } else { WorkRect = new SySal.BasicTypes.Rectangle(); } SySal.BasicTypes.Rectangle RefRect = new SySal.BasicTypes.Rectangle(); RTrackCell[,] WorkCells; if (usereadymap) { MinMatches = Convert.ToUInt32(args[useconsole ? 1 : 3]); System.IO.StreamReader cr = new System.IO.StreamReader(args[0]); while (cr.EndOfStream == false) { System.Text.RegularExpressions.Match m = rx_CellMap.Match(cr.ReadLine()); if (m.Success) { RefRect.MinX = Convert.ToDouble(m.Groups[1].Value); RefRect.MaxX = Convert.ToDouble(m.Groups[2].Value); RefRect.MinY = Convert.ToDouble(m.Groups[3].Value); RefRect.MaxY = Convert.ToDouble(m.Groups[4].Value); cellsize = Convert.ToDouble(m.Groups[5].Value); xcells = Convert.ToInt32(m.Groups[6].Value); ycells = Convert.ToInt32(m.Groups[7].Value); break; } } Center.X = 0.5 * (RefRect.MinX + RefRect.MaxX); Center.Y = 0.5 * (RefRect.MinY + RefRect.MaxY); WorkCells = new RTrackCell[xcells, ycells]; for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { SySal.BasicTypes.Rectangle rect = new SySal.BasicTypes.Rectangle(); rect.MinX = RefRect.MinX + ix * cellsize; rect.MaxX = rect.MinX + cellsize; rect.MinY = RefRect.MinY + iy * cellsize; rect.MaxY = rect.MinY + cellsize; WorkCells[ix, iy] = new RTrackCell(rect, Center); WorkCells[ix, iy].Result = NumericalTools.ComputationResult.InvalidInput; } } while (cr.EndOfStream == false) { System.Text.RegularExpressions.Match m = rx_Cell.Match(cr.ReadLine()); if (m.Success) { ix = Convert.ToInt32(m.Groups[1].Value); iy = Convert.ToInt32(m.Groups[2].Value); WorkCells[ix, iy].Result = NumericalTools.ComputationResult.OK; WorkCells[ix, iy].Matches = Convert.ToInt32(m.Groups[3].Value); WorkCells[ix, iy].Average.X = Convert.ToDouble(m.Groups[4].Value); WorkCells[ix, iy].Average.Y = Convert.ToDouble(m.Groups[5].Value); WorkCells[ix, iy].AlignInfo.MXX = Convert.ToDouble(m.Groups[6].Value); WorkCells[ix, iy].AlignInfo.MXY = Convert.ToDouble(m.Groups[7].Value); WorkCells[ix, iy].AlignInfo.MYX = Convert.ToDouble(m.Groups[8].Value); WorkCells[ix, iy].AlignInfo.MYY = Convert.ToDouble(m.Groups[9].Value); WorkCells[ix, iy].AlignInfo.TX = Convert.ToDouble(m.Groups[10].Value); WorkCells[ix, iy].AlignInfo.TY = Convert.ToDouble(m.Groups[11].Value); WorkCells[ix, iy].AlignInfo.TZ = Convert.ToDouble(m.Groups[12].Value); WorkCells[ix, iy].SlopeAlignInfo.X = Convert.ToDouble(m.Groups[13].Value); WorkCells[ix, iy].SlopeAlignInfo.Y = Convert.ToDouble(m.Groups[14].Value); } } cr.Close(); if (useconsole) { GridInterpolation G1 = new GridInterpolation(WorkCells, cellsize, RefRect, (int)MinMatches); Console.WriteLine("Type a pair of coordinates ( X Y ) to get the transformation map.\r\nEOF (CTRL+Z to exit)."); double x, y; string line; while ((line = Console.ReadLine()) != null) { System.Text.RegularExpressions.Match m = rx_XY.Match(line); x = Convert.ToDouble(m.Groups[1].Value); y = Convert.ToDouble(m.Groups[2].Value); SySal.BasicTypes.Vector dslope = new SySal.BasicTypes.Vector(); SySal.DAQSystem.Scanning.IntercalibrationInfo dpos = new SySal.DAQSystem.Scanning.IntercalibrationInfo(); bool result = G1.Evaluate(x, y, ref dslope, ref dpos); Console.WriteLine(x + " " + y + " -> " + (result ? "OK" : "FAILED") + " " + dpos.RX + " " + dpos.RY + " " + dpos.MXX + " " + dpos.MXY + " " + dpos.MYX + " " + dpos.MYY + " " + dpos.TX + " " + dpos.TY + " " + dpos.TZ + " " + dslope.X + " " + dslope.Y); } return; } } else { cellsize = Convert.ToDouble(args[3]); double slopetol = Convert.ToDouble(args[4]); double postol = Convert.ToDouble(args[5]); double possweep = Convert.ToDouble(args[6]); double DZ = Convert.ToDouble(args[7]); string selstring = args[8]; MinMatches = Convert.ToUInt32(args[9]); double ZAdj = Convert.ToDouble(args[10]); double ZStep = Convert.ToDouble(args[11]); bool IsParallel = Convert.ToBoolean(args[12]); NumericalTools.CStyleParsedFunction S = new NumericalTools.CStyleParsedFunction(selstring); dSel[] pMap = new dSel[S.ParameterList.Length]; for (j = 0; j < S.ParameterList.Length; j++) { string sp = S.ParameterList[j]; for (i = 0; i < KnownFunctions.Length && String.Compare(sp, KnownFunctions[i].Name, true) != 0; i++) { ; } if (i == KnownFunctions.Length) { throw new Exception("Unknown parameter \"" + sp + "\"."); } pMap[j] = KnownFunctions[i].Evaluate; } SySal.Scanning.Plate.IO.OPERA.LinkedZone reflz = SySal.DataStreams.OPERALinkedZone.FromFile(reftlg); RefRect = reflz.Extents; if (WorkRect.MinX > RefRect.MinX) { RefRect.MinX = WorkRect.MinX; } if (WorkRect.MaxX < RefRect.MaxX) { RefRect.MaxX = WorkRect.MaxX; } if (WorkRect.MinY > RefRect.MinY) { RefRect.MinY = WorkRect.MinY; } if (WorkRect.MaxY < RefRect.MaxY) { RefRect.MaxY = WorkRect.MaxY; } Center.X = 0.5 * (RefRect.MinX + RefRect.MaxX); Center.Y = 0.5 * (RefRect.MinY + RefRect.MaxY); xcells = (int)Math.Ceiling((RefRect.MaxX - RefRect.MinX) / cellsize); ycells = (int)Math.Ceiling((RefRect.MaxY - RefRect.MinY) / cellsize); Console.WriteLine("X/Y Cells: " + xcells + "/" + ycells); if (xcells <= 0 || ycells <= 0) { throw new Exception("Null working area."); } RTrackCell[,] RefCells = new RTrackCell[xcells, ycells]; WorkCells = new RTrackCell[xcells, ycells]; for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { SySal.BasicTypes.Rectangle rect = new SySal.BasicTypes.Rectangle(); rect.MinX = RefRect.MinX + ix * cellsize; rect.MaxX = rect.MinX + cellsize; rect.MinY = RefRect.MinY + iy * cellsize; rect.MaxY = rect.MinY + cellsize; RefCells[ix, iy] = new RTrackCell(rect, Center); WorkCells[ix, iy] = new RTrackCell(rect, Center); } } SySal.Scanning.Plate.IO.OPERA.LinkedZone lz; RTrackCell[,] rtc; for (i = 0; i < 2; i++) { if (i == 0) { lz = reflz; rtc = RefCells; } else { lz = worklz; rtc = WorkCells; } for (j = 0; j < lz.Length; j++) { SySal.Scanning.MIPBaseTrack tk = lz[j] as SySal.Scanning.MIPBaseTrack; for (k = 0; k < pMap.Length; k++) { S[k] = pMap[k](tk); } if (S.Evaluate() != 0.0) { ix = (int)((tk.Info.Intercept.X - RefRect.MinX) / cellsize); iy = (int)((tk.Info.Intercept.Y - RefRect.MinY) / cellsize); if (ix >= 0 && ix < xcells && iy >= 0 && iy < ycells) { RTrack rtr = new RTrack(); rtr.Slope.X = tk.Info.Slope.X; rtr.Slope.Y = tk.Info.Slope.Y; rtr.Position.X = tk.Info.Intercept.X; rtr.Position.Y = tk.Info.Intercept.Y; rtc[ix, iy].Add(rtr); } } } } for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { Console.WriteLine("Ref " + RefCells[ix, iy].Average.X + " " + RefCells[ix, iy].Average.Y + " " + RefCells[ix, iy].Count); Console.WriteLine("Work " + WorkCells[ix, iy].Average.X + " " + WorkCells[ix, iy].Average.Y + " " + WorkCells[ix, iy].Count); } } SySal.Processing.QuickMapping.QuickMapper QM = new SySal.Processing.QuickMapping.QuickMapper(); SySal.Processing.QuickMapping.Configuration qmc = QM.Config as SySal.Processing.QuickMapping.Configuration; qmc.FullStatistics = false; qmc.UseAbsoluteReference = true; qmc.PosTol = postol; qmc.SlopeTol = slopetol; for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { SySal.Tracking.MIPEmulsionTrackInfo[] rinfo = new SySal.Tracking.MIPEmulsionTrackInfo[RefCells[ix, iy].Count]; SySal.Tracking.MIPEmulsionTrackInfo[] winfo = new SySal.Tracking.MIPEmulsionTrackInfo[WorkCells[ix, iy].Count]; for (i = 0; i < 2; i++) { SySal.Tracking.MIPEmulsionTrackInfo[] inf = (i == 0) ? rinfo : winfo; RTrackCell[,] cells = (i == 0) ? RefCells : WorkCells; double dz = (i == 0) ? 0.0 : DZ; for (j = 0; j < inf.Length; j++) { RTrack r = cells[ix, iy].Get(j); inf[j] = new SySal.Tracking.MIPEmulsionTrackInfo(); inf[j].Slope.X = r.Slope.X; inf[j].Slope.Y = r.Slope.Y; inf[j].Intercept.X = r.Position.X; inf[j].Intercept.Y = r.Position.Y; inf[j].Intercept.Z = dz; } } SySal.Scanning.PostProcessing.PatternMatching.TrackPair[] pairs = new SySal.Scanning.PostProcessing.PatternMatching.TrackPair[0]; double bestdz = 0.0; if (rinfo.Length >= 2 && winfo.Length >= 2) { double dz1; if (IsParallel) { System.Collections.ArrayList thrarr = new System.Collections.ArrayList(); for (dz1 = -ZAdj; dz1 <= ZAdj; dz1 += ZStep) { MapThread mthr = new MapThread(); mthr.m_rinfo = rinfo; mthr.m_winfo = winfo; mthr.m_DZ = DZ + dz1; mthr.m_PosSweep = possweep; mthr.m_PosTol = postol; mthr.m_SlopeTol = slopetol; mthr.m_Thread = new System.Threading.Thread(new System.Threading.ThreadStart(mthr.Execute)); mthr.m_Thread.Start(); thrarr.Add(mthr); } foreach (MapThread mt in thrarr) { mt.m_Thread.Join(); if (mt.m_Pairs.Length > pairs.Length) { bestdz = mt.m_DZ - DZ; pairs = mt.m_Pairs; } } } else { for (dz1 = -ZAdj; dz1 <= ZAdj; dz1 += ZStep) { SySal.Scanning.PostProcessing.PatternMatching.TrackPair[] qairs = QM.Match(rinfo, winfo, DZ + dz1, possweep, possweep); if (qairs.Length > pairs.Length) { bestdz = dz1; pairs = qairs; } } } } double[] alignpars = new double[7]; SySal.BasicTypes.Vector2 slopedelta = new SySal.BasicTypes.Vector2(); SySal.BasicTypes.Vector2 slopetolv = new SySal.BasicTypes.Vector2(); double[] dslx = new double[pairs.Length]; double[] dsly = new double[pairs.Length]; for (j = 0; j < pairs.Length; j++) { dslx[j] = pairs[j].First.Info.Slope.X - pairs[j].Second.Info.Slope.X; } PeakFit(dslx, slopetol, out slopedelta.X, out slopetolv.X); for (j = 0; j < pairs.Length; j++) { dsly[j] = pairs[j].First.Info.Slope.Y - pairs[j].Second.Info.Slope.Y; } PeakFit(dsly, slopetol, out slopedelta.Y, out slopetolv.Y); int gooddslopes = 0; for (j = 0; j < pairs.Length; j++) { if ((slopedelta.X - slopetolv.X) < dslx[j] && dslx[j] < (slopedelta.X + slopetolv.X) && (slopedelta.Y - slopetolv.Y) < dsly[j] && dsly[j] < (slopedelta.Y + slopetolv.Y)) { gooddslopes++; } } if (gooddslopes > 0) { double[] DX = new double[gooddslopes]; double[] DY = new double[gooddslopes]; double[] X = new double[gooddslopes]; double[] Y = new double[gooddslopes]; double[] SX = new double[gooddslopes]; double[] SY = new double[gooddslopes]; for (j = i = 0; j < pairs.Length; j++) { if ((slopedelta.X - slopetolv.X) < dslx[j] && dslx[j] < (slopedelta.X + slopetolv.X) && (slopedelta.Y - slopetolv.Y) < dsly[j] && dsly[j] < (slopedelta.Y + slopetolv.Y)) { X[i] = pairs[j].Second.Info.Intercept.X - WorkCells[ix, iy].AlignInfo.RX; Y[i] = pairs[j].Second.Info.Intercept.Y - WorkCells[ix, iy].AlignInfo.RY; SX[i] = pairs[j].Second.Info.Slope.X; SY[i] = pairs[j].Second.Info.Slope.Y; DX[i] = pairs[j].First.Info.Intercept.X - pairs[j].Second.Info.Intercept.X; DY[i] = pairs[j].First.Info.Intercept.Y - pairs[j].Second.Info.Intercept.Y; //System.IO.File.AppendAllText(@"c:\flattentlg.txt", "\r\n" + ix + " " + iy + " " + i + " " + j + " " + pairs.Length + " " + gooddslopes + " " + WorkCells[ix, iy].AlignInfo.RX + " " + WorkCells[ix, iy].AlignInfo.RY + " " + X[i] + " " + Y[i] + " " + SX[i] + " " + SY[i] + " " + DX[i] + " " + DY[i] + " " + bestdz); i++; } } WorkCells[ix, iy].Result = IteratedAffineFocusing(DX, DY, X, Y, SX, SY, postol, ref alignpars); } else { WorkCells[ix, iy].Result = NumericalTools.ComputationResult.InvalidInput; } WorkCells[ix, iy].Matches = pairs.Length; WorkCells[ix, iy].AlignInfo.TZ = alignpars[6] + bestdz; WorkCells[ix, iy].AlignInfo.TX = alignpars[4]; WorkCells[ix, iy].AlignInfo.TY = alignpars[5]; WorkCells[ix, iy].AlignInfo.MXX = 1.0 + alignpars[0]; WorkCells[ix, iy].AlignInfo.MXY = 0.0 + alignpars[1]; WorkCells[ix, iy].AlignInfo.MYX = 0.0 + alignpars[2]; WorkCells[ix, iy].AlignInfo.MYY = 1.0 + alignpars[3]; WorkCells[ix, iy].SlopeAlignInfo = slopedelta; Console.WriteLine("Fit " + WorkCells[ix, iy].Result + " " + WorkCells[ix, iy].AlignInfo.MXX + " " + WorkCells[ix, iy].AlignInfo.MXY + " " + WorkCells[ix, iy].AlignInfo.MYX + " " + WorkCells[ix, iy].AlignInfo.MYY + " " + WorkCells[ix, iy].AlignInfo.TX + " " + WorkCells[ix, iy].AlignInfo.TY + " " + WorkCells[ix, iy].AlignInfo.TZ + " " + WorkCells[ix, iy].SlopeAlignInfo.X + " " + WorkCells[ix, iy].SlopeAlignInfo.Y); } } int goodcells = 0; for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { if (WorkCells[ix, iy].Result == NumericalTools.ComputationResult.OK && WorkCells[ix, iy].Matches >= MinMatches) { goodcells++; } } } Console.WriteLine("Good cells: " + goodcells); Console.WriteLine("--------CELLS"); Console.WriteLine("CELLMAP " + RefRect.MinX + " " + RefRect.MaxX + " " + RefRect.MinY + " " + RefRect.MaxY + " " + cellsize + " " + xcells + " " + ycells); Console.WriteLine("IX\tIY\tN\tX\tY\tMXX\tMXY\tMYX\tMYY\tTX\tTY\tTZ\tTDSX\tTDSY"); for (ix = 0; ix < xcells; ix++) { for (iy = 0; iy < ycells; iy++) { if (WorkCells[ix, iy].Result == NumericalTools.ComputationResult.OK && WorkCells[ix, iy].Matches >= MinMatches) { Console.WriteLine(ix + "\t" + iy + "\t" + WorkCells[ix, iy].Matches + "\t" + WorkCells[ix, iy].Average.X + "\t" + WorkCells[ix, iy].Average.Y + "\t" + WorkCells[ix, iy].AlignInfo.MXX + "\t" + WorkCells[ix, iy].AlignInfo.MXY + "\t" + WorkCells[ix, iy].AlignInfo.MYX + "\t" + WorkCells[ix, iy].AlignInfo.MYY + "\t" + WorkCells[ix, iy].AlignInfo.TX + "\t" + WorkCells[ix, iy].AlignInfo.TY + "\t" + WorkCells[ix, iy].AlignInfo.TZ + "\t" + WorkCells[ix, iy].SlopeAlignInfo.X + "\t" + WorkCells[ix, iy].SlopeAlignInfo.Y); } } } Console.WriteLine("--------ENDCELLS"); } SySal.DataStreams.OPERALinkedZone.Writer outlzw = new SySal.DataStreams.OPERALinkedZone.Writer(outtlg, worklz.Id, worklz.Extents, worklz.Center, worklz.Transform); outlzw.SetZInfo(worklz.Top.TopZ, worklz.Top.BottomZ, worklz.Bottom.TopZ, worklz.Bottom.BottomZ); for (i = 0; i < ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.Side)worklz.Top).ViewCount; i++) { outlzw.AddView(((SySal.Scanning.Plate.IO.OPERA.LinkedZone.Side)worklz.Top).View(i), true); } for (i = 0; i < ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.Side)worklz.Bottom).ViewCount; i++) { outlzw.AddView(((SySal.Scanning.Plate.IO.OPERA.LinkedZone.Side)worklz.Bottom).View(i), false); } SySal.BasicTypes.Vector proj = new SySal.BasicTypes.Vector(); Console.WriteLine("Writing flattened TLG..."); GridInterpolation G = new GridInterpolation(WorkCells, cellsize, RefRect, (int)MinMatches); System.DateTime start = System.DateTime.Now; for (i = 0; i < worklz.Length; i++) { if (i % 1000 == 0) { System.DateTime nw = System.DateTime.Now; if ((nw - start).TotalMilliseconds >= 10000) { Console.WriteLine((i * 100 / worklz.Length) + "%"); start = nw; } } SySal.Tracking.MIPEmulsionTrackInfo baseinfo = worklz[i].Info; SySal.DAQSystem.Scanning.IntercalibrationInfo transforminfo = new SySal.DAQSystem.Scanning.IntercalibrationInfo(); transforminfo.RX = Center.X; transforminfo.RY = Center.Y; SySal.BasicTypes.Vector tds = new SySal.BasicTypes.Vector(); G.Evaluate(baseinfo.Intercept.X, baseinfo.Intercept.Y, ref tds, ref transforminfo); proj.X = -baseinfo.Slope.X * transforminfo.TZ; proj.Y = -baseinfo.Slope.Y * transforminfo.TZ; proj.Z = 0.0; baseinfo.Intercept = transforminfo.Transform(baseinfo.Intercept) + proj; baseinfo.Slope = transforminfo.Deform(baseinfo.Slope) + tds; SySal.Scanning.MIPIndexedEmulsionTrack toptk = worklz[i].Top; SySal.Tracking.MIPEmulsionTrackInfo topinfo = toptk.Info; SySal.Scanning.MIPIndexedEmulsionTrack bottomtk = worklz[i].Bottom; SySal.Tracking.MIPEmulsionTrackInfo bottominfo = bottomtk.Info; topinfo.Intercept = transforminfo.Transform(topinfo.Intercept) + proj; topinfo.Slope = transforminfo.Deform(topinfo.Slope) + tds; bottominfo.Intercept = transforminfo.Transform(bottominfo.Intercept) + proj; bottominfo.Slope = transforminfo.Deform(bottominfo.Slope) + tds; outlzw.AddMIPEmulsionTrack(topinfo, i, ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.MIPIndexedEmulsionTrack)toptk).View.Id, ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.MIPIndexedEmulsionTrack)toptk).OriginalRawData, true); outlzw.AddMIPEmulsionTrack(bottominfo, i, ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.MIPIndexedEmulsionTrack)bottomtk).View.Id, ((SySal.Scanning.Plate.IO.OPERA.LinkedZone.MIPIndexedEmulsionTrack)bottomtk).OriginalRawData, false); outlzw.AddMIPBasetrack(baseinfo, i, i, i); } outlzw.Complete(); Console.WriteLine("Written \"" + outtlg + "\"."); }