1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011, 2012 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #ifndef ANGLES_H
22 #define ANGLES_H
23 
24 
25 #include <ns3/vector.h>
26 #include <vector>
27 
28 namespace ns3 {
29 
30 
31 /**
32  * \brief converts degrees to radians
33  *
34  * \param degrees the angle in degrees
35  * \return the angle in radians
36  */
37 double DegreesToRadians (double degrees);
38 
39 /**
40  * \brief converts degrees to radians
41  *
42  * \param degrees the angles in degrees
43  * \return the angles in radians
44  */
45 std::vector<double> DegreesToRadians (const std::vector<double> &degrees);
46 
47 /**
48  * \brief converts radians to degrees
49  *
50  * \param radians the angle in radians
51  * \return the angle in degrees
52  */
53 double RadiansToDegrees (double radians);
54 
55 /**
56  * \brief converts radians to degrees
57  *
58  * \param radians the angles in radians
59  * \return the angles in degrees
60  */
61 std::vector<double> RadiansToDegrees (const std::vector<double> &radians);
62 
63 /**
64  * \brief Wrap angle in [0, 360)
65  *
66  * \param a the angle in degrees
67  * \return the wrapped angle in degrees
68  */
69 double WrapTo360 (double a);
70 
71 /**
72  * \brief Wrap angle in [-180, 180)
73  *
74  * \param a the angle in degrees
75  * \return the wrapped angle in degrees
76  */
77 double WrapTo180 (double a);
78 
79 /**
80  * \brief Wrap angle in [0, 2*M_PI)
81  *
82  * \param a the angle in radians
83  * \return the wrapped angle in radians
84  */
85 double WrapTo2Pi (double a);
86 
87 /**
88  * \brief Wrap angle in [-M_PI, M_PI)
89  *
90  * \param a the angle in radians
91  * \return the wrapped angle in radians
92  */
93 double WrapToPi (double a);
94 
95 /**
96  *
97  * Class holding the azimuth and inclination angles of spherical coordinates.
98  * The notation is the one used in  "Antenna Theory - Analysis
99  * and Design", C.A. Balanis, Wiley, 2nd Ed., section 2.2 "Radiation pattern".
100  * This notation corresponds to the standard spherical coordinates, with azimuth
101  * measured counterclockwise in the x-y plane off the x-axis, and
102  * inclination measured off the z-axis.
103  * Azimuth is consistently normalized to be in [-M_PI, M_PI).
104  *
105  *          ^
106  *        z |
107  *          |_ inclination
108  *          | \
109  *          | /|
110  *          |/ |   y
111  *          +-------->
112  *         /  \|
113  *        /___/
114  *     x /  azimuth
115  *      |/
116  *
117  */
118 class Angles
119 {
120 public:
121   /**
122    * This constructor allows to specify azimuth and inclination.
123    * Inclination must be in [0, M_PI], while azimuth is
124    * automatically notmalized in [-M_PI, M_PI)
125    *
126    * \param azimuth the azimuth angle in radians
127    * \param inclination the inclination angle in radians
128    */
129   Angles (double azimuth, double inclination);
130 
131   /**
132    * This constructor will initialize azimuth and inclination by converting the
133    * given 3D vector from cartesian coordinates to spherical coordinates
134    * Note: azimuth and inclination angles for a zero-length vector are not defined
135    * and are thus initialized to NAN
136    *
137    * \param v the 3D vector in cartesian coordinates
138    *
139    */
140   Angles (Vector v);
141 
142   /**
143    * This constructor initializes an Angles instance with the angles
144    * of the spherical coordinates of point v respect to point o
145    *
146    * \param v the point (in cartesian coordinates) for which the angles are determined
147    * \param o the origin (in cartesian coordinates) of the spherical coordinate system
148    *
149    */
150   Angles (Vector v, Vector o);
151 
152   /**
153    * Setter for azimuth angle
154    *
155    * \param azimuth angle in radians
156    */
157   void SetAzimuth (double azimuth);
158 
159   /**
160    * Setter for inclination angle
161    *
162    * \param inclination angle in radians. Must be in [0, M_PI]
163    */
164   void SetInclination (double inclination);
165 
166   /**
167    * Getter for azimuth angle
168    *
169    * \return azimuth angle in radians
170    */
171   double GetAzimuth (void) const;
172 
173   /**
174    * Getter for inclination angle
175    *
176    * \return inclination angle in radians
177    */
178   double GetInclination (void) const;
179 
180   // friend methods
181   friend std::ostream& operator<< (std::ostream& os, const Angles& a);
182   friend std::istream& operator>> (std::istream& is, Angles& a);
183 
184   static bool m_printDeg; //!< flag for printing in radians or degrees units
185 
186 private:
187   /**
188    * Default constructor is disabled
189    */
190   Angles ();
191 
192   /**
193     * Normalize the angle azimuth angle range between in [-M_PI, M_PI)
194     * while checking if the angle is valid, i.e., finite and within
195     * the bounds.
196     *
197     * Note: while an arbitrary value for the azimuth angle is valid
198     * and can be wrapped in [-M_PI, M_PI), an inclination angle outside
199     * the [0, M_PI] range can be ambiguos and is thus not valid.
200     */
201   void NormalizeAngles (void);
202 
203   /**
204     * Check if Angle is valid or not
205     * Warns the user if the Angle is undefined (non-finite azimuth or inclination),
206     * throws an assert if the inclination angle is invalid (not in [0, M_PI])
207     */
208   void CheckIfValid (void) const;
209 
210 
211   double m_azimuth; //!< the azimuth angle in radians
212   double m_inclination; //!< the inclination angle in radians
213 };
214 
215 
216 } // namespace ns3
217 
218 #endif // ANGLES_H
219