GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GWcsSTG.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GWcsSTG.cpp - Stereographic (STG) projection class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2021 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 GWcsSTG.cpp
23  * @brief Stereographic (STG) projection class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GMath.hpp"
33 #include "GWcsSTG.hpp"
34 #include "GWcsRegistry.hpp"
35 
36 /* __ Method name definitions ____________________________________________ */
37 #define G_PRJ_S2X "GWcsSTG::prj_s2x(int, int, int, int, double*, double*,"\
38  " double*, double*, int*)"
39 
40 /* __ Macros _____________________________________________________________ */
41 
42 /* __ Coding definitions _________________________________________________ */
43 
44 /* __ Debug definitions __________________________________________________ */
45 
46 /* __ Local prototypes ___________________________________________________ */
47 
48 /* __ Constants __________________________________________________________ */
49 
50 /* __ Globals ____________________________________________________________ */
52 const GWcsRegistry g_wcs_stg_registry(&g_wcs_stg_seed);
53 
54 
55 /*==========================================================================
56  = =
57  = Constructors/destructors =
58  = =
59  ==========================================================================*/
60 
61 /***********************************************************************//**
62  * @brief Void constructor
63  ***************************************************************************/
65 {
66  // Initialise class members
67  init_members();
68 
69  // Return
70  return;
71 }
72 
73 
74 /***********************************************************************//**
75  * @brief Projection constructor
76  *
77  * @param[in] coords Coordinate system.
78  * @param[in] crval1 X value of reference pixel.
79  * @param[in] crval2 Y value of reference pixel.
80  * @param[in] crpix1 X index of reference pixel (starting from 1).
81  * @param[in] crpix2 Y index of reference pixel (starting from 1).
82  * @param[in] cdelt1 Increment in x direction at reference pixel [deg].
83  * @param[in] cdelt2 Increment in y direction at reference pixel [deg].
84  ***************************************************************************/
85 GWcsSTG::GWcsSTG(const std::string& coords,
86  const double& crval1, const double& crval2,
87  const double& crpix1, const double& crpix2,
88  const double& cdelt1, const double& cdelt2) :
89  GWcs(coords, crval1, crval2, crpix1, crpix2, cdelt1, cdelt2)
90 
91 {
92  // Initialise class members
93  init_members();
94 
95  // Setup WCS derived parameters
96  wcs_set();
97 
98  // Return
99  return;
100 }
101 
102 
103 /***********************************************************************//**
104  * @brief Copy constructor
105  *
106  * @param[in] wcs World Coordinate System.
107  ***************************************************************************/
108 GWcsSTG::GWcsSTG(const GWcsSTG& wcs) : GWcs(wcs)
109 {
110  // Initialise class members for clean destruction
111  init_members();
112 
113  // Copy members
114  copy_members(wcs);
115 
116  // Return
117  return;
118 }
119 
120 
121 /***********************************************************************//**
122  * @brief Destructor
123  ***************************************************************************/
125 {
126  // Free members
127  free_members();
128 
129  // Return
130  return;
131 }
132 
133 
134 /*==========================================================================
135  = =
136  = Operators =
137  = =
138  ==========================================================================*/
139 
140 /***********************************************************************//**
141  * @brief Assignment operator
142  *
143  * @param[in] wcs World Coordinate System.
144  * @return World Coordinate System.
145  ***************************************************************************/
147 {
148  // Execute only if object is not identical
149  if (this != &wcs) {
150 
151  // Copy base class members
152  this->GWcs::operator=(wcs);
153 
154  // Free members
155  free_members();
156 
157  // Initialise private members for clean destruction
158  init_members();
159 
160  // Copy members
161  copy_members(wcs);
162 
163  } // endif: object was not identical
164 
165  // Return this object
166  return *this;
167 }
168 
169 
170 /*==========================================================================
171  = =
172  = Public methods =
173  = =
174  ==========================================================================*/
175 
176 /***********************************************************************//**
177  * @brief Clear projection
178  *
179  * This method properly resets the object to an initial state.
180  ***************************************************************************/
181 void GWcsSTG::clear(void)
182 {
183  // Free class members (base and derived classes, derived class first)
184  free_members();
185  this->GWcs::free_members();
187 
188  // Initialise members
190  this->GWcs::init_members();
191  init_members();
192 
193  // Return
194  return;
195 }
196 
197 
198 /***********************************************************************//**
199  * @brief Clone projection
200  *
201  * @return Pointer to deep copy of projection.
202  ***************************************************************************/
204 {
205  return new GWcsSTG(*this);
206 }
207 
208 
209 /***********************************************************************//**
210  * @brief Print projection information
211  *
212  * @param[in] chatter Chattiness (defaults to NORMAL).
213  * @return String containing projection information.
214  ***************************************************************************/
215 std::string GWcsSTG::print(const GChatter& chatter) const
216 {
217  // Initialise result string
218  std::string result;
219 
220  // Continue only if chatter is not silent
221  if (chatter != SILENT) {
222 
223  // Append header
224  result.append("=== GWcsSTG ===");
225 
226  // Append information
227  result.append(wcs_print(chatter));
228 
229  } // endif: chatter was not silent
230 
231  // Return result
232  return result;
233 }
234 
235 
236 /*==========================================================================
237  = =
238  = Private methods =
239  = =
240  ==========================================================================*/
241 
242 /***********************************************************************//**
243  * @brief Initialise class members
244  ***************************************************************************/
246 {
247  // Return
248  return;
249 }
250 
251 
252 /***********************************************************************//**
253  * @brief Copy class members
254  *
255  * @param[in] wcs World Coordinate System.
256  ***************************************************************************/
258 {
259  // Return
260  return;
261 }
262 
263 
264 /***********************************************************************//**
265  * @brief Delete class members
266  ***************************************************************************/
268 {
269  // Return
270  return;
271 }
272 
273 
274 /***********************************************************************//**
275  * @brief Setup of projection
276  *
277  * This method sets up the projection information. The method has been
278  * adapted from the wcslib function prj.c::stgset.
279  *
280  * Given and/or returned:
281  * m_r0 Reset to 180/pi if 0.
282  * m_phi0 Reset to 0.0 if undefined.
283  * m_theta0 Reset to 90.0 if undefined.
284  *
285  * Returned:
286  * m_x0 Fiducial offset in x.
287  * m_y0 Fiducial offset in y.
288  * m_w[0] 2*r0
289  * m_w[1] 1/(2*r0)
290  ***************************************************************************/
291 void GWcsSTG::prj_set(void) const
292 {
293  // Signal that projection has been set (needs to be done before calling
294  // the prj_off() method to avoid an endless loop)
295  m_prjset = true;
296 
297  // Initialise projection parameters
298  m_w.clear();
299 
300  // Precompute
301  if (m_r0 == 0.0) {
303  }
304  m_w.push_back(2.0 * m_r0);
305  m_w.push_back(1.0/m_w[0]);
306 
307  // Compute fiducial offset
308  prj_off(0.0, 90.0);
309 
310  // Return
311  return;
312 }
313 
314 
315 /***********************************************************************//**
316  * @brief Pixel-to-spherical deprojection
317  *
318  * @param[in] nx X vector length.
319  * @param[in] ny Y vector length (0=no replication).
320  * @param[in] sxy Input vector step.
321  * @param[in] spt Output vector step.
322  * @param[in] x Vector of projected x coordinates.
323  * @param[in] y Vector of projected y coordinates.
324  * @param[out] phi Longitude of the projected point in native spherical
325  * coordinates [deg].
326  * @param[out] theta Latitude of the projected point in native spherical
327  * coordinates [deg].
328  * @param[out] stat Status return value for each vector element (always 0)
329  *
330  * Deproject pixel (x,y) coordinates in the plane of projection to native
331  * spherical coordinates (phi,theta).
332  *
333  * This method has been adapted from the wcslib function prj.c::stgx2s().
334  * The interface follows very closely that of wcslib. In contrast to the
335  * wcslib routine, however, the method assumes that the projection has been
336  * setup previously (as this will be done by the constructor).
337  ***************************************************************************/
338 void GWcsSTG::prj_x2s(int nx, int ny, int sxy, int spt,
339  const double* x, const double* y,
340  double* phi, double* theta, int* stat) const
341 {
342  // Initialize projection if required
343  if (!m_prjset) {
344  prj_set();
345  }
346 
347  // Set value replication length mx,my
348  int mx;
349  int my;
350  if (ny > 0) {
351  mx = nx;
352  my = ny;
353  }
354  else {
355  mx = 1;
356  my = 1;
357  ny = nx;
358  }
359 
360  // Do x dependence
361  const double* xp = x;
362  int rowoff = 0;
363  int rowlen = nx * spt;
364  for (int ix = 0; ix < nx; ++ix, rowoff += spt, xp += sxy) {
365  double xj = *xp + m_x0;
366  double* phip = phi + rowoff;
367  for (int iy = 0; iy < my; ++iy, phip += rowlen) {
368  *phip = xj;
369  }
370  }
371 
372  // Do y dependence
373  const double* yp = y;
374  double* phip = phi;
375  double* thetap = theta;
376  int* statp = stat;
377  for (int iy = 0; iy < ny; ++iy, yp += sxy) {
378  double yj = *yp + m_y0;
379  double yj2 = yj*yj;
380  for (int ix = 0; ix < mx; ++ix, phip += spt, thetap += spt) {
381  double xj = *phip;
382  double r = std::sqrt(xj*xj + yj2);
383  if (r == 0.0) {
384  *phip = 0.0;
385  }
386  else {
387  *phip = gammalib::atan2d(xj, -yj);
388  }
389  *thetap = 90.0 - 2.0*gammalib::atand(r*m_w[1]);
390  *(statp++) = 0;
391  }
392  }
393 
394  // Return
395  return;
396 }
397 
398 
399 /***********************************************************************//**
400  * @brief Generic spherical-to-pixel projection
401  *
402  * @param[in] nphi Longitude vector length.
403  * @param[in] ntheta Latitude vector length (0=no replication).
404  * @param[in] spt Input vector step.
405  * @param[in] sxy Output vector step.
406  * @param[in] phi Longitude vector of the projected point in native spherical
407  * coordinates [deg].
408  * @param[in] theta Latitude vector of the projected point in native spherical
409  * coordinates [deg].
410  * @param[out] x Vector of projected x coordinates.
411  * @param[out] y Vector of projected y coordinates.
412  * @param[out] stat Status return value for each vector element (always 0)
413  *
414  * Project native spherical coordinates (phi,theta) to pixel (x,y)
415  * coordinates in the plane of projection.
416  *
417  * This method has been adapted from the wcslib function prj.c::stgs2x().
418  * The interface follows very closely that of wcslib. In contrast to the
419  * wcslib routine, however, the method assumes that the projection has been
420  * setup previously (as this will be done by the constructor).
421  ***************************************************************************/
422 void GWcsSTG::prj_s2x(int nphi, int ntheta, int spt, int sxy,
423  const double* phi, const double* theta,
424  double* x, double* y, int* stat) const
425 {
426  // Initialize projection if required
427  if (!m_prjset) {
428  prj_set();
429  }
430 
431  // Set value replication length mphi,mtheta
432  int mphi;
433  int mtheta;
434  if (ntheta > 0) {
435  mphi = nphi;
436  mtheta = ntheta;
437  }
438  else {
439  mphi = 1;
440  mtheta = 1;
441  ntheta = nphi;
442  }
443 
444  // Initialise status code and statistics
445  int status = 0;
446  int n_invalid = 0;
447 
448  // Do phi dependence
449  const double* phip = phi;
450  int rowoff = 0;
451  int rowlen = nphi * sxy;
452  for (int iphi = 0; iphi < nphi; ++iphi, rowoff += sxy, phip += spt) {
453  double sinphi;
454  double cosphi;
455  gammalib::sincosd(*phip, &sinphi, &cosphi);
456  double* xp = x + rowoff;
457  double* yp = y + rowoff;
458  for (int itheta = 0; itheta < mtheta; ++itheta) {
459  *xp = sinphi;
460  *yp = cosphi;
461  xp += rowlen;
462  yp += rowlen;
463  }
464  }
465 
466  // Do theta dependence
467  const double* thetap = theta;
468  double* xp = x;
469  double* yp = y;
470  int* statp = stat;
471  for (int itheta = 0; itheta < ntheta; ++itheta, thetap += spt) {
472 
473  // Compute sine of Theta
474  double s = 1.0 + gammalib::sind(*thetap);
475 
476  // If 1+sine is zero we cannot proceed. Set all pixels to (0,0) and flag
477  // them as being bad
478  if (s == 0.0) {
479  for (int iphi = 0; iphi < mphi; ++iphi, xp += sxy, yp += sxy) {
480  *xp = 0.0;
481  *yp = 0.0;
482  *(statp++) = 1;
483  n_invalid++;
484  }
485  status = 4;
486  }
487 
488  // ... otherwise proceed, but if strict bound checking has been requested
489  // then flag all pixels as bad that have negative sin(theta)
490  else {
491  double r = m_w[0] * gammalib::cosd(*thetap)/s;
492  for (int iphi = 0; iphi < mphi; ++iphi, xp += sxy, yp += sxy) {
493  *xp = r*(*xp) - m_x0;
494  *yp = -r*(*yp) - m_y0;
495  *(statp++) = 0;
496  }
497  }
498  }
499 
500  // Check status code
501  gammalib::check_prj_s2x_status(G_PRJ_S2X, status, n_invalid);
502 
503  // Return
504  return;
505 }
void init_members(void)
Initialise class members.
Definition: GWcs.cpp:962
Interface definition for the WCS registry class.
void prj_off(const double &phi0, const double &theta0) const
Compute fiducial offset to force (x,y) = (0,0) at (phi0,theta0)
Definition: GWcs.cpp:3232
void prj_x2s(int nx, int ny, int sxy, int spt, const double *x, const double *y, double *phi, double *theta, int *stat) const
Pixel-to-spherical deprojection.
Definition: GWcsSTG.cpp:338
void prj_s2x(int nphi, int ntheta, int spt, int sxy, const double *phi, const double *theta, double *x, double *y, int *stat) const
Generic spherical-to-pixel projection.
Definition: GWcsSTG.cpp:422
const GWcsSTG g_wcs_stg_seed
Definition: GWcsSTG.cpp:51
void free_members(void)
Delete class members.
Definition: GWcs.cpp:1066
void init_members(void)
Initialise class members.
Definition: GWcsSTG.cpp:245
void prj_set(void) const
Setup of projection.
Definition: GWcsSTG.cpp:291
World Coordinate Projection registry class interface definition.
double atand(const double &value)
Compute arc tangens in degrees.
Definition: GMath.cpp:282
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1358
void wcs_set(void) const
Setup of World Coordinate System.
Definition: GWcs.cpp:1262
GWcsSTG(void)
Void constructor.
Definition: GWcsSTG.cpp:64
void sincosd(const double &angle, double *s, double *c)
Compute sine and cosine of angle in degrees.
Definition: GMath.cpp:342
virtual GWcs & operator=(const GWcs &wcs)
Assignment operator.
Definition: GWcs.cpp:180
double m_x0
Fiducial x offset.
Definition: GWcs.hpp:215
GChatter
Definition: GTypemaps.hpp:33
virtual std::string print(const GChatter &chatter=NORMAL) const
Print projection information.
Definition: GWcsSTG.cpp:215
std::string wcs_print(const GChatter &chatter=NORMAL) const
Print WCS information.
Definition: GWcs.cpp:1642
virtual ~GWcsSTG(void)
Destructor.
Definition: GWcsSTG.cpp:124
Stereographic (STG) projection class definition.
void free_members(void)
Delete class members.
std::vector< double > m_w
Intermediate values.
Definition: GWcs.hpp:217
Abstract world coordinate system base class.
Definition: GWcs.hpp:51
double m_y0
Fiducial y offset.
Definition: GWcs.hpp:216
double atan2d(const double &y, const double &x)
Compute arc tangens in degrees.
Definition: GMath.cpp:308
void check_prj_s2x_status(const std::string &origin, const int &status, const int &number)
Checks status of GWcs::prj_s2x method.
Definition: GException.cpp:438
double cosd(const double &angle)
Compute cosine of angle in degrees.
Definition: GMath.cpp:134
Exception handler interface definition.
bool m_prjset
Projection is set.
Definition: GWcs.hpp:211
void init_members(void)
Initialise class members.
virtual void clear(void)
Clear projection.
Definition: GWcsSTG.cpp:181
GWcsSTG & operator=(const GWcsSTG &wcs)
Assignment operator.
Definition: GWcsSTG.cpp:146
void free_members(void)
Delete class members.
Definition: GWcsSTG.cpp:267
const double rad2deg
Definition: GMath.hpp:44
#define G_PRJ_S2X
Definition: GWcsSTG.cpp:37
virtual GWcsSTG * clone(void) const
Clone projection.
Definition: GWcsSTG.cpp:203
double sind(const double &angle)
Compute sine of angle in degrees.
Definition: GMath.cpp:163
Stereographic (STG) projection class definition.
Definition: GWcsSTG.hpp:43
double m_r0
Radius of the generating sphere.
Definition: GWcs.hpp:212
void copy_members(const GWcsSTG &wcs)
Copy class members.
Definition: GWcsSTG.cpp:257
Mathematical function definitions.