A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hierarchical-mobility-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 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  */
21 #include "ns3/pointer.h"
22 
23 namespace ns3 {
24 
25 NS_OBJECT_ENSURE_REGISTERED (HierarchicalMobilityModel);
26 
27 TypeId
29 {
30  static TypeId tid = TypeId ("ns3::HierarchicalMobilityModel")
32  .AddConstructor<HierarchicalMobilityModel> ()
33  .AddAttribute ("Child", "The child mobility model.",
34  PointerValue (),
35  MakePointerAccessor (&HierarchicalMobilityModel::SetChild,
37  MakePointerChecker<MobilityModel> ())
38  .AddAttribute ("Parent", "The parent mobility model.",
39  PointerValue (),
40  MakePointerAccessor (&HierarchicalMobilityModel::SetParent,
42  MakePointerChecker<MobilityModel> ())
43  ;
44  return tid;
45 }
46 
48  : m_child (0),
49  m_parent (0)
50 {
51 }
52 
53 void
55 {
56  Ptr<MobilityModel> oldChild = m_child;
57  Vector pos;
58  if (m_child)
59  {
60  pos = GetPosition ();
62  }
63  m_child = model;
65 
66  // if we had a child before, then we had a valid position before;
67  // try to preserve the old absolute position.
68  if (oldChild)
69  {
70  SetPosition (pos);
71  }
72 }
73 
74 void
76 {
77  Vector pos;
78  if (m_child)
79  {
80  pos = GetPosition ();
81  }
82  if (m_parent)
83  {
85  }
86  m_parent = model;
87  if (m_parent)
88  {
90  }
91  // try to preserve the old position across parent changes
92  if (m_child)
93  {
94  SetPosition (pos);
95  }
96 }
97 
98 
101 {
102  return m_child;
103 }
104 
107 {
108  return m_parent;
109 }
110 
111 Vector
113 {
114  if (!m_parent)
115  {
116  return m_child->GetPosition ();
117  }
118  Vector parentPosition = m_parent->GetPosition ();
119  Vector childPosition = m_child->GetPosition ();
120  return Vector (parentPosition.x + childPosition.x,
121  parentPosition.y + childPosition.y,
122  parentPosition.z + childPosition.z);
123 }
124 void
126 {
127  if (m_child == 0)
128  {
129  return;
130  }
131  // This implementation of DoSetPosition is really an arbitraty choice.
132  // anything else would have been ok.
133  if (m_parent)
134  {
135  Vector parentPosition = m_parent->GetPosition ();
136  Vector childPosition (position.x - parentPosition.x,
137  position.y - parentPosition.y,
138  position.z - parentPosition.z);
139  m_child->SetPosition (childPosition);
140  }
141  else
142  {
143  m_child->SetPosition (position);
144  }
145 }
146 Vector
148 {
149  if (m_parent)
150  {
151  Vector parentSpeed = m_parent->GetVelocity ();
152  Vector childSpeed = m_child->GetVelocity ();
153  Vector speed (parentSpeed.x + childSpeed.x,
154  parentSpeed.y + childSpeed.y,
155  parentSpeed.z + childSpeed.z);
156  return speed;
157  }
158  else
159  {
160  return m_child->GetVelocity ();
161  }
162 }
163 
164 void
166 {
168 }
169 
170 void
172 {
174 }
175 
176 
177 
178 } // namespace ns3
virtual Vector DoGetVelocity(void) const
double x
x coordinate of vector
Definition: vector.h:49
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Vector GetPosition(void) const
a 3d vector
Definition: vector.h:31
Vector GetVelocity(void) const
Keep track of the current position and velocity of an object.
Ptr< MobilityModel > GetParent(void) const
void NotifyCourseChange(void) const
Must be invoked by subclasses when the course of the position changes to notify course change listene...
Vector3D Vector
Definition: vector.h:118
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:310
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void SetChild(Ptr< MobilityModel > model)
Sets the child mobility model to a new one.
Ptr< MobilityModel > GetChild(void) const
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:284
hold objects of type Ptr
Definition: pointer.h:33
void SetParent(Ptr< MobilityModel > model)
Sets the parent mobility model to a new one.
virtual Vector DoGetPosition(void) const
virtual void DoSetPosition(const Vector &position)
double y
y coordinate of vector
Definition: vector.h:53
void SetPosition(const Vector &position)
void ChildChanged(Ptr< const MobilityModel > model)
void ParentChanged(Ptr< const MobilityModel > model)
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
double z
z coordinate of vector
Definition: vector.h:57