A Discrete-Event Network Simulator
API
random-direction-2d-mobility-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
20 
21 #include "ns3/log.h"
22 #include "ns3/pointer.h"
23 #include "ns3/simulator.h"
24 #include "ns3/string.h"
25 
26 #include <algorithm>
27 #include <cmath>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("RandomDirection2dMobilityModel");
33 
34 NS_OBJECT_ENSURE_REGISTERED(RandomDirection2dMobilityModel);
35 
36 TypeId
38 {
39  static TypeId tid =
40  TypeId("ns3::RandomDirection2dMobilityModel")
42  .SetGroupName("Mobility")
43  .AddConstructor<RandomDirection2dMobilityModel>()
44  .AddAttribute("Bounds",
45  "The 2d bounding area",
46  RectangleValue(Rectangle(-100, 100, -100, 100)),
47  MakeRectangleAccessor(&RandomDirection2dMobilityModel::m_bounds),
48  MakeRectangleChecker())
49  .AddAttribute("Speed",
50  "A random variable to control the speed (m/s).",
51  StringValue("ns3::UniformRandomVariable[Min=1.0|Max=2.0]"),
53  MakePointerChecker<RandomVariableStream>())
54  .AddAttribute("Pause",
55  "A random variable to control the pause (s).",
56  StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
58  MakePointerChecker<RandomVariableStream>());
59  return tid;
60 }
61 
63 {
64  m_direction = CreateObject<UniformRandomVariable>();
65 }
66 
67 void
69 {
70  // chain up.
72 }
73 
74 void
76 {
79 }
80 
81 void
83 {
84  double direction = m_direction->GetValue(0, 2 * M_PI);
85  SetDirectionAndSpeed(direction);
86 }
87 
88 void
90 {
91  m_helper.Update();
92  m_helper.Pause();
93  Time pause = Seconds(m_pause->GetValue());
94  m_event.Cancel();
95  m_event =
98 }
99 
100 void
102 {
105  Vector position = m_helper.GetCurrentPosition();
106  double speed = m_speed->GetValue();
107  const Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
108  m_helper.SetVelocity(vector);
109  m_helper.Unpause();
110  Vector next = m_bounds.CalculateIntersection(position, vector);
111  Time delay = Seconds(CalculateDistance(position, next) / speed);
112  m_event.Cancel();
115 }
116 
117 void
119 {
120  double direction = 0;
121 
123  Vector position = m_helper.GetCurrentPosition();
124  switch (m_bounds.GetClosestSideOrCorner(position))
125  {
127  direction = m_direction->GetValue(M_PI_2, M_PI + M_PI_2);
128  break;
129  case Rectangle::LEFTSIDE:
130  direction = m_direction->GetValue(-M_PI_2, M_PI - M_PI_2);
131  break;
132  case Rectangle::TOPSIDE:
133  direction = m_direction->GetValue(M_PI, 2 * M_PI);
134  break;
136  direction = m_direction->GetValue(0, M_PI);
137  break;
139  direction = m_direction->GetValue(M_PI, M_PI + M_PI_2);
140  break;
142  direction = m_direction->GetValue(M_PI + M_PI_2, 2 * M_PI);
143  break;
145  direction = m_direction->GetValue(0, M_PI_2);
146  direction += M_PI / 2;
147  break;
149  direction = m_direction->GetValue(M_PI_2, M_PI);
150  break;
151  }
152  SetDirectionAndSpeed(direction);
153 }
154 
155 Vector
157 {
159  return m_helper.GetCurrentPosition();
160 }
161 
162 void
164 {
165  m_helper.SetPosition(position);
166  m_event.Cancel();
168 }
169 
170 Vector
172 {
173  return m_helper.GetVelocity();
174 }
175 
176 int64_t
178 {
179  m_direction->SetStream(stream);
180  m_speed->SetStream(stream + 1);
181  m_pause->SetStream(stream + 2);
182  return 3;
183 }
184 
185 } // namespace ns3
Vector GetCurrentPosition() const
Get current position vector.
Vector GetVelocity() const
Get velocity; if paused, will return a zero vector.
void Update() const
Update position, if not paused, from last position and time of last update.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void Unpause()
Resume mobility from current position at current velocity.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
void Pause()
Pause mobility at current position.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
Keep track of the current position and velocity of an object.
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:359
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
void DoSetPosition(const Vector &position) override
void SetDirectionAndSpeed(double direction)
Set new velocity and direction, and schedule next pause event.
EventId m_event
event ID of next scheduled event
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
ConstantVelocityHelper m_helper
helper for velocity computations
void BeginPause()
Pause, cancel currently scheduled event, schedule end of pause event.
void DoInitializePrivate()
Sets a new random direction and calls SetDirectionAndSpeed.
void DoInitialize() override
Initialize() implementation.
static TypeId GetTypeId()
Register this type with the TypeId system.
void DoDispose() override
Destructor implementation.
Ptr< RandomVariableStream > m_speed
a random variable to control speed
void ResetDirectionAndSpeed()
Set a new direction and speed.
Ptr< UniformRandomVariable > m_direction
rv to control direction
Ptr< RandomVariableStream > m_pause
a random variable to control pause
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
a 2d rectangle
Definition: rectangle.h:35
Side GetClosestSideOrCorner(const Vector &position) const
Definition: rectangle.cc:64
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:152
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:109