GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSkyDir.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GSkyDir.hpp - Sky direction class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2019 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GSkyDir.hpp
23  * @brief Sky direction class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GSKYDIR_HPP
28 #define GSKYDIR_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 #include "GVector.hpp"
34 #include "GMath.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 class GTime;
38 
39 /* __ Compile options ____________________________________________________ */
40 #define G_SINCOS_CACHE
41 
42 /* __ Kluge for compilation on Sun _______________________________________ */
43 #ifdef sun
44 #undef sun
45 #endif
46 
47 
48 /***********************************************************************//**
49  * @class GSkyDir
50  *
51  * @brief Sky direction class
52  *
53  * This class implements a spherical coordinate on the sky. Two coordinate
54  * systems are supported: celestial (or equatorial), defined by Right
55  * Ascension and Declination, and Galactic, defined by galactic longitude
56  * and galactic latitude. The class provides automatic conversion between
57  * both systems if required. Coordinates are stored in either of the
58  * systems (in units of radians), and conversion is performed (and stored)
59  * if requested. Coordinates can be given and returned in radians or in
60  * degrees. Note that the epoch for celestial coordinates is fixed to J2000.
61  ***************************************************************************/
62 class GSkyDir : public GBase {
63 
64  // Operator friends
65  friend bool operator==(const GSkyDir &a, const GSkyDir &b);
66  friend bool operator!=(const GSkyDir &a, const GSkyDir &b);
67 
68 public:
69  // Constructors and destructors
70  GSkyDir(void);
71  GSkyDir(const GSkyDir& dir);
72  virtual ~GSkyDir(void);
73 
74  // Operators
75  GSkyDir& operator=(const GSkyDir& dir);
76 
77  // Methods
78  void clear(void);
79  GSkyDir* clone(void) const;
80  std::string classname(void) const;
81  void radec(const double& ra, const double& dec);
82  void radec_deg(const double& ra, const double& dec);
83  void lb(const double& l, const double& b);
84  void lb_deg(const double& l, const double& b);
85  void celvector(const GVector& vector);
86  void rotate_deg(const double& phi, const double& theta);
87  void precess(const double& from_epoch, const double& to_epoch);
88  void sun(const GTime& time, const double& epoch = 2000.0);
89  void moon(const GTime& time, const double& epoch = 2000.0);
90  const double& l(void) const;
91  const double& b(void) const;
92  const double& ra(void) const;
93  const double& dec(void) const;
94  double l_deg(void) const;
95  double b_deg(void) const;
96  double ra_deg(void) const;
97  double dec_deg(void) const;
98  GVector celvector(void) const;
99  double cos_dist(const GSkyDir& dir) const;
100  double dist(const GSkyDir& dir) const;
101  double dist_deg(const GSkyDir& dir) const;
102  double posang(const GSkyDir& dir) const;
103  double posang_deg(const GSkyDir& dir) const;
104  std::string print(const GChatter& chatter = NORMAL) const;
105 
106 private:
107  // Private methods
108  void init_members(void);
109  void copy_members(const GSkyDir& dir);
110  void free_members(void);
111  void equ2gal(void) const;
112  void gal2equ(void) const;
113  void euler(const int& type, const double& xin, const double &yin,
114  double* xout, double *yout) const;
115 
116  // Private members
117  mutable bool m_has_lb; //!< Has galactic coordinates
118  mutable bool m_has_radec; //!< Has equatorial coordinates
119  double m_l; //!< Galactic longitude in radians
120  double m_b; //!< Galactic latitude in radians
121  double m_ra; //!< Right Ascension in radians
122  double m_dec; //!< Declination in radians
123 
124  // Sincos cache
125  #if defined(G_SINCOS_CACHE)
126  mutable bool m_has_lb_cache;
127  mutable bool m_has_radec_cache;
128  mutable double m_sin_b;
129  mutable double m_cos_b;
130  mutable double m_sin_dec;
131  mutable double m_cos_dec;
132  #endif
133 };
134 
135 
136 /***********************************************************************//**
137  * @brief Return class name
138  *
139  * @return String containing the class name ("GSkyDir").
140  ***************************************************************************/
141 inline
142 std::string GSkyDir::classname(void) const
143 {
144  return ("GSkyDir");
145 }
146 
147 
148 /***********************************************************************//**
149  * @brief Return galactic longitude in radians
150  *
151  * @return Galactic longitude in radians.
152  ***************************************************************************/
153 inline
154 const double& GSkyDir::l(void) const
155 {
156  if (!m_has_lb && m_has_radec) {
157  equ2gal();
158  }
159  return m_l;
160 }
161 
162 
163 /***********************************************************************//**
164  * @brief Return galactic longitude in degrees
165  *
166  * @return Galactic longitude in degrees.
167  ***************************************************************************/
168 inline
169 double GSkyDir::l_deg(void) const
170 {
171  return (l() * gammalib::rad2deg);
172 }
173 
174 
175 /***********************************************************************//**
176  * @brief Return galactic latitude in radians
177  *
178  * @return Galactic latitude in radians.
179  ***************************************************************************/
180 inline
181 const double& GSkyDir::b(void) const
182 {
183  if (!m_has_lb && m_has_radec) {
184  equ2gal();
185  }
186  return m_b;
187 }
188 
189 
190 /***********************************************************************//**
191  * @brief Returns galactic latitude in degrees
192  *
193  * @return Galactic latitude in degrees.
194  ***************************************************************************/
195 inline
196 double GSkyDir::b_deg(void) const
197 {
198  return (b() * gammalib::rad2deg);
199 }
200 
201 
202 /***********************************************************************//**
203  * @brief Return Right Ascension in radians
204  *
205  * @return Right Ascension in radians.
206  ***************************************************************************/
207 inline
208 const double& GSkyDir::ra(void) const
209 {
210  if (!m_has_radec && m_has_lb) {
211  gal2equ();
212  }
213  return m_ra;
214 }
215 
216 
217 /***********************************************************************//**
218  * @brief Returns Right Ascension in degrees
219  *
220  * @return Right Ascension in degrees.
221  ***************************************************************************/
222 inline
223 double GSkyDir::ra_deg(void) const
224 {
225  return (ra() * gammalib::rad2deg);
226 }
227 
228 
229 /***********************************************************************//**
230  * @brief Return Declination in radians
231  *
232  * @return Declination in radians.
233  ***************************************************************************/
234 inline
235 const double& GSkyDir::dec(void) const
236 {
237  if (!m_has_radec && m_has_lb) {
238  gal2equ();
239  }
240  return m_dec;
241 }
242 
243 
244 /***********************************************************************//**
245  * @brief Returns Declination in degrees
246  *
247  * @return Declination in degrees.
248  ***************************************************************************/
249 inline
250 double GSkyDir::dec_deg(void) const
251 {
252  return (dec() * gammalib::rad2deg);
253 }
254 
255 
256 /***********************************************************************//**
257  * @brief Compute angular distance between sky directions in radians
258  *
259  * @param[in] dir Sky direction to which distance is to be computed.
260  * @return Angular distance (radians).
261  *
262  * Computes the angular distance between two sky directions in radians.
263  ***************************************************************************/
264 inline
265 double GSkyDir::dist(const GSkyDir& dir) const
266 {
267  return (gammalib::acos(cos_dist(dir)));
268 }
269 
270 
271 /***********************************************************************//**
272  * @brief Compute angular distance between sky directions in degrees
273  *
274  * @param[in] dir Sky direction to which distance is to be computed.
275  * @return Angular distance (degrees).
276  *
277  * Computes the angular distance between two sky directions in degrees.
278  ***************************************************************************/
279 inline
280 double GSkyDir::dist_deg(const GSkyDir& dir) const
281 {
282  return (dist(dir) * gammalib::rad2deg);
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Compute position angle between sky directions in degrees
288  *
289  * @param[in] dir Sky direction.
290  * @return Position angle in degrees.
291  *
292  * Returns the position angle of a sky direction in degrees.
293  *
294  * See the posang() method for more information about the computation of the
295  * position angle.
296  ***************************************************************************/
297 inline
298 double GSkyDir::posang_deg(const GSkyDir& dir) const
299 {
300  return (posang(dir) * gammalib::rad2deg);
301 }
302 
303 #endif /* GSKYDIR_HPP */
double m_sin_dec
Definition: GSkyDir.hpp:130
double dist_deg(const GSkyDir &dir) const
Compute angular distance between sky directions in degrees.
Definition: GSkyDir.hpp:280
double m_cos_b
Definition: GSkyDir.hpp:129
double dec_deg(void) const
Returns Declination in degrees.
Definition: GSkyDir.hpp:250
GSkyDir & operator=(const GSkyDir &dir)
Assignment operator.
Definition: GSkyDir.cpp:112
void lb(const double &l, const double &b)
Set galactic sky direction (radians)
Definition: GSkyDir.cpp:229
double m_sin_b
Definition: GSkyDir.hpp:128
void moon(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Moon.
Definition: GSkyDir.cpp:518
const double & b(void) const
Return galactic latitude in radians.
Definition: GSkyDir.hpp:181
void gal2equ(void) const
Convert galactic to equatorial coordinates.
Definition: GSkyDir.cpp:1235
double m_dec
Declination in radians.
Definition: GSkyDir.hpp:122
Definition of interface for all GammaLib classes.
GSkyDir * clone(void) const
Clone sky direction.
Definition: GSkyDir.cpp:160
double acos(const double &arg)
Computes acos by avoiding NaN due to rounding errors.
Definition: GMath.cpp:69
bool m_has_radec_cache
Definition: GSkyDir.hpp:127
Time class.
Definition: GTime.hpp:54
bool m_has_lb_cache
Definition: GSkyDir.hpp:126
std::string print(const GChatter &chatter=NORMAL) const
Print sky direction information.
Definition: GSkyDir.cpp:1108
const double & ra(void) const
Return Right Ascension in radians.
Definition: GSkyDir.hpp:208
virtual ~GSkyDir(void)
Destructor.
Definition: GSkyDir.cpp:90
std::string classname(void) const
Return class name.
Definition: GSkyDir.hpp:142
double m_b
Galactic latitude in radians.
Definition: GSkyDir.hpp:120
double posang_deg(const GSkyDir &dir) const
Compute position angle between sky directions in degrees.
Definition: GSkyDir.hpp:298
void radec(const double &ra, const double &dec)
Set equatorial sky direction (radians)
Definition: GSkyDir.cpp:175
void copy_members(const GSkyDir &dir)
Copy class members.
Definition: GSkyDir.cpp:1175
void sun(const GTime &time, const double &epoch=2000.0)
Set sky direction to direction of Sun.
Definition: GSkyDir.cpp:448
void rotate_deg(const double &phi, const double &theta)
Rotate sky direction by zenith and azimuth angle.
Definition: GSkyDir.cpp:321
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
void init_members(void)
Initialise class members.
Definition: GSkyDir.cpp:1145
bool m_has_lb
Has galactic coordinates.
Definition: GSkyDir.hpp:117
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition: GSkyDir.hpp:223
friend bool operator==(const GSkyDir &a, const GSkyDir &b)
Equality operator.
Definition: GSkyDir.cpp:1312
GChatter
Definition: GTypemaps.hpp:33
const double & l(void) const
Return galactic longitude in radians.
Definition: GSkyDir.hpp:154
const double & dec(void) const
Return Declination in radians.
Definition: GSkyDir.hpp:235
GSkyDir(void)
Constructor.
Definition: GSkyDir.cpp:59
void free_members(void)
Delete class members.
Definition: GSkyDir.cpp:1203
Vector class interface definition.
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition: GSkyDir.cpp:202
double cos_dist(const GSkyDir &dir) const
Compute cosine of angular distance between sky directions.
Definition: GSkyDir.cpp:914
GVector celvector(void) const
Return sky direction as 3D vector in celestial coordinates.
Definition: GSkyDir.cpp:877
void clear(void)
Clear sky direction.
Definition: GSkyDir.cpp:142
friend bool operator!=(const GSkyDir &a, const GSkyDir &b)
Non equality operator.
Definition: GSkyDir.cpp:1376
void euler(const int &type, const double &xin, const double &yin, double *xout, double *yout) const
General coordinate transformation routine for J2000.
Definition: GSkyDir.cpp:1263
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition: GSkyDir.cpp:256
double m_l
Galactic longitude in radians.
Definition: GSkyDir.hpp:119
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition: GSkyDir.hpp:265
bool m_has_radec
Has equatorial coordinates.
Definition: GSkyDir.hpp:118
Vector class.
Definition: GVector.hpp:46
double l_deg(void) const
Return galactic longitude in degrees.
Definition: GSkyDir.hpp:169
double b_deg(void) const
Returns galactic latitude in degrees.
Definition: GSkyDir.hpp:196
double m_cos_dec
Definition: GSkyDir.hpp:131
const double rad2deg
Definition: GMath.hpp:44
double m_ra
Right Ascension in radians.
Definition: GSkyDir.hpp:121
double posang(const GSkyDir &dir) const
Compute position angle between sky directions in radians.
Definition: GSkyDir.cpp:1022
Sky direction class.
Definition: GSkyDir.hpp:62
void precess(const double &from_epoch, const double &to_epoch)
Precess sky direction.
Definition: GSkyDir.cpp:371
Mathematical function definitions.
void equ2gal(void) const
Convert equatorial to galactic coordinates.
Definition: GSkyDir.cpp:1213