GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSkyDirs.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GSkyDirs.cpp - Sky directions container class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2020 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 GSkyDirs.cpp
23  * @brief Sky directions container class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GException.hpp"
33 #include "GFilename.hpp"
34 #include "GSkyDirs.hpp"
35 /*
36 #include "GEbounds.hpp"
37 #include "GFits.hpp"
38 #include "GFitsTable.hpp"
39 #include "GFitsBinTable.hpp"
40 #include "GFitsTableCol.hpp"
41 #include "GFitsTableDoubleCol.hpp"
42 */
43 
44 /* __ Method name definitions ____________________________________________ */
45 #define G_AT "GSkyDirs::at(int&)"
46 #define G_INSERT "GSkyDirs::insert(int&, GSkyDir&)"
47 #define G_REMOVE "GSkyDirs::remove(int&)"
48 
49 /* __ Macros _____________________________________________________________ */
50 
51 /* __ Coding definitions _________________________________________________ */
52 
53 /* __ Debug definitions __________________________________________________ */
54 
55 
56 /*==========================================================================
57  = =
58  = Constructors/destructors =
59  = =
60  ==========================================================================*/
61 
62 /***********************************************************************//**
63  * @brief Void constructor
64  *
65  * Constructs empty sky directions container.
66  ***************************************************************************/
68 {
69  // Initialise members
70  init_members();
71 
72  // Return
73  return;
74 }
75 
76 
77 /***********************************************************************//**
78  * @brief Single sky direction constructor
79  *
80  * @param[in] dir Sky directions.
81  *
82  * Constructs sky directions container from a single sky direction.
83  ***************************************************************************/
85 {
86  // Initialise members
87  init_members();
88 
89  // Append sky direction
90  append(dir);
91 
92  // Return
93  return;
94 }
95 
96 
97 /***********************************************************************//**
98  * @brief Copy constructor
99  *
100  * @param[in] dirs Sky directions.
101  *
102  * Construct sky directions container by copying from another sky directions
103  * container.
104  ***************************************************************************/
106 {
107  // Initialise members
108  init_members();
109 
110  // Copy members
111  copy_members(dirs);
112 
113  // Return
114  return;
115 }
116 
117 
118 /***********************************************************************//**
119  * @brief Destructor
120  ***************************************************************************/
122 {
123  // Free members
124  free_members();
125 
126  // Return
127  return;
128 }
129 
130 
131 /*==========================================================================
132  = =
133  = Operators =
134  = =
135  ==========================================================================*/
136 
137 /***********************************************************************//**
138  * @brief Assignment operator
139  *
140  * @param[in] dirs Sky direction container.
141  * @return Sky direction container.
142  ***************************************************************************/
144 {
145  // Execute only if object is not identical
146  if (this != &dirs) {
147 
148  // Free members
149  free_members();
150 
151  // Initialise members
152  init_members();
153 
154  // Copy members
155  copy_members(dirs);
156 
157  } // endif: object was not identical
158 
159  // Return this object
160  return *this;
161 }
162 
163 
164 /*==========================================================================
165  = =
166  = Public methods =
167  = =
168  ==========================================================================*/
169 
170 /***********************************************************************//**
171  * @brief Clear sky directions container
172  *
173  * Removes all sky directions from the container.
174  ***************************************************************************/
175 void GSkyDirs::clear(void)
176 {
177  // Free members
178  free_members();
179 
180  // Initialise members
181  init_members();
182 
183  // Return
184  return;
185 }
186 
187 
188 /***********************************************************************//**
189  * @brief Clone sky directions container
190  *
191  * @return Pointer to deep copy of sky directions container
192  *
193  * Makes a deep copy of the sky directions container instance.
194  ***************************************************************************/
196 {
197  return new GSkyDirs(*this);
198 }
199 
200 
201 /***********************************************************************//**
202  * @brief Return reference to sky direction (const version)
203  *
204  * @param[in] index Sky direction index [0,...,size()-1].
205  *
206  * @exception GException::out_of_range
207  * Sky direction index is out of range.
208  *
209  * Returns a reference to the sky direction with the specified @p index.
210  ***************************************************************************/
211 const GSkyDir& GSkyDirs::at(const int& index) const
212 {
213  // Raise exception if index is out of range
214  if (index < 0 || index >= size()) {
215  throw GException::out_of_range(G_AT, "Sky direction", index, size());
216  }
217 
218  // Return reference
219  return m_dirs[index];
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Append sky direction to container
225  *
226  * @param[in] dir Sky direction.
227  * @return Reference to appended sky direction.
228  *
229  * Appends sky direction to the container by making a deep copy of the sky
230  * direction.
231  ***************************************************************************/
233 {
234  // Append sky direction to list
235  m_dirs.push_back(dir);
236 
237  // Return reference
238  return m_dirs[size()-1];
239 }
240 
241 
242 /***********************************************************************//**
243  * @brief Insert sky direction into container
244  *
245  * @param[in] index Sky direction index (0,...,size()-1).
246  * @param[in] dir Sky direction.
247  *
248  * @exception GException::out_of_range
249  * Sky direction index is out of range.
250  *
251  * Inserts a sky direction @p dir into the container before the sky
252  * direction with the specified @p index.
253  ***************************************************************************/
254 GSkyDir& GSkyDirs::insert(const int& index, const GSkyDir& dir)
255 {
256  // Compile option: raise exception if index is out of range
257  #if defined(G_RANGE_CHECK)
258  if (is_empty()) {
259  if (index > 0) {
260  throw GException::out_of_range(G_INSERT, "Sky direction", index,
261  size());
262  }
263  }
264  else {
265  if (index < 0 || index >= size()) {
266  throw GException::out_of_range(G_INSERT, "Sky direction", index,
267  size());
268  }
269  }
270  #endif
271 
272  // Inserts sky direction
273  m_dirs.insert(m_dirs.begin()+index, dir);
274 
275  // Return reference
276  return m_dirs[index];
277 }
278 
279 
280 /***********************************************************************//**
281  * @brief Remove sky direction from container
282  *
283  * @param[in] index Sky direction index (0,...,size()-1).
284  *
285  * @exception GException::out_of_range
286  * Sky direction index is out of range.
287  *
288  * Remove sky direction of specified @p index from container.
289  ***************************************************************************/
290 void GSkyDirs::remove(const int& index)
291 {
292  // Compile option: raise exception if index is out of range
293  #if defined(G_RANGE_CHECK)
294  if (index < 0 || index >= size()) {
295  throw GException::out_of_range(G_REMOVE, "Sky direction", index, size());
296  }
297  #endif
298 
299  // Erase sky direction from container
300  m_dirs.erase(m_dirs.begin() + index);
301 
302  // Return
303  return;
304 }
305 
306 
307 /***********************************************************************//**
308  * @brief Append sky directions container
309  *
310  * @param[in] dirs Sky directions container.
311  *
312  * Append sky directions container to the container.
313  ***************************************************************************/
314 void GSkyDirs::extend(const GSkyDirs& dirs)
315 {
316  // Do nothing if sky directions container is empty
317  if (!dirs.is_empty()) {
318 
319  // Get size. Note that we extract the size first to avoid an
320  // endless loop that arises when a container is appended to
321  // itself.
322  int num = dirs.size();
323 
324  // Reserve enough space
325  reserve(size() + num);
326 
327  // Loop over all elements and append them to container
328  for (int i = 0; i < num; ++i) {
329  m_dirs.push_back(dirs[i]);
330  }
331 
332  } // endif: sky directions container was not empty
333 
334  // Return
335  return;
336 }
337 
338 
339 /***********************************************************************//**
340  * @brief Print sky directions container information
341  *
342  * @param[in] chatter Chattiness.
343  * @return String containing sky directions container information.
344  ***************************************************************************/
345 std::string GSkyDirs::print(const GChatter& chatter) const
346 {
347  // Initialise result string
348  std::string result;
349 
350  // Continue only if chatter is not silent
351  if (chatter != SILENT) {
352 
353  // Append header
354  result.append("=== GSkyDirs ===");
355 
356  // Append sky direction container information
357  result.append("\n"+gammalib::parformat("Number of directions"));
358  result.append(gammalib::str(size()));
359 
360  // EXPLICIT: Append sky directions
361  if (chatter >= EXPLICIT) {
362  for (int i = 0; i < size(); ++i) {
363  result.append("\n");
364  result.append(gammalib::parformat("Direction "+gammalib::str(i)));
365  result.append(m_dirs[i].print(chatter));
366  }
367  }
368 
369  } // endif: chatter was not silent
370 
371  // Return result
372  return result;
373 }
374 
375 
376 /*==========================================================================
377  = =
378  = Private methods =
379  = =
380  ==========================================================================*/
381 
382 /***********************************************************************//**
383  * @brief Initialise class members
384  ***************************************************************************/
386 {
387  // Initialise members
388  m_dirs.clear();
389 
390  // Return
391  return;
392 }
393 
394 
395 /***********************************************************************//**
396  * @brief Copy class members
397  *
398  * @param[in] dirs Sky directions container.
399  ***************************************************************************/
401 {
402  // Copy attributes
403  m_dirs = dirs.m_dirs;
404 
405  // Return
406  return;
407 }
408 
409 
410 /***********************************************************************//**
411  * @brief Delete class members
412  ***************************************************************************/
414 {
415  // Return
416  return;
417 }
GSkyDir & at(const int &index)
Return reference to sky direction.
Definition: GSkyDirs.hpp:141
#define G_REMOVE
Definition: GSkyDirs.cpp:47
void free_members(void)
Delete class members.
Definition: GSkyDirs.cpp:413
GSkyDirs & operator=(const GSkyDirs &dirs)
Assignment operator.
Definition: GSkyDirs.cpp:143
GSkyDir & append(const GSkyDir &dir)
Append sky direction to container.
Definition: GSkyDirs.cpp:232
int size(void) const
Return number of sky directions in container.
Definition: GSkyDirs.hpp:156
Sky directions container class definition.
bool is_empty(void) const
Signals if there are no sky directions in container.
Definition: GSkyDirs.hpp:171
Gammalib tools definition.
GSkyDirs(void)
Void constructor.
Definition: GSkyDirs.cpp:67
GSkyDirs * clone(void) const
Clone sky directions container.
Definition: GSkyDirs.cpp:195
#define G_INSERT
Definition: GSkyDirs.cpp:46
#define G_AT
Definition: GSkyDirs.cpp:45
void remove(const int &index)
Remove sky direction from container.
Definition: GSkyDirs.cpp:290
GChatter
Definition: GTypemaps.hpp:33
std::vector< GSkyDir > m_dirs
List of sky directions.
Definition: GSkyDirs.hpp:85
void init_members(void)
Initialise class members.
Definition: GSkyDirs.cpp:385
void clear(void)
Clear sky directions container.
Definition: GSkyDirs.cpp:175
void reserve(const int &num)
Reserves space for sky directions in container.
Definition: GSkyDirs.hpp:185
Exception handler interface definition.
void extend(const GSkyDirs &dirs)
Append sky directions container.
Definition: GSkyDirs.cpp:314
void copy_members(const GSkyDirs &dirs)
Copy class members.
Definition: GSkyDirs.cpp:400
virtual ~GSkyDirs(void)
Destructor.
Definition: GSkyDirs.cpp:121
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
GSkyDir & insert(const int &index, const GSkyDir &dir)
Insert sky direction into container.
Definition: GSkyDirs.cpp:254
Sky direction class.
Definition: GSkyDir.hpp:62
Filename class interface definition.
std::string print(const GChatter &chatter=NORMAL) const
Print sky directions container information.
Definition: GSkyDirs.cpp:345
Sky directions container class.
Definition: GSkyDirs.hpp:49
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489