2#include "ekos/guide/internalguide/starcorrespondence.h"
3#include <ekos_focus_debug.h>
11using Ekos::FocusStars;
18double correspondenceRmsError(
const StarCorrespondence &corr,
const QList<Edge> &stars2,
19 const QVector<int> &starMap, GuiderUtils::Vector gsPosition)
21 const int s2Size = starMap.
size();
24 for (
int s2 = 0; s2 < s2Size; s2++)
26 const int s1 = starMap[s2];
30 double x2 = stars2[s2].x;
31 double y2 = stars2[s2].y;
34 double x1 = gsPosition.x + offset.
x();
35 double y1 = gsPosition.y + offset.
y();
36 double dSq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
41 return sqrt(sumSq2 / num);
50 double maxDistance, FocusStars *stars1Filtered, FocusStars *stars2Filtered)
56 int minStars = std::min(stars1.
size(), stars2.
size());
57 int desiredStars = 0.45 * minStars;
58 const double minFraction =
static_cast<double>(desiredStars) / stars1.
size();
62 StarCorrespondence corr(stars1, mainStar1);
63 corr.setAllowMissingGuideStar(
true);
64 Edge gStar = corr.find(stars2, maxDistance, &starMap,
false, minFraction);
65 GuiderUtils::Vector gsPosition(gStar.x, gStar.y, 0);
68 const int s2Size = starMap.
size();
69 int numAssociated = 0;
71 for (
int s2 = 0; s2 < s2Size; s2++)
73 const int s1 = starMap[s2];
81 *stars1Filtered = FocusStars(edges1);
82 *stars2Filtered = FocusStars(edges2);
85 double corrError = correspondenceRmsError(corr, stars2, starMap, gsPosition);
86 QString debugStr =
QString(
"matchStars: Inputs sized %1 %2 found %3 matches, RMS dist %4")
88 qCDebug(KSTARS_EKOS_FOCUS) << debugStr;
93bool filterFocusStars(
const FocusStars &stars1,
const FocusStars &stars2,
double maxDistance, FocusStars *filtered1,
94 FocusStars *filtered2)
96 int size1 = stars1.getStars().size();
97 int size2 = stars2.getStars().size();
101 constexpr int maxNumStars = 100;
104 if (size1 > maxNumStars)
106 for (
int i = 0; i < maxNumStars; i++)
107 s1.
append(stars1.getStars()[i]);
113 if (size2 > maxNumStars)
115 for (
int i = 0; i < maxNumStars; i++)
116 s2.
append(stars2.getStars()[i]);
122 const int sufficientMatches = std::min(10,
static_cast<int>(0.45 * std::min(size1, size2)));
129 for (
int i = 0; i < 5; ++i)
131 const int seed = rand() % size1;
132 int numMatches = matchStars(*s1ptr, *s2ptr, seed, maxDistance, &f1, &f2);
133 if (numMatches > maxMatches)
137 maxMatches = numMatches;
138 if (numMatches >= sufficientMatches)
break;
141 return (maxMatches > 1);
147FocusStars::FocusStars(
const QList<Edge *> edges,
double maxDist)
148 : maxDistance(maxDist), initialized(true)
151 for (
const auto &edge : edges)
155FocusStars::FocusStars(
const QList<Edge> edges,
double maxDist)
156 : maxDistance(maxDist), initialized(true)
159 for (
const auto &edge : edges)
166double FocusStars::getHFR()
const
171 if (computedHFR >= 0)
173 if (stars.size() == 0)
return -1;
174 std::vector<double> values;
175 for (
auto &star : stars)
176 values.push_back(star.HFR);
177 const int middle = values.size() / 2;
178 std::nth_element(values.begin(), values.begin() + middle, values.end());
179 computedHFR = values[middle];
183bool FocusStars::commonHFR(
const FocusStars &referenceStars,
double *thisHFR,
double *referenceHFR)
const
188 FocusStars commonStars, commonReferenceStars;
189 bool ok = filterFocusStars(*
this, referenceStars, maxDistance, &commonStars, &commonReferenceStars);
190 if (!ok)
return false;
191 *thisHFR = commonStars.getHFR();
192 *referenceHFR = commonReferenceStars.getHFR();
194 if (*thisHFR < 0 || *referenceHFR < 0 || commonStars.getStars().size() == 0 ||
195 commonReferenceStars.getStars().size() == 0)
201void FocusStars::printStars(
const QString &label)
const
205 for (
const auto &star : getStars())
207 fprintf(stderr,
"%s{%6.1f,%6.1f, %5.2f}", first ?
"" :
", ", star.x, star.y, star.HFR);
210 fprintf(stderr,
"}\n");
213double FocusStars::relativeHFR(
const FocusStars &referenceStars,
double referenceHFR)
const
218 double hfrCommon, refHfrCommon;
219 if (!commonHFR(referenceStars, &hfrCommon, &refHfrCommon))
221 qCDebug(KSTARS_EKOS_FOCUS) <<
"Couldn't get a relative HFR, punted!";
223 return (getHFR() / referenceStars.getHFR()) * referenceHFR;
226 if (hfrCommon < 0 || refHfrCommon < 0)
return -1;
228 QString debugStr =
QString(
"RelativeHFR: sizes: %7 %8 hfr: (%1 (%2) / %3 (%4)) * %5 = %6")
229 .
arg(hfrCommon, 0,
'f', 2).
arg(getHFR(), 0,
'f', 2).
arg(refHfrCommon, 0,
'f', 2)
230 .
arg(referenceStars.getHFR(), 0,
'f', 2).
arg(referenceHFR, 0,
'f', 2)
231 .
arg((hfrCommon / refHfrCommon) * referenceHFR, 0,
'f', 2)
232 .
arg(getStars().size()).
arg(referenceStars.getStars().size());
233 qCDebug(KSTARS_EKOS_FOCUS) << debugStr;
235 return (hfrCommon / refHfrCommon) * referenceHFR;
Ekos is an advanced Astrophotography tool for Linux.
QString label(StandardShortcut id)
void append(QList< T > &&value)
qsizetype size() const const
QString arg(Args &&... args) const const
qsizetype size() const const
QByteArray toLatin1() const const