예제 #1
0
 public static void RegisterFailedPyramid(PyramidEntry entry)
 {
     lock (s_SyncLock)
     {
         s_FailedPyramidEntries.Add(entry);
     }
 }
예제 #2
0
 public static void RegisterFailedPyramid(PyramidEntry entry)
 {
     lock(s_SyncLock)
     {
         s_FailedPyramidEntries.Add(entry);
     }
 }
예제 #3
0
		private LeastSquareFittedAstrometry SolveStarPairs(
			IStarMap starMap,
			Dictionary<ImagePixel, IStar> matchedPairs,
			Dictionary<int, ulong> matchedFeatureIdToStarIdIndexes,
			ThreeStarFit.StarPair pair_i,
			ThreeStarFit.StarPair pair_j,
			ThreeStarFit.StarPair pair_k,
			double fittedFocalLength,
			PyramidEntry pyramidLog, 
            int? minMatchedStars = null)
		{
			double RA0Deg, DE0Deg;

			ThreeStarFit coarseFit = new ThreeStarFit(m_PlateConfig, pair_i, pair_j, pair_k);
			if (!coarseFit.IsSolved)
			{
				if (coarseFit.IsSingularity)
				{
                    if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
					    Trace.WriteLine("ThreeStarFit.Var1 - Singularity");

					Dictionary<ImagePixel, IStar> threeStarDict = new Dictionary<ImagePixel, IStar>();

				    try
				    {
                        threeStarDict.Add(ImagePixel.CreateImagePixelWithFeatureId(0, 255, pair_i.XImage, pair_i.YImage), pair_i.Star);
                        threeStarDict.Add(ImagePixel.CreateImagePixelWithFeatureId(1, 255, pair_j.XImage, pair_j.YImage), pair_j.Star);
                        threeStarDict.Add(ImagePixel.CreateImagePixelWithFeatureId(2, 255, pair_k.XImage, pair_k.YImage), pair_k.Star);
				    }
                    catch(ArgumentException)
                    {
                        if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.ThreeStarFitFailed;

                        if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
                            Trace.WriteLine("ThreeStarFit.Var2 - Failed with ArgumentException");

                        return null;
                    }

					DirectTransRotAstrometry threeStarSolution = 
						DirectTransRotAstrometry.SolveByThreeStars(m_PlateConfig, threeStarDict, 2);

					if (threeStarSolution == null)
					{
						if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.ThreeStarFitFailed;

                        if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
						    Trace.WriteLine("ThreeStarFit.Var2 - Failed");
						return null;						
					}
					else
					{
						RA0Deg = threeStarSolution.RA0Deg;
						DE0Deg = threeStarSolution.DE0Deg;
					}
				}
				else
				{
					if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.ThreeStarFitFailed;

                    if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
					    Trace.WriteLine("ThreeStarFit.Var1 - Failed");
					return null;					
				}
			}
			else
			{
				if (pyramidLog != null) pyramidLog.RegisterThreeStarFit(coarseFit);
				RA0Deg = coarseFit.RA0Deg;
				DE0Deg = coarseFit.DE0Deg;
			}
			

#if DEBUG || PYRAMID_DEBUG
		    if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
		    {
                foreach (int key in matchedFeatureIdToStarIdIndexes.Keys)
#if PYRAMID_DEBUG
                    Trace
#else
                    Debug
#endif
                    .WriteLine(string.Format("Star({0}) -> Feature({1})", matchedFeatureIdToStarIdIndexes[key], key));
		    }
#endif

            PlateConstantsSolver solver = new PlateConstantsSolver(m_PlateConfig);
			solver.InitPlateSolution(RA0Deg, DE0Deg);
		    foreach (ImagePixel feature in matchedPairs.Keys)
		    {
		        IStar star = matchedPairs[feature];
		        var kvp = matchedFeatureIdToStarIdIndexes.Single(x => x.Value == star.StarNo);
		        int featureId = kvp.Key;
                solver.AddStar(feature, star, featureId);
		    }

		    LeastSquareFittedAstrometry leastSquareFittedAstrometry = null;
			LeastSquareFittedAstrometry firstFit = null;
			try
			{
				// This is a linear regression when doing simple field alignment. We always use a Linear Fit
				leastSquareFittedAstrometry = solver.SolveWithLinearRegression(
					FitOrder.Linear,
				 	CorePyramidConfig.Default.MinPyramidAlignedStars,
					m_MaxLeastSquareResidual, 
					out firstFit);
			}
			catch (DivideByZeroException)
			{ }

			if (leastSquareFittedAstrometry != null)
			{
				if (pyramidLog != null) pyramidLog.RegisterLinearFit(leastSquareFittedAstrometry);

                if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
				    Trace.WriteLine("Checking possible solution. ");

				List<ulong> usedStarIds = leastSquareFittedAstrometry.FitInfo.AllStarPairs
					.Where(p => p.FitInfo.UsedInSolution)
					.Select(p => p.StarNo)
					.ToList();

				int usedStars = usedStarIds.Count;

				matchedFeatureIdToStarIdIndexes = matchedFeatureIdToStarIdIndexes
					.Where(kvp => usedStarIds.Contains(kvp.Value))
					.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

				List<double> residuals = 
					leastSquareFittedAstrometry.FitInfo.AllStarPairs
						.Where(p => !p.FitInfo.ExcludedForHighResidual)
						.Select(p => p.FitInfo.ResidualArcSec)
						.ToList();


				double secondLargeResidual = 0;

				if (residuals.Count > 0)
				{
					residuals = residuals.OrderByDescending(r => r).ToList();
					secondLargeResidual = residuals.Count > 1 ? residuals[1] : residuals[0];
				}

				double onePixDistArcSec = m_PlateConfig.GetDistanceInArcSec(0, 0, 1, 1);
				if (secondLargeResidual > onePixDistArcSec * CorePyramidConfig.Default.MaxAllowedResidualInPixelsInSuccessfulFit)
				{
					if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.SecondLargestResidualIsTooLarge;

                    if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
					    Trace.WriteLine(string.Format(
						    "Failing preliminary solution because the second largest residual {0}\" is larger than {1}px",
						    secondLargeResidual.ToString("0.0"), CorePyramidConfig.Default.MaxAllowedResidualInPixelsInSuccessfulFit));

					return null;
				}

                if (minMatchedStars.HasValue)
				{
                    if (usedStars < minMatchedStars.Value)
					{
						if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.InsufficientStarsForCalibration;

                        if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
						    Trace.WriteLine(string.Format(
						    "Failing preliminary solution because on {0} stars are used but {1} are required as a minimum for calibration.",
						    usedStars, CorePyramidConfig.Default.MinMatchedStarsForCalibration));

						return null;
					}
				}
			}
			else
			{
				if (pyramidLog != null) pyramidLog.FailureReason = PyramidEntryFailureReason.LinearFitFailed;

			    if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
			    {
                    Debug.WriteLine("DistanceBasedContext.LeastSquareFittedAstrometry Failed!");

                    foreach (PlateConstStarPair pair in solver.Pairs)
                    {
#if PYRAMID_DEBUG
                        Trace
#else
                        Debug
#endif
                        .WriteLine(string.Format("{0} ({1}) -> Residuals: {2}\", {3}\"", pair.StarNo,
                                                      pair.FitInfo.UsedInSolution ? "Included" : "Excluded",
                                                      pair.FitInfo.ResidualRAArcSec.ToString("0.00"),
                                                      pair.FitInfo.ResidualDEArcSec.ToString("0.00")));
                    }

			    }
			}

			if (leastSquareFittedAstrometry != null)
			{
				leastSquareFittedAstrometry.FitInfo.FittedFocalLength = fittedFocalLength;
				if (pyramidLog != null) pyramidLog.RegisterFocalLength(fittedFocalLength);
			}

			return leastSquareFittedAstrometry;
		}
