A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
hierarchical-mobility-model.cc
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  */
20 #include "hierarchical-mobility-model.h"
21 #include "ns3/pointer.h"
22 
23 namespace ns3 {
24 
25 NS_OBJECT_ENSURE_REGISTERED (HierarchicalMobilityModel);
26 
27 TypeId
28 HierarchicalMobilityModel::GetTypeId (void)
29 {
30  static TypeId tid = TypeId ("ns3::HierarchicalMobilityModel")
31  .SetParent<MobilityModel> ()
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 
47 HierarchicalMobilityModel::HierarchicalMobilityModel ()
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 ();
61  m_child->TraceDisconnectWithoutContext ("CourseChange", MakeCallback (&HierarchicalMobilityModel::ChildChanged, this));
62  }
63  m_child = model;
64  m_child->TraceConnectWithoutContext ("CourseChange", MakeCallback (&HierarchicalMobilityModel::ChildChanged, this));
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  {
84  m_parent->TraceDisconnectWithoutContext ("CourseChange", MakeCallback (&HierarchicalMobilityModel::ParentChanged, this));
85  }
86  m_parent = model;
87  if (m_parent)
88  {
89  m_parent->TraceConnectWithoutContext ("CourseChange", MakeCallback (&HierarchicalMobilityModel::ParentChanged, this));
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
165 HierarchicalMobilityModel::ParentChanged (Ptr<const MobilityModel> model)
166 {
168 }
169 
170 void
171 HierarchicalMobilityModel::ChildChanged (Ptr<const MobilityModel> model)
172 {
174 }
175 
176 
177 
178 } // namespace ns3
virtual Vector DoGetVelocity(void) const
double x
Definition: vector.h:49
Vector GetPosition(void) const
a 3d vector
Definition: vector.h:31
Vector GetVelocity(void) const
Ptr< MobilityModel > GetParent(void) const
void NotifyCourseChange(void) const
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:294
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void SetChild(Ptr< MobilityModel > model)
Ptr< MobilityModel > GetChild(void) const
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:268
void SetParent(Ptr< MobilityModel > model)
virtual Vector DoGetPosition(void) const
virtual void DoSetPosition(const Vector &position)
double y
Definition: vector.h:53
void SetPosition(const Vector &position)
double z
Definition: vector.h:57