Kstars

ksutils.h
1 /*
2  SPDX-FileCopyrightText: 2002 Mark Hollomon <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 /** @class KSUtils
8  *@short KStars utility functions
9  *@author Mark Hollomon
10  *@version 1.0
11  *Static functions for various purposes.
12  *The openDataFile() function searches the standard KDE directories
13  *for the data filename given as an argument.
14  *(it is impossible to instantiate a KSUtils object; just use the static functions).
15  */
16 
17 #pragma once
18 
19 #include "dms.h"
20 
21 #include <QPointF>
22 #include <QSharedPointer>
23 #include <QJsonDocument>
24 #include <QJsonArray>
25 #include <QJsonObject>
26 #include "polyfills/qstring_hash.h"
27 #include <unordered_map>
28 #include "config-kstars.h"
29 #include "kstars_debug.h"
30 
31 #if __GNUC__ > 5
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wignored-attributes"
34 #endif
35 #if __GNUC__ > 6
36 #pragma GCC diagnostic ignored "-Wint-in-bool-context"
37 #endif
38 #include <Eigen/Core>
39 #if __GNUC__ > 5
40 #pragma GCC diagnostic pop
41 #endif
42 
43 #include <cstddef>
44 
45 class QFile;
46 class QString;
47 class QTextStream;
48 class SkyPoint;
49 class SkyObject;
50 class StarObject;
51 
52 namespace KSUtils
53 {
54 // Quick checks whether hardware is limited or not
55 // right now the only check is architecture. arm processors are limited while x86 are sufficient
56 bool isHardwareLimited();
57 
58 /** Attempt to open the data file named filename, using the QFile object "file".
59  *First look in the standard KDE directories, then look in a local "data"
60  *subdirectory. If the data file cannot be found or opened, display a warning
61  *messagebox.
62  *@short Open a data file.
63  *@param file The QFile object to be opened
64  *@param s The name of the data file.
65  *@returns bool Returns true if data file was opened successfully.
66  *@returns a reference to the opened file.
67  */
68 bool openDataFile(QFile &file, const QString &s);
69 
70 /** Clamp value into range.
71  * @p x value to clamp.
72  * @p min minimal allowed value.
73  * @p max maximum allowed value.
74  */
75 template <typename T>
76 inline T clamp(T x, T min, T max)
77 {
78  if (x < min)
79  return min;
80  if (x > max)
81  return max;
82  return x;
83 }
84 
85 /** Put angle into range. Period is equal to max-min.
86  *
87  * @p x angle value
88  * @p min minimal angle
89  * @p max maximal angle
90  */
91 template <typename T>
92 inline T reduceAngle(T x, T min, T max)
93 {
94  T delta = max - min;
95  return x - delta * floor((x - min) / delta);
96 }
97 
98 /** Convert from spherical to cartesian coordinate system.
99  * Resulting vector have unit length
100  */
101 inline Eigen::Vector3d fromSperical(const dms &longitude, const dms &latitude)
102 {
103  double sinL, sinB;
104  double cosL, cosB;
105 
106  longitude.SinCos(sinL, cosL);
107  latitude.SinCos(sinB, cosB);
108  return Eigen::Vector3d(cosB * cosL, cosB * sinL, sinB);
109 }
110 
111 /** Convert a vector to a point */
112 inline QPointF vecToPoint(const Eigen::Vector2f &vec)
113 {
114  return QPointF(vec[0], vec[1]);
115 }
116 
117 /** Convert a point to a vector */
118 inline Eigen::Vector2f pointToVec(const QPointF &p)
119 {
120  return Eigen::Vector2f(p.x(), p.y());
121 }
122 
123 /**
124  *@short Create a URL to obtain a DSS image for a given SkyPoint
125  *@note If SkyPoint is a DeepSkyObject, this method automatically
126  *decides the image size required to fit the object.
127  */
128 QString getDSSURL(const SkyPoint *const p);
129 
130 /**
131  *@short Create a URL to obtain a DSS image for a given RA, Dec
132  *@param ra The J2000.0 Right Ascension of the point
133  *@param dec The J2000.0 Declination of the point
134  *@param width The width of the image in arcminutes
135  *@param height The height of the image in arcminutes
136  *@param type The image type, either gif or fits.
137  *@note This method resets height and width to fall within the range accepted by DSS
138  */
139 QString getDSSURL(const dms &ra, const dms &dec, float width = 0, float height = 0,
140  const QString &type = "gif");
141 
142 /**
143  *@short Return a string corresponding to an angle specifying direction
144  *
145  * The angle must measure direction from North, towards East. Both
146  * the azimuth and position angle follow this convention, so this
147  * method can be used to return a string corresponding to the
148  * general heading of a given azimuth / position angle.
149  *
150  *@param angle angle as dms (measured from North, towards East)
151  *@return A localized string corresponding to the approximate direction (eg: NNW)
152  */
153 QString toDirectionString(dms angle);
154 /**
155  *@short Converts StarObject list into SkyObject list
156  *@param starObjList QList of StarObject pointers
157  *@return Returns a pointer to QList of SkyObject pointers
158  *@note Used for Star-Hopper
159  */
160 QList<SkyObject *> *castStarObjListToSkyObjList(QList<StarObject *> *starObjList);
161 
162 /**
163  *@note Avoid using this method for the same reasons as QSharedPointer::data()
164  */
165 template <typename T>
166 QList<T *> makeVanillaPointerList(const QList<QSharedPointer<T>> &spList)
167 {
168  QList<T *> vpList;
169  foreach (QSharedPointer<T> sp, spList)
170  vpList.append(sp.data());
171  return vpList;
172 }
173 
174 /**
175  *@short Return the genetive form of constellation name, given the abbreviation
176  *@param code Three-letter IAU abbreviation of the constellation
177  *@return the genetive form of the constellation name
178  */
179 QString constGenetiveFromAbbrev(const QString &code);
180 
181 /**
182  *@short Return the name of the constellation, given the abbreviation
183  *@param code Three-letter IAU abbreviation of the constellation
184  *@return the nominative form of the constellation name
185  */
186 QString constNameFromAbbrev(const QString &code);
187 
188 /**
189  *@short Return the abbreviation of the constellation, given the full name
190  *@param fullName_ Full name of the constellation
191  *@return the three-letter IAU standard abbreviation of the constellation
192  */
193 QString constNameToAbbrev(const QString &fullName_);
194 
195 /**
196  *@short Return the abbreviation of the constellation, given the genetive form
197  *@param genetive_ the genetive form of the constellation's name
198  *@return the three-letter IAU standard abbreviation of the constellation
199  */
200 QString constGenetiveToAbbrev(const QString &genetive_);
201 
202 /**
203 * Interface into Qt's logging system
204 * @author: Yale Dedis 2011
205 * Adapted from DeDiS project.
206 */
207 class Logging
208 {
209  public:
210  /**
211  * Store all logs into the specified file
212  */
213  static void UseFile();
214 
215  /**
216  * Output logs to stdout
217  */
218  static void UseStdout();
219 
220  /**
221  * Output logs to stderr
222  */
223  static void UseStderr();
224 
225  /**
226  * Use the default logging mechanism
227  */
228  static void UseDefault();
229 
230  /**
231  * Disable logging
232  */
233  static void Disable();
234 
235  /**
236  * @brief SyncFilterRules Sync QtLogging filter rules from Options
237  */
238  static void SyncFilterRules();
239 
240  private:
241  static QString _filename;
242 
243  static void Disabled(QtMsgType type, const QMessageLogContext &context,
244  const QString &msg);
245  static void File(QtMsgType type, const QMessageLogContext &context,
246  const QString &msg);
247  static void Stdout(QtMsgType type, const QMessageLogContext &context,
248  const QString &msg);
249  static void Stderr(QtMsgType type, const QMessageLogContext &context,
250  const QString &msg);
251  static void Write(QTextStream &stream, QtMsgType type,
252  const QMessageLogContext &context, const QString &msg);
253 };
254 
255 QString getDefaultPath(const QString &option);
256 
257 #ifdef Q_OS_OSX
258 void copyResourcesFolderFromAppBundle(QString folder);
259 bool setupMacKStarsIfNeeded(); //The boolean returns true if the data folders are good to go.
260 bool configureAstrometry();
261 bool replaceIndexFileNotYetSet();
262 bool copyRecursively(QString sourceFolder, QString destFolder);
263 #endif
264 
265 // Astrometry Related functions
266 QStringList getAstrometryDefaultIndexFolderPaths();
267 QString getAstrometryConfFilePath();
268 QStringList getAstrometryDataDirs();
269 bool addAstrometryDataDir(const QString &dataDir);
270 bool removeAstrometryDataDir(const QString &dataDir);
271 
272 struct JPLFilter
273 {
274  QByteArray item;
275  QByteArray op;
276  QByteArray value;
277 };
278 
279 class JPLParser
280 {
281  public:
282  JPLParser(const QString &path);
283 
284  template <typename Lambda>
285  void for_each(const Lambda &fct)
286  {
287  for (const auto &item : m_data)
288  {
289  fct([ &, this](const QString & key)
290  {
291  return item.toArray().at(m_field_map.at(key));
292  });
293  }
294  };
295 
296  private:
297  QJsonDocument m_doc;
298  QJsonArray m_data;
299  std::unordered_map<QString, int> m_field_map;
300 };
301 // TODO: Implement Datatypes//Maps for kind, datafields, filters...
302 
303 class MPCParser
304 {
305  public:
306  MPCParser(const QString &path);
307 
308  template <typename Lambda>
309  void for_each(const Lambda &fct)
310  {
311  for (const auto &item : m_data)
312  {
313  fct([ &, this](const QString & key)
314  {
315  return item.toObject().value(key);
316  });
317  }
318  };
319 
320  private:
321  QJsonDocument m_doc;
322  QJsonArray m_data;
323 };
324 
325 /**
326  *@short Generate a query string for downloading comet/asteroid data from JPL.
327  *@param kind The kind of object we want: ast, com.
328  *@param dataFields The columns we want to download.
329  *@param filters Filters for the Data.
330  *@return The query string.
331  */
332 QByteArray getJPLQueryString(const QByteArray &kind, const QByteArray &dataFields,
333  const QVector<JPLFilter> &filters);
334 
335 /**
336  * @brief RAWToJPEG Convert raw image (e.g. CR2) using libraw to a JPEG image
337  * @param rawImage full path to raw image
338  * @param output full path to jpeg image to write to
339  * @return True if conversion is successful, false otherwise.
340  */
341 bool RAWToJPEG(const QString &rawImage, const QString &output, QString &errorMessage);
342 
343 /**
344  * @brief getAvailableRAM Try to get available and total RAM on system
345  * @return availableRAM Available (free and unclaimed) system RAM in bytes. 0 if failed to determine RAM
346  */
347 double getAvailableRAM();
348 
349 } // namespace KSUtils
void append(const T &value)
T * data() const const
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
void SinCos(double &s, double &c) const
Compute Sine and Cosine of the angle simultaneously.
Definition: dms.h:439
Interface into Qt's logging system.
Definition: ksutils.h:207
static void UseStderr()
Output logs to stderr.
Definition: ksutils.cpp:979
static void UseStdout()
Output logs to stdout.
Definition: ksutils.cpp:963
static void UseFile()
Store all logs into the specified file.
Definition: ksutils.cpp:927
subclass of SkyObject specialized for stars.
Definition: starobject.h:32
static void SyncFilterRules()
SyncFilterRules Sync QtLogging filter rules from Options.
Definition: ksutils.cpp:1036
KStars utility functions.
static void Disable()
Disable logging.
Definition: ksutils.cpp:1029
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:37
qreal x() const const
qreal y() const const
static void UseDefault()
Use the default logging mechanism.
Definition: ksutils.cpp:1024
Information about an object in the sky.
Definition: skyobject.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Aug 14 2022 04:13:57 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.