Angle.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 1999 by Russell Edwards
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/genutil/Angle.h
10 
11 // redwards 17 Mar 99 -- Time for a definitive C++ suite of
12 // angle and sky coordinate functions
13 
14 #ifndef ANGLE_H
15 #define ANGLE_H
16 
17 #include <string>
18 #include <iostream>
19 #include <limits>
20 #include <math.h>
21 
22 static const double MilliSecin12Hours = 4.32e7;
23 
24 #define ANGLE_STRLEN 128
25 
27 class Angle
28 {
29 
30  friend class AnglePair;
31 
32  protected:
33  double radians; // angle in radians
34  double wrap_point;
35  virtual void wrap(); // make sure angle is +- pi radians
36  void init() {radians=0.0;wrap_point=M_PI;}
37  public:
38 
39  static bool verbose;
40 
41  enum Type { Degrees, Radians, Turns };
42 
43  static Type default_type;
44 
45  Angle (const Angle& a)
46  {init(); radians=a.radians;wrap_point=a.wrap_point;}
47  Angle (const double& rad = 0);
48 
49  virtual ~Angle () {}
50 
51  void setWrapPoint(double wp){wrap_point=wp; wrap();}
52  double getWrapPoint() {return wrap_point;}
53 
54  // WvS - this not only changes the wrap-point but also changes the scale
55  // void makeArctangle() { setWrapPoint(0.5*M_PI); }
56 
57  void setHMS (int hours, int minutes, double seconds);
58  void getHMS (int& hours, int& minutes, double& seconds) const;
59 
60  int setHMS (const char *);
61 
63 
64  char* getHMS (char* str, int places=3) const;
65 
67  std::string getHMS (int places = 3) const;
68 
69  void setDMS (int degrees, int minutes, double seconds);
70  void getDMS (int& degrees, int& minutes, double& seconds) const;
71 
72  int setDMS (const char *);
73 
75 
76  char* getDMS (char* str, int places=3) const;
77 
79  std::string getDMS (int places = 3) const;
80 
81  // ms is given in milliseconds of an hour
82  void setRadMS(long int ms)
83  { radians = double(ms) * M_PI / MilliSecin12Hours; };
84  long getRadMS() const
85  { return long(radians * MilliSecin12Hours / M_PI); };
86 
87  void setDegrees(double deg)
88  { radians = deg * M_PI/180.0; wrap();};
89 
90  double getDegrees() const
91  { return radians * 180.0/M_PI; };
92 
93  void setTurns(double turns)
94  { radians = turns * 2.0*M_PI; wrap();};
95 
96  double getTurns() const
97  { return radians / (2.0*M_PI); };
98 
99  void setRadians(double rad)
100  { radians = rad; wrap(); };
101 
102  double getRadians() const
103  { return radians; };
104 
105  void setDegMS (double deg_mmss);
106  double getDegMS () const;
107 
108  void setHourMS (double hour_mmss);
109  double getHourMS () const;
110 
111  void setTurnMS (double turn_mmss);
112  double getTurnMS () const;
113 
114  Angle & operator= (const Angle & a);
115  Angle & operator= (const double &val);
116 
117  Angle & operator+= (const Angle & a);
118  Angle & operator-= (const Angle & a);
119  Angle & operator+= (const double & d);
120  Angle & operator-= (const double & d);
121  Angle & operator*= (const double & d);
122  Angle & operator/= (const double & d);
123  friend const Angle operator + (const Angle &, const Angle &);
124  friend const Angle operator - (const Angle &, const Angle &);
125  friend const Angle operator + (const Angle &, double);
126  friend const Angle operator - (const Angle &, double);
127  friend const Angle operator / (const Angle &, double);
128  friend const Angle operator * (const Angle &, double);
129  friend const Angle operator - (const Angle &);
130 
131  friend double operator * (const Angle &, const Angle &);
132 
133  friend int operator > (const Angle &, const Angle &);
134  friend int operator < (const Angle &, const Angle &);
135  friend int operator >= (const Angle &, const Angle &);
136  friend int operator <= (const Angle &, const Angle &);
137  friend int operator == (const Angle &, const Angle &);
138  friend int operator != (const Angle &, const Angle &);
139 
140  inline friend double cast_double(const Angle &a) {return a.radians;}
141 };
142 
143 std::ostream& operator << (std::ostream& os, const Angle& angle);
144 std::istream& operator >> (std::istream& is, Angle& angle);
145 
146 namespace std {
147 
148  // specialize numeric_limits for Angle class
149  template<>
150  class numeric_limits<Angle> : public numeric_limits<double> {
151  };
152 }
153 
154 // More Arctangle stuff... NOTE: remember the Angle = operator
155 // DOESN'T set wrap_point, so only use these to e.g. pass directly
156 // to a function
157 //inline Angle Arctangle() {Angle a; a.makeArctangle(); return a;}
158 //inline Angle Arctangle(const Angle&a1) {Angle a(a1); a.makeArctangle(); return a;}
159 //inline Angle Arctangle(double rad) {Angle a(rad); a.makeArctangle(); return a;}
160 
161 
163 
166 {
167  public:
168  Angle angle1, angle2;
169 
170  // both HMS and DMS in one string
171  int setHMSDMS (const char* coordstr);
172 
173  // HMS and DMS in separate strings
174  int setHMSDMS (const char *, const char *);
175  int setHMSDMS (const std::string&, const std::string&);
176  void getHMSDMS (char* s1, char* s2, int places1=3, int places2=2) const;
177  std::string getHMSDMS (int places1 = 3, int places2 = 2, bool space = true) const;
178 
179  void setDegrees (double, double);
180  void getDegrees (double *, double *) const;
181  std::string getDegrees () const;
182  void setRadians (double, double);
183  void getRadians (double *, double *) const;
184  std::string getRadians () const;
185 
186  Angle angularSeparation (const AnglePair& other) const;
187 
188  // set the angles in radians from
189  // az and ze in milliseconds of an hour
190  void setRadMS(long int , long int );
191  void getRadMS(long int*, long int*);
192 
193  AnglePair & operator= (const AnglePair &);
194  AnglePair & operator*= (const double);
195 
196  // default constructor
197  AnglePair (const double = 0.0, const double = 0.0);
198 
199  // copy constructor
200  AnglePair (const AnglePair&);
201 
202  AnglePair (const Angle &, const Angle &);
203  AnglePair (const std::string& astr);
204 
205  friend AnglePair operator * (const AnglePair&, const double);
206 
207  friend int operator == (const AnglePair &, const AnglePair &);
208  friend int operator != (const AnglePair &, const AnglePair &);
209 
210  friend std::ostream& operator<< (std::ostream&, const AnglePair&);
211 };
212 
213 
214 // redwards : trig functions
215 double sin(const Angle&);
216 double cos(const Angle&);
217 double tan(const Angle&);
218 // Unfortunately can't overload atan as only the return type differs
219 // from the standard library function
220 Angle arctan(double);
221 Angle arctan(double y, double x); // returns atan y/x
222 
223 #endif //ANGLE_H
224 
A convenient exception handling class.
Definition: Error.h:54
AnglePair class : useful for sky positions.
Definition: Angle.h:165
Class for dealing with angular info.
Definition: Angle.h:27

Generated using doxygen 1.8.17