22#include <ns3/angles.h>
45auto curvature = [](
double e,
double ph) {
return sqrt(1 - e * e * sin(ph) * sin(ph)); };
64 double Rn = a / curvature(e, latitudeRadians);
65 double x = (Rn + altitude) * cos(latitudeRadians) * cos(longitudeRadians);
66 double y = (Rn + altitude) * cos(latitudeRadians) * sin(longitudeRadians);
67 double z = ((1 - e * e) * Rn + altitude) * sin(latitudeRadians);
68 Vector cartesianCoordinates = Vector(x, y, z);
69 return cartesianCoordinates;
82 lla.y = atan2(pos.y, pos.x);
87 lla.x = atan2(pos.z, p * (1 - e2));
92 double N = a / sqrt(1 - e2 * sin(tmp.x) * sin(tmp.x));
93 double v = p / cos(tmp.x);
95 lla.x = atan2(pos.z, p * (1 - e2 * N / v));
107 lla.y += lla.y < 0 ? 180 : -180;
109 else if (lla.x < -90.0)
111 lla.x = -180 - lla.x;
112 lla.y += lla.y < 0 ? 180 : -180;
121 NS_ASSERT_MSG(-180.0 <= lla.y,
"Conversion error: longitude too negative");
122 NS_ASSERT_MSG(180.0 > lla.y,
"Conversion error: longitude too positive");
123 NS_ASSERT_MSG(-90.0 <= lla.x,
"Conversion error: latitude too negative");
124 NS_ASSERT_MSG(90.0 >= lla.x,
"Conversion error: latitude too positive");
141 double h0 = refPoint.z;
146 double v = a / curvature(e, phi);
149 double v0 = a / curvature(e, phi0);
151 double U = (v + h) * cos(phi) * sin(lambda - lambda0);
152 double V = (v + h) * (sin(phi) * cos(phi0) - cos(phi) * sin(phi0) * cos(lambda - lambda0)) +
153 e * e * (v0 * sin(phi0) - v * sin(phi)) * cos(phi0);
154 double W = (v + h) * (sin(phi) * sin(phi0) + cos(phi) * cos(phi0) * cos(lambda - lambda0)) +
155 e * e * (v0 * sin(phi0) - v * sin(phi)) * sin(phi0) - (v0 + h0);
157 Vector topocentricCoordinates = Vector(U, V, W);
158 return topocentricCoordinates;
173 double h0 = refPoint.z;
179 double v0 = a / curvature(e, phi0);
181 double X0 = (v0 + h0) * cos(phi0) * cos(lambda0);
182 double Y0 = (v0 + h0) * cos(phi0) * sin(lambda0);
183 double Z0 = ((1 - e * e) * v0 + h0) * sin(phi0);
185 double X = X0 - U * sin(lambda0) - V * sin(phi0) * cos(lambda0) + W * cos(phi0) * cos(lambda0);
186 double Y = Y0 + U * cos(lambda0) - V * sin(phi0) * sin(lambda0) + W * cos(phi0) * sin(lambda0);
187 double Z = Z0 + V * cos(phi0) + W * sin(phi0);
189 double epsilon = e * e / (1 - e * e);
190 double b = a * (1 - f);
191 double p = sqrt(X * X + Y * Y);
192 double q = atan2((Z * a), (p * b));
194 double phi = atan2((Z +
epsilon * b * pow(sin(q), 3)), (p - e * e * a * pow(cos(q), 3)));
195 double lambda = atan2(Y, X);
197 double v = a / curvature(e, phi);
198 double h = (p / cos(phi)) - v;
203 Vector geographicCoordinates = Vector(phi, lambda, h);
204 return geographicCoordinates;
209 double originLongitude,
212 double maxDistFromOrigin,
217 if (originLatitude >= 90)
219 NS_LOG_WARN(
"origin latitude must be less than 90. setting to 89.999");
220 originLatitude = 89.999;
222 else if (originLatitude <= -90)
224 NS_LOG_WARN(
"origin latitude must be greater than -90. setting to -89.999");
225 originLatitude = -89.999;
232 NS_LOG_WARN(
"maximum altitude must be greater than or equal to 0. setting to 0");
238 double originColatitude = (M_PI_2)-originLatitudeRadians;
249 std::list<Vector> generatedPoints;
250 for (
int i = 0; i < numPoints; i++)
255 double phi = uniRand->GetValue(0, M_PI * 2);
261 double theta = M_PI_2 - alpha;
264 double randPointLatitude = asin(sin(theta) * cos(originColatitude) +
265 cos(theta) * sin(originColatitude) * sin(phi));
267 double intermedLong = asin((sin(randPointLatitude) * cos(originColatitude) - sin(theta)) /
268 (cos(randPointLatitude) * sin(originColatitude)));
270 intermedLong = intermedLong + M_PI_2;
274 if (phi > (M_PI_2) && phi <= (3 * M_PI_2))
276 intermedLong = -intermedLong;
280 double randPointLongitude = intermedLong + originLongitudeRadians;
283 double randAltitude = uniRand->GetValue(0, maxAltitude);
293 generatedPoints.push_back(pointPosition);
295 return generatedPoints;
298std::tuple<double, double, double>
326 return std::make_tuple(a, e, f);
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...
#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.
#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.
auto curvature
GRS80 and WGS84 sources.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double DegreesToRadians(double degrees)
converts degrees to radians
double CalculateDistance(const Vector3D &a, const Vector3D &b)
double RadiansToDegrees(double radians)
converts radians to degrees