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  static constexpr int64_t INT_RANGE = 100000000000;
78  // Divide the input by 360.
79  // Multiply it by INT_RANGE and store into an integer.
80  int64_t b(a / (360.0) * INT_RANGE);
81  // Clamp it between [-INT_RANGE / 2, INT_RANGE / 2)
82  b = b % INT_RANGE;
83  if (b < 0)
84  {
85  b += INT_RANGE;
86  }
87  else if (b >= INT_RANGE)
88  {
89  b -= INT_RANGE;
90  }
91  // Divide by INT_RANGE and multiply by 360.
92  return b * (360.0) / INT_RANGE;
93 }
94 
95 double
96 WrapTo180(double a)
97 {
98  static constexpr int64_t INT_RANGE = 100000000000;
99  // Divide the input by 360.
100  // Multiply it by INT_RANGE and store into an integer.
101  int64_t b(a / (360.0) * INT_RANGE);
102  // Clamp it between [-INT_RANGE / 2, INT_RANGE / 2)
103  b = b % INT_RANGE;
104  if (b < -INT_RANGE / 2)
105  {
106  b += INT_RANGE;
107  }
108  else if (b >= INT_RANGE / 2)
109  {
110  b -= INT_RANGE;
111  }
112  // Divide by INT_RANGE and multiply by 360.
113  return b * (360.0) / INT_RANGE;
114 }
115 
116 double
117 WrapTo2Pi(double a)
118 {
119  static constexpr int64_t INT_RANGE = 100000000000;
120  // Divide the input by 2*M_PI.
121  // Multiply it by INT_RANGE and store into an integer.
122  int64_t b(a / (2 * M_PI) * INT_RANGE);
123  // Clamp it between [-INT_RANGE / 2, INT_RANGE / 2)
124  b = b % INT_RANGE;
125  if (b < 0)
126  {
127  b += INT_RANGE;
128  }
129  else if (b >= INT_RANGE)
130  {
131  b -= INT_RANGE;
132  }
133  // Divide by INT_RANGE and multiply by 2*M_PI.
134  return b * (2 * M_PI) / INT_RANGE;
135 }
136 
137 double
138 WrapToPi(double a)
139 {
140  static constexpr int64_t INT_RANGE = 100000000000;
141  // Divide the input by 2*M_PI.
142  // Multiply it by INT_RANGE and store into an integer.
143  int64_t b(a / (2 * M_PI) * INT_RANGE);
144  // Clamp it between [-INT_RANGE / 2, INT_RANGE / 2)
145  b = b % INT_RANGE;
146  if (b < -INT_RANGE / 2)
147  {
148  b += INT_RANGE;
149  }
150  else if (b >= INT_RANGE / 2)
151  {
152  b -= INT_RANGE;
153  }
154  // Divide by INT_RANGE and multiply by 2*M_PI.
155  return b * (2 * M_PI) / INT_RANGE;
156 }
157 
158 std::ostream&
159 operator<<(std::ostream& os, const Angles& a)
160 {
161  double azim;
162  double incl;
163  std::string unit;
164 
165  if (Angles::m_printDeg)
166  {
167  azim = RadiansToDegrees(a.m_azimuth);
169  unit = "deg";
170  }
171  else
172  {
173  azim = a.m_azimuth;
174  incl = a.m_inclination;
175  unit = "rad";
176  }
177 
178  os << "(" << azim << ", " << incl << ") " << unit;
179  return os;
180 }
181 
182 std::istream&
183 operator>>(std::istream& is, Angles& a)
184 {
185  char c;
186  is >> a.m_azimuth >> c >> a.m_inclination;
187  if (c != ':')
188  {
189  is.setstate(std::ios_base::failbit);
190  }
191  return is;
192 }
193 
195  : Angles(NAN, NAN)
196 {
197 }
198 
199 Angles::Angles(double azimuth, double inclination)
200  : m_azimuth(azimuth),
201  m_inclination(inclination)
202 {
203  NormalizeAngles();
204 }
205 
206 Angles::Angles(Vector v)
207  : m_azimuth(std::atan2(v.y, v.x)),
208  m_inclination(std::acos(v.z / v.GetLength()))
209 {
210  // azimuth and inclination angles for zero-length vectors are not defined
211  if (v.x == 0.0 && v.y == 0.0 && v.z == 0.0)
212  {
213  m_azimuth = NAN;
214  m_inclination = NAN;
215  }
216 
217  NormalizeAngles();
218 }
219 
220 Angles::Angles(Vector v, Vector o)
221  : Angles(v - o)
222 {
223 }
224 
225 void
226 Angles::SetAzimuth(double azimuth)
227 {
228  m_azimuth = azimuth;
229  NormalizeAngles();
230 }
231 
232 void
233 Angles::SetInclination(double inclination)
234 {
235  m_inclination = inclination;
236  NormalizeAngles();
237 }
238 
239 double
241 {
242  return m_azimuth;
243 }
244 
245 double
247 {
248  return m_inclination;
249 }
250 
251 void
253 {
254  CheckIfValid();
255 
256  // Normalize azimuth angle
257  if (std::isnan(m_azimuth))
258  {
259  return;
260  }
261 
263 }
264 
265 void
267 {
268  if (std::isfinite(m_inclination) || std::isfinite(m_azimuth))
269  {
270  NS_ASSERT_MSG(0.0 <= m_inclination && m_inclination <= M_PI,
271  "m_inclination=" << m_inclination << " not valid, should be in [0, pi] rad");
272  }
273  else
274  {
275  // infinite or nan inclination or azimuth angle
276  NS_LOG_WARN("Undefined angle: " << *this);
277  }
278 }
279 
280 } // 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:252
double m_inclination
the inclination angle in radians
Definition: angles.h:224
double GetInclination() const
Getter for inclination angle.
Definition: angles.cc:246
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:226
Angles()
Default constructor is disabled.
Definition: angles.cc:194
void SetInclination(double inclination)
Setter for inclination angle.
Definition: angles.cc:233
double m_azimuth
the azimuth angle in radians
Definition: angles.h:223
double GetAzimuth() const
Getter for azimuth angle.
Definition: angles.cc:240
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:266
#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:138
double WrapTo180(double a)
Wrap angle in [-180, 180)
Definition: angles.cc:96
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:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
double WrapTo2Pi(double a)
Wrap angle in [0, 2*M_PI)
Definition: angles.cc:117
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