A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
constant-velocity-helper.cc
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 "constant-velocity-helper.h"
24 
25 namespace ns3 {
26 
27 ConstantVelocityHelper::ConstantVelocityHelper ()
28  : m_paused (true)
29 {
30 }
31 ConstantVelocityHelper::ConstantVelocityHelper (const Vector &position)
32  : m_position (position),
33  m_paused (true)
34 {
35 }
36 ConstantVelocityHelper::ConstantVelocityHelper (const Vector &position,
37  const Vector &vel)
38  : m_position (position),
39  m_velocity (vel),
40  m_paused (true)
41 {
42 }
43 void
44 ConstantVelocityHelper::SetPosition (const Vector &position)
45 {
46  m_position = position;
47  m_velocity = Vector (0.0, 0.0, 0.0);
48  m_lastUpdate = Simulator::Now ();
49 }
50 
51 Vector
52 ConstantVelocityHelper::GetCurrentPosition (void) const
53 {
54  return m_position;
55 }
56 
57 Vector
58 ConstantVelocityHelper::GetVelocity (void) const
59 {
60  return m_paused ? Vector (0.0, 0.0, 0.0) : m_velocity;
61 }
62 void
63 ConstantVelocityHelper::SetVelocity (const Vector &vel)
64 {
65  m_velocity = vel;
66  m_lastUpdate = Simulator::Now ();
67 }
68 
69 void
70 ConstantVelocityHelper::Update (void) const
71 {
72  Time now = Simulator::Now ();
73  NS_ASSERT (m_lastUpdate <= now);
74  Time deltaTime = now - m_lastUpdate;
75  m_lastUpdate = now;
76  if (m_paused)
77  {
78  return;
79  }
80  double deltaS = deltaTime.GetSeconds ();
81  m_position.x += m_velocity.x * deltaS;
82  m_position.y += m_velocity.y * deltaS;
83  m_position.z += m_velocity.z * deltaS;
84 }
85 
86 void
87 ConstantVelocityHelper::UpdateWithBounds (const Rectangle &bounds) const
88 {
89  Update ();
90  m_position.x = std::min (bounds.xMax, m_position.x);
91  m_position.x = std::max (bounds.xMin, m_position.x);
92  m_position.y = std::min (bounds.yMax, m_position.y);
93  m_position.y = std::max (bounds.yMin, m_position.y);
94 }
95 
96 void
97 ConstantVelocityHelper::UpdateWithBounds (const Box &bounds) const
98 {
99  Update ();
100  m_position.x = std::min (bounds.xMax, m_position.x);
101  m_position.x = std::max (bounds.xMin, m_position.x);
102  m_position.y = std::min (bounds.yMax, m_position.y);
103  m_position.y = std::max (bounds.yMin, m_position.y);
104  m_position.z = std::min (bounds.zMax, m_position.z);
105  m_position.z = std::max (bounds.zMin, m_position.z);
106 }
107 
108 void
109 ConstantVelocityHelper::Pause (void)
110 {
111  m_paused = true;
112 }
113 
114 void
115 ConstantVelocityHelper::Unpause (void)
116 {
117  m_paused = false;
118 }
119 
120 } // namespace ns3
double x
Definition: vector.h:49
#define NS_ASSERT(condition)
Definition: assert.h:64
double GetSeconds(void) const
Definition: nstime.h:262
double y
Definition: vector.h:53
static Time Now(void)
Definition: simulator.cc:179
double z
Definition: vector.h:57