1 | /*************************************************************************** |
2 | kstars.h - K Desktop Planetarium |
3 | ------------------- |
4 | begin : Mon Jan 7 2002 |
5 | copyright : (C) 2002 by Mark Hollomon |
6 | email : mhh@mindspring.com |
7 | ***************************************************************************/ |
8 | /*************************************************************************** |
9 | * * |
10 | * This program is free software; you can redistribute it and/or modify * |
11 | * it under the terms of the GNU General Public License as published by * |
12 | * the Free Software Foundation; either version 2 of the License, or * |
13 | * (at your option) any later version. * |
14 | * * |
15 | ***************************************************************************/ |
16 | |
17 | /**@class KSUtils |
18 | *@short KStars utility functions |
19 | *@author Mark Hollomon |
20 | *@version 1.0 |
21 | *Static functions for various purposes. |
22 | *The openDataFile() function searches the standard KDE directories |
23 | *for the data filename given as an argument. |
24 | *(it is impossible to instantiate a KSUtils object; just use the static functions). |
25 | */ |
26 | |
27 | #ifndef KSTARS_KSUTILS_H__ |
28 | #define KSTARS_KSUTILS_H__ |
29 | |
30 | #include <cstddef> |
31 | #include <Eigen/Core> |
32 | using namespace Eigen; |
33 | #include <QPointF> |
34 | #include "dms.h" |
35 | |
36 | class QFile; |
37 | class QString; |
38 | class SkyPoint; |
39 | |
40 | namespace KSUtils { |
41 | /** Attempt to open the data file named filename, using the QFile object "file". |
42 | *First look in the standard KDE directories, then look in a local "data" |
43 | *subdirectory. If the data file cannot be found or opened, display a warning |
44 | *messagebox. |
45 | *@short Open a data file. |
46 | *@param &file The QFile object to be opened |
47 | *@param filename The name of the data file. |
48 | *@returns bool Returns true if data file was opened successfully. |
49 | *@returns a reference to the opened file. |
50 | */ |
51 | bool openDataFile( QFile &file, const QString &filename ); |
52 | |
53 | /** Clamp value into range. |
54 | * @p x value to clamp. |
55 | * @p min minimal allowed value. |
56 | * @p max maximum allowed value. |
57 | */ |
58 | template<typename T> |
59 | inline T clamp(T x, T min, T max) { |
60 | if( x < min ) |
61 | return min; |
62 | if( x > max ) |
63 | return max; |
64 | return x; |
65 | } |
66 | |
67 | /** Put angle into range. Period is equal to max-min. |
68 | * |
69 | * @p x angle value |
70 | * @p min minimal angle |
71 | * @p max maximal angle |
72 | */ |
73 | template<typename T> |
74 | inline T reduceAngle(T x, T min, T max) { |
75 | T delta = max - min; |
76 | return x - delta*floor( (x-min)/delta ); |
77 | } |
78 | |
79 | /** Convert from spherical to cartesian coordiate system. |
80 | * Resulting vector have unit length |
81 | */ |
82 | inline Vector3d fromSperical(dms longitude, dms latitude) { |
83 | double sinL, sinB; |
84 | double cosL, cosB; |
85 | longitude.SinCos( sinL, cosL ); |
86 | latitude.SinCos( sinB, cosB ); |
87 | return Vector3d(cosB*cosL, cosB*sinL, sinB); |
88 | } |
89 | |
90 | /** Convert a vector to a point */ |
91 | inline QPointF vecToPoint(const Vector2f& vec) { |
92 | return QPointF( vec[0], vec[1] ); |
93 | } |
94 | |
95 | /** Convert a point to a vector */ |
96 | inline Vector2f pointToVec(const QPointF& p) { |
97 | return Vector2f(p.x(),p.y()); |
98 | } |
99 | |
100 | /** |
101 | *@short Create a URL to obtain a DSS image for a given SkyPoint |
102 | *@note If SkyPoint is a DeepSkyObject, this method automatically |
103 | *decides the image size required to fit the object. |
104 | */ |
105 | QString getDSSURL( const SkyPoint * const p ); |
106 | |
107 | /** |
108 | *@short Create a URL to obtain a DSS image for a given RA, Dec |
109 | *@param RA The J2000.0 Right Ascension of the point |
110 | *@param Dec The J2000.0 Declination of the point |
111 | *@param width The width of the image in arcminutes |
112 | *@param height The height of the image in arcminutes |
113 | *@param type The image type, either gif or fits. |
114 | *@note This method resets height and width to fall within the range accepted by DSS |
115 | */ |
116 | QString getDSSURL( const dms &ra, const dms &dec, float width = 0, float height = 0, const QString & type = "gif" ); |
117 | |
118 | /** |
119 | *@short Return a string corresponding to an angle specifying direction |
120 | * |
121 | * The angle must measure direction from North, towards East. Both |
122 | * the azimuth and position angle follow this convention, so this |
123 | * method can be used to return a string corresponding to the |
124 | * general heading of a given azimuth / position angle. |
125 | * |
126 | *@param angle angle as dms (measured from North, towards East) |
127 | *@return A localized string corresponding to the approximate direction (eg: NNW) |
128 | */ |
129 | QString toDirectionString( dms angle ); |
130 | |
131 | } |
132 | |
133 | #endif |
134 | |