26 #include "ns3/double.h"
29 #include "ns3/pointer.h"
30 #include "ns3/simulator.h"
31 #include "ns3/string.h"
32 #include "ns3/uinteger.h"
48 TypeId(
"ns3::RandomWalk2dOutdoorMobilityModel")
50 .SetGroupName(
"Mobility")
52 .AddAttribute(
"Bounds",
53 "Bounds of the area to cruise.",
54 RectangleValue(
Rectangle(0.0, 100.0, 0.0, 100.0)),
56 MakeRectangleChecker())
58 "Change current direction and speed after moving for this delay.",
62 .AddAttribute(
"Distance",
63 "Change current direction and speed after moving for this distance.",
66 MakeDoubleChecker<double>())
68 "The mode indicates the condition used to "
69 "change the current speed and direction",
76 .AddAttribute(
"Direction",
77 "A random variable used to pick the direction (radians).",
78 StringValue(
"ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
80 MakePointerChecker<RandomVariableStream>())
83 "A random variable used to pick the speed (m/s)."
84 "The default value is taken from Figure 1 of the paper"
85 "Henderson, L.F., 1971. The statistics of crowd fluids. nature, 229(5284), p.381.",
86 StringValue(
"ns3::NormalRandomVariable[Mean=1.53|Variance=0.040401]"),
88 MakePointerChecker<RandomVariableStream>())
89 .AddAttribute(
"Tolerance",
90 "Tolerance for the intersection point with buildings (m)."
91 "It represents a small distance from where the building limit"
92 "is actually placed, for example to represent a sidewalk.",
95 MakeDoubleChecker<double>())
96 .AddAttribute(
"MaxIterations",
97 "Maximum number of attempts to find an alternative next position"
98 "if the original one is inside a building.",
101 MakeUintegerChecker<uint32_t>());
118 Vector vector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
146 Vector nextPosition = position;
147 nextPosition.x += speed.x * delayLeft.
GetSeconds();
148 nextPosition.y += speed.y * delayLeft.
GetSeconds();
154 bool outdoor = std::get<0>(outdoorBuilding);
167 NS_LOG_LOGIC(
"NextPosition would lead into a building");
175 delaySecondsX = std::abs((nextPosition.x - position.x) / speed.x);
179 delaySecondsY = std::abs((nextPosition.y - position.y) / speed.y);
195 bool outdoor = std::get<0>(outdoorBuilding);
204 std::min(delaySeconds, std::abs((nextPosition.x - position.x) / speed.x));
206 else if (speed.y != 0)
209 std::min(delaySeconds, std::abs((nextPosition.y - position.y) / speed.y));
213 NS_ABORT_MSG(
"RandomWalk2dOutdoorMobilityModel::DoWalk: unable to calculate the "
215 "(the node is stationary).");
225 NS_LOG_LOGIC(
"NextPosition would lead into a building");
234 std::min(delaySecondsX, std::abs((nextPosition.x - position.x) / speed.x));
239 std::min(delaySecondsY, std::abs((nextPosition.y - position.y) / speed.y));
244 NS_ABORT_MSG(
"RandomWalk2dOutdoorMobilityModel::DoWalk: unable to calculate the "
246 "(the node is stationary).");
257 NS_LOG_LOGIC(
"Position " << position <<
" NextPosition " << nextPosition);
264 std::pair<bool, Ptr<Building>>
266 Vector nextPosition)
const
270 bool intersectBuilding =
false;
278 if ((*bit)->IsIntersect(currentPosition, nextPosition))
280 NS_LOG_LOGIC(
"Building " << (*bit)->GetBoundaries() <<
" intersects the line between "
281 << currentPosition <<
" and " << nextPosition);
284 (*bit)->GetBoundaries());
286 intersectBuilding =
true;
287 if (distance < minIntersectionDistance)
289 minIntersectionDistance = distance;
290 minIntersectionDistanceBuilding = (*bit);
295 return std::make_pair(!intersectBuilding, minIntersectionDistanceBuilding);
301 Box boundaries)
const
304 bool inside = boundaries.
IsInside(current);
312 double xIntersect = 0;
313 double yIntersect = 0;
322 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
329 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
336 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
343 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
374 NS_LOG_DEBUG(
"xIntersect " << xIntersect <<
" yIntersect " << yIntersect);
375 return Vector(xIntersect, yIntersect, 0);
418 bool nextWouldBeInside =
true;
421 while (nextWouldBeInside && iter <
m_maxIter)
423 NS_LOG_INFO(
"The next position would be inside a building, compute an alternative");
427 Vector velocityVector(std::cos(direction) * speed, std::sin(direction) * speed, 0.0);
430 Vector nextPosition = intersectPosition;
431 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds();
432 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds();
436 bool outdoor = std::get<0>(outdoorBuilding);
440 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
441 << nextPosition <<
" " << outdoor <<
" building "
442 << std::get<1>(outdoorBuilding)->GetBoundaries());
446 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
447 << nextPosition <<
" " << outdoor);
452 nextWouldBeInside =
false;
467 double speed = distance / delayLeft.
GetSeconds();
470 <<
" diff " << posDiff <<
" dist " << distance);
472 Vector velocityVector(posDiff.x / distance * speed, posDiff.y / distance * speed, 0.0);
475 Vector nextPosition = intersectPosition;
476 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds();
477 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds();
481 bool outdoor = std::get<0>(outdoorBuilding);
485 <<
m_maxIter + 1 <<
" iterations, loop intersect " << intersectPosition
486 <<
" nextPosition " << nextPosition <<
" " << outdoor <<
" building "
487 << std::get<1>(outdoorBuilding)->GetBoundaries());
491 "Not able to find an outdoor position. Try to increase the attribute MaxIterations "
492 "and check the position of the buildings in the scenario.");
496 NS_LOG_LOGIC(
"inside loop intersect " << intersectPosition <<
" nextPosition "
497 << 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.
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 GetClosestSideOrCorner(const Vector &position) const
bool IsInside(const Vector &position) const
Vector CalculateIntersection(const Vector ¤t, const Vector &speed) const
Side
enum for naming sides
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.
bool IsNegative() const
Exactly equivalent to t <= 0.
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,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
#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_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#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.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
double CalculateDistance(const Vector3D &a, const Vector3D &b)