From b4fde3c4591f282f430d74a001524f5a13b729ae Mon Sep 17 00:00:00 2001 From: Nicola Baldo Date: Fri, 13 Nov 2009 16:45:28 +0100 Subject: [PATCH] added UniformDiscPositionAllocator (bug 672) --- src/mobility/position-allocator.cc | 68 ++++++++++++++++++++++++++++++ src/mobility/position-allocator.h | 51 +++++++++++++++++++++- 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/mobility/position-allocator.cc b/src/mobility/position-allocator.cc index 31427117b..c2605608e 100644 --- a/src/mobility/position-allocator.cc +++ b/src/mobility/position-allocator.cc @@ -315,4 +315,72 @@ RandomDiscPositionAllocator::GetNext (void) const } + +NS_OBJECT_ENSURE_REGISTERED (UniformDiscPositionAllocator); + +TypeId +UniformDiscPositionAllocator::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::UniformDiscPositionAllocator") + .SetParent () + .SetGroupName ("Mobility") + .AddConstructor () + .AddAttribute ("rho", + "The radius of the disc", + DoubleValue (0.0), + MakeDoubleAccessor (&UniformDiscPositionAllocator::m_rho), + MakeDoubleChecker ()) + .AddAttribute ("X", + "The x coordinate of the center of the disc.", + DoubleValue (0.0), + MakeDoubleAccessor (&UniformDiscPositionAllocator::m_x), + MakeDoubleChecker ()) + .AddAttribute ("Y", + "The y coordinate of the center of the disc.", + DoubleValue (0.0), + MakeDoubleAccessor (&UniformDiscPositionAllocator::m_y), + MakeDoubleChecker ()) + ; + return tid; +} + +UniformDiscPositionAllocator::UniformDiscPositionAllocator () +{} +UniformDiscPositionAllocator::~UniformDiscPositionAllocator () +{} + +void +UniformDiscPositionAllocator::SetRho (double rho) +{ + m_rho = rho; +} +void +UniformDiscPositionAllocator::SetX (double x) +{ + m_x = x; +} +void +UniformDiscPositionAllocator::SetY (double y) +{ + m_y = y; +} +Vector +UniformDiscPositionAllocator::GetNext (void) const +{ + UniformVariable r (-m_rho, m_rho); + double x,y; + do + { + x = r.GetValue (); + y = r.GetValue (); + } + while (sqrt(x*x + y*y) > m_rho); + + x += m_x; + y += m_y; + NS_LOG_DEBUG ("Disc position x=" << x << ", y=" << y); + return Vector (x, y, 0.0); +} + + } // namespace ns3 diff --git a/src/mobility/position-allocator.h b/src/mobility/position-allocator.h index da6f49f0b..8c066ea1c 100644 --- a/src/mobility/position-allocator.h +++ b/src/mobility/position-allocator.h @@ -182,7 +182,8 @@ private: /** * \brief allocate random positions within a disc - * according to a pair of random variables. + * according to a given distribution for the polar coordinates of each + * node with respect to the provided center of the disc */ class RandomDiscPositionAllocator : public PositionAllocator { @@ -204,6 +205,54 @@ private: double m_y; }; + +/** + * UniformDiscPositionAllocator allocates the positions randomly within a disc \f$ D \f$ lying on the + * plane \f$ z=0 \f$ and having center at coordinates \f$ (x,y,0) \f$ + * and radius \f$ \rho \f$. The random positions are chosen such that, + * for any subset \f$ S \subset D \f$, the expected value of the + * fraction of points which fall into \f$ S \subset D \f$ corresponds + * to \f$ \frac{|S|}{|D|} \f$, i.e., to the ratio of the area of the + * subset to the area of the whole disc. + * + * \note using UniformDiscPositionAllocator is not equivalent to using + * a RandomDiscPositionAllocator with a uniformly-distributed radius, + * since doing that would results in a point distribution which is + * more dense towards the center of the disc. + */ +class UniformDiscPositionAllocator : public PositionAllocator +{ +public: + static TypeId GetTypeId (void); + UniformDiscPositionAllocator (); + virtual ~UniformDiscPositionAllocator (); + + /** + * + * @param rho the value of the radius of the disc + */ + void SetRho (double rho); + + /** + * + * + * @param x the X coordinate of the center of the disc + */ + void SetX (double x); + + /** + * + * @param y the Y coordinate of the center of the disc + */ + void SetY (double y); + + virtual Vector GetNext (void) const; +private: + double m_rho; + double m_x; + double m_y; +}; + } // namespace ns3 #endif /* RANDOM_POSITION_H */