Commit 4a031501e543eb3e60714abef713f09547992bcc

Authored by Karl Kosack
1 parent 7956c168

Added GHorizDir class to store alt/az coordinates

include/GHorizDir.hpp 0 → 100644
  1 +/***************************************************************************
  2 + * GHorizDir.hpp - Horizontal direction class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2008-2013 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 GHorizDir.hpp
  23 + * @brief Horizontal direction class interface definition
  24 + * @author K. Kosack
  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 +
  35 +/* __ Compile options ____________________________________________________ */
  36 +#define G_SINCOS_CACHE
  37 +
  38 +
  39 +/***********************************************************************//**
  40 + * @class GHorizDir
  41 + *
  42 + * @brief Horizontal (Alt/Az) direction class
  43 + *
  44 + * This class is essentially a copy of GSkyDir and implements a
  45 + * spherical coordinate on the sky, in horizontal coordinates as seen
  46 + * from Earth.
  47 + *
  48 + * @note since this class shares much functionality with GSkyDir, a
  49 + * common base-class should probably be created in the future to avoid
  50 + * duplication of code.
  51 + ***************************************************************************/
  52 +class GHorizDir : public GBase {
  53 +
  54 + // Operator friends
  55 + friend bool operator==(const GHorizDir &a, const GHorizDir &b);
  56 + friend bool operator!=(const GHorizDir &a, const GHorizDir &b);
  57 +
  58 +public:
  59 + // Constructors and destructors
  60 + GHorizDir(void);
  61 + GHorizDir(const GHorizDir& dir);
  62 + virtual ~GHorizDir(void);
  63 +
  64 + // Operators
  65 + GHorizDir& operator= (const GHorizDir& dir);
  66 +
  67 + // Methods
  68 + void clear(void);
  69 + GHorizDir* clone(void) const;
  70 + void altaz(const double& alt, const double& az);
  71 + void altaz_deg(const double& alt, const double& az);
  72 + void celvector(const GVector& vector);
  73 + void rotate_deg(const double& phi, const double& theta);
  74 + const double& alt(void) const;
  75 + const double& az(void) const;
  76 + double zenith() const;
  77 + double zenith_deg() const;
  78 + double alt_deg(void) const;
  79 + double az_deg(void) const;
  80 + GVector celvector(void) const;
  81 + double dist(const GHorizDir& dir) const;
  82 + double dist_deg(const GHorizDir& dir) const;
  83 + std::string print(const GChatter& chatter = NORMAL) const;
  84 +
  85 +
  86 +private:
  87 + // Private methods
  88 + void init_members(void);
  89 + void copy_members(const GHorizDir& dir);
  90 + void free_members(void);
  91 +
  92 + // Private members
  93 + double m_alt; //!< altitude in radians
  94 + double m_az; //!< azimuth in radians
  95 +
  96 + // Sincos cache
  97 + // #if defined(G_SINCOS_CACHE)
  98 + // mutable bool m_has_lb_cache;
  99 + // mutable bool m_has_radec_cache;
  100 + // mutable double m_sin_b;
  101 + // mutable double m_cos_b;
  102 + // mutable double m_sin_dec;
  103 + // mutable double m_cos_dec;
  104 + // #endif
  105 +};
  106 +
  107 +
  108 +/***********************************************************************//**
  109 + * @brief Return zenith angle in radians
  110 + *
  111 + * @return zenith angle in radians
  112 + ***************************************************************************/
  113 +inline
  114 +double GHorizDir::zenith()
  115 +{
  116 + return gammalib::pihalf - m_alt;
  117 +};
  118 +
  119 +/***********************************************************************//**
  120 + * @brief Return zenith angle in radians
  121 + *
  122 + * @return zenith angle in radians
  123 + ***************************************************************************/
  124 +inline
  125 +double GHorizDir::zenith_deg()
  126 +{
  127 + return zenith() * gammalib::rad2deg;
  128 +};
  129 +
  130 +
  131 +#endif /* GHORIZDIR_HPP */
