A Discrete-Event Network Simulator
API
constant-velocity-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/simulator.h"
21 #include "ns3/rectangle.h"
22 #include "ns3/box.h"
23 #include "ns3/log.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("ConstantVelocityHelper");
29 
31  : m_paused (true)
32 {
33  NS_LOG_FUNCTION (this);
34 }
36  : m_position (position),
37  m_paused (true)
38 {
39  NS_LOG_FUNCTION (this << position);
40 }
42  const Vector &vel)
43  : m_position (position),
44  m_velocity (vel),
45  m_paused (true)
46 {
47  NS_LOG_FUNCTION (this << position << vel);
48 }
49 void
50 ConstantVelocityHelper::SetPosition (const Vector &position)
51 {
52  NS_LOG_FUNCTION (this << position);
53  m_position = position;
54  m_velocity = Vector (0.0, 0.0, 0.0);
56 }
57 
58 Vector
60 {
61  NS_LOG_FUNCTION (this);
62  return m_position;
63 }
64 
65 Vector
67 {
68  NS_LOG_FUNCTION (this);
69  return m_paused ? Vector (0.0, 0.0, 0.0) : m_velocity;
70 }
71 void
73 {
74  NS_LOG_FUNCTION (this << vel);
75  m_velocity = vel;
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION (this);
83  Time now = Simulator::Now ();
84  NS_ASSERT (m_lastUpdate <= now);
85  Time deltaTime = now - m_lastUpdate;
86  m_lastUpdate = now;
87  if (m_paused)
88  {
89  return;
90  }
91  double deltaS = deltaTime.GetSeconds ();
92  m_position.x += m_velocity.x * deltaS;
93  m_position.y += m_velocity.y * deltaS;
94  m_position.z += m_velocity.z * deltaS;
95 }
96 
97 void
99 {
100  NS_LOG_FUNCTION (this << bounds);
101  Update ();
102  m_position.x = std::min (bounds.xMax, m_position.x);
103  m_position.x = std::max (bounds.xMin, m_position.x);
104  m_position.y = std::min (bounds.yMax, m_position.y);
105  m_position.y = std::max (bounds.yMin, m_position.y);
106 }
107 
108 void
110 {
111  NS_LOG_FUNCTION (this << bounds);
112  Update ();
113  m_position.x = std::min (bounds.xMax, m_position.x);
114  m_position.x = std::max (bounds.xMin, m_position.x);
115  m_position.y = std::min (bounds.yMax, m_position.y);
116  m_position.y = std::max (bounds.yMin, m_position.y);
117  m_position.z = std::min (bounds.zMax, m_position.z);
118  m_position.z = std::max (bounds.zMin, m_position.z);
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
125  m_paused = true;
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this);
132  m_paused = false;
133 }
134 
135 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
double zMin
The z coordinate of the down bound of the box.
Definition: box.h:118
Vector GetCurrentPosition(void) const
Get current position vector.
#define min(a, b)
Definition: 80211b.c:42
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:361
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
double yMax
The y coordinate of the top bound of the rectangle.
Definition: rectangle.h:91
double xMax
The x coordinate of the right bound of the box.
Definition: box.h:112
Vector m_velocity
state variable for velocity
double xMin
The x coordinate of the left bound of the rectangle.
Definition: rectangle.h:88
a 3d box
Definition: box.h:34
double yMax
The y coordinate of the top bound of the box.
Definition: box.h:116
#define max(a, b)
Definition: 80211b.c:43
void Update(void) const
Update position, if not paused, from last position and time of last update.
double xMax
The x coordinate of the right bound of the rectangle.
Definition: rectangle.h:89
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition: rectangle.h:90
double yMin
The y coordinate of the bottom bound of the box.
Definition: box.h:114
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void UpdateWithBounds(const Rectangle &rectangle) const
Update position, if not paused, from last position and time of last update.
void SetVelocity(const Vector &vel)
Set new velocity vector.
double zMax
The z coordinate of the up bound of the box.
Definition: box.h:120
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
Vector m_position
state variable for current position
Vector GetVelocity(void) const
Get velocity; if paused, will return a zero vector.
void Unpause(void)
Resume mobility from current position at current velocity.
bool m_paused
state variable for paused
void SetPosition(const Vector &position)
Set position vector.
Time m_lastUpdate
time of last update
void Pause(void)
Pause mobility at current position.
a 2d rectangle
Definition: rectangle.h:34
double xMin
The x coordinate of the left bound of the box.
Definition: box.h:110