A Discrete-Event Network Simulator
API
angles.cc
Go to the documentation of this file.
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 
22 #include <ns3/log.h>
23 #include <cmath>
24 #include "angles.h"
25 
26 
27 namespace ns3 {
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 
39 double
40 DegreesToRadians (double degrees)
41 {
42  return degrees * DEG_TO_RAD;
43 }
44 
45 
46 double
47 RadiansToDegrees (double radians)
48 {
49  return radians * RAD_TO_DEG;
50 }
51 
52 
53 std::vector<double>
54 DegreesToRadians (const std::vector<double> &degrees)
55 {
56  std::vector<double> radians;
57  radians.reserve (degrees.size ());
58  for (size_t i = 0; i < degrees.size (); i++)
59  {
60  radians.push_back (DegreesToRadians (degrees[i]));
61  }
62  return radians;
63 
64 }
65 
66 
67 std::vector<double>
68 RadiansToDegrees (const std::vector<double> &radians)
69 {
70  std::vector<double> degrees;
71  degrees.reserve (radians.size ());
72  for (size_t i = 0; i < radians.size (); i++)
73  {
74  degrees.push_back (RadiansToDegrees (radians[i]));
75  }
76  return degrees;
77 }
78 
79 
80 double
81 WrapTo360 (double a)
82 {
83  a = fmod (a, 360);
84  if (a < 0)
85  {
86  a += 360;
87  }
88 
89  NS_ASSERT_MSG (0 <= a && a < 360, "Invalid wrap, a=" << a);
90  return a;
91 }
92 
93 
94 double
95 WrapTo180 (double a)
96 {
97  a = fmod (a + 180, 360);
98  if (a < 0)
99  {
100  a += 360;
101  }
102  a -= 180;
103 
104  NS_ASSERT_MSG (-180 <= a && a < 180, "Invalid wrap, a=" << a);
105  return a;
106 }
107 
108 
109 double
110 WrapTo2Pi (double a)
111 {
112  a = fmod (a, 2 * M_PI);
113  if (a < 0)
114  {
115  a += 2 * M_PI;
116  }
117 
118  NS_ASSERT_MSG (0 <= a && a < 2 * M_PI, "Invalid wrap, a=" << a);
119  return a;
120 }
121 
122 
123 double
124 WrapToPi (double a)
125 {
126  a = fmod (a + M_PI, 2 * M_PI);
127  if (a < 0)
128  {
129  a += 2 * M_PI;
130  }
131  a -= M_PI;
132 
133  NS_ASSERT_MSG (-M_PI <= a && a < M_PI, "Invalid wrap, a=" << a);
134  return a;
135 }
136 
137 
138 std::ostream&
139 operator<< (std::ostream& os, const Angles& a)
140 {
141  double azim, incl;
142  std::string unit;
143 
144  if (a.m_printDeg)
145  {
146  azim = RadiansToDegrees (a.m_azimuth);
147  incl = RadiansToDegrees (a.m_inclination);
148  unit = "deg";
149  }
150  else
151  {
152  azim = a.m_azimuth;
153  incl = a.m_inclination;
154  unit = "rad";
155  }
156 
157  os << "(" << azim << ", " << incl << ") " << unit;
158  return os;
159 }
160 
161 std::istream&
162 operator>> (std::istream& is, Angles& a)
163 {
164  char c;
165  is >> a.m_azimuth >> c >> a.m_inclination;
166  if (c != ':')
167  {
168  is.setstate (std::ios_base::failbit);
169  }
170  return is;
171 }
172 
173 
175  : Angles (NAN, NAN)
176 {}
177 
178 
179 Angles::Angles (double azimuth, double inclination)
180  : m_azimuth (azimuth),
181  m_inclination (inclination)
182 {
183  NormalizeAngles ();
184 }
185 
186 
187 Angles::Angles (Vector v)
188  : m_azimuth (std::atan2 (v.y, v.x)),
189  m_inclination (std::acos (v.z / v.GetLength ()))
190 {
191  // azimuth and inclination angles for zero-length vectors are not defined
192  if (v.x == 0.0 && v.y == 0.0 && v.z == 0.0)
193  {
194  m_azimuth = NAN;
195  m_inclination = NAN;
196  }
197 
198  NormalizeAngles ();
199 }
200 
201 Angles::Angles (Vector v, Vector o)
202  : Angles (v - o)
203 {}
204 
205 
206 
207 void
208 Angles::SetAzimuth (double azimuth)
209 {
210  m_azimuth = azimuth;
211  NormalizeAngles ();
212 }
213 
214 
215 void
216 Angles::SetInclination (double inclination)
217 {
218  m_inclination = inclination;
219  NormalizeAngles ();
220 }
221 
222 
223 double
224 Angles::GetAzimuth (void) const
225 {
226  return m_azimuth;
227 }
228 
229 
230 double
232 {
233  return m_inclination;
234 }
235 
236 
237 void
239 {
240  CheckIfValid ();
241 
242  // Normalize azimuth angle
243  if (std::isnan (m_azimuth))
244  {
245  return;
246  }
247 
249 }
250 
251 
252 void
254 {
255  if (std::isfinite (m_inclination) || std::isfinite (m_azimuth))
256  {
257  NS_ASSERT_MSG (0.0 <= m_inclination && m_inclination <= M_PI,
258  "m_inclination=" << m_inclination << " not valid, should be in [0, pi] rad");
259  }
260  else
261  {
262  // infinite or nan inclination or azimuth angle
263  NS_LOG_WARN ("Undefined angle: " << *this);
264  }
265 
266 }
267 
268 }
269 
Class holding the azimuth and inclination angles of spherical coordinates.
Definition: angles.h:119
double GetInclination(void) const
Getter for inclination angle.
Definition: angles.cc:231
double m_inclination
the inclination angle in radians
Definition: angles.h:226
void NormalizeAngles(void)
Normalize the angle azimuth angle range between in [-M_PI, M_PI) while checking if the angle is valid...
Definition: angles.cc:238
void CheckIfValid(void) const
Check if Angle is valid or not Warns the user if the Angle is undefined (non-finite azimuth or inclin...
Definition: angles.cc:253
double GetAzimuth(void) const
Getter for azimuth angle.
Definition: angles.cc:224
static bool m_printDeg
flag for printing in radians or degrees units
Definition: angles.h:198
void SetAzimuth(double azimuth)
Setter for azimuth angle.
Definition: angles.cc:208
Angles()
Default constructor is disabled.
Definition: angles.cc:174
void SetInclination(double inclination)
Setter for inclination angle.
Definition: angles.cc:216
double m_azimuth
the azimuth angle in radians
Definition: angles.h:225
#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:88
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
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:124
double WrapTo180(double a)
Wrap angle in [-180, 180)
Definition: angles.cc:95
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:81
double DegreesToRadians(double degrees)
converts degrees to radians
Definition: angles.cc:40
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
double WrapTo2Pi(double a)
Wrap angle in [0, 2*M_PI)
Definition: angles.cc:110
double RadiansToDegrees(double radians)
converts radians to degrees
Definition: angles.cc:47
const double RAD_TO_DEG
Radians to Degrees conversion constant.
Definition: angles.cc:36
list x
Random number samples.