A Discrete-Event Network Simulator
API
hierarchical-mobility-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
20 
21 #include "ns3/log.h"
22 #include "ns3/pointer.h"
23 #include "ns3/simulator.h"
24 
25 namespace ns3
26 {
27 
28 NS_LOG_COMPONENT_DEFINE("HierarchicalMobilityModel");
29 
30 NS_OBJECT_ENSURE_REGISTERED(HierarchicalMobilityModel);
31 
32 TypeId
34 {
35  static TypeId tid =
36  TypeId("ns3::HierarchicalMobilityModel")
38  .SetGroupName("Mobility")
39  .AddConstructor<HierarchicalMobilityModel>()
40  .AddAttribute("Child",
41  "The child mobility model.",
42  PointerValue(),
45  MakePointerChecker<MobilityModel>())
46  .AddAttribute("Parent",
47  "The parent mobility model.",
48  PointerValue(),
51  MakePointerChecker<MobilityModel>());
52  return tid;
53 }
54 
56  : m_child(nullptr),
57  m_parent(nullptr)
58 {
59  NS_LOG_FUNCTION(this);
60 }
61 
62 void
64 {
65  NS_LOG_FUNCTION(this << model);
66  Ptr<MobilityModel> oldChild = m_child;
67  Vector pos;
68  if (m_child)
69  {
70  NS_LOG_DEBUG("Disconnecting previous child model " << m_child);
71  pos = GetPosition();
73  "CourseChange",
75  }
76  m_child = model;
78  "CourseChange",
80 
81  // if we had a child before, then we had a valid position before;
82  // try to preserve the old absolute position.
83  if (oldChild)
84  {
85  NS_LOG_DEBUG("Restoring previous position " << pos);
86  SetPosition(pos);
87  }
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION(this << model);
94  Vector pos;
95  if (m_child)
96  {
97  pos = GetPosition();
98  }
99  if (m_parent)
100  {
101  NS_LOG_DEBUG("Disconnecting previous parent model " << m_child);
103  "CourseChange",
105  }
106  m_parent = model;
107  if (m_parent)
108  {
110  "CourseChange",
112  }
113  // try to preserve the old position across parent changes
114  if (m_child)
115  {
116  NS_LOG_DEBUG("Restoring previous position " << pos);
117  SetPosition(pos);
118  }
119 }
120 
123 {
124  return m_child;
125 }
126 
129 {
130  return m_parent;
131 }
132 
133 Vector
135 {
136  if (!m_parent)
137  {
138  return m_child->GetPosition();
139  }
140  Vector parentPosition = m_parent->GetPosition();
141  Vector childPosition = m_child->GetPositionWithReference(parentPosition);
142  return Vector(parentPosition.x + childPosition.x,
143  parentPosition.y + childPosition.y,
144  parentPosition.z + childPosition.z);
145 }
146 
147 void
149 {
150  NS_LOG_FUNCTION(this << position);
151  if (!m_child)
152  {
153  return;
154  }
155  // This implementation of DoSetPosition is really an arbitrary choice.
156  // anything else would have been ok.
157  if (m_parent)
158  {
159  Vector parentPosition = m_parent->GetPosition();
160  Vector childPosition(position.x - parentPosition.x,
161  position.y - parentPosition.y,
162  position.z - parentPosition.z);
163  m_child->SetPosition(childPosition);
164  }
165  else
166  {
167  m_child->SetPosition(position);
168  }
169 }
170 
171 Vector
173 {
174  if (m_parent)
175  {
176  Vector parentSpeed = m_parent->GetVelocity();
177  Vector childSpeed = m_child->GetVelocity();
178  Vector speed(parentSpeed.x + childSpeed.x,
179  parentSpeed.y + childSpeed.y,
180  parentSpeed.z + childSpeed.z);
181  return speed;
182  }
183  else
184  {
185  return m_child->GetVelocity();
186  }
187 }
188 
189 void
191 {
193 }
194 
195 void
197 {
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION(this);
205  if (m_parent && !m_parent->IsInitialized())
206  {
207  m_parent->Initialize();
208  }
209  m_child->Initialize();
210 }
211 
212 int64_t
214 {
215  NS_LOG_FUNCTION(this << stream);
216  int64_t streamsAllocated = 0;
217  streamsAllocated += m_parent->AssignStreams(stream);
218  streamsAllocated += m_child->AssignStreams(stream + streamsAllocated);
219  return streamsAllocated;
220 }
221 
222 } // namespace ns3
Ptr< MobilityModel > m_child
pointer to child mobility model
void ChildChanged(Ptr< const MobilityModel > model)
Callback for when child mobility model course change occurs.
Ptr< MobilityModel > m_parent
pointer to parent mobility model
void DoSetPosition(const Vector &position) override
void SetParent(Ptr< MobilityModel > model)
Sets the parent mobility model to a new one, possibly replacing an existing one.
static TypeId GetTypeId()
Register this type with the TypeId system.
Ptr< MobilityModel > GetParent() const
void DoInitialize() override
Initialize() implementation.
void SetChild(Ptr< MobilityModel > model)
Sets the child mobility model to a new one, possibly replacing an existing one.
void ParentChanged(Ptr< const MobilityModel > model)
Callback for when parent mobility model course change occurs.
int64_t DoAssignStreams(int64_t) override
The default implementation does nothing but return the passed-in parameter.
Keep track of the current position and velocity of an object.
Vector GetPositionWithReference(const Vector &referencePosition) const
This method may be used if the position returned may depend on some reference position provided.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Vector GetVelocity() const
Vector GetPosition() const
void SetPosition(const Vector &position)
void NotifyCourseChange() const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Disconnect from a TraceSource a Callback previously connected without a context.
Definition: object-base.cc:343
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
bool IsInitialized() const
Check if the object has been initialized.
Definition: object.cc:212
Hold objects of type Ptr<T>.
Definition: pointer.h:37
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227