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 
28 TypeId
30 {
31  static TypeId tid = TypeId ("ns3::HierarchicalMobilityModel")
33  .AddConstructor<HierarchicalMobilityModel> ()
34  .AddAttribute ("Child", "The child mobility model.",
35  PointerValue (),
36  MakePointerAccessor (&HierarchicalMobilityModel::SetChild,
38  MakePointerChecker<MobilityModel> ())
39  .AddAttribute ("Parent", "The parent mobility model.",
40  PointerValue (),
41  MakePointerAccessor (&HierarchicalMobilityModel::SetParent,
43  MakePointerChecker<MobilityModel> ())
44  ;
45  return tid;
46 }
47 
49  : m_child (0),
50  m_parent (0)
51 {
52 }
53 
54 void
56 {
57  Ptr<MobilityModel> oldChild = m_child;
58  Vector pos;
59  if (m_child)
60  {
61  pos = GetPosition ();
63  }
64  m_child = model;
66 
67  // if we had a child before, then we had a valid position before;
68  // try to preserve the old absolute position.
69  if (oldChild)
70  {
71  SetPosition (pos);
72  }
73 }
74 
75 void
77 {
78  Vector pos;
79  if (m_child)
80  {
81  pos = GetPosition ();
82  }
83  if (m_parent)
84  {
86  }
87  m_parent = model;
88  if (m_parent)
89  {
91  }
92  // try to preserve the old position across parent changes
93  if (m_child)
94  {
95  SetPosition (pos);
96  }
97 }
98 
99 
102 {
103  return m_child;
104 }
105 
108 {
109  return m_parent;
110 }
111 
112 Vector
114 {
115  if (!m_parent)
116  {
117  return m_child->GetPosition ();
118  }
119  Vector parentPosition = m_parent->GetPosition ();
120  Vector childPosition = m_child->GetPosition ();
121  return Vector (parentPosition.x + childPosition.x,
122  parentPosition.y + childPosition.y,
123  parentPosition.z + childPosition.z);
124 }
125 void
127 {
128  if (m_child == 0)
129  {
130  return;
131  }
132  // This implementation of DoSetPosition is really an arbitraty choice.
133  // anything else would have been ok.
134  if (m_parent)
135  {
136  Vector parentPosition = m_parent->GetPosition ();
137  Vector childPosition (position.x - parentPosition.x,
138  position.y - parentPosition.y,
139  position.z - parentPosition.z);
140  m_child->SetPosition (childPosition);
141  }
142  else
143  {
144  m_child->SetPosition (position);
145  }
146 }
147 Vector
149 {
150  if (m_parent)
151  {
152  Vector parentSpeed = m_parent->GetVelocity ();
153  Vector childSpeed = m_child->GetVelocity ();
154  Vector speed (parentSpeed.x + childSpeed.x,
155  parentSpeed.y + childSpeed.y,
156  parentSpeed.z + childSpeed.z);
157  return speed;
158  }
159  else
160  {
161  return m_child->GetVelocity ();
162  }
163 }
164 
165 void
167 {
169 }
170 
171 void
173 {
175 }
176 
177 
178 
179 } // namespace ns3
virtual Vector DoGetVelocity(void) const
double x
x coordinate of vector
Definition: vector.h:49
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:295
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
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:269
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:611
double z
z coordinate of vector
Definition: vector.h:57