GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSkyPixel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GSkyPixel.cpp - Sky map pixel class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-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 GSkyPixel.hpp
23  * @brief Sky map pixel class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GSkyPixel.hpp"
32 #include "GTools.hpp"
33 #include "GException.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_INT "GSkyPixel::operator int()"
37 #define G_DOUBLE "GSkyPixel::operator double()"
38 
39 /* __ Macros _____________________________________________________________ */
40 
41 /* __ Coding definitions _________________________________________________ */
42 
43 /* __ Debug definitions __________________________________________________ */
44 
45 /* __ Prototype __________________________________________________________ */
46 
47 
48 /*==========================================================================
49  = =
50  = Constructors/destructors =
51  = =
52  ==========================================================================*/
53 
54 /***********************************************************************//**
55  * @brief Void constructor
56  ***************************************************************************/
58 {
59  // Initialise members
60  init_members();
61 
62  // Return
63  return;
64 }
65 
66 
67 /***********************************************************************//**
68  * @brief 1D pixel constructor (integer version)
69  *
70  * @param[in] index Pixel index.
71  ***************************************************************************/
72 GSkyPixel::GSkyPixel(const int& index)
73 {
74  // Set members
75  m_size = 1;
76  m_x = double(index);
77  m_y = 0.0;
78 
79  // Return
80  return;
81 }
82 
83 
84 /***********************************************************************//**
85  * @brief 1D pixel constructor (double precision version)
86  *
87  * @param[in] index Pixel index.
88  ***************************************************************************/
89 GSkyPixel::GSkyPixel(const double& index)
90 {
91  // Set members
92  m_size = 1;
93  m_x = index;
94  m_y = 0.0;
95 
96  // Return
97  return;
98 }
99 
100 
101 /***********************************************************************//**
102  * @brief 2D pixel constructor (integer version)
103  *
104  * @param[in] x Pixel X index.
105  * @param[in] y Pixel Y index.
106  ***************************************************************************/
107 GSkyPixel::GSkyPixel(const int& x, const int& y)
108 {
109  // Set members
110  m_size = 2;
111  m_x = double(x);
112  m_y = double(y);
113 
114  // Return
115  return;
116 }
117 
118 
119 /***********************************************************************//**
120  * @brief 2D pixel constructor (double precision version)
121  *
122  * @param[in] x Pixel X index.
123  * @param[in] y Pixel Y index.
124  ***************************************************************************/
125 GSkyPixel::GSkyPixel(const double& x, const double& y)
126 {
127  // Set members
128  m_size = 2;
129  m_x = x;
130  m_y = y;
131 
132  // Return
133  return;
134 }
135 
136 
137 /***********************************************************************//**
138  * @brief Copy constructor
139  *
140  * @param[in] pixel Sky pixel.
141  ***************************************************************************/
143 {
144  // Initialise members
145  init_members();
146 
147  // Copy members
148  copy_members(pixel);
149 
150  // Return
151  return;
152 }
153 
154 
155 /***********************************************************************//**
156  * @brief Destructor
157  ***************************************************************************/
159 {
160  // Free members
161  free_members();
162 
163  // Return
164  return;
165 }
166 
167 
168 /*==========================================================================
169  = =
170  = Operators =
171  = =
172  ==========================================================================*/
173 
174 /***********************************************************************//**
175  * @brief Assignment operator
176  *
177  * @param[in] pixel Sky map pixel.
178  * @return Sky map pixel.
179  ***************************************************************************/
181 {
182  // Execute only if object is not identical
183  if (this != &pixel) {
184 
185  // Free members
186  free_members();
187 
188  // Initialise private members for clean destruction
189  init_members();
190 
191  // Copy members
192  copy_members(pixel);
193 
194  } // endif: object was not identical
195 
196  // Return this object
197  return *this;
198 }
199 
200 
201 /***********************************************************************//**
202  * @brief To integer type conversion
203  *
204  * @return Pixel index.
205  *
206  * Converts the sky map pixel into an integer value.
207  ***************************************************************************/
208 GSkyPixel::operator int() const
209 {
210  // Throw an exception if pixel is not 1D
211  if (!is_1D()) {
212  std::string msg = "Sky map pixel is not 1-dimensional.\n"
213  "Conversion from GSkyPixel to int is only allowed"
214  " for 1-dimensional sky map pixels.";
215  throw GException::invalid_value(G_INT, msg);
216  }
217 
218  // Round pixel to integer value
219  int value = int(m_x + 0.5);
220 
221  // Return value
222  return value;
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief To double type conversion
228  *
229  * @return Pixel index.
230  *
231  * Converts the sky map pixel into a double precision value.
232  ***************************************************************************/
233 GSkyPixel::operator double() const
234 {
235  // Throw an exception if pixel is not 1D
236  if (!is_1D()) {
237  std::string msg = "Sky map pixel is not 1-dimensional.\n"
238  "Conversion from GSkyPixel to double is only allowed"
239  " for 1-dimensional sky map pixels.";
241  }
242 
243  // Return value
244  return m_x;
245 }
246 
247 
248 /*==========================================================================
249  = =
250  = Public methods =
251  = =
252  ==========================================================================*/
253 
254 /***********************************************************************//**
255  * @brief Clear instance
256  *
257  * Set sky map pixel to a clean initial state.
258  ***************************************************************************/
260 {
261  // Free members
262  free_members();
263 
264  // Initialise private members
265  init_members();
266 
267  // Return
268  return;
269 }
270 
271 
272 /***********************************************************************//**
273  * @brief Clone sky map pixel
274  *
275  * @return Pointer to deep copy of sky map pixel.
276  *
277  * Returns a pointer to a deep copy of a sky map pixel.
278  ***************************************************************************/
280 {
281  // Clone pixel
282  return new GSkyPixel(*this);
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Print pixel
288  *
289  * @param[in] chatter Chattiness (defaults to NORMAL).
290  * @return String containing pixel information.
291  ***************************************************************************/
292 std::string GSkyPixel::print(const GChatter& chatter) const
293 {
294  // Initialise result string
295  std::string result;
296 
297  // Continue only if chatter is not silent
298  if (chatter != SILENT) {
299 
300  // Append pixel
301  if (is_1D()) {
302  result.append(gammalib::str(x()));
303  }
304  else if (is_2D()) {
305  result.append("(");
306  result.append(gammalib::str(x()));
307  result.append(",");
308  result.append(gammalib::str(y()));
309  result.append(")");
310  }
311 
312  } // endif: chatter was not silent
313 
314  // Return result
315  return result;
316 }
317 
318 
319 /*==========================================================================
320  = =
321  = Private methods =
322  = =
323  ==========================================================================*/
324 
325 /***********************************************************************//**
326  * @brief Initialise class members
327  ***************************************************************************/
329 {
330  // Initialise members
331  m_size = 0;
332  m_x = 0.0;
333  m_y = 0.0;
334 
335  // Return
336  return;
337 }
338 
339 
340 /***********************************************************************//**
341  * @brief Copy class members
342  *
343  * @param[in] pixel Sky pixel.
344  ***************************************************************************/
346 {
347  // Copy attributes
348  m_size = pixel.m_size;
349  m_x = pixel.m_x;
350  m_y = pixel.m_y;
351 
352  // Return
353  return;
354 }
355 
356 
357 /***********************************************************************//**
358  * @brief Delete class members
359  ***************************************************************************/
361 {
362  // Return
363  return;
364 }
GSkyPixel & operator=(const GSkyPixel &pixel)
Assignment operator.
Definition: GSkyPixel.cpp:180
std::string print(const GChatter &chatter=NORMAL) const
Print pixel.
Definition: GSkyPixel.cpp:292
void free_members(void)
Delete class members.
Definition: GSkyPixel.cpp:360
bool is_1D(void) const
Check if pixel is 1D.
Definition: GSkyPixel.hpp:157
int m_size
Pixel dimension (0=undefined, 1=1D, 2=2D)
Definition: GSkyPixel.hpp:118
GSkyPixel * clone(void) const
Clone sky map pixel.
Definition: GSkyPixel.cpp:279
double m_y
Y index.
Definition: GSkyPixel.hpp:120
void init_members(void)
Initialise class members.
Definition: GSkyPixel.cpp:328
virtual ~GSkyPixel(void)
Destructor.
Definition: GSkyPixel.cpp:158
void clear(void)
Clear instance.
Definition: GSkyPixel.cpp:259
Gammalib tools definition.
double m_x
X index.
Definition: GSkyPixel.hpp:119
const double & index(void) const
Return sky map pixel index.
Definition: GSkyPixel.hpp:256
Sky map pixel class.
Definition: GSkyPixel.hpp:74
void copy_members(const GSkyPixel &pixel)
Copy class members.
Definition: GSkyPixel.cpp:345
GChatter
Definition: GTypemaps.hpp:33
const double & x(void) const
Return x value of sky map pixel.
Definition: GSkyPixel.hpp:271
#define G_DOUBLE
Definition: GSkyPixel.cpp:37
Exception handler interface definition.
GSkyPixel(void)
Void constructor.
Definition: GSkyPixel.cpp:57
bool is_2D(void) const
Check if pixel is 2D.
Definition: GSkyPixel.hpp:169
Sky map pixel class definition.
#define G_INT
Definition: GSkyPixel.cpp:36
const double & y(void) const
Return y value of sky pixel.
Definition: GSkyPixel.hpp:286
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489