24 #include "ns3/double.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/string.h"
27 #include "ns3/pointer.h"
28 #include "ns3/simulator.h"
30 #include "ns3/building.h"
31 #include "ns3/building-list.h"
43 static TypeId tid =
TypeId (
"ns3::RandomWalk2dOutdoorMobilityModel")
45 .SetGroupName (
"Mobility")
47 .AddAttribute (
"Bounds",
48 "Bounds of the area to cruise.",
52 .AddAttribute (
"Time",
53 "Change current direction and speed after moving for this delay.",
57 .AddAttribute (
"Distance",
58 "Change current direction and speed after moving for this distance.",
61 MakeDoubleChecker<double> ())
62 .AddAttribute (
"Mode",
63 "The mode indicates the condition used to "
64 "change the current speed and direction",
69 .AddAttribute (
"Direction",
70 "A random variable used to pick the direction (radians).",
71 StringValue (
"ns3::UniformRandomVariable[Min=0.0|Max=6.283184]"),
73 MakePointerChecker<RandomVariableStream> ())
74 .AddAttribute (
"Speed",
75 "A random variable used to pick the speed (m/s)."
76 "The default value is taken from Figure 1 of the paper"
77 "Henderson, L.F., 1971. The statistics of crowd fluids. nature, 229(5284), p.381.",
78 StringValue (
"ns3::NormalRandomVariable[Mean=1.53|Variance=0.040401]"),
80 MakePointerChecker<RandomVariableStream> ())
81 .AddAttribute (
"Tolerance",
82 "Tolerance for the intersection point with buildings (m)."
83 "It represents a small distance from where the building limit"
84 "is actually placed, for example to represent a sidewalk.",
87 MakeDoubleChecker<double> ())
88 .AddAttribute (
"MaxIterations",
89 "Maximum number of attempts to find an alternative next position"
90 "if the original one is inside a building.",
93 MakeUintegerChecker<uint32_t> ())
111 Vector vector (std::cos (direction) * speed,
112 std::sin (direction) * speed,
136 Vector nextPosition = position;
137 nextPosition.x += speed.x * delayLeft.
GetSeconds ();
138 nextPosition.y += speed.y * delayLeft.
GetSeconds ();
144 bool outdoor = std::get<0> (outdoorBuilding);
155 NS_LOG_LOGIC (
"NextPosition would lead into a building");
157 Time delay =
Seconds ((nextPosition.x - position.x) / speed.x);
170 bool outdoor = std::get<0> (outdoorBuilding);
175 Time delay =
Seconds ((nextPosition.x - position.x) / speed.x);
181 NS_LOG_LOGIC (
"NextPosition would lead into a building");
183 Time delay =
Seconds ((nextPosition.x - position.x) / speed.x);
190 NS_LOG_LOGIC (
"Position " << position <<
" NextPosition " << nextPosition);
197 std::pair<bool, Ptr<Building> >
202 bool intersectBuilding =
false;
210 if ((*bit)->IsIntersect (currentPosition, nextPosition))
213 <<
" intersects the line between " << currentPosition
214 <<
" and " << nextPosition);
216 currentPosition, nextPosition, (*bit)->GetBoundaries ());
218 intersectBuilding =
true;
219 if (distance < minIntersectionDistance)
221 minIntersectionDistance = distance;
222 minIntersectionDistanceBuilding = (*bit);
227 return std::make_pair (!intersectBuilding, minIntersectionDistanceBuilding);
234 bool inside = boundaries.
IsInside (current);
242 double xIntersect = 0;
243 double yIntersect = 0;
252 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
259 (next.y - current.y) / (next.x - current.x) * (xIntersect - current.x) + current.y;
266 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
273 (next.x - current.x) / (next.y - current.y) * (yIntersect - current.y) + current.x;
276 NS_LOG_INFO (
"xIntersect " << xIntersect <<
" yIntersect " << yIntersect);
277 return Vector (xIntersect, yIntersect, 0);
313 bool nextWouldBeInside =
true;
316 while (nextWouldBeInside && iter <
m_maxIter)
318 NS_LOG_INFO (
"The next position would be inside a building, compute an alternative");
322 Vector velocityVector (std::cos (direction) * speed,
323 std::sin (direction) * speed,
327 Vector nextPosition = intersectPosition;
328 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds ();
329 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds ();
333 bool outdoor = std::get<0> (outdoorBuilding);
337 NS_LOG_LOGIC (
"inside loop intersect " << intersectPosition <<
" nextPosition "
338 << nextPosition <<
" " << outdoor <<
" building " << std::get<1> (outdoorBuilding)->GetBoundaries ());
342 NS_LOG_LOGIC (
"inside loop intersect " << intersectPosition <<
" nextPosition "
343 << nextPosition <<
" " << outdoor);
348 nextWouldBeInside =
false;
357 NS_LOG_INFO (
"Move back to the previous position");
363 double speed = distance / delayLeft.
GetSeconds ();
366 <<
" intersectPosition " << intersectPosition
367 <<
" diff " << posDiff <<
" dist " << distance
370 Vector velocityVector (posDiff.x / distance * speed,
371 posDiff.y / distance * speed,
375 Vector nextPosition = intersectPosition;
376 nextPosition.x += velocityVector.x * delayLeft.
GetSeconds ();
377 nextPosition.y += velocityVector.y * delayLeft.
GetSeconds ();
381 bool outdoor = std::get<0> (outdoorBuilding);
385 <<
m_maxIter + 1 <<
" iterations, loop intersect "
386 << intersectPosition <<
" nextPosition "
387 << nextPosition <<
" " << outdoor
388 <<
" building " << std::get<1> (outdoorBuilding)->GetBoundaries ());
390 NS_FATAL_ERROR (
"Not able to find an outdoor position. Try to increase the attribute MaxIterations and check the position of the buildings in the scenario.");
394 NS_LOG_LOGIC (
"inside loop intersect " << intersectPosition <<
" nextPosition "
395 << 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.
static Iterator End(void)
static Iterator Begin(void)
void Unpause(void)
Resume mobility from current position at current velocity.
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void SetPosition(const Vector &position)
Set position vector.
void SetVelocity(const Vector &vel)
Set new velocity vector.
void Update(void) const
Update position, if not paused, from last position and time of last update.
Vector GetCurrentPosition(void) const
Get current position 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(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Keep track of the current position and velocity of an object.
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
virtual void DoDispose(void)
Destructor implementation.
virtual void DoInitialize(void)
Initialize() implementation.
Smart pointer class similar to boost::intrusive_ptr.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
2D random walk mobility model which avoids buildings.
virtual Vector DoGetPosition(void) const
void AvoidBuilding(Time delayLeft, Vector intersectPosition)
Avoid a building.
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.
void DoInitializePrivate(void)
Perform initialization of the object before MobilityModel::DoInitialize ()
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 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
virtual void DoInitialize(void)
Initialize() implementation.
virtual void DoDispose(void)
Destructor implementation.
virtual Vector DoGetVelocity(void) const
double m_epsilon
Tolerance for the intersection point with buildings.
virtual int64_t DoAssignStreams(int64_t)
The default implementation does nothing but return the passed-in parameter.
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).
enum Mode m_mode
whether in time or distance mode
virtual void DoSetPosition(const Vector &position)
Time m_modeTime
Change current direction and speed after this delay.
static TypeId GetTypeId(void)
Register this type with the TypeId system.
Ptr< RandomVariableStream > m_speed
rv for picking speed
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(Time const &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(void) 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)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeRectangleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeRectangleChecker(void)
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#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.
Ptr< const AttributeChecker > MakeEnumChecker(int 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.
double CalculateDistance(const Vector3D &a, const Vector3D &b)