23#include "ns3/building-list.h"
24#include "ns3/building.h"
25#include "ns3/double.h"
28#include "ns3/pointer.h"
29#include "ns3/simulator.h"
30#include "ns3/string.h"
31#include "ns3/uinteger.h"
46 TypeId(
"ns3::RandomWalk2dOutdoorMobilityModel")
48 .SetGroupName(
"Mobility")
50 .AddAttribute(
"Bounds",
51 "Bounds of the area to cruise.",
54 MakeRectangleChecker())
56 "Change current direction and speed after moving for this delay.",
60 .AddAttribute(
"Distance",
61 "Change current direction and speed after moving for this distance.",
64 MakeDoubleChecker<double>())
66 "The mode indicates the condition used to "
67 "change the current speed and direction",
74 .AddAttribute(
"Direction",
75 "A random variable used to pick the direction (radians).",
76 StringValue(
"ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
78 MakePointerChecker<RandomVariableStream>())
81 "A random variable used to pick the speed (m/s)."
82 "The default value is taken from Figure 1 of the paper"
83 "Henderson, L.F., 1971. The statistics of crowd fluids. nature, 229(5284), p.381.",
84 StringValue(
"ns3::NormalRandomVariable[Mean=1.53|Variance=0.040401]"),
86 MakePointerChecker<RandomVariableStream>())
87 .AddAttribute(
"Tolerance",
88 "Tolerance for the intersection point with buildings (m)."
89 "It represents a small distance from where the building limit"
90 "is actually placed, for example to represent a sidewalk.",
93 MakeDoubleChecker<double>())
94 .AddAttribute(
"MaxIterations",
95 "Maximum number of attempts to find an alternative next position"
96 "if the original one is inside a building.",
99 MakeUintegerChecker<uint32_t>());
116 Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
139 Vector nextPosition = position;
140 nextPosition.x += speed.x * delayLeft.
GetSeconds();
141 nextPosition.y += speed.y * delayLeft.
GetSeconds();
147 bool outdoor = std::get<0>(outdoorBuilding);
160 NS_LOG_LOGIC(
"NextPosition would lead into a building");
163 Time delay =
Seconds((nextPosition.x - position.x) / speed.x);
177 bool outdoor = std::get<0>(outdoorBuilding);
182 Time delay =
Seconds((nextPosition.x - position.x) / speed.x);
190 NS_LOG_LOGIC(
"NextPosition would lead into a building");
193 Time delay =
Seconds((nextPosition.x - position.x) / speed.x);
201 NS_LOG_LOGIC(
"Position " << position <<
" NextPosition " << nextPosition);
208std::pair<bool, Ptr<Building>>
210 Vector nextPosition)
const
214 bool intersectBuilding =
false;
215 double minIntersectionDistance = std::numeric_limits<double>::max();
222 if ((*bit)->IsIntersect(currentPosition, nextPosition))
224 NS_LOG_LOGIC(
"Building " << (*bit)->GetBoundaries() <<
" intersects the line between "
225 << currentPosition <<
" and " << nextPosition);
228 (*bit)->GetBoundaries());
230 intersectBuilding =
true;
231 if (distance < minIntersectionDistance)
233 minIntersectionDistance = distance;
234 minIntersectionDistanceBuilding = (*bit);
239 return std::make_pair(!intersectBuilding, minIntersectionDistanceBuilding);
245 Box boundaries)
const
248 bool inside = boundaries.
IsInside(current);
256 double xIntersect = 0;
257 double yIntersect = 0;
266 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
273 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
280 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
287 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
290 NS_LOG_INFO(
"xIntersect " << xIntersect <<
" yIntersect " << yIntersect);
291 return Vector(xIntersect, yIntersect, 0);
327 bool nextWouldBeInside =
true;
330 while (nextWouldBeInside && iter <
m_maxIter)
332 NS_LOG_INFO(
"The next position would be inside a building, compute an alternative");
336 Vector velocityVector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
339 Vector nextPosition = intersectPosition;
340 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds();
341 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds();
345 bool outdoor = std::get<0>(outdoorBuilding);
349 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
350 << nextPosition <<
" " << outdoor <<
" building "
351 << std::get<1>(outdoorBuilding)->GetBoundaries());
355 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
356 << nextPosition <<
" " << outdoor);
361 nextWouldBeInside =
false;
376 double speed = distance / delayLeft.
GetSeconds();
379 <<
" diff " << posDiff <<
" dist " << distance);
381 Vector velocityVector(posDiff.x / distance * speed, posDiff.y / distance * speed, 0.0);
384 Vector nextPosition = intersectPosition;
385 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds();
386 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds();
390 bool outdoor = std::get<0>(outdoorBuilding);
394 <<
m_maxIter + 1 <<
" iterations, loop intersect " << intersectPosition
395 <<
" nextPosition " << nextPosition <<
" " << outdoor <<
" building "
396 << std::get<1>(outdoorBuilding)->GetBoundaries());
400 "Not able to find an outdoor position. Try to increase the attribute MaxIterations "
401 "and check the position of the buildings in the scenario.");
405 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
406 << nextPosition <<
" " << outdoor);
double yMax
The y coordinate of the top bound of the box.
bool IsInside(const Vector &position) const
double xMin
The x coordinate of the left bound of the box.
double yMin
The y coordinate of the bottom bound of the box.
double xMax
The x coordinate of the right bound of the box.
std::vector< Ptr< Building > >::const_iterator Iterator
Const Iterator.
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Hold variables of type enum.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
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.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
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.
2D random walk mobility model which avoids buildings.
void AvoidBuilding(Time delayLeft, Vector intersectPosition)
Avoid a building.
void DoInitializePrivate()
Perform initialization of the object before MobilityModel::DoInitialize ()
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
double m_modeDistance
Change direction and speed after this distance.
ConstantVelocityHelper m_helper
helper for this object
Rectangle m_bounds
Bounds of the area to cruise.
Vector m_prevPosition
Store the previous position in case a step back is needed.
void Rebound(Time timeLeft)
Performs the rebound of the node if it reaches a boundary.
uint32_t m_maxIter
Maximum number of tries to find the next position.
Vector DoGetPosition() const override
Vector CalculateIntersectionFromOutside(const Vector ¤t, const Vector &next, const Box boundaries) const
Compute the intersecting point of the box represented by boundaries and the line between current and ...
EventId m_event
stored event ID
Ptr< RandomVariableStream > m_direction
rv for picking direction
void DoDispose() override
Destructor implementation.
Mode m_mode
whether in time or distance mode
double m_epsilon
Tolerance for the intersection point with buildings.
void DoWalk(Time delayLeft)
Walk according to position and velocity, until distance is reached, time is reached,...
std::pair< bool, Ptr< Building > > IsLineClearOfBuildings(Vector currentPosition, Vector nextPosition) const
Check if there is a building between two positions (or if the nextPosition is inside a building).
static TypeId GetTypeId()
Register this type with the TypeId system.
Vector DoGetVelocity() const override
void DoInitialize() override
Initialize() implementation.
Time m_modeTime
Change current direction and speed after this delay.
Ptr< RandomVariableStream > m_speed
rv for picking speed
void DoSetPosition(const Vector &position) override
Side GetClosestSide(const Vector &position) const
bool IsInside(const Vector &position) const
Vector CalculateIntersection(const Vector ¤t, const Vector &speed) const
Side
enum for naming sides
AttributeValue implementation for Rectangle.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
static void Remove(const EventId &id)
Remove an event from the event list.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
AttributeValue implementation for Time.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double CalculateDistance(const Vector3D &a, const Vector3D &b)
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.