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
24
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{
98 m_helper.UpdateWithBounds(m_bounds);
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);
103 m_helper.Unpause();
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
116 m_helper.UpdateWithBounds(m_bounds);
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
151{
152 m_helper.UpdateWithBounds(m_bounds);
153 return m_helper.GetCurrentPosition();
154}
155
156void
158{
159 m_helper.SetPosition(position);
160 m_event.Cancel();
162}
163
164Vector
166{
167 return m_helper.GetVelocity();
168}
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
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
a 2d rectangle
Definition rectangle.h:24
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
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:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
STL class.
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:1345
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeRectangleChecker()
Definition rectangle.cc:173
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Definition rectangle.h:114
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Definition vector.cc:98