예제 #4
0
		internal IAstrometricFit IsSuccessfulMatch(
			IStarMap starMap,
			int i, int j, int k,
			DistanceEntry ijEntry, DistanceEntry ikEntry, DistanceEntry jkEntry,
            ulong iStarNo, ulong starNo2, ulong starNo3, double fittedFocalLength, bool isRatioFittedFocalLength, double toleranceInArcSec)
		{
			i--;
			j--;
			k--;
			ImagePixel feature_i = GetCenterOfFeature(starMap.GetFeatureById(i), starMap);
			ImagePixel feature_j = GetCenterOfFeature(starMap.GetFeatureById(j), starMap);
			ImagePixel feature_k = GetCenterOfFeature(starMap.GetFeatureById(k), starMap);

			#region find the numbers of the three stars: i, j, k
#if ASTROMETRY_DEBUG
			Trace.Assert(ijEntry.Star1.StarNo == iStarNo || ijEntry.Star2.StarNo == iStarNo);
#endif

            ulong jStarNo, kStarNo;
			if (ijEntry.Star1.StarNo == iStarNo)
			{
				jStarNo = ijEntry.Star2.StarNo;
				if (ijEntry.Star2.StarNo == starNo2) kStarNo = starNo3; else kStarNo = starNo2;
			}
			else
			{
				jStarNo = ijEntry.Star1.StarNo;
				if (ijEntry.Star1.StarNo == starNo2) kStarNo = starNo3; else kStarNo = starNo2;
			}

#if ASTROMETRY_DEBUG
			Trace.Assert(ikEntry.Star1.StarNo == kStarNo || ikEntry.Star2.StarNo == kStarNo);
			Trace.Assert(jkEntry.Star1.StarNo == kStarNo || jkEntry.Star2.StarNo == kStarNo);
#endif

			#endregion

			//if (DebugResolvedStars != null)
			//{
			//    uint ii = 0, jj = 0, kk = 0;
			//    if (DebugResolvedStars.TryGetValue(i + 1, out ii) &&
			//        DebugResolvedStars.TryGetValue(j + 1, out jj) &&
			//        DebugResolvedStars.TryGetValue(k + 1, out kk))
			//    {
			//        if (ii == iStarNo && jj == starNo2 && kk == starNo3)
			//        {
			//            Debugger.Break();
			//        }
			//        else
			//            Trace.WriteLine(string.Format("PYRAMID: {0} = {1}, {2} = {3}, {4} = {5}",
			//                                        i, i == ii ? "YES" : "NO"
			//                                      , j, j == jj ? "YES" : "NO"
			//                                      , k, k == kk ? "YES" : "NO"));
			//    }
			//    else
			//        Trace.WriteLine(string.Format("PYRAMID: {0} = {1}, {2} = {3}, {4} = {5}",
			//                                        i, i == ii ? "YES" : "MISSING"
			//                                      , j, j == jj ? "YES" : "MISSING"
			//                                      , k, k == kk ? "YES" : "MISSING"));
			//}

#if ASTROMETRY_DEBUG
            PyramidEntry pyramidLog = new PyramidEntry(i, j, k, feature_i, feature_j, feature_k, iStarNo, jStarNo, kStarNo);
#endif
            //// Note this is actually cheap way to confirm whether the 3 stars are good or not.
            //List<IStar> threeStars = m_CelestialAllStars.FindAll(s => s.StarNo == iStarNo || s.StarNo == jStarNo || s.StarNo == kStarNo);
            //if (threeStars.Count == 3)
            //{
            //    Dictionary<AstroPixel, IStar> threeStarDict = new Dictionary<AstroPixel, IStar>();

            //    IStar stari = threeStars.Find(s => s.StarNo == iStarNo);
            //    threeStarDict.Add(feature_i, stari);

            //    IStar starj = threeStars.Find(s => s.StarNo == jStarNo);
            //    threeStarDict.Add(feature_j, starj);

            //    IStar stark = threeStars.Find(s => s.StarNo == kStarNo);
            //    threeStarDict.Add(feature_k, stark);

            //    DirectTransRotAstrometry solution = DirectTransRotAstrometry.SolveByThreeStars(m_PlateConfig, threeStarDict);
            //    if (solution != null)
            //    {
            //        pyramidLog.RegisterPreliminaryThreeStarFit(solution);
            //    }
            //}

		    int locatedStars = 3;
			m_MatchedPairs.Clear();
			m_AmbiguousMatches.Clear();
			m_MatchedFeatureIdToStarIdIndexes.Clear();

            List<ulong> usedPyramidAngles = new List<ulong>();

			foreach (StarMapFeature feature in starMap.Features)
			{
				if (feature.FeatureId == i) continue;
				if (feature.FeatureId == j) continue;
				if (feature.FeatureId == k) continue;

				long idx_ix = ((long)i << 32) + (long)feature.FeatureId;

				double dist_ix;
				ImagePixel feature_x = GetCenterOfFeature(feature, starMap);

				if (m_MatchedPairs.ContainsKey(feature_x)) continue;

				if (isRatioFittedFocalLength || !m_FeaturesDistanceCache.TryGetValue(idx_ix, out dist_ix))
				{
					dist_ix = m_PlateConfig.GetDistanceInArcSec(feature_i.X, feature_i.Y, feature_x.X, feature_x.Y, fittedFocalLength);
					long idx_xi = ((long)feature.FeatureId << 32) + (long)i;

					if (!isRatioFittedFocalLength)
					{
						m_FeaturesDistanceCache.Add(idx_ix, dist_ix);
						m_FeaturesDistanceCache.Add(idx_xi, dist_ix);
					}
				}

                 
                Dictionary<ulonglong, DistanceEntry> iStarDists = m_StarsDistanceCache[iStarNo];
                foreach (ulonglong key in iStarDists.Keys)
				{
					// We have found a distance that matches the current feature
				    ulong xStarNo = key.Lo;
					if (usedPyramidAngles.IndexOf(xStarNo) != -1) continue;

					DistanceEntry entry_ix = iStarDists[key];
					if (entry_ix.DistanceArcSec + toleranceInArcSec < dist_ix) continue;
					if (entry_ix.DistanceArcSec - toleranceInArcSec > dist_ix) continue;

                    Dictionary<ulonglong, DistanceEntry> xStarDists = m_StarsDistanceCache[xStarNo];

					#region Test the J-X pair
                    ulonglong jxKey = new ulonglong(xStarNo , jStarNo);

					DistanceEntry entry_jx;
					if (!xStarDists.TryGetValue(jxKey, out entry_jx)) continue;

					long idx_jx = ((long)j << 32) + (long)feature.FeatureId;
					double dist_jx;
					if (isRatioFittedFocalLength || !m_FeaturesDistanceCache.TryGetValue(idx_jx, out dist_jx))
					{
						dist_jx = m_PlateConfig.GetDistanceInArcSec(feature_j.X, feature_j.Y, feature_x.X, feature_x.Y, fittedFocalLength);
						long idx_xj = ((long)feature.FeatureId << 32) + (long)j;

						if (!isRatioFittedFocalLength)
						{
							m_FeaturesDistanceCache.Add(idx_jx, dist_jx);
							m_FeaturesDistanceCache.Add(idx_xj, dist_jx);
						}
					}

					if (entry_jx.DistanceArcSec + toleranceInArcSec < dist_jx) continue;
					if (entry_jx.DistanceArcSec - toleranceInArcSec > dist_jx) continue;
					#endregion

					#region Test the K-X pair
                    ulonglong kxKey = new ulonglong(xStarNo, kStarNo);
					DistanceEntry entry_kx;
					if (!xStarDists.TryGetValue(kxKey, out entry_kx)) continue;

					long idx_kx = ((long)k << 32) + (long)feature.FeatureId;
					double dist_kx;
					if (isRatioFittedFocalLength || !m_FeaturesDistanceCache.TryGetValue(idx_kx, out dist_kx))
					{
						dist_kx = m_PlateConfig.GetDistanceInArcSec(feature_k.X, feature_k.Y, feature_x.X, feature_x.Y, fittedFocalLength);
						long idx_xk = ((long)feature.FeatureId << 32) + (long)k;

						if (!isRatioFittedFocalLength)
						{
							m_FeaturesDistanceCache.Add(idx_kx, dist_kx);
							m_FeaturesDistanceCache.Add(idx_xk, dist_kx);
						}
					}

					if (entry_kx.DistanceArcSec + toleranceInArcSec < dist_kx) continue;
					if (entry_kx.DistanceArcSec - toleranceInArcSec > dist_kx) continue;
					#endregion

					// If we are here, then we have found another star

					locatedStars++;
					IStar xStar = entry_kx.Star1.StarNo == xStarNo ? entry_kx.Star1 : entry_kx.Star2;

#if ASTROMETRY_DEBUG
					Trace.Assert(xStar.StarNo != iStarNo);
					Trace.Assert(xStar.StarNo != jStarNo);
					Trace.Assert(xStar.StarNo != kStarNo);
#endif

					if (RegisterRecognizedPair(feature_x, xStar, feature.FeatureId))
					{
						usedPyramidAngles.Add(xStar.StarNo);
					}

					//Console.WriteLine(string.Format("      {0} ({1}) {2}\" {3}\" {4}\"", xStarNo, feature.FeatureId, dist_ix.ToString("0.0"), dist_jx.ToString("0.0"), dist_kx.ToString("0.0")));
				}
			}

			if (locatedStars >= CorePyramidConfig.Default.MinPyramidAlignedStars)
			{
				ThreeStarFit.StarPair pair_i = new ThreeStarFit.StarPair(feature_i.X, feature_i.Y);
				ThreeStarFit.StarPair pair_j = new ThreeStarFit.StarPair(feature_j.X, feature_j.Y);
				ThreeStarFit.StarPair pair_k = new ThreeStarFit.StarPair(feature_k.X, feature_k.Y);

				if (ijEntry.Star1.StarNo == iStarNo)
				{
					pair_i.RADeg = ijEntry.Star1.RADeg;
					pair_i.DEDeg = ijEntry.Star1.DEDeg;
					pair_i.Star = ijEntry.Star1;

					pair_j.RADeg = ijEntry.Star2.RADeg;
					pair_j.DEDeg = ijEntry.Star2.DEDeg;
					pair_j.Star = ijEntry.Star2;

#if ASTROMETRY_DEBUG
					Trace.Assert(ijEntry.Star1.StarNo == iStarNo);
					Trace.Assert(ijEntry.Star2.StarNo == jStarNo);
#endif

					RegisterRecognizedPair(feature_i, ijEntry.Star1, i);
					RegisterRecognizedPair(feature_j, ijEntry.Star2, j);
				}
				else
				{
					pair_i.RADeg = ijEntry.Star2.RADeg;
					pair_i.DEDeg = ijEntry.Star2.DEDeg;
					pair_i.Star = ijEntry.Star2;

					pair_j.RADeg = ijEntry.Star1.RADeg;
					pair_j.DEDeg = ijEntry.Star1.DEDeg;
					pair_j.Star = ijEntry.Star1;

#if ASTROMETRY_DEBUG
					Trace.Assert(ijEntry.Star2.StarNo == iStarNo);
					Trace.Assert(ijEntry.Star1.StarNo == jStarNo);
#endif

					RegisterRecognizedPair(feature_i, ijEntry.Star2, i);
					RegisterRecognizedPair(feature_j, ijEntry.Star1, j);
				}

				if (ikEntry.Star1.StarNo == kStarNo)
				{
					pair_k.RADeg = ikEntry.Star1.RADeg;
					pair_k.DEDeg = ikEntry.Star1.DEDeg;
					pair_k.Star = ikEntry.Star1;

#if ASTROMETRY_DEBUG
					Trace.Assert(ikEntry.Star1.StarNo == kStarNo);
#endif
					RegisterRecognizedPair(feature_k, ikEntry.Star1, k);
				}
				else
				{
					pair_k.RADeg = ikEntry.Star2.RADeg;
					pair_k.DEDeg = ikEntry.Star2.DEDeg;
					pair_k.Star = ikEntry.Star2;

#if ASTROMETRY_DEBUG
					Trace.Assert(ikEntry.Star2.StarNo == kStarNo);
#endif

					RegisterRecognizedPair(feature_k, ikEntry.Star2, k);
				}

				if (m_AmbiguousMatches.Count > 0 &&
					locatedStars - m_AmbiguousMatches.Count >= CorePyramidConfig.Default.MinPyramidAlignedStars)
				{
					// If we have sufficient number of stars and ambiguous stars (close doubles that satisfy more than one solution)
					// then remove all ambiguous stars before proceeding
					foreach (ImagePixel matchedPixel in m_AmbiguousMatches)
					{
						IStar matchedStar = m_MatchedPairs[matchedPixel];

						int featureToRemove = -1;
						foreach (int featureId in m_MatchedFeatureIdToStarIdIndexes.Keys)
						{
							if (m_MatchedFeatureIdToStarIdIndexes[featureId] == matchedStar.StarNo)
							{
								featureToRemove = featureId;
								break;
							}
						}

						m_MatchedFeatureIdToStarIdIndexes.Remove(featureToRemove);
						m_MatchedPairs.Remove(matchedPixel);
					}
				}

                if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
				    Debug.WriteLine(string.Format("Attempting DistanceBasedContext.LeastSquareFittedAstrometry ({0}={1}; {2}={3}; {4}={5})", i, iStarNo, j, jStarNo, k, kStarNo));
				
                return SolveStarPairs(
					starMap, m_MatchedPairs, m_MatchedFeatureIdToStarIdIndexes, pair_i, pair_j, pair_k,					
					fittedFocalLength, 
#if ASTROMETRY_DEBUG
					pyramidLog
#else
					null
#endif
					);
			}
			else
			{
#if PYRAMID_DEBUG || DEBUG

                foreach(ImagePixel pixel in m_MatchedPairs.Keys)
                {
                    IStar star = m_MatchedPairs[pixel];
                    foreach(int featureId in m_MatchedFeatureIdToStarIdIndexes.Keys)
                    {
                        if (m_MatchedFeatureIdToStarIdIndexes[featureId] == star.StarNo)
                        {
                            if (TangraConfig.Settings.TraceLevels.PlateSolving.TraceVerbose())
                            {
#if DEBUG
                                Debug
#endif
#if PYRAMID_DEBUG
                                Trace 
#endif
                                .WriteLine(string.Format("({0}, {1}) - StarNo: {2}; FeatureId: {3}", pixel.X, pixel.Y, star.StarNo, featureId));
                            }

                            break;
                        }
                    }
                }
#endif

#if ASTROMETRY_DEBUG
				pyramidLog.FailBecauseOfTooFiewLocatedStars(locatedStars);
				AstrometricFitDebugger.RegisterFailedPyramid(pyramidLog);
#endif
			}

			return null;
		}