Commit 4fe83f553c519edda926471e1e96b6bb582fbed9

Authored by Andreas Specovius
Committed by Jürgen Knödlseder
1 parent d371f910

Added transform_to_local() method. Implemented caching of m_posang cos and sin.

include/GSkyRegionRect.hpp
... ... @@ -93,18 +93,24 @@ public:
93 93 bool overlaps(const GSkyRegion& reg) const;
94 94 std::string print(const GChatter& chatter = NORMAL) const;
95 95  
  96 + GSkyDir transform_to_local(const GSkyDir& skydir) const;
  97 +
96 98 protected:
97 99 // Protected methods
98 100 void init_members(void);
99 101 void copy_members(const GSkyRegionRect& region);
100 102 void free_members(void);
101 103 void compute_solid_angle(void);
  104 + void update_cache(void);
102 105  
103 106 // Protected members
104   - GSkyDir m_centre; //!< Centre or reference point of the region
105   - double m_halfwidth; //!< Half width of the region [deg]
  107 + GSkyDir m_centre; //!< Centre or reference point of the region
  108 + double m_halfwidth; //!< Half width of the region [deg]
106 109 double m_halfheight; //!< Half height of the region [deg]
107 110 double m_posang; //!< Position angle, counterclockwise from North [radians]
  111 +
  112 + double m_posang_cos; //!< Cache for transform_to_local
  113 + double m_posang_sin; //!< Cache for transform_to_local
108 114 };
109 115  
110 116  
... ... @@ -153,44 +159,6 @@ double GSkyRegionRect::posang_deg(void) const
153 159  
154 160  
155 161 /***********************************************************************//**
156   - * @brief Set position angle of rectangular region
157   - *
158   - * @param[in] posang Position angle [radians].
159   - *
160   - * Sets the position angle of the rectangular sky region. The position angle
161   - * is counted counterclockwise from North.
162   - ***************************************************************************/
163   -inline
164   -void GSkyRegionRect::posang(const double& posang)
165   -{
166   - // Set the position angle
167   - m_posang = posang;
168   -
169   - // Return
170   - return;
171   -}
172   -
173   -
174   -/***********************************************************************//**
175   - * @brief Set position angle of rectangular region
176   - *
177   - * @param[in] posang Position angle [degrees].
178   - *
179   - * Sets the position angle of the rectangular sky region. The position angle
180   - * is counted counterclockwise from North.
181   - ***************************************************************************/
182   -inline
183   -void GSkyRegionRect::posang_deg(const double& posang)
184   -{
185   - // Set the position angle
186   - m_posang = posang * gammalib::deg2rad;
187   -
188   - // Return
189   - return;
190   -}
191   -
192   -
193   -/***********************************************************************//**
194 162 * @brief Return region width extension (in degrees)
195 163 *
196 164 * @return Region width [deg].
... ...
pyext/GSkyRegionRect.i
... ... @@ -72,6 +72,8 @@ public:
72 72 bool contains(const GSkyDir& dir) const;
73 73 bool contains(const GSkyRegion& reg) const;
74 74 bool overlaps(const GSkyRegion& reg) const;
  75 +
  76 + GSkyDir transform_to_local(const GSkyDir& skydir) const;
75 77 };
76 78  
77 79  
... ...
src/sky/GSkyRegionRect.cpp
... ... @@ -250,6 +250,48 @@ GSkyRegionRect* GSkyRegionRect::clone(void) const
250 250  
251 251  
252 252 /***********************************************************************//**
  253 + * @brief Set position angle of rectangular region
  254 + *
  255 + * @param[in] posang Position angle [radians].
  256 + *
  257 + * Sets the position angle of the rectangular sky region. The position angle
  258 + * is counted counterclockwise from North.
  259 + ***************************************************************************/
  260 +void GSkyRegionRect::posang(const double& posang)
  261 +{
  262 + // Set the position angle
  263 + m_posang = posang;
  264 +
  265 + // Update the cache
  266 + update_cache();
  267 +
  268 + // Return
  269 + return;
  270 +}
  271 +
  272 +
  273 +/***********************************************************************//**
  274 + * @brief Set position angle of rectangular region
  275 + *
  276 + * @param[in] posang Position angle [degrees].
  277 + *
  278 + * Sets the position angle of the rectangular sky region. The position angle
  279 + * is counted counterclockwise from North.
  280 + ***************************************************************************/
  281 +void GSkyRegionRect::posang_deg(const double& posang)
  282 +{
  283 + // Set the position angle
  284 + m_posang = posang * gammalib::deg2rad;
  285 +
  286 + // Update the cache
  287 + update_cache();
  288 +
  289 + // Return
  290 + return;
  291 +}
  292 +
  293 +
  294 +/***********************************************************************//**
253 295 * @brief Set width of rectangular region
254 296 *
255 297 * @param[in] width Width [deg].
... ... @@ -586,6 +628,38 @@ bool GSkyRegionRect::overlaps(const GSkyRegion&amp; reg) const
586 628 }
587 629  
588 630  
  631 +/***********************************************************************//**
  632 + * @brief Transform a sky direction to the local coordinate system.
  633 + *
  634 + * @param[in] skydir Sky direction in global coordinate system.
  635 + * @return Sky direction in local region object coordinates.
  636 + *
  637 + * Transform the sky direction :skydir: to the local coordinate system. The
  638 + * origin of the local coordinate system is fixed to the center of the
  639 + * rectangle and aligned in +ra (width) and +dec (height).
  640 + ***************************************************************************/
  641 +GSkyDir GSkyRegionRect::transform_to_local(const GSkyDir& skydir) const
  642 +{
  643 + // Compute separation (in radians)
  644 + double dx = skydir.ra() - m_centre.ra();
  645 + double dy = skydir.dec() - m_centre.dec();
  646 +
  647 + // Correct for spherical coordinate system (global dx -> local dx)
  648 + dx *= std::cos(skydir.dec());
  649 +
  650 + // Rotate with neg. PA
  651 + double new_x = m_posang_cos*dx - m_posang_sin*dy;
  652 + double new_y = m_posang_sin*dx + m_posang_cos*dy;
  653 +
  654 + // Create output sky direction
  655 + GSkyDir transformed;
  656 + transformed.radec(new_x, new_y);
  657 +
  658 + // Return
  659 + return transformed;
  660 +}
  661 +
  662 +
589 663  
590 664 /*==========================================================================
591 665 = =
... ... @@ -623,6 +697,10 @@ void GSkyRegionRect::copy_members(const GSkyRegionRect&amp; region)
623 697 m_halfheight = region.m_halfheight;
624 698 m_posang = region.m_posang;
625 699  
  700 + // Copy cache
  701 + m_posang_cos = region.m_posang_cos;
  702 + m_posang_sin = region.m_posang_sin;
  703 +
626 704 // Return
627 705 return;
628 706 }
... ... @@ -650,3 +728,17 @@ void GSkyRegionRect::compute_solid_angle(void)
650 728 // Return
651 729 return;
652 730 }
  731 +
  732 +
  733 +/***********************************************************************//**
  734 + * @brief Update the cache.
  735 + ***************************************************************************/
  736 +void GSkyRegionRect::update_cache(void)
  737 +{
  738 + // Precompute cos and sin of position angle
  739 + m_posang_cos = std::cos(m_posang);
  740 + m_posang_sin = std::sin(m_posang);
  741 +
  742 + // Return
  743 + return;
  744 +}
... ...