From f671c117735a7bd8bb1354de690932b134190872 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 19 Jul 2007 10:05:07 +0200 Subject: [PATCH] remove mobility model helper, re-enable random direction model with new helper --- SConstruct | 4 +- src/node/mobility-model-helper.cc | 185 -------------------- src/node/mobility-model-helper.h | 80 --------- src/node/random-direction-mobility-model.cc | 115 +++++------- src/node/random-direction-mobility-model.h | 22 ++- 5 files changed, 53 insertions(+), 353 deletions(-) delete mode 100644 src/node/mobility-model-helper.cc delete mode 100644 src/node/mobility-model-helper.h diff --git a/SConstruct b/SConstruct index cac6a76a6..ac117488f 100644 --- a/SConstruct +++ b/SConstruct @@ -255,7 +255,6 @@ node.add_sources ([ 'random-walk-2d-mobility-model.cc', 'hierarchical-mobility-model.cc', 'ns2-mobility-file-topology.cc', - 'mobility-model-helper.cc', 'position-2d.cc', 'position.cc', 'random-position.cc', @@ -264,6 +263,7 @@ node.add_sources ([ 'random-waypoint-mobility-model.cc', 'rectangle-default-value.cc', 'rectangle.cc', + 'random-direction-mobility-model.cc', ]) node.add_inst_headers ([ 'node.h', @@ -290,7 +290,6 @@ node.add_inst_headers ([ 'random-walk-2d-mobility-model.h', 'hierarchical-mobility-model.h', 'ns2-mobility-file-topology.h', - 'mobility-model-helper.h', 'position-2d.h', 'position.h', 'random-position.h', @@ -299,6 +298,7 @@ node.add_inst_headers ([ 'random-waypoint-mobility-model.h', 'rectangle-default-value.h', 'rectangle.h', + 'random-direction-mobility-model.h', ]) applications = build.Ns3Module ('applications', 'src/applications') diff --git a/src/node/mobility-model-helper.cc b/src/node/mobility-model-helper.cc deleted file mode 100644 index aa41f758e..000000000 --- a/src/node/mobility-model-helper.cc +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include "ns3/simulator.h" -#include "mobility-model-helper.h" - -namespace ns3 { - -Position2D::Position2D (double _x, double _y) - : x (_x), - y (_y) -{} -Position2D::Position2D () - : x (0.0), - y (0.0) -{} - -Speed2D::Speed2D (double _dx, double _dy) - : dx (_dx), - dy (_dy) -{} - -Speed2D::Speed2D () - : dx (0.0), - dy (0.0) -{} - -MobilityModelHelper::MobilityModelHelper () -{} -void -MobilityModelHelper::InitializePosition (const struct Position2D &position) -{ - m_position = position; - m_speed.dx = 0.0; - m_speed.dy = 0.0; - m_lastUpdate = Simulator::Now (); - m_pauseEnd = Simulator::Now (); -} -void -MobilityModelHelper::Update (const struct AreaBounds &bounds) const -{ - Time now = Simulator::Now (); - if (m_pauseEnd > now) - { - return; - } - Time last = std::max (now, m_pauseEnd); - if (m_lastUpdate >= last) - { - return; - } - Time deltaTime = now - last; - m_lastUpdate = now; - double deltaS = deltaTime.GetSeconds (); - m_position.x += m_speed.dx * deltaS; - m_position.y += m_speed.dy * deltaS; - m_position.x = std::min (m_position.x, bounds.xMax); - m_position.y = std::min (m_position.y, bounds.yMax); - m_position.x = std::max (m_position.x, bounds.xMin); - m_position.y = std::max (m_position.y, bounds.yMin); -} -Position2D -MobilityModelHelper::CalculateIntersection (const struct AreaBounds &bounds) const -{ - double xMaxY = m_position.y + (bounds.xMax - m_position.x) / m_speed.dx * m_speed.dy; - double xMinY = m_position.y + (bounds.xMin - m_position.x) / m_speed.dx * m_speed.dy; - double yMaxX = m_position.x + (bounds.yMax - m_position.y) / m_speed.dy * m_speed.dx; - double yMinX = m_position.x + (bounds.yMin - m_position.y) / m_speed.dy * m_speed.dx; - bool xMaxOk = xMaxY <= bounds.yMax && xMaxY >= bounds.yMin; - bool xMinOk = xMinY <= bounds.yMax && xMinY >= bounds.yMin; - bool yMaxOk = yMaxX <= bounds.xMax && yMaxX >= bounds.xMin; - bool yMinOk = yMinX <= bounds.xMax && yMinX >= bounds.xMin; - if (xMaxOk && m_speed.dx >= 0) - { - return Position2D (bounds.xMax, xMaxY); - } - else if (xMinOk && m_speed.dx <= 0) - { - return Position2D (bounds.xMin, xMinY); - } - else if (yMaxOk && m_speed.dy >= 0) - { - return Position2D (yMaxX, bounds.yMax); - } - else if (yMinOk && m_speed.dy <= 0) - { - return Position2D (yMinX, bounds.yMin); - } - else - { - NS_ASSERT (false); - // quiet compiler - return Position2D (0.0, 0.0); - } -} -double -MobilityModelHelper::Distance (const Position2D &a, const Position2D &b) const -{ - double dx = a.x - b.x; - double dy = a.y - b.y; - return sqrt (dx * dx + dy * dy); -} - -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &maxMovementDelay, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - Time seconds = Seconds (Distance (intersection, m_position) / speed); - seconds = std::min (seconds, maxMovementDelay); - return seconds; -} -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - double maxDistance, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - double distance = Distance (intersection, m_position); - distance = std::min (distance, maxDistance); - double seconds = distance / speed; - return Seconds (seconds); -} -Time -MobilityModelHelper::Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &pauseDelay) -{ - Update (bounds); - m_pauseEnd = Simulator::Now () + pauseDelay; - m_speed.dx = std::cos (direction) * speed; - m_speed.dy = std::sin (direction) * speed; - Position2D intersection = CalculateIntersection (bounds); - double seconds = Distance (intersection, m_position) / speed; - return Seconds (seconds); -} - -Position2D -MobilityModelHelper::GetCurrentPosition (const struct AreaBounds &bounds) const -{ - Update (bounds); - return m_position; -} - -Speed2D -MobilityModelHelper::GetSpeed (void) const -{ - return m_speed; -} - -enum MobilityModelHelper::Side -MobilityModelHelper::GetSide (const struct AreaBounds &bounds) const -{ - if (m_position.x == bounds.xMin) - { - return MobilityModelHelper::LEFT; - } - if (m_position.x == bounds.xMax) - { - return MobilityModelHelper::RIGHT; - } - if (m_position.y == bounds.yMin) - { - return MobilityModelHelper::BOTTOM; - } - if (m_position.y == bounds.yMax) - { - return MobilityModelHelper::TOP; - } - return MobilityModelHelper::NONE; -} - -} // namespace ns3 diff --git a/src/node/mobility-model-helper.h b/src/node/mobility-model-helper.h deleted file mode 100644 index c92514f5b..000000000 --- a/src/node/mobility-model-helper.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef MOBILITY_MODEL_HELPER_H -#define MOBILITY_MODEL_HELPER_H - -#include "ns3/nstime.h" - -namespace ns3 { - -class Position2D -{ -public: - Position2D (double _x, double _y); - Position2D (); - double x; - double y; -}; - -struct AreaBounds -{ - double xMin; - double xMax; - double yMin; - double yMax; -}; - -class Speed2D -{ -public: - Speed2D (double _dx, double _dy); - Speed2D (); - double dx; - double dy; -}; - -class MobilityModelHelper -{ - public: - enum Side { - LEFT, - RIGHT, - TOP, - BOTTOM, - NONE - }; - MobilityModelHelper (); - void InitializePosition (const struct Position2D &position); - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &maxMovementDelay, - const Time &pauseDelay); - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - double maxDistance, - const Time &pauseDelay); - // move in specified direction until - // we hit the area bounds. - // return delay until we reach the bounds. - Time Reset (const struct AreaBounds &bounds, - double direction, - double speed, - const Time &pauseDelay); - - Position2D GetCurrentPosition (const struct AreaBounds &bounds) const; - Speed2D GetSpeed (void) const; - enum MobilityModelHelper::Side GetSide (const struct AreaBounds &bounds) const; - - private: - double Distance (const Position2D &a, const Position2D &b) const; - void Update (const struct AreaBounds &bounds) const; - Position2D CalculateIntersection (const struct AreaBounds &bounds) const; - mutable Position2D m_position; - Speed2D m_speed; - mutable Time m_lastUpdate; - Time m_pauseEnd; -}; - -} // namespace ns3 - -#endif /* MOBILITY_MODEL_HELPER_H */ diff --git a/src/node/random-direction-mobility-model.cc b/src/node/random-direction-mobility-model.cc index a2f59fbca..a847628fe 100644 --- a/src/node/random-direction-mobility-model.cc +++ b/src/node/random-direction-mobility-model.cc @@ -31,8 +31,8 @@ const double RandomDirectionMobilityModel::PI = 3.1415; const InterfaceId RandomDirectionMobilityModel::iid = MakeInterfaceId ("RandomDirectionMobilityModel", MobilityModel::iid); const ClassId RandomDirectionMobilityModel::cid = - MakeClassId ("RandomDirectionMobilityModel", - RandomDirectionMobilityModel::iid); + MakeClassId ("RandomDirectionMobilityModel", + RandomDirectionMobilityModel::iid); static RandomVariableDefaultValue @@ -42,44 +42,29 @@ static RandomVariableDefaultValue static RandomVariableDefaultValue g_pauseVariable ("RandomDirectionPause", - "A random variable to control the duration of of the pause of a RandomDiretion mobility model.", + "A random variable to control the duration " + "of the pause of a RandomDiretion mobility model.", "Constant:2"); static RectangleDefaultValue - g_rectangle ("RandomDirectionArea", + g_bounds ("RandomDirectionArea", "The bounding area for the RandomDirection model.", -100, 100, -100, 100); RandomDirectionParameters::RandomDirectionParameters () - : m_speedVariable (g_speedVariable.GetCopy ()), + : m_bounds (g_bounds.GetValue ()), + m_speedVariable (g_speedVariable.GetCopy ()), m_pauseVariable (g_pauseVariable.GetCopy ()) -{ - m_bounds.xMin = g_rectangle.GetMinX (); - m_bounds.xMax = g_rectangle.GetMaxX (); - m_bounds.yMin = g_rectangle.GetMinY (); - m_bounds.yMax = g_rectangle.GetMaxY (); -} -RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax) - : m_speedVariable (g_speedVariable.GetCopy ()), - m_pauseVariable (g_pauseVariable.GetCopy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; -} -RandomDirectionParameters::RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + +{} +RandomDirectionParameters::RandomDirectionParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable) - : m_speedVariable (speedVariable.Copy ()), + : m_bounds (bounds), + m_speedVariable (speedVariable.Copy ()), m_pauseVariable (pauseVariable.Copy ()) -{ - m_bounds.xMin = xMin; - m_bounds.xMax = xMax; - m_bounds.yMin = yMin; - m_bounds.yMax = yMax; -} +{} RandomDirectionParameters::~RandomDirectionParameters () { @@ -102,26 +87,22 @@ RandomDirectionParameters::SetPause (const RandomVariable &pauseVariable) m_pauseVariable = pauseVariable.Copy (); } void -RandomDirectionParameters::SetBounds (double xMin, double xMax, double yMin, double yMax) +RandomDirectionParameters::SetBounds (const Rectangle &bounds) { - m_bounds.xMin = xMin; - m_bounds.yMin = yMin; - m_bounds.xMax = xMax; - m_bounds.yMax = yMax; + m_bounds = bounds; } Ptr -RandomDirectionMobilityModel::GetDefaultParameters (void) +RandomDirectionParameters::GetCurrent (void) { - static Ptr parameters = Create (); - if (parameters->m_bounds.xMin != g_rectangle.GetMinX () || - parameters->m_bounds.yMin != g_rectangle.GetMinY () || - parameters->m_bounds.xMax != g_rectangle.GetMaxX () || - parameters->m_bounds.yMax != g_rectangle.GetMaxY () || + static Ptr parameters = 0; + if (parameters == 0 || + g_bounds.IsDirty () || g_speedVariable.IsDirty () || g_pauseVariable.IsDirty ()) { parameters = Create (); + g_bounds.ClearDirtyFlag (); g_speedVariable.ClearDirtyFlag (); g_pauseVariable.ClearDirtyFlag (); } @@ -130,29 +111,16 @@ RandomDirectionMobilityModel::GetDefaultParameters (void) RandomDirectionMobilityModel::RandomDirectionMobilityModel () - : m_parameters (GetDefaultParameters ()) + : m_parameters (RandomDirectionParameters::GetCurrent ()) { SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomDirectionMobilityModel::RandomDirectionMobilityModel (double x, double y) - : m_parameters (GetDefaultParameters ()) -{ - SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters) : m_parameters (parameters) { SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); -} -RandomDirectionMobilityModel::RandomDirectionMobilityModel (Ptr parameters, - double x, double y) - : m_parameters (parameters) -{ - SetInterfaceId (RandomDirectionMobilityModel::iid); - InitializeDirectionAndSpeed (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } void RandomDirectionMobilityModel::DoDispose (void) @@ -162,7 +130,7 @@ RandomDirectionMobilityModel::DoDispose (void) MobilityModel::DoDispose (); } void -RandomDirectionMobilityModel::InitializeDirectionAndSpeed (void) +RandomDirectionMobilityModel::Start (void) { double direction = UniformVariable::GetSingleValue (0, 2 * PI); SetDirectionAndSpeed (direction); @@ -171,53 +139,52 @@ void RandomDirectionMobilityModel::SetDirectionAndSpeed (double direction) { double speed = m_parameters->m_speedVariable->GetValue (); + const Speed vector (std::cos (direction) * speed, + std::sin (direction) * speed, + 0.0); Time pause = Seconds (m_parameters->m_pauseVariable->GetValue ()); - Time delay = m_helper.Reset (m_parameters->m_bounds, - direction, speed, - pause); + Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + m_helper.Reset (vector, pause); + Position next = m_parameters->m_bounds.CalculateIntersection (position, vector); + Time delay = Seconds (CalculateDistance (position, next) / speed); m_event = Simulator::Schedule (delay + pause, &RandomDirectionMobilityModel::ResetDirectionAndSpeed, this); + NotifyCourseChange (); } void RandomDirectionMobilityModel::ResetDirectionAndSpeed (void) { double direction = UniformVariable::GetSingleValue (0, PI); - switch (m_helper.GetSide (m_parameters->m_bounds)) + Position position = m_helper.GetCurrentPosition (m_parameters->m_bounds); + switch (m_parameters->m_bounds.GetClosestSide (position)) { - case MobilityModelHelper::RIGHT: + case Rectangle::RIGHT: direction += PI / 2; break; - case MobilityModelHelper::LEFT: + case Rectangle::LEFT: direction += - PI / 2; break; - case MobilityModelHelper::TOP: + case Rectangle::TOP: direction += PI; break; - case MobilityModelHelper::BOTTOM: + case Rectangle::BOTTOM: direction += 0.0; break; - case MobilityModelHelper::NONE: - NS_ASSERT (false); - break; } SetDirectionAndSpeed (direction); - NotifyCourseChange (); } Position RandomDirectionMobilityModel::DoGet (void) const { - Position2D position = m_helper.GetCurrentPosition (m_parameters->m_bounds); - return Position (position.x, position.y, 0.0); + return m_helper.GetCurrentPosition (m_parameters->m_bounds); } void RandomDirectionMobilityModel::DoSet (const Position &position) { - Position2D pos (position.x, position.y); - m_helper.InitializePosition (pos); + m_helper.InitializePosition (position); Simulator::Remove (m_event); - InitializeDirectionAndSpeed (); - NotifyCourseChange (); + m_event = Simulator::ScheduleNow (&RandomDirectionMobilityModel::Start, this); } diff --git a/src/node/random-direction-mobility-model.h b/src/node/random-direction-mobility-model.h index 30acc0721..73c54eb0c 100644 --- a/src/node/random-direction-mobility-model.h +++ b/src/node/random-direction-mobility-model.h @@ -26,8 +26,9 @@ #include "ns3/nstime.h" #include "ns3/event-id.h" #include "ns3/component-manager.h" +#include "ns3/rectangle.h" #include "mobility-model.h" -#include "mobility-model-helper.h" +#include "static-speed-helper.h" namespace ns3 { @@ -37,22 +38,22 @@ class RandomDirectionParameters : public Object { public: RandomDirectionParameters (); - RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax); - RandomDirectionParameters (double xMin, double xMax, double yMin, double yMax, + RandomDirectionParameters (const Rectangle &bounds, const RandomVariable &speedVariable, const RandomVariable &pauseVariable); virtual ~RandomDirectionParameters (); void SetSpeed (const RandomVariable &speedVariable); void SetPause (const RandomVariable &pauseVariable); - void SetBounds (double xMin, double xMax, double yMin, double yMax); + void SetBounds (const Rectangle &bounds); private: friend class RandomDirectionMobilityModel; - struct AreaBounds m_bounds; + + static Ptr GetCurrent (void); + + Rectangle m_bounds; RandomVariable *m_speedVariable; RandomVariable *m_pauseVariable; - std::string m_speedVariableValue; - std::string m_pauseVariableValue; }; class RandomDirectionMobilityModel : public MobilityModel @@ -62,12 +63,9 @@ class RandomDirectionMobilityModel : public MobilityModel static const ClassId cid; RandomDirectionMobilityModel (); - RandomDirectionMobilityModel (double x, double y); RandomDirectionMobilityModel (Ptr parameters); - RandomDirectionMobilityModel (Ptr parameters, - double x, double y); private: - static Ptr GetDefaultParameters (void); + void Start (void); void ResetDirectionAndSpeed (void); void SetDirectionAndSpeed (double direction); void InitializeDirectionAndSpeed (void); @@ -78,7 +76,7 @@ class RandomDirectionMobilityModel : public MobilityModel static const double PI; Ptr m_parameters; EventId m_event; - MobilityModelHelper m_helper; + StaticSpeedHelper m_helper; }; } // namespace ns3