Skip to content

Instantly share code, notes, and snippets.

@vedranmiletic
Last active August 29, 2015 14:07
Show Gist options
  • Save vedranmiletic/0838afc9337564843439 to your computer and use it in GitHub Desktop.
Save vedranmiletic/0838afc9337564843439 to your computer and use it in GitHub Desktop.
SphericalVector2D and SphericalVector3D
diff --git a/src/core/model/vector.cc b/src/core/model/vector.cc
--- a/src/core/model/vector.cc
+++ b/src/core/model/vector.cc
@@ -29,12 +29,19 @@
ATTRIBUTE_HELPER_CPP (Vector3D);
ATTRIBUTE_HELPER_CPP (Vector2D);
+ATTRIBUTE_HELPER_CPP (SphericalVector3D);
+ATTRIBUTE_HELPER_CPP (SphericalVector2D);
// compatibility for mobility code
Ptr<const AttributeChecker> MakeVectorChecker (void)
{
NS_LOG_FUNCTION_NOARGS ();
return MakeVector3DChecker ();
}
+Ptr<const AttributeChecker> MakeSphericalVectorChecker (void)
+{
+ NS_LOG_FUNCTION_NOARGS ();
+ return MakeSphericalVector3DChecker ();
+}
Vector3D::Vector3D (double _x, double _y, double _z)
@@ -67,6 +74,36 @@
NS_LOG_FUNCTION (this);
}
+SphericalVector3D::SphericalVector3D (double _lat, double _lon, double _alt)
+ : lat (_lat),
+ lon (_lon),
+ alt (_alt)
+{
+ NS_LOG_FUNCTION (this << _lat << _lon << _alt);
+}
+
+SphericalVector3D::SphericalVector3D ()
+ : lat (0.0),
+ lon (0.0),
+ alt (0.0)
+{
+ NS_LOG_FUNCTION (this);
+}
+
+SphericalVector2D::SphericalVector2D (double _lat, double _lon)
+ : lat (_lat),
+ lon (_lon)
+{
+ NS_LOG_FUNCTION (this << _lat << _lon);
+}
+
+SphericalVector2D::SphericalVector2D ()
+ : lat (0.0),
+ lon (0.0)
+{
+ NS_LOG_FUNCTION (this);
+}
+
double
CalculateDistance (const Vector3D &a, const Vector3D &b)
{
@@ -86,6 +123,20 @@
double distance = std::sqrt (dx * dx + dy * dy);
return distance;
}
+double
+CalculateDistance (const SphericalVector3D &a, const SphericalVector3D &b)
+{
+ NS_LOG_FUNCTION (a << b);
+ double sphericalDistance = 2 * std::asin (std::sqrt ((1 - std::cos (b.lat * (M_PI / 180) - a.lat * (M_PI / 180))) / 2 + std::cos (a.lat * (M_PI / 180)) * std::cos (b.lat * (M_PI / 180)) * (1 - std::cos (b.lon * (M_PI / 180) - a.lon * (M_PI / 180))) / 2));
+ return sphericalDistance * fmin (a.alt, b.alt) + std::fabs (b.alt - a.alt);
+}
+double
+CalculateDistance (const SphericalVector2D &a, const SphericalVector2D &b)
+{
+ NS_LOG_FUNCTION (a << b);
+ double distance = 2 * std::asin (std::sqrt ((1 - std::cos (b.lat - a.lat)) / 2 + std::cos (a.lat) * std::cos (b.lat) * (1 - std::cos (b.lon - a.lon)) / 2));
+ return distance;
+}
std::ostream &operator << (std::ostream &os, const Vector3D &vector)
{
@@ -118,5 +169,36 @@
}
return is;
}
+std::ostream &operator << (std::ostream &os, const SphericalVector3D &vector)
+{
+ os << vector.lat << ":" << vector.lon << ":" << vector.alt;
+ return os;
+}
+std::istream &operator >> (std::istream &is, SphericalVector3D &vector)
+{
+ char c1, c2;
+ is >> vector.lat >> c1 >> vector.lon >> c2 >> vector.alt;
+ if (c1 != ':' ||
+ c2 != ':')
+ {
+ is.setstate (std::ios_base::failbit);
+ }
+ return is;
+}
+std::ostream &operator << (std::ostream &os, const SphericalVector2D &vector)
+{
+ os << vector.lat << ":" << vector.lon;
+ return os;
+}
+std::istream &operator >> (std::istream &is, SphericalVector2D &vector)
+{
+ char c1;
+ is >> vector.lat >> c1 >> vector.lon;
+ if (c1 != ':')
+ {
+ is.setstate (std::ios_base::failbit);
+ }
+ return is;
+}
} // namespace ns3
diff --git a/src/core/model/vector.h b/src/core/model/vector.h
--- a/src/core/model/vector.h
+++ b/src/core/model/vector.h
@@ -85,6 +85,65 @@
};
/**
+ * \brief a 3D spherical vector
+ */
+class SphericalVector3D
+{
+public:
+ /**
+ * \param _lat latitude coordinate of vector
+ * \param _lon longitude coordinate of vector
+ * \param _alt altitude coordinate of vector
+ *
+ * Create vector (_lat, _lon, _alt)
+ */
+ SphericalVector3D (double _lat, double _lon, double _alt);
+ /**
+ * Create vector (0.0, 0.0, 0.0)
+ */
+ SphericalVector3D ();
+ /**
+ * latitude coordinate of vector
+ */
+ double lat;
+ /**
+ * longitude coordinate of vector
+ */
+ double lon;
+ /**
+ * altitude coordinate of vector
+ */
+ double alt;
+};
+
+/**
+ * \brief a 2D spherical vector
+ */
+class SphericalVector2D
+{
+public:
+ /**
+ * \param _lat latitude coordinate of vector
+ * \param _lon longitude coordinate of vector
+ *
+ * Create vector (_lat, _lon)
+ */
+ SphericalVector2D (double _lat, double _lon);
+ /**
+ * Create vector vector (0.0, 0.0)
+ */
+ SphericalVector2D ();
+ /**
+ * latitude coordinate of vector
+ */
+ double lat;
+ /**
+ * longitude coordinate of vector
+ */
+ double lon;
+};
+
+/**
* \param a one point
* \param b another point
* \returns the cartesian distance between a and b.
@@ -99,6 +158,20 @@
double CalculateDistance (const Vector2D &a, const Vector2D &b);
/**
+ * \param a one spherical point
+ * \param b another spherical point
+ * \returns the spherical distance between a and b.
+ */
+double CalculateDistance (const SphericalVector3D &a, const SphericalVector3D &b);
+
+/**
+ * \param a one spherical point
+ * \param b another spherical point
+ * \returns the spherical distance between a and b.
+ */
+double CalculateDistance (const SphericalVector2D &a, const SphericalVector2D &b);
+
+/**
* \class ns3::Vector3DValue
* \brief hold objects of type ns3::Vector3D
*/
@@ -106,20 +179,39 @@
* \class ns3::Vector2DValue
* \brief hold objects of type ns3::Vector2D
*/
+/**
+ * \class ns3::SphericalVector3DValue
+ * \brief hold objects of type ns3::SphericalVector3D
+ */
+/**
+ * \class ns3::SphericalVector2DValue
+ * \brief hold objects of type ns3::SphericalVector2D
+ */
ATTRIBUTE_HELPER_HEADER (Vector3D);
ATTRIBUTE_HELPER_HEADER (Vector2D);
+ATTRIBUTE_HELPER_HEADER (SphericalVector3D);
+ATTRIBUTE_HELPER_HEADER (SphericalVector2D);
std::ostream &operator << (std::ostream &os, const Vector3D &vector);
std::istream &operator >> (std::istream &is, Vector3D &vector);
std::ostream &operator << (std::ostream &os, const Vector2D &vector);
std::istream &operator >> (std::istream &is, Vector2D &vector);
+std::ostream &operator << (std::ostream &os, const SphericalVector3D &vector);
+std::istream &operator >> (std::istream &is, SphericalVector3D &vector);
+std::ostream &operator << (std::ostream &os, const SphericalVector2D &vector);
+std::istream &operator >> (std::istream &is, SphericalVector2D &vector);
// for compatibility with mobility models
typedef Vector3D Vector;
typedef Vector3DValue VectorValue;
typedef Vector3DChecker VectorChecker;
+typedef SphericalVector3D SphericalVector;
+typedef SphericalVector3DValue SphericalVectorValue;
+typedef SphericalVector3DChecker SphericalVectorChecker;
ATTRIBUTE_ACCESSOR_DEFINE (Vector);
+ATTRIBUTE_ACCESSOR_DEFINE (SphericalVector);
Ptr<const AttributeChecker> MakeVectorChecker (void);
+Ptr<const AttributeChecker> MakeSphericalVectorChecker (void);
} // namespace ns3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment