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

KDE's Doxygen guidelines are available online.