Kstars

starcorrespondence.h
1 /*
2  SPDX-FileCopyrightText: 2020 Hy Murveit <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include <QObject>
10 #include <QList>
11 #include <QVector>
12 #include <QVector2D>
13 
14 #include "fitsviewer/fitsdata.h"
15 #include "vect.h"
16 
17 /*
18  * This class is useful to track a guide star by noting its position relative to other stars.
19  * It is intended to be resilient to translation, a bit of positional noise, and slight field rotation.
20  * The class is initialized with a set of reference (x,y) positions from stars, where one is
21  * designated a guide star. Then, given a set of new input star positions, it determines a mapping
22  * of the new stars to the references. Some reference stars may not be in the new star group, and
23  * there may be new stars that don't appear in the references. However, the guide star must appear
24  * in both sets for this to be successful.
25  */
26 
27 class StarCorrespondence
28 {
29  public:
30  // Initializes with reference stars.
31  // One of the stars with index guideStar is a special "guide star".
32  StarCorrespondence(const QList<Edge> &stars, int guideStar);
33  StarCorrespondence();
34  ~StarCorrespondence() {}
35 
36  // If the default constructor was called, then initialize must be called with the
37  // reference star positions and the index in stars of the guideStar.
38  void initialize(const QList<Edge> &stars, int guideStar);
39 
40  // Clears the references.
41  void reset();
42 
43  // Associate the input stars with the reference stars.
44  // StarMap[i] will contain the index of a reference star that corresponds to the ith star.
45  // Some input stars may have no reference (starMap->at(i) == -1), and some references may
46  // not correspond to any input stars. There will be no star-reference mapping with
47  // distance longer than maxDistance. If adapt is true, the input positions are used
48  // to incrementally adapt the reference positions.
49  GuiderUtils::Vector find(const QList<Edge> &stars, double maxDistance, QVector<int> *starMap, bool adapt = true,
50  double minFraction = 0.5);
51 
52  // Returns the number of reference stars.
53  int size() const
54  {
55  return references.size();
56  }
57 
58  // Return a reference to the ith reference star. Caller's responsiblity
59  // to make sure i >= 0 && i < references.size();
60  // Recompute the reference coordinates as we may adapt them.
61  Edge reference(int i) const
62  {
63  Edge star = references[i];
64  star.x = references[guideStarIndex].x + guideStarOffsets[i].x;
65  star.y = references[guideStarIndex].y + guideStarOffsets[i].y;
66  return star;
67  }
68  QVector2D offset(int i) const
69  {
70  QVector2D offset;
71  offset.setX(guideStarOffsets[i].x);
72  offset.setY(guideStarOffsets[i].y);
73  return offset;
74  }
75 
76  int guideStar() const
77  {
78  return guideStarIndex;
79  }
80 
81  void setAllowMissingGuideStar(bool value = true)
82  {
83  allowMissingGuideStar = value;
84  }
85 
86  void setImageSize(int width, int height)
87  {
88  imageWidth = width;
89  imageHeight = height;
90  }
91 
92  private:
93  // The Offsets structure is used to keep the positions of the reference stars
94  // relative to the guide star.
95  struct Offsets
96  {
97  double x; // The x position of the star (in pixels), relative to the guide star.
98  double y; // The y position of the star (in pixels), relative to the guide star.
99 
100  Offsets(double x_, double y_) : x(x_), y(y_) {}
101  Offsets() : x(0), y(0) {} // RPi compiler required this constructor.
102  };
103 
104  // Update the reference-star offsets given the new star positions.
105  // The adaption is similar to a 25-sample moving average.
106  void initializeAdaptation();
107  void adaptOffsets(const QList<Edge> &stars, const QVector<int> &starMap);
108 
109  // Utility used by find. Useful for iterating when the guide star is missing.
110  int findInternal(const QList<Edge> &stars, double maxDistance, QVector<int> *starMap,
111  int guideStarIndex, const QVector<Offsets> &offsets,
112  int *numFound, int *numNotFound, double minFraction) const;
113 
114  // Used to when guide star is missing. Creates offsets as if other stars were the guide star.
115  void makeOffsets(const QVector<Offsets> &offsets, QVector<Offsets> *targetOffsets, int targetStar) const;
116 
117  // When the guide star is missing, but star correspondence was successful, use the positions
118  // of the stars that were found to create a virtual guide star--inferring a guide star position
119  // using the offsets from the (unfound) guide star to the stars that were found.
120  // Offsets are offsets that were created for a new "substitude guide star".
121  // StarMap is the map made for that substitude by findInternal().
122  // Offset is the offset from the original guide star to that substitute guide star.
123  GuiderUtils::Vector inventStarPosition(const QList<Edge> &stars, QVector<int> &starMap,
124  QVector<Offsets> offsets, Offsets offset) const;
125 
126  // Finds the star closest to x,y. Returns the index in sortedStars.
127  // sortedStars should be sorted in x, which allows for a speedup in search.
128  int findClosestStar(double x, double y, const QList<Edge> sortedStars,
129  double maxDistance, double *distance) const;
130 
131  // The offsets of the reference stars relative to the guide star.
132  QVector<Offsets> guideStarOffsets;
133 
134  QList<Edge> references; // The original reference stars.
135  int guideStarIndex; // The index of the guide star in references.
136  bool initialized = false; // Set to true once the references and guideStar index has been set.
137 
138  // If this is true, it will attempt star correspondence even if the guide star is missing.
139  bool allowMissingGuideStar { false };
140 
141  // IIR filter parameter used to adapt offsets.
142  double alpha;
143 
144  // Setting imageWidth and height can speed up searching for stars in an image
145  // by eliminating positions far outside the image.
146  int imageWidth { 100000000 };
147  int imageHeight { 100000000 };
148 
149  // A copy of the original reference offsets used so that the values don't move too far.
150  QVector<Offsets> originalGuideStarOffsets;
151 };
152 
void initialize(StandardShortcut id)
void setX(float x)
void setY(float y)
QAction * find(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 04:06:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.