Exemplo n.º 1
0
		private bool TryMatchingPairsFromPreviousFit(IStarMap starMap)
		{
			ThreeStarFit.StarPair[] pairs = new ThreeStarFit.StarPair[3];
			Dictionary<ImagePixel, IStar> matchedPairs = new Dictionary<ImagePixel, IStar>();
            Dictionary<int, ulong> matchedFeatureIdToStarIdIndexes = new Dictionary<int, ulong>();

			int idx = 0;
			foreach (PlateConstStarPair pair in m_PreviousFit.AllStarPairs)
			{
				if (pair.FitInfo.ExcludedForHighResidual) continue;
                StarMapFeature ftr = starMap.GetFeatureInRadius((int)pair.x, (int)pair.y, (int)CoreAstrometrySettings.Default.SearchArea);
				if (ftr != null)
				{
					ImagePixel center = starMap.GetCentroid(
                        (int)pair.x, (int)pair.y, (int)CoreAstrometrySettings.Default.SearchArea);
					IStar star = null;

					foreach (IStar s in m_CelestialPyramidStars)
					{
						if (s.StarNo == pair.StarNo)
						{
							star = s;
							break;
						}
					}

					if (star != null &&
						center != null &&
						!matchedFeatureIdToStarIdIndexes.ContainsKey(ftr.FeatureId))
					{
						if (idx < 3)
						{
							pairs[idx] = new ThreeStarFit.StarPair(center.X, center.Y);
							pairs[idx].RADeg = star.RADeg;
							pairs[idx].DEDeg = star.DEDeg;
							pairs[idx].Star = star;
							idx++;
						}

						matchedPairs.Add(center, star);
						matchedFeatureIdToStarIdIndexes.Add(ftr.FeatureId, star.StarNo);
					}
				}
			}

			// Shortcurcuit to FeautreId - StarNo detection
			if (matchedPairs.Count >= m_Settings.MinimumNumberOfStars)
			{
                // When there was a previous fit and we have sufficient stars from the current star map
                // then don't require % of the bright features to approve the solution. Do it as a calibration fit (not too precise)
                LeastSquareFittedAstrometry fit = SolveStarPairs(
                    starMap, matchedPairs,
                    matchedFeatureIdToStarIdIndexes,
                    pairs[0], pairs[1], pairs[2], m_PreviousFit.FittedFocalLength, null, 
                    TangraConfig.Settings.Astrometry.MinimumNumberOfStars);

                if (fit != null)
                {
                    m_Solution = fit;
                    m_MatchedPairs = matchedPairs;
                    m_MatchedFeatureIdToStarIdIndexes = matchedFeatureIdToStarIdIndexes;
                    return true;
                }
            }

			return false;
		}
Exemplo n.º 2
0
		private bool TryMatchingPairsFromManualPairs(IStarMap starMap)
		{
			ThreeStarFit.StarPair[] pairs = new ThreeStarFit.StarPair[3];
			Dictionary<ImagePixel, IStar> matchedPairs = new Dictionary<ImagePixel, IStar>();
            Dictionary<int, ulong> matchedFeatureIdToStarIdIndexes = new Dictionary<int, ulong>();

			int idx = 0;
			foreach (StarMapFeature feature in m_ManualPairs.Keys)
			{
				IStar star = m_ManualPairs[feature];
				ImagePixel center = feature.GetCenter();

                StarMapFeature ftr = starMap.GetFeatureInRadius((int)center.X, (int)center.Y, (int)CoreAstrometrySettings.Default.SearchArea);
				if (ftr != null)
				{
					if (!matchedFeatureIdToStarIdIndexes.ContainsKey(ftr.FeatureId))
					{
						if (idx < 3)
						{
							pairs[idx] = new ThreeStarFit.StarPair(center.X, center.Y);
							pairs[idx].RADeg = star.RADeg;
							pairs[idx].DEDeg = star.DEDeg;
							pairs[idx].Star = star;
							idx++;
						}

						matchedPairs.Add(center, star);
						matchedFeatureIdToStarIdIndexes.Add(ftr.FeatureId, star.StarNo);
					}
				}
			}

			if (matchedPairs.Count >= m_Settings.MinimumNumberOfStars)
			{
                // When there was a previous fit and we have sufficient stars from the current star map
                // then don't require % of the bright features to approve the solution. Do it as a calibration fit (not too precise)
                LeastSquareFittedAstrometry fit = SolveStarPairs(
                    starMap, matchedPairs,
                    matchedFeatureIdToStarIdIndexes,
                    pairs[0], pairs[1], pairs[2], m_PlateConfig.EffectiveFocalLength, null, 
                    TangraConfig.Settings.Astrometry.MinimumNumberOfStars);

                if (fit != null)
                {
                    m_Solution = fit;
                    m_MatchedPairs = matchedPairs;
                    m_MatchedFeatureIdToStarIdIndexes = matchedFeatureIdToStarIdIndexes;
                    return true;
                }
            }

			return false;
		}
Exemplo n.º 3
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;
		}