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