A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
random-direction-2d-mobility-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/simulator.h"
21 #include <algorithm>
22 #include <cmath>
23 #include "ns3/log.h"
24 #include "ns3/string.h"
25 #include "ns3/pointer.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("RandomDirection2dMobilityModel");
29 
30 namespace ns3 {
31 
32 const double RandomDirection2dMobilityModel::PI = 3.14159265358979323846;
33 
34 NS_OBJECT_ENSURE_REGISTERED (RandomDirection2dMobilityModel)
35  ;
36 
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::RandomDirection2dMobilityModel")
43  .SetGroupName ("Mobility")
44  .AddConstructor<RandomDirection2dMobilityModel> ()
45  .AddAttribute ("Bounds", "The 2d bounding area",
46  RectangleValue (Rectangle (-100, 100, -100, 100)),
47  MakeRectangleAccessor (&RandomDirection2dMobilityModel::m_bounds),
48  MakeRectangleChecker ())
49  .AddAttribute ("Speed", "A random variable to control the speed (m/s).",
50  StringValue ("ns3::UniformRandomVariable[Min=1.0|Max=2.0]"),
51  MakePointerAccessor (&RandomDirection2dMobilityModel::m_speed),
52  MakePointerChecker<RandomVariableStream> ())
53  .AddAttribute ("Pause", "A random variable to control the pause (s).",
54  StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
55  MakePointerAccessor (&RandomDirection2dMobilityModel::m_pause),
56  MakePointerChecker<RandomVariableStream> ())
57  ;
58  return tid;
59 }
60 
62 {
63  m_direction = CreateObject <UniformRandomVariable> ();
64 }
65 
66 void
68 {
69  // chain up.
71 }
72 void
74 {
77 }
78 
79 void
81 {
82  double direction = m_direction->GetValue (0, 2 * PI);
83  SetDirectionAndSpeed (direction);
84 }
85 
86 void
88 {
89  m_helper.Update ();
90  m_helper.Pause ();
91  Time pause = Seconds (m_pause->GetValue ());
92  m_event.Cancel ();
95 }
96 
97 void
99 {
102  Vector position = m_helper.GetCurrentPosition ();
103  double speed = m_speed->GetValue ();
104  const Vector vector (std::cos (direction) * speed,
105  std::sin (direction) * speed,
106  0.0);
107  m_helper.SetVelocity (vector);
108  m_helper.Unpause ();
109  Vector next = m_bounds.CalculateIntersection (position, vector);
110  Time delay = Seconds (CalculateDistance (position, next) / speed);
111  m_event.Cancel ();
112  m_event = Simulator::Schedule (delay,
115 }
116 void
118 {
119  double direction = m_direction->GetValue (0, PI);
120 
122  Vector position = m_helper.GetCurrentPosition ();
123  switch (m_bounds.GetClosestSide (position))
124  {
125  case Rectangle::RIGHT:
126  direction += PI / 2;
127  break;
128  case Rectangle::LEFT:
129  direction += -PI / 2;
130  break;
131  case Rectangle::TOP:
132  direction += PI;
133  break;
134  case Rectangle::BOTTOM:
135  direction += 0.0;
136  break;
137  }
138  SetDirectionAndSpeed (direction);
139 }
140 Vector
142 {
144  return m_helper.GetCurrentPosition ();
145 }
146 void
148 {
149  m_helper.SetPosition (position);
151  m_event.Cancel ();
153 }
154 Vector
156 {
157  return m_helper.GetVelocity ();
158 }
159 int64_t
161 {
162  m_direction->SetStream (stream);
163  m_speed->SetStream (stream + 1);
164  m_pause->SetStream (stream + 2);
165  return 3;
166 }
167 
168 
169 
170 } // namespace ns3
NS_LOG_COMPONENT_DEFINE("RandomDirection2dMobilityModel")
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition: rectangle.cc:89
hold variables of type string
Definition: string.h:19
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
hold objects of type ns3::Rectangle
Keep track of the current position and velocity of an object.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition: vector.cc:71
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoSetPosition(const Vector &position)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:258
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
void SetVelocity(const Vector &vel)
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
Side GetClosestSide(const Vector &position) const
Definition: rectangle.cc:56
void UpdateWithBounds(const Rectangle &rectangle) const
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
void SetPosition(const Vector &position)
a unique identifier for an interface.
Definition: type-id.h:49
a 2d rectangle
Definition: rectangle.h:33
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343