File indexing completed on 2024-04-14 14:12:01

0001 /*
0002     SPDX-FileCopyrightText: 2021 Hy Murveit <hy@murveit.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ekos/focus/focusstars.h"
0008 
0009 #include <QTest>
0010 #include <cmath>
0011 #include <memory>
0012 
0013 #include <QObject>
0014 
0015 class TestFocusStars : public QObject
0016 {
0017         Q_OBJECT
0018 
0019     public:
0020         TestFocusStars();
0021         ~TestFocusStars() override = default;
0022 
0023     private slots:
0024         void basicTest();
0025 };
0026 
0027 #include "testfocusstars.moc"
0028 
0029 using Ekos::FocusStars;
0030 
0031 TestFocusStars::TestFocusStars() : QObject()
0032 {
0033 }
0034 
0035 #define CompareFloat(d1,d2) QVERIFY(fabs((d1) - (d2)) < .0001)
0036 
0037 Edge makeEdge(float x, float y, float hfr)
0038 {
0039     Edge e;
0040     e.x = x;
0041     e.y = y;
0042     e.HFR = hfr;
0043     return e;
0044 }
0045 
0046 struct Triple
0047 {
0048     double x, y, h;
0049 };
0050 QList<Edge> MakeEdges(const std::vector<Triple> triples)
0051 {
0052     QList<Edge> edges;
0053     for (const auto &t : triples)
0054         edges.append(makeEdge(t.x, t.y, t.h));
0055     return edges;
0056 };
0057 
0058 void TestFocusStars::basicTest()
0059 {
0060     double maxDistance = 1.0;
0061     FocusStars f1(MakeEdges(
0062     {
0063         {2.0, 2.0, 3.0},
0064         {5.0, 5.0, 4.0},
0065         {7.0, 5.0, 4.5},
0066         {5.0, 1.0, 1.0},
0067         {6.0, 3.0, 2.5}
0068     }), maxDistance);
0069 
0070     FocusStars f2(MakeEdges(
0071     {
0072         {2.1, 2.2, 3.0},
0073         {5.3, 5.2, 2.0},
0074         {7.2, 5.1, 4.5},
0075         {15.0, 11.0, 11.0},
0076         {16.0, 13.0, 12.5}
0077     }), maxDistance);
0078 
0079     CompareFloat(3.0, f1.getHFR());
0080     CompareFloat(4.5, f2.getHFR());
0081 
0082     double h1, h2;
0083     f1.commonHFR(f2, &h1, &h2);
0084     // The stars in common are the 1st 3 of each.
0085     CompareFloat(4.0, h1);
0086     CompareFloat(3.0, h2);
0087 
0088     double hh1, hh2;
0089     f2.commonHFR(f1, &hh2, &hh1);
0090     CompareFloat(hh1, h1);
0091     CompareFloat(hh2, h2);
0092 
0093     double r1 = f1.relativeHFR(f2, 10);
0094     CompareFloat(13.33333, r1);
0095 
0096     double r2 = f2.relativeHFR(f1, 10);
0097     CompareFloat(7.5, r2);
0098 }
0099 
0100 QTEST_GUILESS_MAIN(TestFocusStars)