A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
geographic-positions.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Benjamin Cizdziel <ben.cizdziel@gmail.com>
7 */
8
10
11#include <ns3/angles.h>
12#include <ns3/log.h>
13
14#include <cmath>
15
16NS_LOG_COMPONENT_DEFINE("GeographicPositions");
17
18namespace
19{
20/**
21 * GRS80 and WGS84 sources
22 *
23 * Moritz, H. "Geodetic Reference System 1980." GEODETIC REFERENCE SYSTEM 1980.
24 * <https://web.archive.org/web/20170712034716/http://www.gfy.ku.dk/~iag/HB2000/part4/grs80_corr.htm>.
25 *
26 * "Department of Defense World Geodetic System 1984." National Imagery and
27 * Mapping Agency, 1 Jan. 2000.
28 * <https://web.archive.org/web/20200730231853/http://earth-info.nga.mil/GandG/publications/tr8350.2/wgs84fin.pdf>.
29 */
30
31/**
32 * \brief Lambda function for computing the curvature
33 */
34auto curvature = [](double e, double ph) { return sqrt(1 - e * e * sin(ph) * sin(ph)); };
35} // namespace
36
37namespace ns3
38{
39
40Vector
42 double longitude,
43 double altitude,
44 EarthSpheroidType sphType)
45{
47 double latitudeRadians = DegreesToRadians(latitude);
48 double longitudeRadians = DegreesToRadians(longitude);
49
50 // Retrieve radius, first eccentricity and flattening according to the specified Earth's model
51 auto [a, e, f] = GetRadiusEccentFlat(sphType);
52
53 double Rn = a / curvature(e, latitudeRadians); // radius of curvature
54 double x = (Rn + altitude) * cos(latitudeRadians) * cos(longitudeRadians);
55 double y = (Rn + altitude) * cos(latitudeRadians) * sin(longitudeRadians);
56 double z = ((1 - e * e) * Rn + altitude) * sin(latitudeRadians);
57 Vector cartesianCoordinates = Vector(x, y, z);
58 return cartesianCoordinates;
59}
60
61Vector
63{
64 NS_LOG_FUNCTION(pos << sphType);
65
66 // Retrieve radius, first eccentricity and flattening according to the specified Earth's model
67 auto [a, e, f] = GetRadiusEccentFlat(sphType);
68
69 Vector lla;
70 Vector tmp;
71 lla.y = atan2(pos.y, pos.x); // longitude (rad), in +/- pi
72
73 double e2 = e * e;
74 // sqrt (pos.x^2 + pos.y^2)
75 double p = CalculateDistance(pos, {0, 0, pos.z});
76 lla.x = atan2(pos.z, p * (1 - e2)); // init latitude (rad), in +/- pi
77
78 do
79 {
80 tmp = lla;
81 double N = a / sqrt(1 - e2 * sin(tmp.x) * sin(tmp.x));
82 double v = p / cos(tmp.x);
83 lla.z = v - N; // altitude
84 lla.x = atan2(pos.z, p * (1 - e2 * N / v));
85 }
86 // 1 m difference is approx 1 / 30 arc seconds = 9.26e-6 deg
87 while (fabs(lla.x - tmp.x) > DegreesToRadians(0.00000926));
88
89 lla.x = RadiansToDegrees(lla.x);
90 lla.y = RadiansToDegrees(lla.y);
91
92 // canonicalize (latitude) x in [-90, 90] and (longitude) y in [-180, 180)
93 if (lla.x > 90.0)
94 {
95 lla.x = 180 - lla.x;
96 lla.y += lla.y < 0 ? 180 : -180;
97 }
98 else if (lla.x < -90.0)
99 {
100 lla.x = -180 - lla.x;
101 lla.y += lla.y < 0 ? 180 : -180;
102 }
103 if (lla.y == 180.0)
104 {
105 lla.y = -180;
106 }
107
108 // make sure lat/lon in the right range to double check canonicalization
109 // and conversion routine
110 NS_ASSERT_MSG(-180.0 <= lla.y, "Conversion error: longitude too negative");
111 NS_ASSERT_MSG(180.0 > lla.y, "Conversion error: longitude too positive");
112 NS_ASSERT_MSG(-90.0 <= lla.x, "Conversion error: latitude too negative");
113 NS_ASSERT_MSG(90.0 >= lla.x, "Conversion error: latitude too positive");
114
115 return lla;
116}
117
118Vector
120 Vector refPoint,
121 EarthSpheroidType sphType)
122{
123 NS_LOG_FUNCTION(pos << sphType);
124
125 double phi = DegreesToRadians(pos.x);
126 double lambda = DegreesToRadians(pos.y);
127 double h = pos.z;
128 double phi0 = DegreesToRadians(refPoint.x);
129 double lambda0 = DegreesToRadians(refPoint.y);
130 double h0 = refPoint.z;
131 // Retrieve radius, first eccentricity and flattening according to the specified Earth's model
132 auto [a, e, f] = GetRadiusEccentFlat(sphType);
133
134 // the radius of curvature in the prime vertical at latitude
135 double v = a / curvature(e, phi);
136 // the radius of curvature in the prime vertical at latitude
137 // of the reference point
138 double v0 = a / curvature(e, phi0);
139
140 double U = (v + h) * cos(phi) * sin(lambda - lambda0);
141 double V = (v + h) * (sin(phi) * cos(phi0) - cos(phi) * sin(phi0) * cos(lambda - lambda0)) +
142 e * e * (v0 * sin(phi0) - v * sin(phi)) * cos(phi0);
143 double W = (v + h) * (sin(phi) * sin(phi0) + cos(phi) * cos(phi0) * cos(lambda - lambda0)) +
144 e * e * (v0 * sin(phi0) - v * sin(phi)) * sin(phi0) - (v0 + h0);
145
146 Vector topocentricCoordinates = Vector(U, V, W);
147 return topocentricCoordinates;
148}
149
150Vector
152 Vector refPoint,
153 EarthSpheroidType sphType)
154{
155 NS_LOG_FUNCTION(pos << sphType);
156
157 double U = pos.x;
158 double V = pos.y;
159 double W = pos.z;
160 double phi0 = DegreesToRadians(refPoint.x);
161 double lambda0 = DegreesToRadians(refPoint.y);
162 double h0 = refPoint.z;
163 // Retrieve radius, first eccentricity and flattening according to the specified Earth's model
164 auto [a, e, f] = GetRadiusEccentFlat(sphType);
165
166 // the radius of curvature in the prime vertical at latitude
167 // of the reference point
168 double v0 = a / curvature(e, phi0);
169
170 double X0 = (v0 + h0) * cos(phi0) * cos(lambda0);
171 double Y0 = (v0 + h0) * cos(phi0) * sin(lambda0);
172 double Z0 = ((1 - e * e) * v0 + h0) * sin(phi0);
173
174 double X = X0 - U * sin(lambda0) - V * sin(phi0) * cos(lambda0) + W * cos(phi0) * cos(lambda0);
175 double Y = Y0 + U * cos(lambda0) - V * sin(phi0) * sin(lambda0) + W * cos(phi0) * sin(lambda0);
176 double Z = Z0 + V * cos(phi0) + W * sin(phi0);
177
178 double epsilon = e * e / (1 - e * e);
179 double b = a * (1 - f);
180 double p = sqrt(X * X + Y * Y);
181 double q = atan2((Z * a), (p * b));
182
183 double phi = atan2((Z + epsilon * b * pow(sin(q), 3)), (p - e * e * a * pow(cos(q), 3)));
184 double lambda = atan2(Y, X);
185
186 double v = a / curvature(e, phi);
187 double h = (p / cos(phi)) - v;
188
189 phi = RadiansToDegrees(phi);
190 lambda = RadiansToDegrees(lambda);
191
192 Vector geographicCoordinates = Vector(phi, lambda, h);
193 return geographicCoordinates;
194}
195
196std::list<Vector>
198 double originLongitude,
199 double maxAltitude,
200 int numPoints,
201 double maxDistFromOrigin,
203{
205 // fixes divide by zero case and limits latitude bounds
206 if (originLatitude >= 90)
207 {
208 NS_LOG_WARN("origin latitude must be less than 90. setting to 89.999");
209 originLatitude = 89.999;
210 }
211 else if (originLatitude <= -90)
212 {
213 NS_LOG_WARN("origin latitude must be greater than -90. setting to -89.999");
214 originLatitude = -89.999;
215 }
216
217 // restricts maximum altitude from being less than zero (below earth's surface).
218 // sets maximum altitude equal to zero if parameter is set to be less than zero.
219 if (maxAltitude < 0)
220 {
221 NS_LOG_WARN("maximum altitude must be greater than or equal to 0. setting to 0");
222 maxAltitude = 0;
223 }
224
225 double originLatitudeRadians = DegreesToRadians(originLatitude);
226 double originLongitudeRadians = DegreesToRadians(originLongitude);
227 double originColatitude = (M_PI_2)-originLatitudeRadians;
228
229 // maximum alpha allowed (arc length formula)
230 double a = maxDistFromOrigin / EARTH_SPHERE_RADIUS;
231 if (a > M_PI)
232 {
233 // pi is largest alpha possible (polar angle from origin that
234 // points can be generated within)
235 a = M_PI;
236 }
237
238 std::list<Vector> generatedPoints;
239 for (int i = 0; i < numPoints; i++)
240 {
241 // random distance from North Pole (towards center of earth)
242 double d = uniRand->GetValue(0, EARTH_SPHERE_RADIUS - EARTH_SPHERE_RADIUS * cos(a));
243 // random angle in latitude slice (wrt Prime Meridian), radians
244 double phi = uniRand->GetValue(0, M_PI * 2);
245 // random angle from Center of Earth (wrt North Pole), radians
246 double alpha = acos((EARTH_SPHERE_RADIUS - d) / EARTH_SPHERE_RADIUS);
247
248 // shift coordinate system from North Pole referred to origin point referred
249 // reference: http://en.wikibooks.org/wiki/General_Astronomy/Coordinate_Systems
250 double theta = M_PI_2 - alpha; // angle of elevation of new point wrt
251 // origin point (latitude in coordinate
252 // system referred to origin point)
253 double randPointLatitude = asin(sin(theta) * cos(originColatitude) +
254 cos(theta) * sin(originColatitude) * sin(phi));
255 // declination
256 double intermedLong = asin((sin(randPointLatitude) * cos(originColatitude) - sin(theta)) /
257 (cos(randPointLatitude) * sin(originColatitude)));
258 // right ascension
259 intermedLong = intermedLong + M_PI_2; // shift to longitude 0
260
261 // flip / mirror point if it has phi in quadrant II or III (wasn't
262 // resolved correctly by arcsin) across longitude 0
263 if (phi > (M_PI_2) && phi <= (3 * M_PI_2))
264 {
265 intermedLong = -intermedLong;
266 }
267
268 // shift longitude to be referenced to origin
269 double randPointLongitude = intermedLong + originLongitudeRadians;
270
271 // random altitude above earth's surface
272 double randAltitude = uniRand->GetValue(0, maxAltitude);
273
274 // convert coordinates from geographic to cartesian
276 RadiansToDegrees(randPointLatitude),
277 RadiansToDegrees(randPointLongitude),
278 randAltitude,
279 SPHERE);
280
281 // add generated coordinate points to list
282 generatedPoints.push_back(pointPosition);
283 }
284 return generatedPoints;
285}
286
287std::tuple<double, double, double>
289{
290 double a; // radius, in meters
291 double e; // first eccentricity
292 double f; // first flattening
293
294 switch (type)
295 {
296 case SPHERE:
300 break;
301 case GRS80:
305 break;
306 case WGS84:
310 break;
311 default:
312 NS_FATAL_ERROR("The specified earth model is not supported!");
313 }
314
315 return std::make_tuple(a, e, f);
316}
317
318} // namespace ns3
static std::list< Vector > RandCartesianPointsAroundGeographicPoint(double originLatitude, double originLongitude, double maxAltitude, int numPoints, double maxDistFromOrigin, Ptr< UniformRandomVariable > uniRand)
Generates uniformly distributed random points (in ECEF Cartesian coordinates) within a given altitude...
static constexpr double EARTH_SPHERE_FLATTENING
Earth's flattening if modeled as a perfect sphere.
static constexpr double EARTH_SPHERE_ECCENTRICITY
Earth's eccentricity if modeled as a perfect sphere.
static constexpr double EARTH_SPHERE_RADIUS
Spheroid model to use for earth: perfect sphere (SPHERE), Geodetic Reference System 1980 (GRS80),...
EarthSpheroidType
The possible Earth spheroid models. .
static constexpr double EARTH_GRS80_FLATTENING
Earth's first flattening as defined by GRS80 https://en.wikipedia.org/wiki/Geodetic_Reference_System_...
static constexpr double EARTH_WGS84_ECCENTRICITY
Earth's first eccentricity as defined by https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84.
static Vector TopocentricToGeographicCoordinates(Vector pos, Vector refPoint, EarthSpheroidType sphType)
Conversion from topocentric to geographic.
static Vector GeographicToCartesianCoordinates(double latitude, double longitude, double altitude, EarthSpheroidType sphType)
Converts earth geographic/geodetic coordinates (latitude and longitude in degrees) with a given altit...
static constexpr double EARTH_WGS84_FLATTENING
Earth's first flattening as defined by WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System#WGS8...
static Vector GeographicToTopocentricCoordinates(Vector pos, Vector refPoint, EarthSpheroidType sphType)
Conversion from geographic to topocentric coordinates.
static Vector CartesianToGeographicCoordinates(Vector pos, EarthSpheroidType sphType)
Inverse of GeographicToCartesianCoordinates using [1].
static constexpr double EARTH_SEMIMAJOR_AXIS
<Earth's semi-major axis in meters as defined by both GRS80 and WGS84 https://en.wikipedia....
static std::tuple< double, double, double > GetRadiusEccentFlat(EarthSpheroidType type)
static constexpr double EARTH_GRS80_ECCENTRICITY
Earth's first eccentricity as defined by GRS80 https://en.wikipedia.org/wiki/Geodetic_Reference_Syste...
Smart pointer class similar to boost::intrusive_ptr.
#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_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#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 DegreesToRadians(double degrees)
converts degrees to radians
Definition angles.cc:28
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition vector.cc:98
double RadiansToDegrees(double radians)
converts radians to degrees
Definition angles.cc:34