... ...
include/Makefile.am
... ... @@ -77,6 +77,7 @@ pkginclude_HEADERS = GammaLib.hpp \
77 77 GTestSuites.hpp \
78 78 GTestCase.hpp \
79 79 GSkyDir.hpp \
  80 + GHorizDir.hpp \
80 81 GSkyPixel.hpp \
81 82 GSkymap.hpp \
82 83 GSkyRegion.hpp \
... ...
src/sky/GHorizDir.cpp 0 → 100644
  1 +/***************************************************************************
  2 + * GHorizDir.cpp - Horizontal direction class *
  3 + * ----------------------------------------------------------------------- *
  4 + * copyright (C) 2008-2013 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 GHorizDir.cpp
  23 + * @brief Horiz direction class implementation
  24 + * @author K. Kosack
  25 + */
  26 +
  27 +/* __ Includes ___________________________________________________________ */
  28 +#ifdef HAVE_CONFIG_H
  29 +#include <config.h>
  30 +#endif
  31 +#include <cmath>
  32 +#include "GException.hpp"
  33 +#include "GTools.hpp"
  34 +#include "GMath.hpp"
  35 +#include "GHorizDir.hpp"
  36 +#include "GMatrix.hpp"
  37 +#include "GVector.hpp"
  38 +
  39 +/* __ Method name definitions ____________________________________________ */
  40 +
  41 +/* __ Macros _____________________________________________________________ */
  42 +
  43 +/* __ Coding definitions _________________________________________________ */
  44 +
  45 +/* __ Debug definitions __________________________________________________ */
  46 +
  47 +/* __ Prototypes _________________________________________________________ */
  48 +
  49 +/*==========================================================================
  50 + = =
  51 + = Constructors/destructors =
  52 + = =
  53 + ==========================================================================*/
  54 +
  55 +/***********************************************************************//**
  56 + * @brief Constructor
  57 + ***************************************************************************/
  58 +GHorizDir::GHorizDir(void)
  59 +{
  60 + // Initialise members
  61 + init_members();
  62 +
  63 + // Return
  64 + return;
  65 +}
  66 +
  67 +
  68 +/***********************************************************************//**
  69 + * @brief Copy constructor
  70 + *
  71 + * @param[in] dir Horiz direction.
  72 + ***************************************************************************/
  73 +GHorizDir::GHorizDir(const GHorizDir& dir)
  74 +{
  75 + // Initialise members
  76 + init_members();
  77 +
  78 + // Copy members
  79 + copy_members(dir);
  80 +
  81 + // Return
  82 + return;
  83 +}
  84 +
  85 +
  86 +/***********************************************************************//**
  87 + * @brief Destructor
  88 + ***************************************************************************/
  89 +GHorizDir::~GHorizDir(void)
  90 +{
  91 + // Free members
  92 + free_members();
  93 +
  94 + // Return
  95 + return;
  96 +}
  97 +
  98 +
  99 +/*==========================================================================
  100 + = =
  101 + = Operators =
  102 + = =
  103 + ==========================================================================*/
  104 +
  105 +/***********************************************************************//**
  106 + * @brief Assignment operator
  107 + *
  108 + * @param[in] dir Horiz direction.
  109 + * @return Horiz direction.
  110 + ***************************************************************************/
  111 +GHorizDir& GHorizDir::operator= (const GHorizDir& dir)
  112 +{
  113 + // Execute only if object is not identical
  114 + if (this != &dir) {
  115 +
  116 + // Free members
  117 + free_members();
  118 +
  119 + // Initialise members
  120 + init_members();
  121 +
  122 + // Copy members
  123 + copy_members(dir);
  124 +
  125 + } // endif: object was not identical
  126 +
  127 + // Return this object
  128 + return *this;
  129 +}
  130 +
  131 +
  132 +/*==========================================================================
  133 + = =
  134 + = Public methods =
  135 + = =
  136 + ==========================================================================*/
  137 +
  138 +/***********************************************************************//**
  139 + * @brief Clear sky direction
  140 + ***************************************************************************/
  141 +void GHorizDir::clear(void)
  142 +{
  143 + // Free members
  144 + free_members();
  145 +
  146 + // Initialise members
  147 + init_members();
  148 +
  149 + // Return
  150 + return;
  151 +}
  152 +
  153 +
  154 +/***********************************************************************//**
  155 + * @brief Clone sky direction
  156 + *
  157 + * @return Pointer to deep copy of sky direction.
  158 + ***************************************************************************/
  159 +GHorizDir* GHorizDir::clone(void) const
  160 +{
  161 + // Clone sky direction
  162 + return new GHorizDir(*this);
  163 +}
  164 +
  165 +
  166 +/***********************************************************************//**
  167 + * @brief Set equatorial sky direction (radians)
  168 + *
  169 + * @param[in] ra Right Ascension in radians.
  170 + * @param[in] dec Declination in radians.
  171 + *
  172 + * Sets Right Ascension and Declination in radians.
  173 + ***************************************************************************/
  174 +void GHorizDir::altaz(const double& alt, const double& az)
  175 +{
  176 +
  177 + // Set direction
  178 + m_alt = alt;
  179 + m_az = az;
  180 +
  181 + // Return
  182 + return;
  183 +}
  184 +
  185 +
  186 +/***********************************************************************//**
  187 + * @brief Set equatorial sky direction (degrees)
  188 + *
  189 + * @param[in] ra Right Ascension in degrees.
  190 + * @param[in] dec Declination in degrees.
  191 + *
  192 + * Sets Right Ascension and Declination in degrees.
  193 + ***************************************************************************/
  194 +void GHorizDir::altaz_deg(const double& alt, const double& az)
  195 +{
  196 +
  197 + // Set direction
  198 + m_ra = alt * gammalib::deg2rad;
  199 + m_dec = az * gammalib::deg2rad;
  200 +
  201 + // Return
  202 + return;
  203 +}
  204 +
  205 +
  206 +/***********************************************************************//**
  207 + * @brief Set sky direction from 3D vector in horizontal coordinates
  208 + *
  209 + * @param[in] vector 3D vector.
  210 + *
  211 + * Convert a 3-dimensional vector in celestial coordinates into a sky
  212 + * direction. The transformation is given by
  213 + *
  214 + * \f[
  215 + * \alpha = \arctan \left( \frac{x_1}{x_0} \right)
  216 + * \f]
  217 + *
  218 + * \f[
  219 + * \delta = \arcsin x_2
  220 + * \f]
  221 + ***************************************************************************/
  222 +void GHorizDir::celvector(const GVector& vector)
  223 +{
  224 +
  225 +
  226 + // Convert vector into sky position
  227 + m_alt = std::asin(vector[2]);
  228 + m_az = std::atan2(vector[1], vector[0]);
  229 +
  230 + // Return
  231 + return;
  232 +}
  233 +
  234 +
  235 +/***********************************************************************//**
  236 + * @brief Rotate sky direction by zenith and azimuth angle
  237 + *
  238 + * @param[in] phi Azimuth angle (deg).
  239 + * @param[in] theta Zenith angle (deg).
  240 + *
  241 + * Rotate sky direction by a zenith and azimuth angle given in the system
  242 + * of the sky direction and aligned in celestial coordinates.
  243 + * The azimuth angle is counted counter clockwise from celestial north
  244 + * (this is identical to the astronomical definition of a position angle).
  245 + ***************************************************************************/
  246 +void GHorizDir::rotate_deg(const double& phi, const double& theta)
  247 +{
  248 +
  249 + // Allocate Euler and rotation matrices
  250 + GMatrix ry;
  251 + GMatrix rz;
  252 +
  253 + // Set up rotation matrix to rotate from native coordinates to
  254 + // celestial coordinates
  255 + ry.eulery(m_alt * gammalib::rad2deg - 90.0);
  256 + rz.eulerz(-m_az * gammalib::rad2deg);
  257 + GMatrix rot = (ry * rz).transpose();
  258 +
  259 + // Set up native coordinate vector
  260 + double phi_rad = phi * gammalib::deg2rad;
  261 + double theta_rad = theta * gammalib::deg2rad;
  262 + double cos_phi = std::cos(phi_rad);
  263 + double sin_phi = std::sin(phi_rad);
  264 + double cos_theta = std::cos(theta_rad);
  265 + double sin_theta = std::sin(theta_rad);
  266 + GVector native(-cos_phi*sin_theta, sin_phi*sin_theta, cos_theta);
  267 +
  268 + // Rotate vector into celestial coordinates
  269 + GVector dir = rot * native;
  270 +
  271 + // Convert vector into sky position
  272 + celvector(dir);
  273 +
  274 + // Return
  275 + return;
  276 +}
  277 +
  278 +
  279 +/***********************************************************************//**
  280 + * @brief Return sky direction as 3D vector in celestial coordinates
  281 + *
  282 + * @return Horiz direction as 3D vector in celestial coordinates.
  283 + ***************************************************************************/
  284 +GVector GHorizDir::celvector(void) const
  285 +{
  286 + // Compute 3D vector
  287 + double cosaz = std::cos(m_az);
  288 + double sinaz = std::sin(m_az);
  289 + double cosalt = std::cos(m_alt);
  290 + double sinalt = std::sin(m_alt);
  291 + GVector vector(cosdec*cosaz, cosdec*sinaz, sinalt);
  292 +
  293 + // Return vector
  294 + return vector;
  295 +}
  296 +
  297 +
  298 +/***********************************************************************//**
  299 + * @brief Compute angular distance between sky directions in radians
  300 + *
  301 + * @param[in] dir Horiz direction to which distance is to be computed.
  302 + * @return Angular distance in radians.
  303 + *
  304 + * Computes the angular distance between two sky directions in radians.
  305 + ***************************************************************************/
  306 +double GHorizDir::dist(const GHorizDir& dir) const
  307 +{
  308 + // Initialise cosine of distance
  309 + double cosdis;
  310 +
  311 + // Compute dependent on coordinate system availability. This speeds
  312 + // up things by avoiding unnecessary coordinate transformations.
  313 +
  314 + cosdis = sin(m_alt) * sin(dir.alt()) +
  315 + cos(m_alt) * cos(dir.alt()) *
  316 + std::cos(dir.az() - m_az);
  317 +
  318 + // Compute distance (use argument save GTools function)
  319 + double dist = gammalib::acos(cosdis);
  320 +
  321 + // Return distance
  322 + return dist;
  323 +}
  324 +
  325 +
  326 +/***********************************************************************//**
  327 + * @brief Compute angular distance between sky directions in degrees
  328 + *
  329 + * @param[in] dir Horiz direction to which distance is to be computed.
  330 + * @return Angular distance in degrees.
  331 + *
  332 + * Computes the angular distance between two sky directions in degrees.
  333 + ***************************************************************************/
  334 +double GHorizDir::dist_deg(const GHorizDir& dir) const
  335 +{
  336 + // Return distance in degrees
  337 + return (dist(dir) * gammalib::rad2deg);
  338 +}
  339 +
  340 +
  341 +
  342 +/***********************************************************************//**
  343 + * @brief Print horizontal direction information
  344 + *
  345 + * @param[in] chatter Chattiness (defaults to NORMAL).
  346 + * @return String containing sky direction information.
  347 + ***************************************************************************/
  348 +std::string GHorizDir::print(const GChatter& chatter) const
  349 +{
  350 + // Initialise result string
  351 + std::string result;
  352 +
  353 + // Continue only if chatter is not silent
  354 + if (chatter != SILENT) {
  355 +
  356 + // Put coordinates in string
  357 + result = "(Az,Alt)=("+gammalib::str(m_az*gammalib::rad2deg) + "," +
  358 + gammalib::str(m_alt*gammalib::rad2deg)+")";
  359 +
  360 + } // endif: chatter was not silent
  361 +
  362 + // Return result
  363 + return result;
  364 +}
  365 +
  366 +
  367 +/*==========================================================================
  368 + = =
  369 + = Private methods =
  370 + = =
  371 + ==========================================================================*/
  372 +
  373 +/***********************************************************************//**
  374 + * @brief Initialise class members
  375 + ***************************************************************************/
  376 +void GHorizDir::init_members(void)
  377 +{
  378 + // Initialise members
  379 + m_az = 0.0;
  380 + m_alt = 0.0;
  381 +
  382 + // Return
  383 + return;
  384 +}
  385 +
  386 +
  387 +/***********************************************************************//**
  388 + * @brief Copy class members
  389 + *
  390 + * @param[in] dir Horiz direction.
  391 + ***************************************************************************/
  392 +void GHorizDir::copy_members(const GHorizDir& dir)
  393 +{
  394 + m_az = dir.m_az;
  395 + m_alt = dir.m_alt;
  396 +
  397 + // Return
  398 + return;
  399 +}
  400 +
  401 +
  402 +/***********************************************************************//**
  403 + * @brief Delete class members
  404 + ***************************************************************************/
  405 +void GHorizDir::free_members(void)
  406 +{
  407 + // Return
  408 + return;
  409 +}
  410 +
  411 +
  412 +/*==========================================================================
  413 + = =
  414 + = Friends =
  415 + = =
  416 + ==========================================================================*/
  417 +
  418 +/***********************************************************************
  419 + * @brief Equality operator
  420 + *
  421 + * @param[in] a First horiz direction.
  422 + * @param[in] b Second horiz direction.
  423 + *
  424 + * Compare two horiz directions. If the coordinate is at the pole, the
  425 + * Azimuth value is irrelevant.
  426 + *
  427 + * @todo: really should test for being within some tolerance here
  428 + *
  429 + ***************************************************************************/
  430 +bool operator==(const GHorizDir &a, const GHorizDir &b)
  431 +{
  432 + // Initialise result
  433 + bool equal = false;
  434 +
  435 + // Compute dependent on coordinate system availability. This speeds
  436 + // up things by avoiding unnecessary coordinate transformations.
  437 +
  438 + if (std::abs(std::abs(a.m_alt) - 90.0) < 1e-10) {
  439 + equal = (a.m_alt == b.m_alt);
  440 + }
  441 + else {
  442 + equal = (a.m_alt == b.m_alt && a.m_az == b.m_az);
  443 + }
  444 +
  445 + // Return equality
  446 + return equal;
  447 +}
  448 +
  449 +
  450 +/***********************************************************************//**
  451 + * @brief Non equality operator
  452 + *
  453 + * @param[in] a First sky direction.
  454 + * @param[in] b Second sky direction.
  455 + ***************************************************************************/
  456 +bool operator!=(const GHorizDir &a, const GHorizDir &b)
  457 +{
  458 + // Return non equality
  459 + return (!(a==b));
  460 +}
  461 +
... ...
src/sky/Makefile.am
... ... @@ -7,6 +7,7 @@ INCLUDES = -I$(top_srcdir)/include \
7 7  
8 8 # Define sources for this directory
9 9 sources = GSkyDir.cpp \
  10 + GHorizDir.cpp \
10 11 GSkyPixel.cpp \
11 12 GSkymap.cpp \
12 13 GSkyRegions.cpp \
... ...