A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
random-direction-2d-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/log.h"
11#include "ns3/pointer.h"
12#include "ns3/simulator.h"
13#include "ns3/string.h"
14
15#include <algorithm>
16#include <cmath>
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("RandomDirection2dMobilityModel");
22
23NS_OBJECT_ENSURE_REGISTERED(RandomDirection2dMobilityModel);
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::RandomDirection2dMobilityModel")
31 .SetGroupName("Mobility")
32 .AddConstructor<RandomDirection2dMobilityModel>()
33 .AddAttribute("Bounds",
34 "The 2d bounding area",
35 RectangleValue(Rectangle(-100, 100, -100, 100)),
38 .AddAttribute("Speed",
39 "A random variable to control the speed (m/s).",
40 StringValue("ns3::UniformRandomVariable[Min=1.0|Max=2.0]"),
43 .AddAttribute("Pause",
44 "A random variable to control the pause (s).",
45 StringValue("ns3::ConstantRandomVariable[Constant=2.0]"),
48 return tid;
49}
50
55
60
61void
67
68void
74
75void
77{
78 double direction = m_direction->GetValue(0, 2 * M_PI);
79 SetDirectionAndSpeed(direction);
80}
81
82void
93
94void
96{
99 Vector position = m_helper.GetCurrentPosition();
100 double speed = m_speed->GetValue();
101 const Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
102 m_helper.SetVelocity(vector);
104 Vector next = m_bounds.CalculateIntersection(position, vector);
105 Time delay = Seconds(CalculateDistance(position, next) / speed);
106 m_event.Cancel();
109}
110
111void
113{
114 double direction = 0;
115
117 Vector position = m_helper.GetCurrentPosition();
118 switch (m_bounds.GetClosestSideOrCorner(position))
119 {
121 direction = m_direction->GetValue(M_PI_2, M_PI + M_PI_2);
122 break;
124 direction = m_direction->GetValue(-M_PI_2, M_PI - M_PI_2);
125 break;
127 direction = m_direction->GetValue(M_PI, 2 * M_PI);
128 break;
130 direction = m_direction->GetValue(0, M_PI);
131 break;
133 direction = m_direction->GetValue(M_PI, M_PI + M_PI_2);
134 break;
136 direction = m_direction->GetValue(M_PI + M_PI_2, 2 * M_PI);
137 break;
139 direction = m_direction->GetValue(0, M_PI_2);
140 direction += M_PI / 2;
141 break;
143 direction = m_direction->GetValue(M_PI_2, M_PI);
144 break;
145 }
146 SetDirectionAndSpeed(direction);
147}
148
149Vector
155
156void
163
164Vector
169
170int64_t
172{
173 m_direction->SetStream(stream);
174 m_speed->SetStream(stream + 1);
175 m_pause->SetStream(stream + 2);
176 return 3;
177}
178
179} // 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:44
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:440
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
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:24
Side GetClosestSideOrCorner(const Vector &position) const
Definition rectangle.cc:53
Vector CalculateIntersection(const Vector &current, const Vector &speed) const
Definition rectangle.cc:141
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeRectangleChecker()
Definition rectangle.cc:176
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Definition rectangle.h:114
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition vector.cc:98