A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hierarchical-mobility-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
9
10#include "ns3/log.h"
11#include "ns3/pointer.h"
12#include "ns3/simulator.h"
13
14namespace ns3
15{
16
17NS_LOG_COMPONENT_DEFINE("HierarchicalMobilityModel");
18
19NS_OBJECT_ENSURE_REGISTERED(HierarchicalMobilityModel);
20
21TypeId
23{
24 static TypeId tid =
25 TypeId("ns3::HierarchicalMobilityModel")
27 .SetGroupName("Mobility")
28 .AddConstructor<HierarchicalMobilityModel>()
29 .AddAttribute("Child",
30 "The child mobility model.",
35 .AddAttribute("Parent",
36 "The parent mobility model.",
41 return tid;
42}
43
45 : m_child(nullptr),
46 m_parent(nullptr)
47{
48 NS_LOG_FUNCTION(this);
49}
50
51void
53{
54 NS_LOG_FUNCTION(this << model);
55 Ptr<MobilityModel> oldChild = m_child;
56 Vector pos;
57 if (m_child)
58 {
59 NS_LOG_DEBUG("Disconnecting previous child model " << m_child);
60 pos = GetPosition();
61 m_child->TraceDisconnectWithoutContext(
62 "CourseChange",
64 }
65 m_child = model;
66 m_child->TraceConnectWithoutContext(
67 "CourseChange",
69
70 // if we had a child before, then we had a valid position before;
71 // try to preserve the old absolute position.
72 if (oldChild)
73 {
74 NS_LOG_DEBUG("Restoring previous position " << pos);
75 SetPosition(pos);
76 }
77}
78
79void
81{
82 NS_LOG_FUNCTION(this << model);
83 Vector pos;
84 if (m_child)
85 {
86 pos = GetPosition();
87 }
88 if (m_parent)
89 {
90 NS_LOG_DEBUG("Disconnecting previous parent model " << m_child);
91 m_parent->TraceDisconnectWithoutContext(
92 "CourseChange",
94 }
95 m_parent = model;
96 if (m_parent)
97 {
98 m_parent->TraceConnectWithoutContext(
99 "CourseChange",
101 }
102 // try to preserve the old position across parent changes
103 if (m_child)
104 {
105 NS_LOG_DEBUG("Restoring previous position " << pos);
106 SetPosition(pos);
107 }
108}
109
112{
113 return m_child;
114}
115
118{
119 return m_parent;
120}
121
122Vector
124{
125 if (!m_parent)
126 {
127 return m_child->GetPosition();
128 }
129 Vector parentPosition = m_parent->GetPosition();
130 Vector childPosition = m_child->GetPositionWithReference(parentPosition);
131 return Vector(parentPosition.x + childPosition.x,
132 parentPosition.y + childPosition.y,
133 parentPosition.z + childPosition.z);
134}
135
136void
138{
139 NS_LOG_FUNCTION(this << position);
140 if (!m_child)
141 {
142 return;
143 }
144 // This implementation of DoSetPosition is really an arbitrary choice.
145 // anything else would have been ok.
146 if (m_parent)
147 {
148 Vector parentPosition = m_parent->GetPosition();
149 Vector childPosition(position.x - parentPosition.x,
150 position.y - parentPosition.y,
151 position.z - parentPosition.z);
152 m_child->SetPosition(childPosition);
153 }
154 else
155 {
156 m_child->SetPosition(position);
157 }
158}
159
160Vector
162{
163 if (m_parent)
164 {
165 Vector parentSpeed = m_parent->GetVelocity();
166 Vector childSpeed = m_child->GetVelocity();
167 Vector speed(parentSpeed.x + childSpeed.x,
168 parentSpeed.y + childSpeed.y,
169 parentSpeed.z + childSpeed.z);
170 return speed;
171 }
172 else
173 {
174 return m_child->GetVelocity();
175 }
176}
177
178void
183
184void
189
190void
192{
193 NS_LOG_FUNCTION(this);
194 if (m_parent && !m_parent->IsInitialized())
195 {
196 m_parent->Initialize();
197 }
198 m_child->Initialize();
199}
200
201int64_t
203{
204 NS_LOG_FUNCTION(this << stream);
205 int64_t streamsAllocated = 0;
206 streamsAllocated += m_parent->AssignStreams(stream);
207 streamsAllocated += m_child->AssignStreams(stream + streamsAllocated);
208 return streamsAllocated;
209}
210
211} // 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.
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 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...
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
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:684