A Discrete-Event Network Simulator
API
angles.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
20 #include "angles.h"
21 
22 #include <ns3/log.h>
23 
24 #include <cmath>
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("Angles");
30 
31 bool Angles::m_printDeg = false;
32 
34 const double DEG_TO_RAD = M_PI / 180.0;
36 const double RAD_TO_DEG = 180.0 / M_PI;
37 
38 double
39 DegreesToRadians(double degrees)
40 {
41  return degrees * DEG_TO_RAD;
42 }
43 
44 double
45 RadiansToDegrees(double radians)
46 {
47  return radians * RAD_TO_DEG;
48 }
49 
50 std::vector<double>
51 DegreesToRadians(const std::vector<double>& degrees)
52 {
53  std::vector<double> radians;
54  radians.reserve(degrees.size());
55  for (size_t i = 0; i < degrees.size(); i++)
56  {
57  radians.push_back(DegreesToRadians(degrees[i]));
58  }
59  return radians;
60 }
61 
62 std::vector<double>
63 RadiansToDegrees(const std::vector<double>& radians)
64 {
65  std::vector<double> degrees;
66  degrees.reserve(radians.size());
67  for (size_t i = 0; i < radians.size(); i++)
68  {
69  degrees.push_back(RadiansToDegrees(radians[i]));
70  }
71  return degrees;
72 }
73 
74 double
75 WrapTo360(double a)
76 {
77  a = fmod(a, 360);
78  if (a < 0)
79  {
80  a += 360;
81  }
82 
83  NS_ASSERT_MSG(0 <= a && a < 360, "Invalid wrap, a=" << a);
84  return a;
85 }
86 
87 double
88 WrapTo180(double a)
89 {
90  a = fmod(a + 180, 360);
91  if (a < 0)
92  {
93  a += 360;
94  }
95  a -= 180;
96 
97  NS_ASSERT_MSG(-180 <= a && a < 180, "Invalid wrap, a=" << a);
98  return a;
99 }
100 
101 double
102 WrapTo2Pi(double a)
103 {
104  a = fmod(a, 2 * M_PI);
105  if (a < 0)
106  {
107  a += 2 * M_PI;
108  }
109 
110  NS_ASSERT_MSG(0 <= a && a < 2 * M_PI, "Invalid wrap, a=" << a);
111  return a;
112 }
113 
114 double
115 WrapToPi(double a)
116 {
117  a = fmod(a + M_PI, 2 * M_PI);
118  if (a < 0)
119  {
120  a += 2 * M_PI;
121  }
122  a -= M_PI;
123 
124  NS_ASSERT_MSG(-M_PI <= a && a < M_PI, "Invalid wrap, a=" << a);
125  return a;
126 }
127 
128 std::ostream&
129 operator<<(std::ostream& os, const Angles& a)
130 {
131  double azim;
132  double incl;
133  std::string unit;
134 
135  if (Angles::m_printDeg)
136  {
137  azim = RadiansToDegrees(a.m_azimuth);
139  unit = "deg";
140  }
141  else
142  {
143  azim = a.m_azimuth;
144  incl = a.m_inclination;
145  unit = "rad";
146  }
147 
148  os << "(" << azim << ", " << incl << ") " << unit;
149  return os;
150 }
151 
152 std::istream&
153 operator>>(std::istream& is, Angles& a)
154 {
155  char c;
156  is >> a.m_azimuth >> c >> a.m_inclination;
157  if (c != ':')
158  {
159  is.setstate(std::ios_base::failbit);
160  }
161  return is;
162 }
163 
165  : Angles(NAN, NAN)
166 {
167 }
168 
169 Angles::Angles(double azimuth, double inclination)
170  : m_azimuth(azimuth),
171  m_inclination(inclination)
172 {
173  NormalizeAngles();
174 }
175 
176 Angles::Angles(Vector v)
177  : m_azimuth(std::atan2(v.y, v.x)),
178  m_inclination(std::acos(v.z / v.GetLength()))
179 {
180  // azimuth and inclination angles for zero-length vectors are not defined
181  if (v.x == 0.0 && v.y == 0.0 && v.z == 0.0)
182  {
183  m_azimuth = NAN;
184  m_inclination = NAN;
185  }
186 
187  NormalizeAngles();
188 }
189 
190 Angles::Angles(Vector v, Vector o)
191  : Angles(v - o)
192 {
193 }
194 
195 void
196 Angles::SetAzimuth(double azimuth)
197 {
198  m_azimuth = azimuth;
199  NormalizeAngles();
200 }
201 
202 void
203 Angles::SetInclination(double inclination)
204 {
205  m_inclination = inclination;
206  NormalizeAngles();
207 }
208 
209 double
211 {
212  return m_azimuth;
213 }
214 
215 double
217 {
218  return m_inclination;
219 }
220 
221 void
223 {
224  CheckIfValid();
225 
226  // Normalize azimuth angle
227  if (std::isnan(m_azimuth))
228  {
229  return;
230  }
231 
233 }
234 
235 void
237 {
238  if (std::isfinite(m_inclination) || std::isfinite(m_azimuth))
239  {
240  NS_ASSERT_MSG(0.0 <= m_inclination && m_inclination <= M_PI,
241  "m_inclination=" << m_inclination << " not valid, should be in [0, pi] rad");
242  }
243  else
244  {
245  // infinite or nan inclination or azimuth angle
246  NS_LOG_WARN("Undefined angle: " << *this);
247  }
248 }
249 
250 } // namespace ns3
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:118
void NormalizeAngles()
Normalize the angle azimuth angle range between in [-M_PI, M_PI) while checking if the angle is valid...
Definition: angles.cc:222
double m_inclination
the inclination angle in radians
Definition: angles.h:224
double GetInclination() const
Getter for inclination angle.
Definition: angles.cc:216
static bool m_printDeg
flag for printing in radians or degrees units
Definition: angles.h:197
void SetAzimuth(double azimuth)
Setter for azimuth angle.
Definition: angles.cc:196
Angles()
Default constructor is disabled.
Definition: angles.cc:164
void SetInclination(double inclination)
Setter for inclination angle.
Definition: angles.cc:203
double m_azimuth
the azimuth angle in radians
Definition: angles.h:223
double GetAzimuth() const
Getter for azimuth angle.
Definition: angles.cc:210
void CheckIfValid() const
Check if Angle is valid or not Warns the user if the Angle is undefined (non-finite azimuth or inclin...
Definition: angles.cc:236
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double WrapToPi(double a)
Wrap angle in [-M_PI, M_PI)
Definition: angles.cc:115
double WrapTo180(double a)
Wrap angle in [-180, 180)
Definition: angles.cc:88
const double DEG_TO_RAD
Degrees to Radians conversion constant.
Definition: angles.cc:34
double WrapTo360(double a)
Wrap angle in [0, 360)
Definition: angles.cc:75
double DegreesToRadians(double degrees)
converts degrees to radians
Definition: angles.cc:39
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
double WrapTo2Pi(double a)
Wrap angle in [0, 2*M_PI)
Definition: angles.cc:102
double RadiansToDegrees(double radians)
converts radians to degrees
Definition: angles.cc:45
const double RAD_TO_DEG
Radians to Degrees conversion constant.
Definition: angles.cc:36