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
27namespace ns3 {
28
30
31bool Angles::m_printDeg = false;
32
34const double DEG_TO_RAD = M_PI / 180.0;
36const double RAD_TO_DEG = 180.0 / M_PI;
37
38
39double
40DegreesToRadians (double degrees)
41{
42 return degrees * DEG_TO_RAD;
43}
44
45
46double
47RadiansToDegrees (double radians)
48{
49 return radians * RAD_TO_DEG;
50}
51
52
53std::vector<double>
54DegreesToRadians (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
67std::vector<double>
68RadiansToDegrees (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
80double
81WrapTo360 (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
94double
95WrapTo180 (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
109double
110WrapTo2Pi (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
123double
124WrapToPi (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
138std::ostream&
139operator<< (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);
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
161std::istream&
162operator>> (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
179Angles::Angles (double azimuth, double inclination)
180 : m_azimuth (azimuth),
181 m_inclination (inclination)
182{
184}
185
186
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
199}
200
201Angles::Angles (Vector v, Vector o)
202 : Angles (v - o)
203{}
204
205
206
207void
208Angles::SetAzimuth (double azimuth)
209{
210 m_azimuth = azimuth;
212}
213
214
215void
216Angles::SetInclination (double inclination)
217{
218 m_inclination = inclination;
220}
221
222
223double
225{
226 return m_azimuth;
227}
228
229
230double
232{
233 return m_inclination;
234}
235
236
237void
239{
240 CheckIfValid ();
241
242 // Normalize azimuth angle
243 if (std::isnan (m_azimuth))
244 {
245 return;
246 }
247
249}
250
251
252void
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
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
double WrapTo180(double a)
Wrap angle in [-180, 180)
Definition: angles.cc:95
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
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
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.
STL namespace.